CHighScoreScreen.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * CHighScoreScreen.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CHighScoreScreen.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/WindowHandler.h"
  14. #include "../gui/Shortcut.h"
  15. #include "../widgets/TextControls.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/Images.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../render/Canvas.h"
  20. #include "../CGameInfo.h"
  21. #include "../CVideoHandler.h"
  22. #include "../CMusicHandler.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/CCreatureHandler.h"
  26. #include "../../lib/constants/EntityIdentifiers.h"
  27. #include "../../lib/TextOperations.h"
  28. #include "vstd/DateUtils.h"
  29. auto HighScoreCalculation::calculate()
  30. {
  31. struct result
  32. {
  33. int basic;
  34. int total;
  35. int sumDays;
  36. bool cheater;
  37. };
  38. std::vector<int> scoresBasic;
  39. std::vector<int> scoresTotal;
  40. double sumBasic = 0;
  41. double sumTotal = 0;
  42. int sumDays = 0;
  43. bool cheater = false;
  44. for(auto & param : parameters)
  45. {
  46. double tmp = 200 - (param.day + 10) / (param.townAmount + 5) + (param.allDefeated ? 25 : 0) + (param.hasGrail ? 25 : 0);
  47. scoresBasic.push_back(static_cast<int>(tmp));
  48. sumBasic += tmp;
  49. if(param.difficulty == 0)
  50. tmp *= 0.8;
  51. if(param.difficulty == 1)
  52. tmp *= 1.0;
  53. if(param.difficulty == 2)
  54. tmp *= 1.3;
  55. if(param.difficulty == 3)
  56. tmp *= 1.6;
  57. if(param.difficulty == 4)
  58. tmp *= 2.0;
  59. scoresTotal.push_back(static_cast<int>(tmp));
  60. sumTotal += tmp;
  61. sumDays += param.day;
  62. if(param.usedCheat)
  63. cheater = true;
  64. }
  65. if(scoresBasic.size() == 1)
  66. return result { scoresBasic[0], scoresTotal[0], sumDays , cheater};
  67. return result { static_cast<int>((sumBasic / parameters.size()) * 5.0), static_cast<int>((sumTotal / parameters.size()) * 5.0), sumDays, cheater };
  68. }
  69. CreatureID HighScoreCalculation::getCreatureForPoints(int points, bool campaign)
  70. {
  71. static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
  72. auto creatures = configCreatures["creatures"].Vector();
  73. int divide = campaign ? 5 : 1;
  74. for(auto & creature : creatures)
  75. if(points / divide <= creature["max"].Integer() && points / divide >= creature["min"].Integer())
  76. return CreatureID::decode(creature["creature"].String());
  77. return -1;
  78. }
  79. CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
  80. : CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
  81. {
  82. addUsedEvents(SHOW_POPUP);
  83. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  84. pos = center(Rect(0, 0, 800, 600));
  85. updateShadow();
  86. addHighScores();
  87. addButtons();
  88. }
  89. void CHighScoreScreen::showPopupWindow(const Point & cursorPosition)
  90. {
  91. for (int i = 0; i < 11; i++)
  92. {
  93. Rect r = Rect(80, 40 + i * 50, 635, 50);
  94. if(r.isInside(cursorPosition - pos))
  95. {
  96. std::string tmp = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"][std::to_string(i)]["datetime"].String();
  97. if(!tmp.empty())
  98. CRClickPopup::createAndPush(tmp);
  99. }
  100. }
  101. }
  102. void CHighScoreScreen::addButtons()
  103. {
  104. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  105. buttons.clear();
  106. buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaginClick(); }));
  107. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonStandardClick(); }));
  108. buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }));
  109. buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }));
  110. }
  111. void CHighScoreScreen::addHighScores()
  112. {
  113. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  114. background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
  115. texts.clear();
  116. images.clear();
  117. // Header
  118. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433")));
  119. texts.push_back(std::make_shared<CLabel>(220, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
  120. if(highscorepage == HighScorePage::SCENARIO)
  121. {
  122. texts.push_back(std::make_shared<CLabel>(400, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435")));
  123. texts.push_back(std::make_shared<CLabel>(555, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
  124. texts.push_back(std::make_shared<CLabel>(625, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  125. }
  126. else
  127. {
  128. texts.push_back(std::make_shared<CLabel>(410, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672")));
  129. texts.push_back(std::make_shared<CLabel>(590, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  130. }
  131. // Content
  132. int y = 65;
  133. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
  134. for (int i = 0; i < 11; i++)
  135. {
  136. auto & curData = data[std::to_string(i)];
  137. ColorRGBA color = (i == highlighted) ? Colors::YELLOW : Colors::WHITE;
  138. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(i+1)));
  139. std::string tmp = curData["player"].String();
  140. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 13));
  141. texts.push_back(std::make_shared<CLabel>(220, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  142. if(highscorepage == HighScorePage::SCENARIO)
  143. {
  144. std::string tmp = curData["land"].String();
  145. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
  146. texts.push_back(std::make_shared<CLabel>(400, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  147. texts.push_back(std::make_shared<CLabel>(555, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["days"].Integer())));
  148. texts.push_back(std::make_shared<CLabel>(625, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  149. }
  150. else
  151. {
  152. std::string tmp = curData["campaign"].String();
  153. TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
  154. texts.push_back(std::make_shared<CLabel>(410, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
  155. texts.push_back(std::make_shared<CLabel>(590, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  156. }
  157. if(curData["points"].Integer() > 0)
  158. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(curData["points"].Integer(), highscorepage == HighScorePage::CAMPAIGN)]->getIconIndex(), 0, 670, y - 15 + i * 50));
  159. }
  160. }
  161. void CHighScoreScreen::buttonCampaginClick()
  162. {
  163. highscorepage = HighScorePage::CAMPAIGN;
  164. addHighScores();
  165. addButtons();
  166. redraw();
  167. }
  168. void CHighScoreScreen::buttonStandardClick()
  169. {
  170. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  171. highscorepage = HighScorePage::SCENARIO;
  172. addHighScores();
  173. addButtons();
  174. redraw();
  175. }
  176. void CHighScoreScreen::buttonResetClick()
  177. {
  178. CInfoWindow::showYesNoDialog(
  179. CGI->generaltexth->allTexts[666],
  180. {},
  181. [this]()
  182. {
  183. Settings entry = persistentStorage.write["highscore"];
  184. entry->clear();
  185. addHighScores();
  186. addButtons();
  187. redraw();
  188. },
  189. 0
  190. );
  191. }
  192. void CHighScoreScreen::buttonExitClick()
  193. {
  194. close();
  195. }
  196. CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc)
  197. : CWindowObject(BORDERED), won(won), calc(calc)
  198. {
  199. addUsedEvents(LCLICK);
  200. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  201. pos = center(Rect(0, 0, 800, 600));
  202. updateShadow();
  203. if(won)
  204. {
  205. int border = 100;
  206. int textareaW = ((pos.w - 2 * border) / 4);
  207. std::vector<std::string> t = { "438", "439", "440", "441", "676" };
  208. for (int i = 0; i < 5; i++)
  209. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 450, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt." + t[i])));
  210. std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
  211. t = { std::to_string(calc.calculate().sumDays), std::to_string(calc.calculate().basic), CGI->generaltexth->translate("core.arraytxt." + std::to_string((142 + calc.parameters[0].difficulty))), std::to_string(calc.calculate().total), creatureName };
  212. for (int i = 0; i < 5; i++)
  213. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 530, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, t[i]));
  214. CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
  215. }
  216. else
  217. CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
  218. video = won ? "HSANIM.SMK" : "LOSEGAME.SMK";
  219. }
  220. int CHighScoreInputScreen::addEntry(std::string text) {
  221. for (int i = 0; i < 11; i++)
  222. {
  223. if(calc.calculate().cheater)
  224. i = 10;
  225. JsonNode node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)];
  226. if(node["points"].isNull() || node["points"].Integer() <= calc.calculate().total)
  227. {
  228. // move following entries down
  229. for (int j = 10; j + 1 >= i; j--)
  230. {
  231. JsonNode node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(j - 1)];
  232. Settings entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(j)];
  233. entry->Struct() = node.Struct();
  234. }
  235. Settings entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["player"];
  236. entry->String() = text;
  237. if(calc.isCampaign)
  238. {
  239. Settings entry2 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["campaign"];
  240. entry2->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaign;
  241. }
  242. else
  243. {
  244. Settings entry3 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["land"];
  245. entry3->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].land;
  246. }
  247. Settings entry4 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["days"];
  248. entry4->Integer() = calc.calculate().sumDays;
  249. Settings entry5 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["points"];
  250. entry5->Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
  251. Settings entry6 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["datetime"];
  252. entry6->String() = vstd::getFormattedDateTime(std::time(0));
  253. return i;
  254. }
  255. }
  256. return -1;
  257. }
  258. void CHighScoreInputScreen::show(Canvas & to)
  259. {
  260. CCS->videoh->update(pos.x, pos.y, to.getInternalSurface(), true, false,
  261. [&]()
  262. {
  263. if(won)
  264. {
  265. CCS->videoh->close();
  266. video = "HSLOOP.SMK";
  267. CCS->videoh->open(VideoPath::builtin(video));
  268. }
  269. else
  270. close();
  271. });
  272. redraw();
  273. CIntObject::show(to);
  274. }
  275. void CHighScoreInputScreen::activate()
  276. {
  277. CCS->videoh->open(VideoPath::builtin(video));
  278. CIntObject::activate();
  279. }
  280. void CHighScoreInputScreen::deactivate()
  281. {
  282. CCS->videoh->close();
  283. CIntObject::deactivate();
  284. }
  285. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  286. {
  287. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  288. if(!won)
  289. {
  290. close();
  291. return;
  292. }
  293. if(!input)
  294. {
  295. input = std::make_shared<CHighScoreInput>(
  296. [&] (std::string text)
  297. {
  298. if(!text.empty())
  299. {
  300. int pos = addEntry(text);
  301. close();
  302. GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
  303. }
  304. else
  305. close();
  306. });
  307. }
  308. }
  309. CHighScoreInput::CHighScoreInput(std::function<void(std::string text)> readyCB)
  310. : CWindowObject(0), ready(readyCB)
  311. {
  312. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  313. pos = center(Rect(0, 0, 232, 212));
  314. updateShadow();
  315. background = std::make_shared<CPicture>(ImagePath::builtin("HIGHNAME"));
  316. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  317. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  318. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  319. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  320. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, 0);
  321. textInput->setText(settings["general"]["playerName"].String());
  322. }
  323. void CHighScoreInput::okay()
  324. {
  325. ready(textInput->getText());
  326. }
  327. void CHighScoreInput::abort()
  328. {
  329. ready("");
  330. }