1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/mainmenu/CHighScoreScreen.cpp

390 lines
13 KiB
C++
Raw Normal View History

2023-09-22 01:39:35 +02:00
/*
* CHighScoreScreen.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CHighScoreScreen.h"
#include "../gui/CGuiHandler.h"
2023-09-22 20:39:20 +02:00
#include "../gui/WindowHandler.h"
#include "../gui/Shortcut.h"
2023-09-22 01:39:35 +02:00
#include "../widgets/TextControls.h"
#include "../widgets/Buttons.h"
#include "../widgets/Images.h"
2023-09-23 16:28:28 +02:00
#include "../widgets/MiscWidgets.h"
2023-09-22 01:39:35 +02:00
#include "../windows/InfoWindows.h"
#include "../render/Canvas.h"
#include "../CGameInfo.h"
2023-09-22 20:39:20 +02:00
#include "../CVideoHandler.h"
2023-09-22 21:45:12 +02:00
#include "../CMusicHandler.h"
2023-09-22 01:39:35 +02:00
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/constants/EntityIdentifiers.h"
2023-09-23 13:14:45 +02:00
#include "../../lib/TextOperations.h"
#include "vstd/DateUtils.h"
2023-09-22 01:39:35 +02:00
2023-09-23 01:03:15 +02:00
auto HighScoreCalculation::calculate()
2023-09-23 00:21:36 +02:00
{
2023-09-23 14:51:39 +02:00
struct result
{
int basic;
int total;
int sumDays;
bool cheater;
};
std::vector<int> scoresBasic;
std::vector<int> scoresTotal;
double sumBasic = 0;
double sumTotal = 0;
int sumDays = 0;
bool cheater = false;
for(auto & param : parameters)
{
double tmp = 200 - (param.day + 10) / (param.townAmount + 5) + (param.allDefeated ? 25 : 0) + (param.hasGrail ? 25 : 0);
scoresBasic.push_back(static_cast<int>(tmp));
sumBasic += tmp;
if(param.difficulty == 0)
tmp *= 0.8;
if(param.difficulty == 1)
tmp *= 1.0;
if(param.difficulty == 2)
tmp *= 1.3;
if(param.difficulty == 3)
tmp *= 1.6;
if(param.difficulty == 4)
tmp *= 2.0;
scoresTotal.push_back(static_cast<int>(tmp));
sumTotal += tmp;
sumDays += param.day;
if(param.usedCheat)
cheater = true;
}
if(scoresBasic.size() == 1)
return result { scoresBasic[0], scoresTotal[0], sumDays , cheater};
return result { static_cast<int>((sumBasic / parameters.size()) * 5.0), static_cast<int>((sumTotal / parameters.size()) * 5.0), sumDays, cheater };
2023-09-23 01:36:01 +02:00
}
CreatureID HighScoreCalculation::getCreatureForPoints(int points, bool campaign)
{
2023-09-23 14:51:39 +02:00
static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
auto creatures = configCreatures["creatures"].Vector();
int divide = campaign ? 5 : 1;
2023-09-23 01:36:01 +02:00
2023-09-23 14:51:39 +02:00
for(auto & creature : creatures)
if(points / divide <= creature["max"].Integer() && points / divide >= creature["min"].Integer())
return CreatureID::decode(creature["creature"].String());
2023-09-23 01:36:01 +02:00
2023-09-23 14:51:39 +02:00
return -1;
2023-09-23 00:21:36 +02:00
}
2023-09-23 13:14:45 +02:00
CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
: CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
2023-09-22 01:39:35 +02:00
{
2023-09-23 14:51:39 +02:00
addUsedEvents(SHOW_POPUP);
2023-09-22 01:39:35 +02:00
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos = center(Rect(0, 0, 800, 600));
updateShadow();
2023-09-23 14:51:39 +02:00
addHighScores();
addButtons();
}
void CHighScoreScreen::showPopupWindow(const Point & cursorPosition)
{
for (int i = 0; i < 11; i++)
{
Rect r = Rect(80, 40 + i * 50, 635, 50);
if(r.isInside(cursorPosition - pos))
{
std::string tmp = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"][std::to_string(i)]["datetime"].String();
if(!tmp.empty())
CRClickPopup::createAndPush(tmp);
}
}
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::addButtons()
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-23 14:51:39 +02:00
buttons.clear();
2023-09-22 01:39:35 +02:00
2023-09-23 20:41:30 +02:00
buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaignClick(); }));
buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonScenarioClick(); }));
2023-09-23 14:51:39 +02:00
buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }));
buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }));
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::addHighScores()
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-23 14:51:39 +02:00
background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
texts.clear();
images.clear();
// Header
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433"))); // rank
texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434"))); // player
2023-09-23 14:51:39 +02:00
if(highscorepage == HighScorePage::SCENARIO)
{
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435"))); // land
texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436"))); // days
texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
2023-09-23 14:51:39 +02:00
}
else
{
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672"))); // campaign
texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
2023-09-23 14:51:39 +02:00
}
// Content
2023-09-23 15:11:48 +02:00
int y = 66;
2023-09-23 14:51:39 +02:00
auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
for (int i = 0; i < 11; i++)
{
auto & curData = data[std::to_string(i)];
ColorRGBA color = (i == highlighted) ? Colors::YELLOW : Colors::WHITE;
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(i + 1)));
2023-09-23 14:51:39 +02:00
std::string tmp = curData["player"].String();
TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 13));
2023-09-23 15:11:48 +02:00
texts.push_back(std::make_shared<CLabel>(225, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
2023-09-23 14:51:39 +02:00
if(highscorepage == HighScorePage::SCENARIO)
{
std::string tmp = curData["land"].String();
TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
2023-09-23 15:11:48 +02:00
texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
texts.push_back(std::make_shared<CLabel>(557, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["days"].Integer())));
texts.push_back(std::make_shared<CLabel>(627, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
2023-09-23 14:51:39 +02:00
}
else
{
std::string tmp = curData["campaign"].String();
TextOperations::trimRightUnicode(tmp, std::max(0, (int)TextOperations::getUnicodeCharactersCount(tmp) - 25));
2023-09-23 15:11:48 +02:00
texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, tmp));
texts.push_back(std::make_shared<CLabel>(592, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
2023-09-23 14:51:39 +02:00
}
if(curData["points"].Integer() > 0)
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));
}
2023-09-22 01:39:35 +02:00
}
2023-09-23 20:41:30 +02:00
void CHighScoreScreen::buttonCampaignClick()
2023-09-22 01:39:35 +02:00
{
2023-09-23 14:51:39 +02:00
highscorepage = HighScorePage::CAMPAIGN;
addHighScores();
addButtons();
redraw();
2023-09-22 01:39:35 +02:00
}
2023-09-23 20:41:30 +02:00
void CHighScoreScreen::buttonScenarioClick()
2023-09-22 01:39:35 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-23 14:51:39 +02:00
highscorepage = HighScorePage::SCENARIO;
addHighScores();
addButtons();
redraw();
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::buttonResetClick()
{
2023-09-23 14:51:39 +02:00
CInfoWindow::showYesNoDialog(
CGI->generaltexth->allTexts[666],
{},
[this]()
{
Settings entry = persistentStorage.write["highscore"];
entry->clear();
addHighScores();
addButtons();
redraw();
},
0
);
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::buttonExitClick()
{
2023-09-23 14:51:39 +02:00
close();
2023-09-22 01:39:35 +02:00
}
2023-09-22 20:39:20 +02:00
2023-09-23 00:21:36 +02:00
CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc)
2023-09-23 01:03:15 +02:00
: CWindowObject(BORDERED), won(won), calc(calc)
2023-09-22 20:39:20 +02:00
{
2023-09-23 15:11:48 +02:00
addUsedEvents(LCLICK | KEYBOARD);
2023-09-22 20:39:20 +02:00
2023-09-23 14:51:39 +02:00
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-22 20:39:20 +02:00
pos = center(Rect(0, 0, 800, 600));
updateShadow();
2023-09-23 16:28:28 +02:00
background = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), Colors::BLACK);
2023-09-23 14:51:39 +02:00
if(won)
{
int border = 100;
int textareaW = ((pos.w - 2 * border) / 4);
2023-09-23 20:41:30 +02:00
std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
2023-09-23 14:51:39 +02:00
for (int i = 0; i < 5; i++)
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])));
std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
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 };
for (int i = 0; i < 5; i++)
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]));
2023-09-22 21:45:12 +02:00
2023-09-23 14:51:39 +02:00
CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
}
else
CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
2023-09-22 21:45:12 +02:00
2023-09-23 14:51:39 +02:00
video = won ? "HSANIM.SMK" : "LOSEGAME.SMK";
2023-09-22 20:39:20 +02:00
}
2023-09-23 14:51:39 +02:00
int CHighScoreInputScreen::addEntry(std::string text) {
for (int i = 0; i < 11; i++)
{
if(calc.calculate().cheater)
i = 10;
2023-09-23 20:41:30 +02:00
JsonNode baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"];
2023-09-23 14:51:39 +02:00
2023-09-23 20:41:30 +02:00
if(baseNode[std::to_string(i)]["points"].isNull() || baseNode[std::to_string(i)]["points"].Integer() <= calc.calculate().total)
2023-09-23 14:51:39 +02:00
{
// move following entries down
for (int j = 10; j + 1 >= i; j--)
{
2023-09-23 20:41:30 +02:00
JsonNode node = baseNode[std::to_string(j - 1)];
2023-09-23 14:51:39 +02:00
Settings entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(j)];
entry->Struct() = node.Struct();
}
2023-09-23 20:41:30 +02:00
Settings currentEntry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)];
currentEntry["player"].String() = text;
2023-09-23 14:51:39 +02:00
if(calc.isCampaign)
2023-09-23 20:41:30 +02:00
currentEntry["campaign"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaign;
2023-09-23 14:51:39 +02:00
else
2023-09-23 20:41:30 +02:00
currentEntry["land"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].land;
currentEntry["days"].Integer() = calc.calculate().sumDays;
currentEntry["points"].Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
currentEntry["datetime"].String() = vstd::getFormattedDateTime(std::time(0));
2023-09-23 14:51:39 +02:00
return i;
}
}
return -1;
2023-09-22 20:39:20 +02:00
}
void CHighScoreInputScreen::show(Canvas & to)
{
2023-09-22 21:45:12 +02:00
CCS->videoh->update(pos.x, pos.y, to.getInternalSurface(), true, false,
2023-09-23 14:51:39 +02:00
[&]()
{
if(won)
{
CCS->videoh->close();
video = "HSLOOP.SMK";
CCS->videoh->open(VideoPath::builtin(video));
}
else
close();
});
redraw();
2023-09-22 20:39:20 +02:00
CIntObject::show(to);
}
void CHighScoreInputScreen::activate()
{
2023-09-23 16:28:28 +02:00
if(!CCS->videoh->open(VideoPath::builtin(video)))
{
if(!won)
close();
}
else
background = nullptr;
2023-09-22 20:39:20 +02:00
CIntObject::activate();
}
void CHighScoreInputScreen::deactivate()
{
2023-09-23 14:51:39 +02:00
CCS->videoh->close();
2023-09-22 20:39:20 +02:00
CIntObject::deactivate();
}
void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
{
2023-09-23 14:51:39 +02:00
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
if(!won)
{
close();
return;
}
if(!input)
{
input = std::make_shared<CHighScoreInput>(
[&] (std::string text)
{
if(!text.empty())
{
int pos = addEntry(text);
close();
GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
}
else
close();
});
}
2023-09-22 20:39:20 +02:00
}
2023-09-23 15:11:48 +02:00
void CHighScoreInputScreen::keyPressed(EShortcut key)
{
clickPressed(Point());
}
2023-09-22 20:39:20 +02:00
CHighScoreInput::CHighScoreInput(std::function<void(std::string text)> readyCB)
2023-09-23 20:41:30 +02:00
: CWindowObject(0, ImagePath::builtin("HIGHNAME")), ready(readyCB)
2023-09-22 20:39:20 +02:00
{
2023-09-23 14:51:39 +02:00
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-22 20:39:20 +02:00
pos = center(Rect(0, 0, 232, 212));
updateShadow();
2023-09-23 14:51:39 +02:00
text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
2023-09-22 20:39:20 +02:00
2023-09-23 14:51:39 +02:00
buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
2023-09-22 20:39:20 +02:00
statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
2023-09-23 14:51:39 +02:00
textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, 0);
textInput->setText(settings["general"]["playerName"].String());
2023-09-22 20:39:20 +02:00
}
void CHighScoreInput::okay()
{
2023-09-23 14:51:39 +02:00
ready(textInput->getText());
2023-09-22 20:39:20 +02:00
}
void CHighScoreInput::abort()
{
2023-09-23 14:51:39 +02:00
ready("");
2023-09-22 20:39:20 +02:00
}