Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #pragma warning (disable:4786)
  9. //=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
  10. //
  11. // The copyright to the contents herein is the property of Valve, L.L.C.
  12. // The contents may be used and/or copied only with the written permission of
  13. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  14. // the agreement/contract under which the contents have been supplied.
  15. //
  16. // Purpose: Implementation of CCustomAward
  17. //
  18. // $Workfile: $
  19. // $Date: $
  20. //
  21. //------------------------------------------------------------------------------------------------------
  22. // $Log: $
  23. //
  24. // $NoKeywords: $
  25. //=============================================================================
  26. #include <string.h>
  27. #include "TFStatsApplication.h"
  28. #include "CustomAward.h"
  29. #include "TextFile.h"
  30. #include "memdbg.h"
  31. //------------------------------------------------------------------------------------------------------
  32. // Function: CCustomAward::extendedinfo
  33. // Purpose: writes extra info about the award winner, like their score or something.
  34. // The extra info string is defined by the user in the configuration file like the
  35. //rest of a custom award's properties.
  36. // Input: html - the html file to write to
  37. //------------------------------------------------------------------------------------------------------
  38. void CCustomAward::extendedinfo(CHTMLFile& html)
  39. {
  40. if (extraInfoMsg.empty())
  41. return;
  42. char str[500];
  43. char outputstring[2000]={0};
  44. strcpy(str,extraInfoMsg.c_str());
  45. char delims[]={" \n\t"};
  46. char* temp=NULL;
  47. temp=strtok(str,delims);
  48. while(temp!=NULL)
  49. {
  50. char word[500];
  51. if (strnicmp(temp,"%player",strlen("%player"))==0)
  52. {
  53. char* more=&temp[strlen("%player")];
  54. sprintf(word,"%s%s",winnerName.c_str(),more);
  55. }
  56. else if (strnicmp(temp,"%winner",strlen("%winner"))==0)
  57. {
  58. char* more=&temp[strlen("%winner")];
  59. sprintf(word,"%s%s",winnerName.c_str(),more);
  60. }
  61. else if (strnicmp(temp,"%score",strlen("%score"))==0)
  62. {
  63. char* more=&temp[strlen("%score")];
  64. if (!namemode)
  65. sprintf(word,"%li%s",plrscores[winnerID],more);
  66. else
  67. sprintf(word,"%li%s",stringscores[winnerName],more);
  68. }
  69. else if (strnicmp(temp,"%number",strlen("%number"))==0)
  70. {
  71. //right now this is just the score
  72. char* more=&temp[strlen("%number")];
  73. if (!namemode)
  74. sprintf(word,"%li%s",plrnums[winnerID],more);
  75. else
  76. sprintf(word,"%li%s",stringscores[winnerName],more);
  77. }
  78. else
  79. strcpy(word,temp);
  80. strcat(outputstring," ");
  81. strcat(outputstring,word);
  82. temp=strtok(NULL,delims);
  83. }
  84. html.write(outputstring);
  85. }
  86. //------------------------------------------------------------------------------------------------------
  87. // Function: CCustomAward::noWinner
  88. // Purpose: writes some html saying that no one won this award. The noWinnerMsg
  89. // is defined by the user in the configuration file like all other custom
  90. // award properties
  91. // Input: html - the html file to write to
  92. //------------------------------------------------------------------------------------------------------
  93. void CCustomAward::noWinner(CHTMLFile& html)
  94. {
  95. if (noWinnerMsg.empty())
  96. return;
  97. html.write(noWinnerMsg.c_str());}
  98. //------------------------------------------------------------------------------------------------------
  99. // Function: CCustomAward::readCustomAward
  100. // Purpose: Factory method to read an award from a config file and return an
  101. // instance of the CCustomAward class
  102. // Input: f - the configuration file to read from
  103. // g_pMatchInfo - a pointer to a matchinfo object to give to the new award
  104. // Output: CCustomAward*
  105. //------------------------------------------------------------------------------------------------------
  106. CCustomAward* CCustomAward::readCustomAward(CTextFile& f)
  107. {
  108. const char* token=f.getToken();
  109. while (token)
  110. {
  111. if (!stricmp(token,"Award"))
  112. break;
  113. else if (!stricmp(token,"{"))
  114. f.discardBlock();
  115. token=f.getToken();
  116. }
  117. if (!token)
  118. return NULL;
  119. f.discard("{");
  120. token=f.getToken();
  121. CCustomAward* pCustAward=new TRACKED CCustomAward(g_pMatchInfo);
  122. while (token)
  123. {
  124. if (stricmp(token,"trigger")==0)
  125. {
  126. CCustomAwardTrigger* ptrig=CCustomAwardTrigger::readTrigger(f);
  127. pCustAward->triggers.push_back(ptrig);
  128. }
  129. else if (stricmp(token,"extraInfo")==0)
  130. {
  131. f.discard("=");
  132. pCustAward->extraInfoMsg=f.readString();
  133. f.discard(";");
  134. }
  135. else if (stricmp(token,"noWinnerMessage")==0)
  136. {
  137. f.discard("=");
  138. pCustAward->noWinnerMsg=f.readString();
  139. f.discard(";");
  140. }
  141. else if (stricmp(token,"name")==0)
  142. {
  143. f.discard("=");
  144. pCustAward->awardName=f.readString();
  145. f.discard(";");
  146. }
  147. else if (stricmp(token,"}")==0)
  148. {
  149. break;
  150. }
  151. else
  152. g_pApp->fatalError("Unrecognized Award property name while parsing %s: \"%s\" is not a property of an Award!",f.fileName().c_str(),token);
  153. token = f.getToken();
  154. }
  155. return pCustAward;
  156. }
  157. //------------------------------------------------------------------------------------------------------
  158. // Function: CCustomAward::getWinner
  159. // Purpose: generates the winner of this custom award.
  160. //------------------------------------------------------------------------------------------------------
  161. void CCustomAward::getWinner()
  162. {
  163. fNoWinner=true;
  164. CEventListIterator it;
  165. for (it=g_pMatchInfo->eventList()->begin();it!=g_pMatchInfo->eventList()->end();it++)
  166. {
  167. list<CCustomAwardTrigger*>::iterator tli;
  168. for (tli=triggers.begin();tli!=triggers.end();++tli)
  169. {
  170. if ((*tli)->matches(*it))
  171. {
  172. //increase the players count by X score.
  173. //scan for best at the end
  174. //different triggers have the player name/id placed differently. :(
  175. //if this returns -1, store based on name. (this way we can give awards to things other than player names
  176. //while remaining in this award/trigger hierarchy structure)
  177. PID ID =(*tli)->plrIDFromEvent(*it);
  178. if (ID==-1)
  179. {
  180. string ws=(*tli)->getTrackString(*it);
  181. stringscores[ws]+=(*tli)->plrValue;
  182. stringnums[ws]++;
  183. fNoWinner=false;
  184. namemode=true;
  185. }
  186. else
  187. {
  188. plrscores[ID]+=(*tli)->plrValue;
  189. plrnums[ID]++;
  190. fNoWinner=false;
  191. namemode=false;
  192. }
  193. }
  194. }
  195. }
  196. if (fNoWinner)
  197. return;
  198. if (!namemode)
  199. {
  200. //now scan and find highest score.
  201. map<PID,int>::iterator scores_it;
  202. scores_it=plrscores.begin();
  203. winnerID=(*scores_it).first;
  204. int winnerScore=(*scores_it).second;
  205. for (scores_it=plrscores.begin();scores_it!=plrscores.end();++scores_it)
  206. {
  207. int ID=(*scores_it).first;
  208. int score=(*scores_it).second;
  209. if (score > winnerScore)
  210. {
  211. winnerScore=score;
  212. winnerID=ID;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. //now scan and find highest score.
  219. map<string,int>::iterator scores_it;
  220. scores_it=stringscores.begin();
  221. winnerID=-1;
  222. winnerName=(*scores_it).first;
  223. int winnerScore=(*scores_it).second;
  224. for (scores_it=stringscores.begin();scores_it!=stringscores.end();++scores_it)
  225. {
  226. string name=(*scores_it).first;
  227. int score=(*scores_it).second;
  228. if (score > winnerScore)
  229. {
  230. winnerScore=score;
  231. winnerName=name;
  232. }
  233. }
  234. }
  235. }