CHighScoreScreen.cpp 13 KB

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