Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 48.90 KB | None | 0 0
  1. #include "statslog.h"
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <gsql/gmysql.h>
  6. #include <gsql/gsql.h>
  7. pthread_mutex_t statsmutex;
  8. GText::String _mysql_user;
  9. GText::String _mysql_pass;
  10. GText::String _mysql_host;
  11. GText::String _mysql_port;
  12. GText::String _mysql_db;
  13. GText::String _degredation;
  14. //#define DEBUG_SQL_QUERIES 1
  15. int _mingames = 3;
  16. int _inactivedays = 30;
  17. pthread_t statsthreadid;
  18. void initDatabase()
  19. {
  20. static GSQL::GSQL *instance = NULL;
  21. instance = new GSQL::GMySQL(_mysql_host,_mysql_user,_mysql_pass,atoi(_mysql_port.data()));
  22. if(instance == NULL)
  23. return;
  24. if(!instance->does_database_exist(_mysql_db))
  25. instance->create_database(_mysql_db);
  26. instance->select_databse(_mysql_db);
  27. if(!instance->does_table_exist("ctf"))
  28. {
  29. instance->query("CREATE TABLE IF NOT EXISTS `ctf` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `cap` bigint(20) NOT NULL, `teamcap` bigint(20) NOT NULL, `win` bigint(20) NOT NULL, `tie` bigint(20) NOT NULL, `lose` bigint(20) NOT NULL,`points` BIGINT( 20 ) NOT NULL DEFAULT '1000', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  30. }
  31. if(!instance->does_table_exist("inf"))
  32. {
  33. instance->query("CREATE TABLE IF NOT EXISTS `inf` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `cap` bigint(20) NOT NULL, `teamcap` bigint(20) NOT NULL, `win` bigint(20) NOT NULL, `tie` bigint(20) NOT NULL, `lose` bigint(20) NOT NULL,`points` BIGINT( 20 ) NOT NULL DEFAULT '1000', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  34. }
  35. if(!instance->does_table_exist("htf"))
  36. {
  37. instance->query("CREATE TABLE IF NOT EXISTS `htf` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `win` bigint(20) NOT NULL, `tie` bigint(20) NOT NULL, `lose` bigint(20) NOT NULL,`points` BIGINT( 20 ) NOT NULL DEFAULT '1000', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  38. }
  39. if(!instance->does_table_exist("tdm"))
  40. {
  41. instance->query("CREATE TABLE IF NOT EXISTS `tdm` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `win` bigint(20) NOT NULL, `tie` bigint(20) NOT NULL, `lose` bigint(20) NOT NULL,`points` BIGINT( 20 ) NOT NULL DEFAULT '1000', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  42. }
  43. if(!instance->does_table_exist("dm"))
  44. {
  45. instance->query("CREATE TABLE IF NOT EXISTS `dm` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `rank` bigint(20) NOT NULL, `played` bigint(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  46. }
  47. if(!instance->does_table_exist("pm"))
  48. {
  49. instance->query("CREATE TABLE IF NOT EXISTS `pm` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `rank` bigint(20) NOT NULL, `played` bigint(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  50. }
  51. if(!instance->does_table_exist("rm"))
  52. {
  53. instance->query("CREATE TABLE IF NOT EXISTS `rm` (`id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(24) NOT NULL, `hwid` varchar(11) NOT NULL, `time_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `killed` bigint(20) NOT NULL, `death` bigint(20) NOT NULL, `rank` bigint(20) NOT NULL, `played` bigint(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=133");
  54. }
  55. if(instance != NULL)
  56. delete instance;
  57. }
  58.  
  59. static GText::String tds(const GText::String &str)
  60. {
  61. GText::String rets;
  62. int l;
  63. for(l=0;l<str.length();l++)
  64. if(str[l] == '\'')
  65. rets += "\\\'";
  66. else
  67. rets += str[l];
  68. // printf("tds %s -> %s\n",str.data(),rets.data());
  69. return rets;
  70. }
  71. void query(const GText::String &q, GSQL::GSQL::QueryResult *qr = NULL)
  72. {
  73.  
  74. GSQL::GSQL *sql = NULL;
  75. sql = new GSQL::GMySQL(_mysql_host,_mysql_user,_mysql_pass,atoi(_mysql_port.data()));
  76. if(sql != NULL)
  77. {
  78. sql->select_databse(_mysql_db);
  79. #ifdef DEBUG_SQL_QUERIES
  80. printf("QUERY : %s\n",q.data());
  81. #endif // DEBUG_SQL_QUERIES
  82. sql->query(q,qr);
  83. delete sql;
  84. }
  85. }
  86.  
  87. void degradeStats()
  88. {
  89. GSQL::GSQL::StoreQueryResult sqr;
  90. try
  91. {
  92. // DM
  93. query("UPDATE dm SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), rank=FLOOR(rank*" + _degredation + "), played=FLOOR(played*" + _degredation + "), time_updated=time_updated", &sqr);
  94. // PM
  95. query("UPDATE pm SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), rank=FLOOR(rank*" + _degredation + "), played=FLOOR(played*" + _degredation + "), time_updated=time_updated", &sqr);
  96. // RM
  97. query("UPDATE rm SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), rank=FLOOR(rank*" + _degredation + "), played=FLOOR(played*" + _degredation + "), time_updated=time_updated", &sqr);
  98. // CTF
  99. query("UPDATE ctf SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), cap=FLOOR(cap*" + _degredation + "), teamcap=FLOOR(teamcap*" + _degredation + "), win=FLOOR(win*" + _degredation + "), tie=FLOOR(tie*" + _degredation + "), lose=FLOOR(lose*" + _degredation + "), time_updated=time_updated", &sqr);
  100. // INF
  101. query("UPDATE inf SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), cap=FLOOR(cap*" + _degredation + "), teamcap=FLOOR(teamcap*" + _degredation + "), win=FLOOR(win*" + _degredation + "), tie=FLOOR(tie*" + _degredation + "), lose=FLOOR(lose*" + _degredation + "), time_updated=time_updated", &sqr);
  102. // HTF
  103. query("UPDATE htf SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), win=FLOOR(win*" + _degredation + "), tie=FLOOR(tie*" + _degredation + "), lose=FLOOR(lose*" + _degredation + "), time_updated=time_updated", &sqr);
  104. // TDM
  105. query("UPDATE tdm SET killed=FLOOR(killed*" + _degredation + "), death=FLOOR(death*" + _degredation + "), win=FLOOR(win*" + _degredation + "), tie=FLOOR(tie*" + _degredation + "), lose=FLOOR(lose*" + _degredation + "), time_updated=time_updated", &sqr);
  106. }
  107. catch(GException &exp)
  108. {
  109. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  110.  
  111. }
  112. }
  113. void saveLastUpdate(time_t update)
  114. {
  115. FILE * updatefile;
  116. updatefile = fopen("lastupdate.dat", "wb");
  117. if(updatefile != NULL)
  118. {
  119. fwrite(&update, sizeof(time_t), 1, updatefile);
  120. fclose(updatefile);
  121. }
  122. }
  123. time_t loadLastUpdate()
  124. {
  125. time_t update = 0;
  126. FILE * updatefile;
  127. updatefile = fopen("lastupdate.dat", "rb");
  128. if(updatefile != NULL)
  129. {
  130. fread(&update, sizeof(time_t), 1, updatefile);
  131. fclose(updatefile);
  132. }
  133. return update;
  134.  
  135. }
  136. void * statsthread(void * arg)
  137. {
  138. time_t lastupdate = loadLastUpdate();
  139. time_t now;
  140. struct tm * timeinfo;
  141.  
  142. while(1)
  143. {
  144. time(&now);
  145. // printf("Running Degrade Loop Now: %lu Update: %lu \n",(unsigned long)now, (unsigned long)lastupdate);
  146.  
  147. timeinfo = localtime(&now);
  148. if(timeinfo->tm_hour == 4 && timeinfo->tm_wday == 6 && (now-lastupdate) > 7200)
  149. {
  150. // printf("Degrading Stats\n");
  151. degradeStats();
  152. lastupdate = now;
  153. saveLastUpdate(now);
  154. }
  155. sleep(10);
  156. }
  157.  
  158. }
  159. void initStats()
  160. {
  161.  
  162. pthread_create(&statsthreadid, NULL, statsthread, NULL);
  163. }
  164.  
  165. void setStatsInfo(GText::String mysqluser,GText::String mysqlpass, GText::String mysqlhost, GText::String mysqlport, GText::String mysqldb, int inactivedays, int mingames, double degredation)
  166. {
  167. _mysql_user = mysqluser;
  168. _mysql_pass = mysqlpass;
  169. _mysql_host = mysqlhost;
  170. _mysql_port = mysqlport;
  171. _mysql_db = mysqldb;
  172. _inactivedays = inactivedays;
  173. _mingames = mingames;
  174. _degredation = GText::String(degredation);
  175. initDatabase();
  176. }
  177. void deinitStats()
  178. {
  179.  
  180. }
  181. GText::String checkStats(GText::String name, int gamestyle)
  182. {
  183. GText::String line("");
  184. try
  185. {
  186. //printf("Checking Stats for %s\n", name.data());
  187. GSQL::GSQL::StoreQueryResult sqr;
  188. // CTF
  189. if(gamestyle == 3)
  190. {
  191. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0) + COALESCE(cap/teamcap,0)))*1000+points) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, COALESCE(ROUND(cap/teamcap*100),0) AS capratio, win+tie+lose AS played, COALESCE(ROUND(win/(win+lose)*100),0) AS winratio, (SELECT COUNT(*)+1 FROM ctf WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0) + COALESCE(cap/teamcap,0)))*1000+points)>score AND (win+tie+lose) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (win+tie+lose) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death FROM ctf WHERE name LIKE \'%" + tds(name) + "%\' LIMIT 1",&sqr);
  192. if(sqr.rows() == 0) // didnt exist in the database
  193. {
  194. //line = "No stats for user: " + name;
  195. }
  196. else
  197. {
  198. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  199. {
  200. line += "CTF Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Cap: " + sqr.data(sqr.col_id_from_name("capratio"),0) + "% Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  201. }
  202. else
  203. {
  204. line += "CTF Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Cap: " + sqr.data(sqr.col_id_from_name("capratio"),0) + "% Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  205. }
  206. }
  207. }
  208. // INF
  209. if(gamestyle == 5)
  210. {
  211. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0) + COALESCE(cap/teamcap,0)))*1000+points) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, COALESCE(ROUND(cap/teamcap*100),0) AS capratio, win+tie+lose AS played, COALESCE(ROUND(win/(win+lose)*100),0) AS winratio, (SELECT COUNT(*)+1 FROM inf WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0) + COALESCE(cap/teamcap,0)))*1000+points)>score AND (win+tie+lose) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (win+tie+lose) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death FROM inf WHERE name LIKE \'%" + tds(name) + "%\' LIMIT 1",&sqr);
  212. if(sqr.rows() == 0) // didnt exist in the database
  213. {
  214. //line = "No stats for user: " + name;
  215. }
  216. else
  217. {
  218. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  219. {
  220. line += "INF Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Cap: " + sqr.data(sqr.col_id_from_name("capratio"),0) + "% Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  221. }
  222. else
  223. {
  224. line += "INF Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Cap: " + sqr.data(sqr.col_id_from_name("capratio"),0) + "% Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  225. }
  226. }
  227. }
  228.  
  229. // HTF
  230. if(gamestyle == 6)
  231. {
  232.  
  233. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0)))*1000+points) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, win+tie+lose AS played, COALESCE(ROUND(win/(win+lose)*100),0) AS winratio, (SELECT COUNT(*)+1 FROM htf WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0)))*1000+points)>score AND (win+tie+lose) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (win+tie+lose) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death FROM htf WHERE name LIKE\'%" + tds(name) + "%\' LIMIT 1",&sqr);
  234. if(sqr.rows() == 0) // didnt exist in the database
  235. {
  236. //line = "No stats for user: " + name;
  237. }
  238. else
  239. {
  240. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  241. {
  242. line += "HTF Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  243. }
  244. else
  245. {
  246. line += "HTF Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  247. }
  248. }
  249. }
  250. // TDM
  251. if(gamestyle == 2)
  252. {
  253.  
  254. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0)))*1000+points) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, win+tie+lose AS played, COALESCE(ROUND(win/(win+lose)*100),0) AS winratio, (SELECT COUNT(*)+1 FROM tdm WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(win/(win+lose),0)))*1000+points)>score AND (win+tie+lose) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (win+tie+lose) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death FROM tdm WHERE name\'%" + tds(name) + "%\' LIMIT 1",&sqr);
  255. if(sqr.rows() == 0) // didnt exist in the database
  256. {
  257. //line = "No stats for user: " + name;
  258. }
  259. else
  260. {
  261. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  262. {
  263. line += "TDM Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  264. }
  265. else
  266. {
  267. line += "TDM Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Win: " + sqr.data(sqr.col_id_from_name("winratio"),0) + "% Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  268. }
  269. }
  270. }
  271. // DM
  272. if(gamestyle == 0)
  273. {
  274.  
  275. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, (SELECT COUNT(*)+1 FROM dm WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000)>score AND (played) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (played) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death,played FROM dm WHERE name LIKE \'%" + tds(name) + "%\' LIMIT 1",&sqr);
  276. if(sqr.rows() == 0) // didnt exist in the database
  277. {
  278. //line = "No stats for user: " + name;
  279. }
  280. else
  281. {
  282. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  283. {
  284. line += "DM Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  285. }
  286. else
  287. {
  288. line += "DM Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  289. }
  290. }
  291. }
  292. // PM
  293. if(gamestyle == 1)
  294. {
  295.  
  296. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, (SELECT COUNT(*)+1 FROM pm WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000)>score AND (played) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (played) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death,played FROM pm WHERE name LIKE\'%" + tds(name) + "%\' LIMIT 1",&sqr);
  297. if(sqr.rows() == 0) // didnt exist in the database
  298. {
  299. //line = "No stats for user: " + name;
  300. }
  301. else
  302. {
  303. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  304. {
  305. line += "PM Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  306. }
  307. else
  308. {
  309. line += "PM Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  310. }
  311. }
  312. }
  313. // RM
  314. if(gamestyle == 4)
  315. {
  316.  
  317. query("SELECT ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000) AS score, COALESCE(ROUND(killed/death,2),\'Inf\') AS kpd, (SELECT COUNT(*)+1 FROM rm WHERE time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND ROUND((COALESCE(killed/death,killed) * (COALESCE(played/(rank+1),0)))*1000)>score AND (played) >= " + GText::String(_mingames) +") AS place, (time_updated > TIMESTAMPADD(DAY,-" + GText::String(_inactivedays) + ",NOW()) AND (played) >= " + GText::String(_mingames) +") AS ranked,id,name,time_updated,killed,death,played FROM rm WHERE name LIKE \'%" + tds(name) + "%\' LIMIT 1",&sqr);
  318. if(sqr.rows() == 0) // didnt exist in the database
  319. {
  320. //line = "No stats for user: " + name;
  321. }
  322. else
  323. {
  324. if(sqr.data(sqr.col_id_from_name("ranked"),0) == "1")
  325. {
  326. line += "RM Rank: " + sqr.data(sqr.col_id_from_name("place"),0) + " Score: " + sqr.data(sqr.col_id_from_name("score"),0) + " K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  327. }
  328. else
  329. {
  330. line += "RM Rank: N/A K/D: " + GText::String(atof(sqr.data(sqr.col_id_from_name("killed"),0).data())/atof(sqr.data(sqr.col_id_from_name("death"),0).data())) + " Rounds: " + sqr.data(sqr.col_id_from_name("played"),0) + " - " + sqr.data(sqr.col_id_from_name("name"),0) + " ";
  331. }
  332. }
  333. }
  334. }
  335. catch(GException &exp)
  336. {
  337. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  338.  
  339. }
  340. if(line == "")
  341. {
  342. line = "No stats found for: " + name;
  343. }
  344. return line;
  345. }
  346. struct players
  347. {
  348. GText::String name;
  349. int kills;
  350.  
  351. };
  352. std::vector <struct players> getRankedPlayers(struct refreshX * packet)
  353. {
  354. std::vector <struct players> list;
  355. struct players tempplayer;
  356. int loop = 0;
  357. int loop2 = 0;
  358. for(loop=0;loop < 32;loop++)
  359. {
  360. if(packet->players[loop].team != 255)
  361. {
  362. tempplayer.name = GText::String(packet->players[loop].name);
  363. tempplayer.kills = packet->players[loop].kills;
  364. list.push_back(tempplayer);
  365. }
  366. }
  367. for(loop=0;loop < list.size();loop++)
  368. {
  369. for(loop2=0;loop2 < list.size();loop2++)
  370. {
  371. if (list[loop].kills > list[loop2].kills) //comparing member of struct
  372. {
  373. tempplayer = list[loop];
  374. list[loop] = list[loop2];
  375. list[loop2] = tempplayer;
  376. }
  377.  
  378. }
  379. }
  380. return list;
  381. }
  382. int getrank(GText::String name, std::vector <struct players> list)
  383. {
  384. int retur = 1;
  385. int loop = 0;
  386. for(loop=0;loop < list.size();loop++)
  387. {
  388. if(name == list[loop].name)
  389. {
  390. retur = loop+1;
  391. break;
  392. }
  393. }
  394. return retur;
  395.  
  396. }
  397. struct teams
  398. {
  399. int team;
  400. int score;
  401. };
  402. int getWinner(int alphascore, int bravoscore, int charliescore, int deltascore)
  403. {
  404. //printf("A: %i B: %i C: %i D: %i\n",alphascore,bravoscore,charliescore,deltascore);
  405. std::vector<struct teams> list;
  406. struct teams tempteam;
  407. int loop,loop2;
  408. tempteam.team = 1;
  409. tempteam.score = alphascore;
  410. list.push_back(tempteam);
  411. tempteam.team = 2;
  412. tempteam.score = bravoscore;
  413. list.push_back(tempteam);
  414. tempteam.team = 3;
  415. tempteam.score = charliescore;
  416. list.push_back(tempteam);
  417. tempteam.team = 4;
  418. tempteam.score = deltascore;
  419. list.push_back(tempteam);
  420. for(loop=0;loop < list.size();loop++)
  421. {
  422. for(loop2=0;loop2 < list.size();loop2++)
  423. {
  424. if (list[loop].score > list[loop2].score) //comparing member of struct
  425. {
  426. tempteam = list[loop];
  427. list[loop] = list[loop2];
  428. list[loop2] = tempteam;
  429. }
  430. }
  431. }
  432.  
  433. //printf("TEAM[0]: %i SCORE: %i\n", list[0].team,list[0].score);
  434. //printf("TEAM[1]: %i SCORE: %i\n", list[1].team,list[1].score);
  435. //printf("TEAM[2]: %i SCORE: %i\n", list[2].team,list[2].score);
  436. //printf("TEAM[3]: %i SCORE: %i\n", list[3].team,list[3].score);
  437. if(list[0].score == list[1].score)
  438. {
  439. return 0;
  440. }
  441. return list[0].team;
  442.  
  443. }
  444.  
  445. void updateStats(struct refreshX * packet)
  446. {
  447. //printf("Updating stats\n");
  448. if(_mysql_host == "")
  449. return;
  450. int loop = 0;
  451. int teams = 0;
  452. int alphawin = 0;
  453. int bravowin = 0;
  454. int charliewin = 0;
  455. int deltawin = 0;
  456. int alphaloose = 0;
  457. int bravoloose = 0;
  458. int charlieloose = 0;
  459. int deltaloose = 0;
  460. int alphaplayers = 0;
  461. int bravoplayers = 0;
  462. int charlieplayers = 0;
  463. int deltaplayers = 0;
  464. int alphapoints = 0;
  465. int bravopoints = 0;
  466. int charliepoints = 0;
  467. int deltapoints = 0;
  468. int newalphapoints = 0;
  469. int newbravopoints = 0;
  470. int newcharliepoints = 0;
  471. int newdeltapoints = 0;
  472. int points = 0;
  473. int tie = 0;
  474. double t=0;
  475. double n=0;
  476. int winner = getWinner(packet->alphascore,packet->bravoscore,packet->charliescore,packet->deltascore);
  477. std::vector <struct players> list = getRankedPlayers(packet);
  478. GSQL::GSQL::StoreQueryResult sqr;
  479. if(packet->gamestyle == 2) //TDM Special case
  480. {
  481. switch(winner)
  482. {
  483. case 0:
  484. tie = 1;
  485. break;
  486. case 1:
  487. alphawin = 1;
  488. bravoloose = 1;
  489. charlieloose = 1;
  490. deltaloose = 1;
  491. break;
  492. case 2:
  493. bravowin = 1;
  494. alphaloose = 1;
  495. charlieloose = 1;
  496. deltaloose = 1;
  497. break;
  498. case 3:
  499. charliewin = 1;
  500. alphaloose = 1;
  501. bravoloose = 1;
  502. deltaloose = 1;
  503. break;
  504. case 4:
  505. deltawin = 1;
  506. alphaloose = 1;
  507. bravoloose = 1;
  508. deltaloose = 1;
  509. break;
  510. default:
  511. tie = 1;
  512. break;
  513.  
  514. }
  515.  
  516. }
  517. else
  518. {
  519. if(packet->alphascore > packet->bravoscore) // alpha won
  520. {
  521. alphawin = 1;
  522. bravoloose = 1;
  523.  
  524. }
  525. else if(packet->alphascore < packet->bravoscore) // bravo won
  526. {
  527. bravowin = 1;
  528. alphaloose = 1;
  529. }
  530. else if(packet->alphascore == packet->bravoscore)
  531. {
  532. tie = 1;
  533. }
  534. }
  535. // calculate skill poins for Team based gamestyles except tdm which is on the TODO list
  536. if(packet->gamestyle == 3 || packet->gamestyle == 5 || packet->gamestyle == 6 || packet->gamestyle == 2)
  537. {
  538. for(loop=0;loop < 32;loop++)
  539. {
  540. if(packet->players[loop].team == 1) // alpha
  541. {
  542. alphaplayers++;
  543. try
  544. {
  545. if(packet->gamestyle == 3) // CTF
  546. {
  547. query("SELECT * FROM ctf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  548. if(sqr.rows() == 0) // user doesn't exist
  549. {
  550. alphapoints += 1000;
  551. }
  552. else // user exist
  553. {
  554. alphapoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  555. }
  556. }
  557. else if(packet->gamestyle == 5) // INF
  558. {
  559. query("SELECT * FROM inf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  560. if(sqr.rows() == 0) // user doesn't exist
  561. {
  562. alphapoints += 1000;
  563. }
  564. else // user exist
  565. {
  566. alphapoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  567. }
  568. }
  569. else if(packet->gamestyle == 6) // HTF
  570. {
  571. query("SELECT * FROM htf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  572. if(sqr.rows() == 0) // user doesn't exist
  573. {
  574. alphapoints += 1000;
  575. }
  576. else // user exist
  577. {
  578. alphapoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  579. }
  580. }
  581.  
  582.  
  583. }
  584. catch(GException &exp)
  585. {
  586. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  587.  
  588. }
  589. }
  590. else if(packet->players[loop].team == 2) // bravo
  591. {
  592. try
  593. {
  594. if(packet->gamestyle == 3) // CTF
  595. {
  596. query("SELECT * FROM ctf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  597. if(sqr.rows() == 0) // user doesn't exist
  598. {
  599. bravopoints += 1000;
  600. }
  601. else // user exist
  602. {
  603. bravopoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  604. }
  605. }
  606. else if(packet->gamestyle == 5) // INF
  607. {
  608. query("SELECT * FROM inf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  609. if(sqr.rows() == 0) // user doesn't exist
  610. {
  611. bravopoints += 1000;
  612. }
  613. else // user exist
  614. {
  615. bravopoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  616. }
  617. }
  618. else if(packet->gamestyle == 6) // HTF
  619. {
  620. query("SELECT * FROM htf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  621. if(sqr.rows() == 0) // user doesn't exist
  622. {
  623. bravopoints += 1000;
  624. }
  625. else // user exist
  626. {
  627. bravopoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  628. }
  629. }
  630.  
  631.  
  632. }
  633. catch(GException &exp)
  634. {
  635. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  636.  
  637. }
  638. bravoplayers++;
  639. }
  640. else if(packet->players[loop].team == 3) // charlie
  641. {
  642. try
  643. {
  644. if(packet->gamestyle == 2) // TDM
  645. {
  646. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  647. if(sqr.rows() == 0) // user doesn't exist
  648. {
  649. charliepoints += 1000;
  650. }
  651. else // user exist
  652. {
  653. charliepoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  654. }
  655. }
  656. }
  657. catch(GException &exp)
  658. {
  659. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  660.  
  661. }
  662. charlieplayers++;
  663. }
  664. else if(packet->players[loop].team == 4) // delta
  665. {
  666. try
  667. {
  668. if(packet->gamestyle == 2) // TDM
  669. {
  670. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  671. if(sqr.rows() == 0) // user doesn't exist
  672. {
  673. deltapoints += 1000;
  674. }
  675. else // user exist
  676. {
  677. deltapoints += atoi(sqr.data(sqr.col_id_from_name("points"),0).data());
  678. }
  679. }
  680. }
  681. catch(GException &exp)
  682. {
  683. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  684.  
  685. }
  686. deltaplayers++;
  687. }
  688. }
  689. if(alphaplayers != 0)
  690. {
  691. teams++;
  692. }
  693. if(bravoplayers != 0)
  694. {
  695. teams++;
  696. }
  697. if(charlieplayers != 0)
  698. {
  699. teams++;
  700. }
  701. if(deltaplayers != 0)
  702. {
  703. teams++;
  704. }
  705.  
  706. if(alphawin == 1 && teams > 1) // alpha won
  707. {
  708. t = (double)50*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams);
  709. n = (double)1+pow((double)10,((double)(alphapoints)-(double)((bravopoints+charliepoints+deltapoints)/(teams-1)))/(double)(1000*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams)));
  710. points = (int)(t/n);
  711. newalphapoints += points/teams;
  712. newbravopoints -= points/teams;
  713. newcharliepoints -= points/teams;
  714. newdeltapoints -= points/teams;
  715. }
  716. else if(bravowin == 1 && teams > 1) // bravo won
  717. {
  718. t = (double)50*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams);
  719. n = (double)1+pow((double)10,((double)(bravopoints)-(double)((alphapoints+charliepoints+deltapoints)/(teams-1)))/(double)(1000*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams)));
  720. points = (int)(t/n);
  721. newalphapoints -= points/teams;
  722. newbravopoints += points/teams;
  723. newcharliepoints -= points/teams;
  724. newdeltapoints -= points/teams;
  725. }
  726. else if(charliewin == 1 && teams > 1) // charlie won
  727. {
  728. t = (double)50*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams);
  729. n = (double)1+pow((double)10,((double)(charliepoints)-(double)((alphapoints+bravopoints+deltapoints)/(teams-1)))/(double)(1000*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams)));
  730. points = (int)(t/n);
  731. newalphapoints -= points/teams;
  732. newbravopoints -= points/teams;
  733. newcharliepoints += points/teams;
  734. newdeltapoints -= points/teams;
  735. }
  736. else if(deltawin == 1 && teams > 1) // delta won
  737. {
  738. t = (double)50*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams);
  739. n = (double)1+pow((double)10,((double)(deltapoints)-(double)((alphapoints+bravopoints+charliepoints)/(teams-1)))/(double)(1000*((alphaplayers+bravoplayers+charlieplayers+deltaplayers)/teams)));
  740. points = (int)(t/n);
  741. newalphapoints -= points/teams;
  742. newbravopoints -= points/teams;
  743. newcharliepoints -= points/teams;
  744. newdeltapoints += points/teams;
  745. }
  746.  
  747.  
  748. }
  749. for(loop=0;loop < 32;loop++)
  750. {
  751. try
  752. {
  753.  
  754. if(packet->players[loop].team == 1 && packet->gamestyle == 3) // alpha CTF
  755. {
  756. query("SELECT * FROM ctf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  757. if(sqr.rows() == 0) // user doesn't exist
  758. {
  759. query("INSERT INTO ctf (name,hwid,killed,death,cap,teamcap,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(packet->players[loop].caps)) + ", " + tds(GText::String(packet->alphascore)) + ", " + tds(GText::String(alphawin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(alphaloose)) + ", "+ tds(GText::String(1000+newalphapoints)) + ")",&sqr);
  760. }
  761. else // user exist so lets update it
  762. {
  763. query("UPDATE ctf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", cap=cap+" + tds(GText::String(packet->players[loop].caps)) + ", teamcap=teamcap+" + tds(GText::String(packet->alphascore)) + ", win=win+" + tds(GText::String(alphawin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(alphaloose)) + ", points=points+" + tds(GText::String(newalphapoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  764. }
  765. }
  766. else if(packet->players[loop].team == 2 && packet->gamestyle == 3) // bravo CTF
  767. {
  768. query("SELECT * FROM ctf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  769. if(sqr.rows() == 0) // user doesn't exist
  770. {
  771. query("INSERT INTO ctf (name,hwid,killed,death,cap,teamcap,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(packet->players[loop].caps)) + ", " + tds(GText::String(packet->bravoscore)) + ", " + tds(GText::String(bravowin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(bravoloose)) + ", "+ tds(GText::String(1000+newbravopoints)) + ")",&sqr);
  772. }
  773. else // user exist so lets update it
  774. {
  775. query("UPDATE ctf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", cap=cap+" + tds(GText::String(packet->players[loop].caps)) + ", teamcap=teamcap+" + tds(GText::String(packet->bravoscore)) + ", win=win+" + tds(GText::String(bravowin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(bravoloose)) + ", points=points+" + tds(GText::String(newbravopoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  776. }
  777. }
  778. else if(packet->players[loop].team == 1 && packet->gamestyle == 5) // alpha INF
  779. {
  780. query("SELECT * FROM inf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  781. if(sqr.rows() == 0) // user doesn't exist
  782. {
  783. query("INSERT INTO inf (name,hwid,killed,death,cap,teamcap,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(packet->players[loop].caps*30)) + ", " + tds(GText::String(packet->alphascore)) + ", " + tds(GText::String(alphawin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(alphaloose)) + ", " + tds(GText::String(1000+newalphapoints)) + ")",&sqr);
  784. }
  785. else // user exist so lets update it
  786. {
  787. query("UPDATE inf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", cap=cap+" + tds(GText::String(packet->players[loop].caps*30)) + ", teamcap=teamcap+" + tds(GText::String(packet->alphascore)) + ", win=win+" + tds(GText::String(alphawin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(alphaloose)) + ", points=points+" + tds(GText::String(newalphapoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  788. }
  789. }
  790. else if(packet->players[loop].team == 2 && packet->gamestyle == 5) // bravo INF
  791. {
  792. query("SELECT * FROM inf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  793. if(sqr.rows() == 0) // user doesn't exist
  794. {
  795. query("INSERT INTO inf (name,hwid,killed,death,cap,teamcap,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(0)) + ", " + tds(GText::String(0)) + ", " + tds(GText::String(bravowin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(bravoloose)) + ", " + tds(GText::String(1000+newbravopoints)) + ")",&sqr);
  796. }
  797. else // user exist so lets update it
  798. {
  799. query("UPDATE inf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", cap=cap+" + tds(GText::String(0)) + ", teamcap=teamcap+" + tds(GText::String(0)) + ", win=win+" + tds(GText::String(bravowin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(bravoloose)) + ", points=points+" + tds(GText::String(newbravopoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  800. }
  801. }
  802. else if(packet->players[loop].team == 1 && packet->gamestyle == 6) // alpha HTF
  803. {
  804. query("SELECT * FROM htf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  805. if(sqr.rows() == 0) // user doesn't exist
  806. {
  807. query("INSERT INTO htf (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(alphawin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(alphaloose)) + ", " + tds(GText::String(1000+newalphapoints)) + ")",&sqr);
  808. }
  809. else // user exist so lets update it
  810. {
  811. query("UPDATE htf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(alphawin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(alphaloose)) + ", points=points+" + tds(GText::String(newalphapoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  812. }
  813. }
  814. else if(packet->players[loop].team == 2 && packet->gamestyle == 6) // bravo HTF
  815. {
  816. query("SELECT * FROM htf WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  817. if(sqr.rows() == 0) // user doesn't exist
  818. {
  819. query("INSERT INTO htf (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(bravowin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(bravoloose)) + ", " + tds(GText::String(1000+newbravopoints)) + ")",&sqr);
  820. }
  821. else // user exist so lets update it
  822. {
  823. query("UPDATE htf SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(bravowin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(bravoloose)) + ", points=points+" + tds(GText::String(newbravopoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  824. }
  825. }
  826. else if(packet->players[loop].team == 1 && packet->gamestyle == 2) // alpha TDM
  827. {
  828. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  829. if(sqr.rows() == 0) // user doesn't exist
  830. {
  831. query("INSERT INTO tdm (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(alphawin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(alphaloose)) + ", " + tds(GText::String(1000+newalphapoints)) + ")",&sqr);
  832. }
  833. else // user exist so lets update it
  834. {
  835. query("UPDATE tdm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(alphawin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(alphaloose)) + ", points=points+" + tds(GText::String(newalphapoints)) + " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  836. }
  837. }
  838. else if(packet->players[loop].team == 2 && packet->gamestyle == 2) // bravo TDM
  839. {
  840. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  841. if(sqr.rows() == 0) // user doesn't exist
  842. {
  843. query("INSERT INTO tdm (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(bravowin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(bravoloose)) + ", " + tds(GText::String(1000+newbravopoints))+ ")",&sqr);
  844. }
  845. else // user exist so lets update it
  846. {
  847. query("UPDATE tdm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(bravowin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(bravoloose)) + ", points=points+" + tds(GText::String(newbravopoints))+ " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  848. }
  849. }
  850. else if(packet->players[loop].team == 3 && packet->gamestyle == 2) // charlie TDM
  851. {
  852. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  853. if(sqr.rows() == 0) // user doesn't exist
  854. {
  855. query("INSERT INTO tdm (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(charliewin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(charlieloose)) + ", " + tds(GText::String(1000+newcharliepoints)) + ")",&sqr);
  856. }
  857. else // user exist so lets update it
  858. {
  859. query("UPDATE tdm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(charliewin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(charlieloose)) + ", points=points+" + tds(GText::String(newcharliepoints))+ " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  860. }
  861. }
  862. else if(packet->players[loop].team == 4 && packet->gamestyle == 2) // delta TDM
  863. {
  864. query("SELECT * FROM tdm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  865. if(sqr.rows() == 0) // user doesn't exist
  866. {
  867. query("INSERT INTO tdm (name,hwid,killed,death,win,tie,lose,points) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(deltawin)) + ", " + tds(GText::String(tie)) + ", "+ tds(GText::String(deltaloose)) + ", " + tds(GText::String(1000+newdeltapoints)) + ")",&sqr);
  868. }
  869. else // user exist so lets update it
  870. {
  871. query("UPDATE tdm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", win=win+" + tds(GText::String(deltawin)) + ", tie=tie+" + tds(GText::String(tie)) + ", lose=lose+" + tds(GText::String(deltaloose)) + ", points=points+" + tds(GText::String(newdeltapoints))+ " WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  872. }
  873. }
  874. else if(packet->players[loop].team == 0 && packet->gamestyle == 0) // DM
  875. {
  876. query("SELECT * FROM dm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  877. if(sqr.rows() == 0) // user doesn't exist
  878. {
  879. query("INSERT INTO dm (name,hwid,killed,death,rank,played) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", 1)",&sqr);
  880. }
  881. else // user exist so lets update it
  882. {
  883. query("UPDATE dm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", rank=rank+" + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", played=played+1 WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  884. }
  885. }
  886. else if(packet->players[loop].team == 0 && packet->gamestyle == 1) // PM
  887. {
  888. query("SELECT * FROM pm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  889. if(sqr.rows() == 0) // user doesn't exist
  890. {
  891. query("INSERT INTO pm (name,hwid,killed,death,rank,played) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", 1)",&sqr);
  892. }
  893. else // user exist so lets update it
  894. {
  895. query("UPDATE pm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", rank=rank+" + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", played=played+1 WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  896. }
  897. }
  898. else if(packet->players[loop].team == 0 && packet->gamestyle == 4) // RM
  899. {
  900. query("SELECT * FROM rm WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) +"\'",&sqr);
  901. if(sqr.rows() == 0) // user doesn't exist
  902. {
  903. query("INSERT INTO rm (name,hwid,killed,death,rank,played) VALUES (\'" + tds(GText::String(packet->players[loop].name)) + "\', " + tds(GText::String(packet->players[loop].hwid)) + "\', " + tds(GText::String(packet->players[loop].kills)) + ", " + tds(GText::String(packet->players[loop].deaths)) + ", " + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", 1)",&sqr);
  904. }
  905. else // user exist so lets update it
  906. {
  907. query("UPDATE rm SET name='" + tds(GText::String(packet->players[loop].name)) + "', killed=killed+" + tds(GText::String(packet->players[loop].kills)) + ", death=death+" + tds(GText::String(packet->players[loop].deaths)) + ", rank=rank+" + tds(GText::String(getrank(GText::String(packet->players[loop].name),list))) + ", played=played+1 WHERE hwid=\'" + tds(GText::String(packet->players[loop].hwid)) + "\'",&sqr);
  908. }
  909. }
  910. }
  911. catch(GException &exp)
  912. {
  913. fprintf(stderr,"Unhandled exception %s\n",exp.what());
  914.  
  915. }
  916. }
  917. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement