-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1688 from kubaau/cheats_refactoring
Refactoring of existing cheats (see #1679)
- Loading branch information
Showing
9 changed files
with
342 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "CheatCommandTracker.h" | ||
#include "Cheats.h" | ||
#include "driver/KeyEvent.h" | ||
|
||
namespace { | ||
auto makeCircularBuffer(const std::string& str) | ||
{ | ||
return boost::circular_buffer<char>{cbegin(str), cend(str)}; | ||
} | ||
const auto enableCheatsStr = makeCircularBuffer("winter"); | ||
} // namespace | ||
|
||
CheatCommandTracker::CheatCommandTracker(Cheats& cheats) : cheats_(cheats), lastChars_(enableCheatsStr.size()) {} | ||
|
||
void CheatCommandTracker::onKeyEvent(const KeyEvent& ke) | ||
{ | ||
if(!cheats_.areCheatsAllowed()) | ||
return; | ||
|
||
if(checkSpecialKeyEvent(ke)) | ||
lastChars_.clear(); | ||
else | ||
onCharKeyEvent(ke); | ||
} | ||
|
||
void CheatCommandTracker::onChatCommand(const std::string& cmd) | ||
{ | ||
if(!cheats_.areCheatsAllowed()) | ||
return; | ||
|
||
if(cmd == "apocalypsis") | ||
cheats_.armageddon(); | ||
} | ||
|
||
bool CheatCommandTracker::checkSpecialKeyEvent(const KeyEvent& ke) | ||
{ | ||
if(ke.kt == KeyType::Char) | ||
return false; | ||
|
||
switch(ke.kt) | ||
{ | ||
case KeyType::F10: cheats_.toggleHumanAIPlayer(); break; | ||
default: break; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void CheatCommandTracker::onCharKeyEvent(const KeyEvent& ke) | ||
{ | ||
lastChars_.push_back(ke.c); | ||
|
||
if(lastChars_ == enableCheatsStr) | ||
cheats_.toggleCheatMode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
|
||
#include <boost/circular_buffer.hpp> | ||
#include <string> | ||
|
||
class Cheats; | ||
struct KeyEvent; | ||
|
||
class CheatCommandTracker | ||
{ | ||
public: | ||
CheatCommandTracker(Cheats& cheats); | ||
|
||
void onKeyEvent(const KeyEvent& ke); | ||
void onChatCommand(const std::string& cmd); | ||
|
||
private: | ||
bool checkSpecialKeyEvent(const KeyEvent& ke); | ||
void onCharKeyEvent(const KeyEvent& ke); | ||
|
||
Cheats& cheats_; | ||
boost::circular_buffer<char> lastChars_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "Cheats.h" | ||
#include "network/GameClient.h" | ||
#include "world/GameWorldBase.h" | ||
|
||
Cheats::Cheats(GameWorldBase& world) : world_(world) {} | ||
|
||
bool Cheats::areCheatsAllowed() const | ||
{ | ||
return world_.IsSinglePlayer(); | ||
} | ||
|
||
void Cheats::toggleCheatMode() | ||
{ | ||
isCheatModeOn_ = !isCheatModeOn_; | ||
} | ||
|
||
void Cheats::toggleHumanAIPlayer() const | ||
{ | ||
if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn()) | ||
GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy}); | ||
} | ||
|
||
void Cheats::armageddon() const | ||
{ | ||
if(isCheatModeOn()) | ||
GAMECLIENT.CheatArmageddon(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
|
||
class GameWorldBase; | ||
|
||
class Cheats | ||
{ | ||
public: | ||
Cheats(GameWorldBase& world); | ||
|
||
bool areCheatsAllowed() const; | ||
|
||
void toggleCheatMode(); | ||
bool isCheatModeOn() const { return isCheatModeOn_; } | ||
|
||
void toggleHumanAIPlayer() const; | ||
void armageddon() const; | ||
|
||
private: | ||
bool isCheatModeOn_ = false; | ||
GameWorldBase& world_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "CheatCommandTracker.h" | ||
#include "Cheats.h" | ||
#include "driver/KeyEvent.h" | ||
#include "worldFixtures/CreateEmptyWorld.h" | ||
#include "worldFixtures/WorldFixture.h" | ||
|
||
BOOST_AUTO_TEST_SUITE(CheatCommandTrackerTests) | ||
|
||
namespace { | ||
template<unsigned T_numPlayers> | ||
struct CheatCommandTrackerFixture : WorldFixture<CreateEmptyWorld, T_numPlayers> | ||
{ | ||
Cheats cheats_{this->world}; | ||
CheatCommandTracker tracker_{cheats_}; | ||
|
||
KeyEvent makeKeyEvent(unsigned c) { return {KeyType::Char, c, false, false, false}; } | ||
KeyEvent makeKeyEvent(KeyType kt) { return {kt, 0, false, false, false}; } | ||
|
||
void trackString(const std::string& str) | ||
{ | ||
for(char c : str) | ||
tracker_.onKeyEvent(makeKeyEvent(c)); | ||
} | ||
}; | ||
using CheatCommandTrackerFixture1P = CheatCommandTrackerFixture<1>; | ||
using CheatCommandTrackerFixture2P = CheatCommandTrackerFixture<2>; | ||
} // namespace | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsOffByDefault, CheatCommandTrackerFixture1P) | ||
{ | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOn, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeCannotBeTurnedOn_InMultiplayer, CheatCommandTrackerFixture2P) | ||
{ | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOff, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOnAndOffRepeatedly, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenIncomplete, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winte"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenInterruptedByAnotherKeyType, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("win"); | ||
tracker_.onKeyEvent(makeKeyEvent(KeyType::F10)); | ||
trackString("ter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenInterruptedByAnotherLetter, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("wainter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenOrderOfCharactersIsWrong, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winetr"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenOrderOfCharactersIsWrong_Wraparound, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("rwinte"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenACharacterIsRepeated, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("winnter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsTurnedOn_WhenTheFirstCharacterIsRepeated, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("wwwinter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(CheatModeIsTurnedOn_EvenWhenWrongInputsWereProvidedBefore, CheatCommandTrackerFixture1P) | ||
{ | ||
trackString("www"); | ||
auto ke = makeKeyEvent('1'); | ||
ke.alt = true; | ||
tracker_.onKeyEvent(ke); | ||
trackString("interwitter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
trackString("winter"); | ||
BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
Oops, something went wrong.