00001 #include "playerlist.h" 00002 00003 //ta bort 00004 #include <stdio.h> 00005 00006 //Constructors 00007 PlayerList::PlayerList(){ 00008 this->numplayers =0; 00009 this->isnotfull = true; 00010 this->setMaxPlayers(2); 00011 //freeslots = (bool *)malloc(maxplayers*sizeof(bool)); 00012 //for(int i=0;i<maxplayers;i++) 00013 // freeslots[i] = true; 00014 } 00015 00016 int PlayerList::getMaxPlayers(){ 00017 return(maxplayers); 00018 } 00019 00020 00021 //Warning this will erase all players in this class. 00022 int PlayerList::setMaxPlayers(int num){ 00023 this->maxplayers = num; 00024 players = (Player **)malloc(maxplayers*sizeof(Player)); 00025 00026 freeslots = (bool *)malloc(maxplayers*sizeof(bool)); 00027 for(int i=0;i<maxplayers;i++){ 00028 freeslots[i] = true; 00029 players[i] = NULL; 00030 } 00031 if(players == NULL && freeslots == NULL) 00032 return(-1); 00033 return(maxplayers); 00034 } 00035 00036 int PlayerList::getNumPlayers(){ 00037 return(numplayers); 00038 } 00039 00040 00041 Player* PlayerList::getPlayerById(int playerid){ 00042 /* 00043 for(int i=0; i<numplayers;i++) 00044 if(playerid == players[i]->getID()) 00045 return(players[i]); 00046 return(NULL);*/ 00047 return(players[playerid]); 00048 } 00049 00050 char *PlayerList::getPlayerNameById(int playerid){ 00051 Player *p = getPlayerById(playerid); 00052 char *name = (char *)malloc(strlen(p->getName())*sizeof(char)); 00053 strcpy(name, p->getName()); 00054 return(name); 00055 } 00056 00057 00058 00059 int PlayerList::addPlayer(Player *p){ 00060 if(!isnotfull) 00061 return(-1); 00062 numplayers++; 00063 /* 00064 for(int i=0;i<numplayers;i++) 00065 if(freeslots[i]) 00066 break;*/; 00067 int i = p->getID(); 00068 players[i] = new Player(*p); 00069 freeslots[i] = false; 00070 if(numplayers == maxplayers) 00071 isnotfull = false; 00072 return(numplayers); 00073 } 00074 00075 00076 00077 //TESTA 00078 int PlayerList::removePlayerById(int playerid){ 00079 /* 00080 for(int i=0; i<numplayers;i++) 00081 if(playerid == players[i]->getID()){ 00082 numplayers--; 00083 Player *p = players[i]; 00084 free(p); 00085 freeslots[i] = true; 00086 isnotfull = true; 00087 return(numplayers); 00088 } 00089 */ 00090 if(!players[playerid]) 00091 return(-1); 00092 numplayers--; 00093 free(players[playerid]); 00094 players[playerid] = NULL; 00095 freeslots[playerid] = true; 00096 isnotfull = true; 00097 return(numplayers); 00098 } 00099 00100 00101 void PlayerList::removeAllPlayers(){ 00102 for(int i=0; i<numplayers;i++){ 00103 Player *p = players[i]; 00104 free(p); 00105 freeslots[i] = true; 00106 } 00107 numplayers = 0; 00108 isnotfull = true; 00109 } 00110 00111 00112 00113 int PlayerList::setPlayerLatancy(int playerid, int latancy){ 00114 if(!players[playerid]) 00115 return(-1); 00116 players[playerid]->setLatancy(latancy); 00117 return(0); 00118 } 00119 00120 00121 int PlayerList::getPlayerLatancy(int playerid){ 00122 if(!players[playerid]) 00123 return(-1); 00124 return(players[playerid]->getLatancy()); 00125 } 00126 00127 00128 int PlayerList::setLastTimeStamp(int playerid, DWORD timestamp){ 00129 Player *p = this->getPlayerById(playerid); 00130 if(p == NULL) 00131 return(-1); 00132 p->setLastTimeStamp(timestamp); 00133 return(1); 00134 } 00135 00136 00137 DWORD PlayerList::getLastTimeSamp(int playerid){ 00138 if(!players[playerid]) 00139 return(-1); 00140 return(players[playerid]->getLastTimeStamp()); 00141 } 00142 00143 bool PlayerList::isNotFull(){ 00144 return(isnotfull); 00145 } 00146 00147 00148 int PlayerList::getFreeId(){ 00149 int i; 00150 for(i=1;i<maxplayers;i++) 00151 if(freeslots[i]) 00152 return(i); 00153 return(-1); 00154 } 00155 00156 void PlayerList::setPlayerName(int playerid, const char *name){ 00157 (this->getPlayerById(playerid))->setName(name); 00158 }