#include "version.h" #include #include #include #include #include "saacproto_serv.h" #include "main.h" #include "char.h" #include "util.h" #include "defend.h" #ifdef _DEFEND_NEWUSERS extern gmsv gs[MAXCONNECTION]; #define DEFEND_FILENAME "db/defend/defend.txt" #define SCORE_FILENAME "db/defend/score.txt" DefendArrayList *DEFENDUserList = NULL; ScoreArrayList *DEFENDScoreList = NULL; static int DEFEND_MAXSCORE = 0; static int DEFEND_MINSCORE = 0; void DEFEND_InitList() { log("DEFEND_InitList( %d) - %4.2f KB.. \n", DEFEND_MAXLISTS, (float)(sizeof( struct _tagDefendArrayList)*DEFEND_MAXLISTS)/1024 ); DEFENDUserList = (DefendArrayList*)calloc( 1, sizeof( struct _tagDefendArrayList)*DEFEND_MAXLISTS); if( DEFENDUserList == NULL ){ log("Can't calloc sizeof(%4.2fKB.)err !!\n", (float)(sizeof( struct _tagDefendArrayList)*DEFEND_MAXLISTS)/1024 ); return; } DEFENDScoreList = (ScoreArrayList*)calloc( 1, sizeof( struct _tagScoreArrayList)*DEFEND_SCOREMAXLISTS); if( DEFENDScoreList == NULL ){ log("Can't calloc sizeof(%4.2fKB.)err !!\n", (float)(sizeof( struct _tagDefendArrayList)*DEFEND_SCOREMAXLISTS)/1024 ); return; } DEFEND_resetUserForList(); DEFEND_ReadUserForList( DEFEND_FILENAME); DEFEND_resetScoreForList(); DEFEND_ReadScoreForList( SCORE_FILENAME); } int DEFEND_getUse( int si) { if( si >= DEFEND_MAXLISTS || si < 0 ) return -1; return DEFENDUserList[ si].use; } int DEFEND_setUse( int si, int num) { int olds; if( si >= DEFEND_MAXLISTS || si < 0 ) return -1; olds = DEFENDUserList[ si].use; DEFENDUserList[ si].use = num; return olds; } void DEFEND_resetUserForList() { int i; for( i=0; i 0 ){ DEFEND_delOneUserForList( j); } continue; } DEFEND_copyOneUserForList( j, j+1); } } } for( i=0; i= DEFEND_MAXLISTS ) return -1; ti = i; for( i=(DEFEND_MAXLISTS-1); i>ti; i--){ if( DEFEND_getUse( i-1) <= 0 ) continue; DEFEND_copyOneUserForList( i, i-1); } memcpy( DEFENDUserList[ ti].cdkey, cdkey, sizeof( DEFENDUserList[ ti].cdkey) ); memcpy( DEFENDUserList[ ti].name, name, sizeof( DEFENDUserList[ ti].name) ); DEFENDUserList[ ti].defnums = nums; //次数 DEFENDUserList[ ti].type = type; DEFENDUserList[ ti].score = score; DEFENDUserList[ ti].use = 1; return ti; } void DEFEND_LoadUserForList( char *listarg) { char buf2[256]; char cdkey[256], name[256]; int type, nums, score; easyGetTokenFromBuf( listarg, ',', 1, buf2, sizeof(buf2)); memcpy( cdkey, buf2, sizeof( cdkey)); easyGetTokenFromBuf( listarg, ',', 2, buf2, sizeof(buf2)); memcpy( name, buf2, sizeof( name)); easyGetTokenFromBuf( listarg, ',', 3, buf2, sizeof(buf2)); nums = atoi( buf2); easyGetTokenFromBuf( listarg, ',', 4, buf2, sizeof(buf2)); score = atoi( buf2); easyGetTokenFromBuf( listarg, ',', 5, buf2, sizeof(buf2)); type = atoi( buf2); if( DEFEND_addUserForList( cdkey, name, score, nums, type) != -1 ){ DEFEND_ShowUserLists(); DEFEND_SendToUserLists(); } } void DEFEND_ReadUserForList( char *filename) { FILE *fp=NULL; char buf1[256], buf2[256]; char cdkey[256], name[256]; int type, nums, score; fp = fopen( filename, "r"); if( fp == NULL ){ log( "Can't Read %s..!\n", filename); return; } while( fgets( buf1 , sizeof( buf1 ) , fp )){ easyGetTokenFromBuf( buf1, ',', 1, buf2, sizeof(buf2)); memcpy( cdkey, buf2, sizeof( cdkey)); easyGetTokenFromBuf( buf1, ',', 2, buf2, sizeof(buf2)); memcpy( name, buf2, sizeof( name)); easyGetTokenFromBuf( buf1, ',', 3, buf2, sizeof(buf2)); nums = atoi( buf2); easyGetTokenFromBuf( buf1, ',', 4, buf2, sizeof(buf2)); score = atoi( buf2); easyGetTokenFromBuf( buf1, ',', 5, buf2, sizeof(buf2)); type = atoi( buf2); DEFEND_addUserForList( cdkey, name, score, nums, type); } fclose( fp); DEFEND_ShowUserLists(); } void DEFEND_ShowUserLists() { int i; if( DEFENDUserList == NULL ) return; for( i=0; i= DEFEND_SCOREMAXLISTS || si < 0 ) return -1; return DEFENDScoreList[ si].use; } void DEFEND_ReadScoreForList( char *filename) { FILE *fp=NULL; char buf1[256], buf2[256]; char cdkey[256], name[256], fromname[256]; int score; fp = fopen( filename, "r"); if( fp == NULL ){ log( "Can't Read %s..!\n", filename); return; } while( fgets( buf1 , sizeof( buf1 ) , fp )){ int i; if( strlen(buf1) < 1 || strlen(buf1) > 512 ) continue; for( i=0; i