⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tournament.c

📁 打魔兽战网的都知道他是什么
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */#include "common/setup_before.h"#ifdef HAVE_STRING_H# include <string.h>#else# ifdef HAVE_STRINGS_H#  include <strings.h># endif#endif#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#include <errno.h>#ifdef STDC_HEADERS# include <stdlib.h>#else# ifdef HAVE_MALLOC_H#  include <malloc.h># endif#endif#include "common/eventlog.h"#include "common/packet.h"#include "common/tag.h"#include "common/list.h"#include "common/util.h"#include "common/xalloc.h"#include "compat/strerror.h"#include "account.h"#include "anongame_maplists.h"#include "tournament.h"#include "common/setup_after.h"static t_tournament_info * tournament_info = NULL;static t_list * tournament_head=NULL;static int tournamentlist_create(void);static int gamelist_destroy(void);static t_tournament_user * tournament_get_user(t_account * account);//static int tournament_get_in_game_status(t_account * account);/*****/static int tournamentlist_create(void){    tournament_head = list_create();    return 0;}static int gamelist_destroy(void){    t_elem *		curr;    t_tournament_user * user;        if (tournament_head) {	LIST_TRAVERSE(tournament_head,curr)	{	    if (!(user = elem_get_data(curr))) {		eventlog(eventlog_level_error,__FUNCTION__,"tournament list contains NULL item");		continue;	    }	    	    if (list_remove_elem(tournament_head,&curr)<0)		eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");	    	    if (user->name)		xfree((void *)user->name); /* avoid warning */	    	    xfree(user);	    	}		if (list_destroy(tournament_head)<0)	    return -1;	tournament_head = NULL;    }    return 0;}/*****/extern int tournament_check_client(t_clienttag clienttag){    if (clienttag==CLIENTTAG_WAR3XP_UINT && tournament_info->game_client==2)	return 1;    if (clienttag==CLIENTTAG_WARCRAFT3_UINT && tournament_info->game_client==1)	return 1;	    return -1;}extern int tournament_signup_user(t_account * account){    t_tournament_user * user;    if (!(account))	return -1;        if ((user = tournament_get_user(account))) {	eventlog(eventlog_level_info,__FUNCTION__,"user \"%s\" already signed up in tournament",account_get_name(account));	return 0;    }        user = xmalloc(sizeof(t_tournament_user));    user->name		= xstrdup(account_get_name(account));    user->wins		= 0;    user->losses	= 0;    user->ties		= 0;    user->in_game	= 0;    user->in_finals	= 0;            list_prepend_data(tournament_head,user);        eventlog(eventlog_level_info,__FUNCTION__,"added user \"%s\" to tournament",account_get_name(account));    return 0;}static t_tournament_user * tournament_get_user(t_account * account){    t_elem const * curr;    t_tournament_user * user;        if (tournament_head)	LIST_TRAVERSE(tournament_head,curr)	{	    user = elem_get_data(curr);	    if (strcmp(user->name, account_get_name(account)) == 0)		return user;	}        return NULL;}extern int tournament_user_signed_up(t_account * account){    if (!(tournament_get_user(account)))	return -1;            return 0;}extern int tournament_add_stat(t_account * account, int stat){    t_tournament_user * user;        if (!(user = tournament_get_user(account)))	return -1;        if (stat == 1)	user->wins++;    if (stat == 2)	user->losses++;    if (stat == 3)	user->ties++;        return 0;}extern int tournament_get_stat(t_account * account, int stat){    t_tournament_user * user;        if (!(user = tournament_get_user(account)))	return 0;        if (stat == 1)	return user->wins;    if (stat == 2)	return user->losses;    if (stat == 3)	return user->ties;    return 0;}extern int tournament_get_player_score(t_account * account){    t_tournament_user * user;    int score;        if (!(user = tournament_get_user(account)))	return 0;        score = user->wins * 3 + user->ties - user->losses;        if (score < 0)	return 0;	    return score;}    extern int tournament_set_in_game_status(t_account * account, int status){    t_tournament_user * user;        if (!(user = tournament_get_user(account)))	return -1;        user->in_game = status;        return 0;}/*static int tournament_get_in_game_status(t_account * account){    t_tournament_user * user;        if (!(user = tournament_get_user(account)))	return 0;        return user->in_game;}*/extern int tournament_get_in_finals_status(t_account * account){    t_tournament_user * user;        if (!(user = tournament_get_user(account)))	return 0;        return user->in_finals;}extern int tournament_get_game_in_progress(void){    t_elem const * curr;    t_tournament_user * user;        if (tournament_head)	LIST_TRAVERSE_CONST(tournament_head,curr)	{	    user = elem_get_data(curr);	    if (user->in_game == 1)		return 1;	}        return 0;}extern int tournament_is_arranged(void){    if (tournament_info->game_selection == 2)	return 1;    else	return 0;}extern int tournament_get_totalplayers(void){    return tournament_info->game_type * 2;}void tournament_check_date(unsigned int *mon, unsigned int *mday, unsigned int *year, unsigned int *hour, unsigned int *min, unsigned int *sec, char const * caller){  if (*mon>12)  {    eventlog(eventlog_level_error,__FUNCTION__,"got invalid month (%u) in %s",*mon,caller);    *mon = 12;  }  if (*mday>31)  {    eventlog(eventlog_level_error,__FUNCTION__,"got invalid mday (%u) in %s",*mday,caller);    *mday = 31;  }  if (*hour>23)  {    eventlog(eventlog_level_error,__FUNCTION__,"got invalid hour (%u) from %s",*hour,caller);    *hour = 23;  }  if (*min >59)  {    eventlog(eventlog_level_error,__FUNCTION__,"got invalid min (%u) from %s",*min,caller);    *min = 59;  }  if (*sec >59)  {    eventlog(eventlog_level_error,__FUNCTION__,"got invalid sec (%u) from %s",*sec,caller);    *sec = 59;  }  return;}/*****/extern int tournament_init(char const * filename){    FILE * fp;    unsigned int line,pos,mon,day,year,hour,min,sec;    char *buff,*temp,*pointer,*value;    char format[30];    char *variable;    char *sponsor = NULL;    char *have_sponsor = NULL;    char *have_icon = NULL;    struct tm * timestamp = xmalloc(sizeof(struct tm));        sprintf(format,"%%02u/%%02u/%%04u %%02u:%%02u:%%02u");    tournament_info = xmalloc(sizeof(t_tournament_info));    tournament_info->start_preliminary	= 0;    tournament_info->end_signup		= 0;    tournament_info->end_preliminary	= 0;    tournament_info->start_round_1	= 0;    tournament_info->start_round_2	= 0;    tournament_info->start_round_3	= 0;    tournament_info->start_round_4	= 0;    tournament_info->tournament_end	= 0;    tournament_info->game_selection	= 1; /* Default to PG */    tournament_info->game_type		= 1; /* Default to 1v1 */    tournament_info->game_client	= 2; /* Default to FT */    tournament_info->races		= 0x3F; /* Default to all races */    tournament_info->format		= xstrdup("");    tournament_info->sponsor		= xstrdup("");    tournament_info->thumbs_down	= 0;        anongame_tournament_maplists_destroy();        if (!filename) {	eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");        xfree((void *)timestamp);	return -1;    }        if (!(fp = fopen(filename,"r"))) {	eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));	xfree((void *)timestamp);	return -1;    }        for (line=1; (buff = file_get_line(fp)); line++) {	for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++);	if (buff[pos]=='\0' || buff[pos]=='#') {	    continue;	}	if ((temp = strrchr(buff,'#'))) {	    unsigned int len;	    unsigned int endpos;	    	    *temp = '\0';	    len = strlen(buff)+1;	    for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--);	    buff[endpos+1] = '\0';	}		if (strcmp(buff,"[MAPS]") == 0) {	    char *clienttag, *mapname, *mname;	    t_clienttag ctag;	    	    for (; (buff = file_get_line(fp));) {		for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++);		if (buff[pos]=='\0' || buff[pos]=='#') {		    continue;		}		if ((temp = strrchr(buff,'#'))) {		    unsigned int len;		    unsigned int endpos;		    		    *temp = '\0';		    len = strlen(buff)+1;		    for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--);		    buff[endpos+1] = '\0';		}		/* FIXME: use next_token() */		if (!(clienttag = strtok(buff, " \t"))) { /* strtok modifies the string it is passed */		    continue;		}		if (strlen(clienttag) != 4) {		    continue;		}		if (strcmp(buff,"[ENDMAPS]") == 0) {		    break;		}		if (!(mapname = strtok(NULL," \t"))) {		    continue;		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -