CHighScoreScreen.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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(), [&](){ buttonCampaginClick(); }));
  108. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonStandardClick(); }));
  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")));
  120. texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
  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")));
  124. texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
  125. texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  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")));
  130. texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  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::buttonCampaginClick()
  163. {
  164. highscorepage = HighScorePage::CAMPAIGN;
  165. addHighScores();
  166. addButtons();
  167. redraw();
  168. }
  169. void CHighScoreScreen::buttonStandardClick()
  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" };
  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 node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)];
  228. if(node["points"].isNull() || node["points"].Integer() <= calc.calculate().total)
  229. {
  230. // move following entries down
  231. for (int j = 10; j + 1 >= i; j--)
  232. {
  233. JsonNode node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][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 entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["player"];
  238. entry->String() = text;
  239. if(calc.isCampaign)
  240. {
  241. Settings entry2 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["campaign"];
  242. entry2->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaign;
  243. }
  244. else
  245. {
  246. Settings entry3 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["land"];
  247. entry3->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].land;
  248. }
  249. Settings entry4 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["days"];
  250. entry4->Integer() = calc.calculate().sumDays;
  251. Settings entry5 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["points"];
  252. entry5->Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
  253. Settings entry6 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["datetime"];
  254. entry6->String() = vstd::getFormattedDateTime(std::time(0));
  255. return i;
  256. }
  257. }
  258. return -1;
  259. }
  260. void CHighScoreInputScreen::show(Canvas & to)
  261. {
  262. CCS->videoh->update(pos.x, pos.y, to.getInternalSurface(), true, false,
  263. [&]()
  264. {
  265. if(won)
  266. {
  267. CCS->videoh->close();
  268. video = "HSLOOP.SMK";
  269. CCS->videoh->open(VideoPath::builtin(video));
  270. }
  271. else
  272. close();
  273. });
  274. redraw();
  275. CIntObject::show(to);
  276. }
  277. void CHighScoreInputScreen::activate()
  278. {
  279. if(!CCS->videoh->open(VideoPath::builtin(video)))
  280. {
  281. if(!won)
  282. close();
  283. }
  284. else
  285. background = nullptr;
  286. CIntObject::activate();
  287. }
  288. void CHighScoreInputScreen::deactivate()
  289. {
  290. CCS->videoh->close();
  291. CIntObject::deactivate();
  292. }
  293. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  294. {
  295. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  296. if(!won)
  297. {
  298. close();
  299. return;
  300. }
  301. if(!input)
  302. {
  303. input = std::make_shared<CHighScoreInput>(
  304. [&] (std::string text)
  305. {
  306. if(!text.empty())
  307. {
  308. int pos = addEntry(text);
  309. close();
  310. GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
  311. }
  312. else
  313. close();
  314. });
  315. }
  316. }
  317. void CHighScoreInputScreen::keyPressed(EShortcut key)
  318. {
  319. clickPressed(Point());
  320. }
  321. CHighScoreInput::CHighScoreInput(std::function<void(std::string text)> readyCB)
  322. : CWindowObject(0), ready(readyCB)
  323. {
  324. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  325. pos = center(Rect(0, 0, 232, 212));
  326. updateShadow();
  327. background = std::make_shared<CPicture>(ImagePath::builtin("HIGHNAME"));
  328. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  329. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  330. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  331. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  332. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, 0);
  333. textInput->setText(settings["general"]["playerName"].String());
  334. }
  335. void CHighScoreInput::okay()
  336. {
  337. ready(textInput->getText());
  338. }
  339. void CHighScoreInput::abort()
  340. {
  341. ready("");
  342. }