Skip to content

Commit 01f3e37

Browse files
Merge pull request #171 from Longwater1234/dev
Only highlight pieces which belong to current player
2 parents e7d93fa + d794159 commit 01f3e37

File tree

9 files changed

+34
-21
lines changed

9 files changed

+34
-21
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
22

33
project(
44
"SpaceCheckers"
5-
VERSION 1.0.12
5+
VERSION 1.0.13
66
HOMEPAGE_URL "https://github.com/Longwater1234/space-checkers")
77

88
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
99

1010
set(CMAKE_CXX_STANDARD 17)
1111
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
1212
set(CMAKE_CXX_EXTENSIONS OFF)
13+
1314
# On macOS using Frameworks, NOT static (dylib)
1415
if(NOT APPLE)
1516
set(SFML_STATIC_LIBRARIES TRUE)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This game can connect to both Private and Public game servers. The backend serve
2727
- MS Visual Studio 2022 or newer (NOT vscode), with "**Desktop C++ Development**" bundle.
2828
- Please download "Visual C++ 64bit" edition of SFML; ignore others.
2929
- Move your unzipped `SFML-2.6.x` folder to its own home, example: `C:/SFML/SFML-2.6.1`.
30-
- Edit **line 24** in [CMakeLists.txt](CMakeLists.txt#L24), to set value `SFML_HOME` to folder path you moved SFML into (from previous step)
30+
- Edit **line 25** in [CMakeLists.txt](CMakeLists.txt#L25), to set value `SFML_HOME` to folder path you moved SFML into (from previous step)
3131

3232
### For macOS (x64 & arm64)
3333

src/AppVersion.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// clang-format off
77
namespace chk {
8-
constexpr const char* APP_VERSION = "1.0.12";
8+
constexpr const char* APP_VERSION = "1.0.13";
99
}
1010

1111
#endif // APP_VERSION_HPP

src/Cell.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const inline sf::Vector2f &Cell::getPos() const
121121
}
122122

123123
/**
124-
* (ONLY FOR PLAYABLE CELLS) Highlight the currently clicked cell with a piece with BLUE
124+
* (ONLY FOR PLAYABLE CELLS) Highlight with BLUE the currently clicked cell with a piece
125125
*/
126126
inline void Cell::highlightActive()
127127
{

src/WsClient.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void WsClient::runMainLoop()
204204

205205
else {
206206
// already connected
207-
this->runServerLoop();
207+
this->readIncomingSignals();
208208
}
209209

210210
// some error happened 🙁
@@ -216,6 +216,7 @@ void WsClient::runMainLoop()
216216
} else if (this->haveWinner) {
217217
this->showWinnerPopup();
218218
}
219+
// clang-format on
219220
}
220221

221222
/**
@@ -331,7 +332,7 @@ void WsClient::setOnWinLoseCallback(const onWinLoseCallback &callback)
331332

332333
/**
333334
* Send Protobuf response back to server.
334-
*
335+
*
335336
* @param payload the request body
336337
* @return TRUE if sent successfully, else FALSE
337338
*/
@@ -351,10 +352,10 @@ bool WsClient::replyServer(const chk::payload::BasePayload &payload) const
351352
}
352353

353354
/**
354-
* Exchange messages with the server and update the game accordingly. if any
355-
* error happens, close connection
355+
* Read messages from server and update the game accordingly. If any
356+
* error happens or match ends, close connection
356357
*/
357-
void WsClient::runServerLoop()
358+
void WsClient::readIncomingSignals()
358359
{
359360
for (const auto &msg : this->msgBuffer.getAll())
360361
{
@@ -365,9 +366,9 @@ void WsClient::runServerLoop()
365366
chk::payload::BasePayload basePayload;
366367
if (!basePayload.ParseFromString(msg))
367368
{
369+
this->isDead = true;
368370
std::scoped_lock lg{this->mut};
369371
this->deathNote = "Profobuf: Could not parse payload";
370-
this->isDead = true;
371372
return;
372373
}
373374

@@ -390,6 +391,7 @@ void WsClient::runServerLoop()
390391
else if (basePayload.has_exit_payload())
391392
{
392393
this->isDead = true;
394+
std::scoped_lock lg{this->mut};
393395
this->deathNote = basePayload.notice();
394396
spdlog::error(basePayload.notice());
395397
}

src/WsClient.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using onDeathCallback = std::function<void(std::string_view notice)>;
2727
using onMovePieceCallback = std::function<void(const chk::payload::MovePayload &)>;
2828
// when opponent captures my piece
2929
using onCaptureCallback = std::function<void(const chk::payload::CapturePayload &)>;
30-
// when we got a winner or loser
30+
// when we get a winner or loser
3131
using onWinLoseCallback = std::function<void(std::string_view notice)>;
3232
// CDN address
3333
constexpr auto cloudfront = "https://d1txhef4jwuosv.cloudfront.net/ws_server_locations.json";
@@ -72,7 +72,7 @@ class WsClient final
7272
std::mutex mut;
7373
std::unique_ptr<ix::WebSocket> webSocketPtr = nullptr; // our Websocket object
7474
void showErrorPopup(); // whenver there is an error (from server)
75-
void runServerLoop(); // while connected, keep exchanging messages with server
75+
void readIncomingSignals(); // while connected, keep reading messages from server
7676
static void showHint(const char *tip);
7777
void tryConnect(std::string_view address);
7878
void showConnectWindow();

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main()
1818
GOOGLE_PROTOBUF_VERIFY_VERSION;
1919
auto window = sf::RenderWindow{sf::VideoMode{600, 700}, "SpaceCheckers", sf::Style::Titlebar | sf::Style::Close};
2020
window.setFramerateLimit(60);
21-
ImGui::SFML::Init(window, false);
21+
(void)ImGui::SFML::Init(window, false);
2222
// ImGui::StyleColorsLight(); //<-- light color theme
2323
std::unique_ptr<chk::GameManager> manager = nullptr;
2424

@@ -39,7 +39,7 @@ int main()
3939
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
4040
ImFont *imfont = io.Fonts->AddFontFromFileTTF(chk::getResourcePath(chk::FONT_PATH).c_str(), chk::FONT_SIZE);
4141
IM_ASSERT(imfont != nullptr);
42-
ImGui::SFML::UpdateFontTexture();
42+
(void)ImGui::SFML::UpdateFontTexture();
4343

4444
sf::Image appIcon;
4545
if (appIcon.loadFromFile(chk::getResourcePath(chk::ICON_PATH)))

src/managers/GameManager.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void GameManager::handleCapturePiece(const chk::PlayerPtr &hunter, const chk::Pl
163163
this->forcedMoves.clear();
164164
if (isKingBefore == isKingNow)
165165
{
166-
GameManager::identifyTargets(hunter, targetCell);
166+
this->identifyTargets(hunter, targetCell);
167167
}
168168

169169
if (this->forcedMoves.empty())
@@ -226,7 +226,7 @@ bool GameManager::isHunterActive() const
226226
}
227227

228228
/**
229-
* Using cached gameMap, get the PieceId found at this cell_index
229+
* Using cached gameMap, get the piece_id found at this cell
230230
*
231231
* @param cell_idx the clicked cell
232232
* @return positive number or -1 if not found
@@ -242,7 +242,8 @@ short GameManager::getPieceFromCell(const int cell_idx) const
242242

243243
/**
244244
* Match cells to pieces at game launch, using position, and cache it to Hashmap
245-
* @param pieceList vector of all pieces
245+
*
246+
* @param pieceList vector containing all pieces
246247
*/
247248
void GameManager::matchCellsToPieces(const std::vector<chk::PiecePtr> &pieceList)
248249
{
@@ -308,13 +309,17 @@ void chk::GameManager::handleCellTap(const chk::PlayerPtr &hunter, const chk::Pl
308309
if (pieceId != -1)
309310
{
310311
// YES, it has one! VERIFY IF THERE IS ANY PENDING "forced captures".
311-
// If yes, ensure hunter is SELECTED!
312312
if (!this->getForcedMoves().empty() && this->forcedMoves.find(pieceId) == forcedMoves.end())
313313
{
314314
this->showForcedMoves(hunter, cell);
315315
return;
316316
}
317-
// OTHERWISE, store it in buffer (for a SIMPLE/CAPTURE move next)!
317+
// Does current player own this piece?
318+
if (!hunter->hasThisPiece(pieceId))
319+
{
320+
return;
321+
}
322+
// Store it in buffer (for a SIMPLE/CAPTURE move next!)
318323
buffer.addItem(pieceId);
319324
this->setSourceCell(cell->getIndex());
320325
cell->highlightActive();
@@ -413,7 +418,7 @@ bool GameManager::awayFromEdge(const int cell_idx) const
413418
/**
414419
* Collect all possible next "forced captures" for this hunter.
415420
* @param hunter Current player
416-
* @param singleCell if not NULL, only collect around this cell. Otherwise, loop ENTIRE board
421+
* @param singleCell if NOT null, then only collect around this cell. Otherwise, loop ENTIRE board
417422
*/
418423
void GameManager::identifyTargets(const PlayerPtr &hunter, const chk::Block &singleCell)
419424
{
@@ -424,7 +429,7 @@ void GameManager::identifyTargets(const PlayerPtr &hunter, const chk::Block &sin
424429
const short pieceId = this->getPieceFromCell(singleCell->getIndex());
425430
if (gameMap.find(singleCell->getIndex()) == gameMap.end() || !hunter->hasThisPiece(pieceId))
426431
{
427-
// this CELL is not usable, OR piece not OWNED by hunter
432+
// this CELL is not usable, OR piece not owned to hunter
428433
return;
429434
}
430435
this->collectFrontLHS(hunter, singleCell);

src/managers/OnlineGameManager.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ inline void OnlineGameManager::handleCellTap(const chk::PlayerPtr &hunter, const
416416
this->showForcedMoves(hunter, cell);
417417
return;
418418
}
419+
// Does current player own this piece?
420+
if (!hunter->hasThisPiece(pieceId))
421+
{
422+
return;
423+
}
419424
// OTHERWISE, store it in buffer (for a simple move, on next turn)
420425
buffer.addItem(pieceId);
421426
this->setSourceCell(cell->getIndex());

0 commit comments

Comments
 (0)