Skip to content

Commit f6baefc

Browse files
committed
Apply review remarks:
- avoid early returns - rename cheatStr to enableCheatsStr - rename canCheatModeBeOn() to areCheatsAllowed() - trackCharKeyEvent return void - hold cheatCmdTracker_ by value instead of unique_ptr
1 parent e693275 commit f6baefc

File tree

5 files changed

+23
-42
lines changed

5 files changed

+23
-42
lines changed

libs/s25main/CheatCommandTracker.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@ auto makeCircularBuffer(const std::string& str)
1111
{
1212
return boost::circular_buffer<char>{cbegin(str), cend(str)};
1313
}
14-
const auto cheatStr = makeCircularBuffer("winter");
14+
const auto enableCheatsStr = makeCircularBuffer("winter");
1515
} // namespace
1616

17-
CheatCommandTracker::CheatCommandTracker(Cheats& cheats) : cheats_(cheats), lastChars_(cheatStr.size()) {}
17+
CheatCommandTracker::CheatCommandTracker(Cheats& cheats) : cheats_(cheats), lastChars_(enableCheatsStr.size()) {}
1818

1919
void CheatCommandTracker::trackKeyEvent(const KeyEvent& ke)
2020
{
2121
if(trackSpecialKeyEvent(ke))
22-
{
2322
lastChars_.clear();
24-
return;
25-
}
26-
27-
trackCharKeyEvent(ke);
23+
else
24+
trackCharKeyEvent(ke);
2825
}
2926

3027
void CheatCommandTracker::trackChatCommand(const std::string& cmd)
@@ -47,12 +44,10 @@ bool CheatCommandTracker::trackSpecialKeyEvent(const KeyEvent& ke)
4744
return true;
4845
}
4946

50-
bool CheatCommandTracker::trackCharKeyEvent(const KeyEvent& ke)
47+
void CheatCommandTracker::trackCharKeyEvent(const KeyEvent& ke)
5148
{
5249
lastChars_.push_back(ke.c);
5350

54-
if(lastChars_ == cheatStr)
51+
if(lastChars_ == enableCheatsStr)
5552
cheats_.toggleCheatMode();
56-
57-
return true;
5853
}

libs/s25main/CheatCommandTracker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CheatCommandTracker
2020

2121
private:
2222
bool trackSpecialKeyEvent(const KeyEvent& ke);
23-
bool trackCharKeyEvent(const KeyEvent& ke);
23+
void trackCharKeyEvent(const KeyEvent& ke);
2424

2525
Cheats& cheats_;
2626
boost::circular_buffer<char> lastChars_;

libs/s25main/Cheats.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,44 @@
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#include "Cheats.h"
6-
#include "CheatCommandTracker.h"
76
#include "network/GameClient.h"
87
#include "world/GameWorldBase.h"
98

10-
Cheats::Cheats(GameWorldBase& world) : cheatCmdTracker_(std::make_unique<CheatCommandTracker>(*this)), world_(world) {}
9+
Cheats::Cheats(GameWorldBase& world) : cheatCmdTracker_(*this), world_(world) {}
1110

1211
Cheats::~Cheats() = default;
1312

1413
void Cheats::trackKeyEvent(const KeyEvent& ke)
1514
{
16-
if(!canCheatModeBeOn())
17-
return;
18-
19-
cheatCmdTracker_->trackKeyEvent(ke);
15+
if(areCheatsAllowed())
16+
cheatCmdTracker_.trackKeyEvent(ke);
2017
}
2118

2219
void Cheats::trackChatCommand(const std::string& cmd)
2320
{
24-
if(!canCheatModeBeOn())
25-
return;
26-
27-
cheatCmdTracker_->trackChatCommand(cmd);
21+
if(areCheatsAllowed())
22+
cheatCmdTracker_.trackChatCommand(cmd);
2823
}
2924

3025
void Cheats::toggleCheatMode()
3126
{
32-
if(!canCheatModeBeOn())
33-
return;
34-
35-
isCheatModeOn_ = !isCheatModeOn_;
27+
if(areCheatsAllowed())
28+
isCheatModeOn_ = !isCheatModeOn_;
3629
}
3730

3831
void Cheats::toggleHumanAIPlayer()
3932
{
40-
if(!isCheatModeOn())
41-
return;
42-
43-
if(GAMECLIENT.IsReplayModeOn())
44-
return;
45-
46-
GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy});
33+
if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn())
34+
GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy});
4735
}
4836

4937
void Cheats::armageddon()
5038
{
51-
if(!isCheatModeOn())
52-
return;
53-
54-
GAMECLIENT.CheatArmageddon();
39+
if(isCheatModeOn())
40+
GAMECLIENT.CheatArmageddon();
5541
}
5642

57-
bool Cheats::canCheatModeBeOn() const
43+
bool Cheats::areCheatsAllowed() const
5844
{
5945
return world_.IsSinglePlayer();
6046
}

libs/s25main/Cheats.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
#pragma once
66

7+
#include "CheatCommandTracker.h"
78
#include <memory>
89
#include <string>
910

10-
class CheatCommandTracker;
1111
class GameWorldBase;
1212
struct KeyEvent;
1313

@@ -27,9 +27,9 @@ class Cheats
2727
void armageddon();
2828

2929
private:
30-
bool canCheatModeBeOn() const;
30+
bool areCheatsAllowed() const;
3131

32-
std::unique_ptr<CheatCommandTracker> cheatCmdTracker_;
32+
CheatCommandTracker cheatCmdTracker_;
3333
bool isCheatModeOn_ = false;
3434
GameWorldBase& world_;
3535
};

tests/s25Main/integration/testCheats.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace {
1212
template<unsigned T_numPlayers>
1313
struct CheatWorldFixture : WorldFixture<CreateEmptyWorld, T_numPlayers>
1414
{
15-
Cheats& cheats = world.GetCheats();
15+
Cheats& cheats = this->world.GetCheats();
1616
};
1717

1818
using CheatWorldFixture1P = CheatWorldFixture<1>;

0 commit comments

Comments
 (0)