Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 29, 2024
1 parent 92559b9 commit 69e8d5d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 42 deletions.
7 changes: 4 additions & 3 deletions server/src/game/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "Game.hpp"
#include <algorithm>
#include <memory>
#include "Game.hpp"
#include "Catan.hpp"
#include "INetwork.hpp"
#include "Logger.hpp"
#include "Catan.hpp"

Game::Game()
{
Expand Down Expand Up @@ -218,7 +218,8 @@ bool Game::startGame(const Player &player)
if (_selectedGame.length() == 0) {
return false;
}
if (std::find(_availableGameName.begin(), _availableGameName.end(), _selectedGame) == _availableGameName.end()) {
if (std::find(_availableGameName.begin(), _availableGameName.end(), _selectedGame)
== _availableGameName.end()) {
return false;
}
if (player._peer->getId() != _keyOwner) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/game/Game.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <memory>
#include "INetwork.hpp"
#include "IGame.hpp"
#include "INetwork.hpp"
#include "Player.hpp"

class Game {
Expand Down
62 changes: 36 additions & 26 deletions server/src/game/games/ConfigUI.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "ConfigUI.hpp"
#include <fstream>
#include <functional>
#include <string>
#include "ConfigUI.hpp"
#include "INetwork.hpp"
#include "Logger.hpp"
#include "PathResolver.hpp"

ConfigUI::ConfigUI(const std::string &identifier):
_identifier(identifier),
_filePath(PathResolver::resolve("assets/uiconf/" + identifier))
ConfigUI::ConfigUI(const std::string &identifier)
: _identifier(identifier),
_filePath(PathResolver::resolve("assets/uiconf/" + identifier))
{
std::ifstream file(_filePath);
std::string all;
Expand All @@ -24,12 +24,12 @@ ConfigUI::ConfigUI(const std::string &identifier):
_fileChunk.push_back(bufferS);
all = all + bufferS;
}
_fileHash = std::hash<std::string>{}(all);
_fileHash = std::hash<std::string> {}(all);
}

void ConfigUI::addPeer(std::shared_ptr<INetwork::IPeer> peer)
{
_peers[peer->getId()] = peer;
_peers[peer->getId()] = peer;
_peersState[peer->getId()] = ConfigState();
}

Expand All @@ -47,44 +47,52 @@ void ConfigUI::update()
}
const auto messageType = message.at("type").template get<std::string>();
if (messageType == "uiConfigHash") {
if (!message.contains("name") || !message.contains("hash") || !message.at("name").is_string() || !message.at("hash").is_string()) {
if (!message.contains("name") || !message.contains("hash")
|| !message.at("name").is_string() || !message.at("hash").is_string()) {
continue;
}
const auto name = message.at("name").template get<std::string>();
const auto hash = message.at("hash").template get<std::string>();
if (name != _identifier) {
network.send(pEer, {
{"type", "uiConfigHash"},
{"name", _identifier},
network.send(
pEer,
{
{"type", "uiConfigHash"},
{"name", _identifier },
});
continue;
}
if (hash != _fileHash) {
pState._ok = false;
pState._nbChunk = _fileChunk.size();
pState._ok = false;
pState._nbChunk = _fileChunk.size();
pState._currentChunk = -1;
pState._hashClient = "";
pState._hashClient = "";
} else {
pState._ok = true;
Logger::debug("CONFIGUI: one client ok");
}
} else if (messageType == "uiConfig") {
if (!message.contains("name") || !message.contains("nbChunk") || !message.at("name").is_string() || !message.at("nbChunk").is_number()) {
if (!message.contains("name") || !message.contains("nbChunk")
|| !message.at("name").is_string() || !message.at("nbChunk").is_number()) {
continue;
}
network.send(pEer, {
{"type", "uiConfigHash"},
{"name", _identifier},
network.send(
pEer,
{
{"type", "uiConfigHash"},
{"name", _identifier },
});
}
}
if (pState._ok) {
continue;
}
if (!pState._askedHash) {
network.send(_peers[key], {
{"type", "uiConfigHash"},
{"name", _identifier},
network.send(
_peers[key],
{
{"type", "uiConfigHash"},
{"name", _identifier },
});
pState._askedHash = true;
continue;
Expand All @@ -99,12 +107,14 @@ void ConfigUI::update()
if (pState._currentChunk == pState._nbChunk) {
continue;
}
network.send(pEer, {
{"type", "uiConfig"},
{"name", _identifier},
{"nbChunk", pState._nbChunk},
{"chunkIndex", pState._currentChunk},
{"data", _fileChunk[pState._currentChunk]},
network.send(
pEer,
{
{"type", "uiConfig" },
{"name", _identifier },
{"nbChunk", pState._nbChunk },
{"chunkIndex", pState._currentChunk },
{"data", _fileChunk[pState._currentChunk]},
});
}
return;
Expand Down
7 changes: 3 additions & 4 deletions server/src/game/games/ConfigUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ class ConfigUI {
void update();

private:

class ConfigState {
public:
bool _askedHash = false;
bool _ok = false;
int _nbChunk = -1;
bool _askedHash = false;
bool _ok = false;
int _nbChunk = -1;
int _currentChunk = -1;
std::string _hashClient;
};
Expand Down
8 changes: 4 additions & 4 deletions server/src/game/games/IGame.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <unordered_map>
#include <string>
#include <unordered_map>
#include "Player.hpp"

class IGame {
public:
virtual ~IGame() = default;
virtual ~IGame() = default;
virtual void init(std::unordered_map<std::string, Player> &players) = 0;
virtual void update() = 0;
virtual bool isFinished() = 0;
virtual void update() = 0;
virtual bool isFinished() = 0;
};
4 changes: 2 additions & 2 deletions server/src/game/games/catan/Catan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include <memory>
#include "Logger.hpp"

Catan::Catan(): _players(_tmpPlayers)
Catan::Catan() : _players(_tmpPlayers)
{
Logger::debug("Catan game created");
}

void Catan::init(std::unordered_map<std::string, Player> &players)
{
_players = players;
_players = players;
_configUi = std::make_unique<ConfigUI>("catan.json");
for (const auto &[key, pLayer] : _players) {
_playersState[key] = PlayerState();
Expand Down
4 changes: 2 additions & 2 deletions server/src/game/games/catan/Catan.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "IGame.hpp"
#include "ConfigUI.hpp"
#include "IGame.hpp"

class Catan : public IGame {
public:
Expand All @@ -20,7 +20,7 @@ class Catan : public IGame {

std::unordered_map<std::string, PlayerState> _playersState;

bool _isFinished = false;
bool _isFinished = false;
bool _allConfigOk = false;

std::unique_ptr<ConfigUI> _configUi;
Expand Down

0 comments on commit 69e8d5d

Please sign in to comment.