1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

codereview

This commit is contained in:
Laserlicht 2023-09-23 20:41:30 +02:00 committed by GitHub
parent ca96749c45
commit 6f8b62d77d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 58 deletions

View File

@ -1690,7 +1690,7 @@ void CPlayerInterface::requestReturningToMainMenu(bool won)
param.difficulty = cb->getStartInfo()->difficulty;
param.day = cb->getDate();
param.townAmount = cb->howManyTowns();
param.usedCheat = cb->getPlayerState(*cb->getPlayerID())->enteredWinningCheatCode;
param.usedCheat = cb->getPlayerState(*cb->getPlayerID())->cheated;
param.hasGrail = false;
for(const CGHeroInstance * h : cb->getHeroesInfo())
if(h->hasArt(ArtifactID::GRAIL))
@ -1698,27 +1698,27 @@ void CPlayerInterface::requestReturningToMainMenu(bool won)
param.allDefeated = true;
for (PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
{
auto ps = cb->getPlayerState(player);
auto ps = cb->getPlayerState(player, false);
if(ps && player != *cb->getPlayerID())
if(!ps->checkVanquished())
param.allDefeated = false;
}
param.land = cb->getMapHeader()->name;
HighScoreCalculation calc;
calc.parameters.push_back(param);
calc.isCampaign = false;
HighScoreCalculation highScoreCalc;
highScoreCalc.parameters.push_back(param);
highScoreCalc.isCampaign = false;
if(won && cb->getStartInfo()->campState)
CSH->startCampaignScenario(param, cb->getStartInfo()->campState);
else
{
GH.dispatchMainThread(
[won, calc]()
[won, highScoreCalc]()
{
CSH->endGameplay();
GH.defActionsDef = 63;
CMM->menu->switchToTab("main");
GH.windows().createAndPushWindow<CHighScoreInputScreen>(won, calc);
GH.windows().createAndPushWindow<CHighScoreInputScreen>(won, highScoreCalc);
}
);
}

View File

@ -600,7 +600,7 @@ void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameSta
CMM->disable();
client = new CClient();
calc = nullptr;
highScoreCalc = nullptr;
switch(si->mode)
{
@ -678,14 +678,14 @@ void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared
if (!cs)
ourCampaign = si->campState;
if(calc == nullptr)
if(highScoreCalc == nullptr)
{
calc = std::make_shared<HighScoreCalculation>();
calc->isCampaign = true;
calc->parameters.clear();
highScoreCalc = std::make_shared<HighScoreCalculation>();
highScoreCalc->isCampaign = true;
highScoreCalc->parameters.clear();
}
param.campaign = cs->getName();
calc->parameters.push_back(param);
highScoreCalc->parameters.push_back(param);
GH.dispatchMainThread([ourCampaign, this]()
{
@ -709,7 +709,7 @@ void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared
else
{
CMM->openCampaignScreen(ourCampaign->campaignSet);
GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *calc);
GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *highScoreCalc);
}
};
if(epilogue.hasPrologEpilog)

View File

@ -89,7 +89,7 @@ class CServerHandler : public IServerAPI, public LobbyInfo
std::vector<std::string> myNames;
std::shared_ptr<HighScoreCalculation> calc;
std::shared_ptr<HighScoreCalculation> highScoreCalc;
void threadHandleConnection();
void threadRunServer();

View File

@ -349,7 +349,7 @@ void CVideoPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
show(x, y, dst, update);
}
void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update, std::function<void()> restart)
void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update, std::function<void()> onVideoRestart)
{
if (sws == nullptr)
return;
@ -368,8 +368,8 @@ void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, boo
show(x,y,dst,update);
else
{
if(restart)
restart();
if(onVideoRestart)
onVideoRestart();
VideoPath filenameToReopen = fname; // create copy to backup this->fname
open(filenameToReopen);
nextFrame();

View File

@ -101,7 +101,7 @@ public:
void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> restart = 0) override; //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true, std::function<void()> onVideoRestart = nullptr) override; //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
// Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
bool openAndPlayVideo(const VideoPath & name, int x, int y, bool stopOnKey = false, bool scale = false) override;

View File

@ -122,8 +122,8 @@ void CHighScoreScreen::addButtons()
buttons.clear();
buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaginClick(); }));
buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonStandardClick(); }));
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(); }));
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(); }));
}
@ -138,19 +138,19 @@ void CHighScoreScreen::addHighScores()
images.clear();
// Header
texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433")));
texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
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
if(highscorepage == HighScorePage::SCENARIO)
{
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435")));
texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
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
}
else
{
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672")));
texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
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
}
// Content
@ -187,7 +187,7 @@ void CHighScoreScreen::addHighScores()
}
}
void CHighScoreScreen::buttonCampaginClick()
void CHighScoreScreen::buttonCampaignClick()
{
highscorepage = HighScorePage::CAMPAIGN;
addHighScores();
@ -195,7 +195,7 @@ void CHighScoreScreen::buttonCampaginClick()
redraw();
}
void CHighScoreScreen::buttonStandardClick()
void CHighScoreScreen::buttonScenarioClick()
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
highscorepage = HighScorePage::SCENARIO;
@ -241,7 +241,7 @@ CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc
{
int border = 100;
int textareaW = ((pos.w - 2 * border) / 4);
std::vector<std::string> t = { "438", "439", "440", "441", "676" };
std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
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])));
@ -264,37 +264,27 @@ int CHighScoreInputScreen::addEntry(std::string text) {
if(calc.calculate().cheater)
i = 10;
JsonNode node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)];
JsonNode baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"];
if(node["points"].isNull() || node["points"].Integer() <= calc.calculate().total)
if(baseNode[std::to_string(i)]["points"].isNull() || baseNode[std::to_string(i)]["points"].Integer() <= calc.calculate().total)
{
// move following entries down
for (int j = 10; j + 1 >= i; j--)
{
JsonNode node = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(j - 1)];
JsonNode node = baseNode[std::to_string(j - 1)];
Settings entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(j)];
entry->Struct() = node.Struct();
}
Settings entry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["player"];
entry->String() = text;
Settings currentEntry = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)];
currentEntry["player"].String() = text;
if(calc.isCampaign)
{
Settings entry2 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["campaign"];
entry2->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaign;
}
currentEntry["campaign"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaign;
else
{
Settings entry3 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["land"];
entry3->String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].land;
}
Settings entry4 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["days"];
entry4->Integer() = calc.calculate().sumDays;
Settings entry5 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["points"];
entry5->Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
Settings entry6 = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"][std::to_string(i)]["datetime"];
entry6->String() = vstd::getFormattedDateTime(std::time(0));
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));
return i;
}
@ -373,14 +363,13 @@ void CHighScoreInputScreen::keyPressed(EShortcut key)
}
CHighScoreInput::CHighScoreInput(std::function<void(std::string text)> readyCB)
: CWindowObject(0), ready(readyCB)
: CWindowObject(0, ImagePath::builtin("HIGHNAME")), ready(readyCB)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos = center(Rect(0, 0, 232, 212));
updateShadow();
background = std::make_shared<CPicture>(ImagePath::builtin("HIGHNAME"));
text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);

View File

@ -50,8 +50,8 @@ private:
void addButtons();
void addHighScores();
void buttonCampaginClick();
void buttonStandardClick();
void buttonCampaignClick();
void buttonScenarioClick();
void buttonResetClick();
void buttonExitClick();
@ -71,7 +71,6 @@ public:
class CHighScoreInput : public CWindowObject
{
std::shared_ptr<CPicture> background;
std::shared_ptr<CMultiLineLabel> text;
std::shared_ptr<CButton> buttonOk;
std::shared_ptr<CButton> buttonCancel;

View File

@ -17,7 +17,7 @@
VCMI_LIB_NAMESPACE_BEGIN
PlayerState::PlayerState()
: color(-1), human(false), enteredWinningCheatCode(false),
: color(-1), human(false), cheated(false), enteredWinningCheatCode(false),
enteredLosingCheatCode(false), status(EPlayerStatus::INGAME)
{
setNodeType(PLAYER);
@ -29,6 +29,7 @@ PlayerState::PlayerState(PlayerState && other) noexcept:
human(other.human),
team(other.team),
resources(other.resources),
cheated(other.cheated),
enteredWinningCheatCode(other.enteredWinningCheatCode),
enteredLosingCheatCode(other.enteredLosingCheatCode),
status(other.status),

View File

@ -38,6 +38,7 @@ public:
std::vector<ConstTransitivePtr<CGDwelling> > dwellings; //used for town growth
std::vector<QuestInfo> quests; //store info about all received quests
bool cheated;
bool enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
EPlayerStatus status;
std::optional<ui8> daysWithoutCastle;
@ -82,6 +83,7 @@ public:
h & visitedObjects;
h & status;
h & daysWithoutCastle;
h & cheated;
h & enteredLosingCheatCode;
h & enteredWinningCheatCode;
h & static_cast<CBonusSystemNode&>(*this);

View File

@ -2509,6 +2509,7 @@ void PlayerCheated::applyGs(CGameState * gs) const
gs->getPlayerState(player)->enteredLosingCheatCode = losingCheatCode;
gs->getPlayerState(player)->enteredWinningCheatCode = winningCheatCode;
gs->getPlayerState(player)->cheated = true;
}
void PlayerStartsTurn::applyGs(CGameState * gs) const