Skip to content

Commit

Permalink
Add some files dor server catan
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 29, 2024
1 parent 1a44767 commit 92559b9
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 24 deletions.
1 change: 1 addition & 0 deletions client/assets/uiconf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
1 change: 1 addition & 0 deletions client/src/menu/gamependingmenu/GamePendingMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void GamePendingMenu::update(raylib::Window &window)
continue;
}
}
_gamesMode.setCurrentGameMode(game);
menuState.setState(M_GAMEMENU);
return;
}
Expand Down
14 changes: 9 additions & 5 deletions client/src/uiconf/UIConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,16 @@ std::string UIConf::hash(const std::string &identifier)
{
std::ifstream file(UIConf::toFile(identifier));
std::string all;
std::string line;

if (file.is_open() && !file.bad()) {
while (std::getline(file, all)) {
all += line;
std::string bufferS;
std::vector<char> buffer(500, 0);

while (!file.eof()) {
bufferS.clear();
file.read(buffer.data(), buffer.size());
for (int i = 0; buffer[i] != '\0'; i++) {
bufferS.push_back(buffer[i]);
}
all = all + bufferS;
}
const auto hashed = std::hash<std::string> {}(all);
return std::to_string(hashed);
Expand Down
2 changes: 1 addition & 1 deletion client/src/utils/Archive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>

constexpr int MAX_PACKET_SIZE = 2048;
constexpr int COMPRESSION_LEVEL = 5;
constexpr int COMPRESSION_LEVEL = 1;

class Archive {
public:
Expand Down
9 changes: 9 additions & 0 deletions server/assets/uiconf/catan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "<configName>",
"assets": {
},
"page": [
],
"popups": {
}
}
2 changes: 2 additions & 0 deletions server/src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/Game.cpp
${CMAKE_CURRENT_SOURCE_DIR}/GamesManager.cpp
)

add_subdirectory(games)
27 changes: 20 additions & 7 deletions server/src/game/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#include "Game.hpp"
#include <algorithm>
#include <memory>
#include "Game.hpp"
#include "INetwork.hpp"
#include "Logger.hpp"

Game::Player::Player(std::shared_ptr<INetwork::IPeer> peer, const std::string &username)
: _peer(peer),
_username(username)
{
}
#include "Catan.hpp"

Game::Game()
{
Expand All @@ -25,6 +20,10 @@ void Game::update()
if (_players.size() == 0) {
_isFinished = true;
}
if (this->isStarted() && _game) {
_game->update();
return;
}
for (auto &[key, player] : _players) {
while (network.hasPacket(player._peer)) {
const auto message = network.receive(player._peer);
Expand Down Expand Up @@ -153,6 +152,9 @@ void Game::update()

bool Game::isFinished()
{
if (_isStarted && _game) {
return _game->isFinished();
}
return _isFinished;
}

Expand Down Expand Up @@ -216,6 +218,9 @@ bool Game::startGame(const Player &player)
if (_selectedGame.length() == 0) {
return false;
}
if (std::find(_availableGameName.begin(), _availableGameName.end(), _selectedGame) == _availableGameName.end()) {
return false;
}
if (player._peer->getId() != _keyOwner) {
return false;
}
Expand All @@ -224,5 +229,13 @@ bool Game::startGame(const Player &player)
return false;
}
}
if (_selectedGame == "Catan") {
_game = std::make_unique<Catan>();
_game->init(_players);
} else {
Logger::error("GAME: unknow game name");
return false;
}
_isStarted = true;
return true;
}
13 changes: 3 additions & 10 deletions server/src/game/Game.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#pragma once

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

class Game {
public:
class Player {
public:
Player(std::shared_ptr<INetwork::IPeer> peer, const std::string &username);
Player() = default;
std::shared_ptr<INetwork::IPeer> _peer;
bool _isDisconnected = false;
bool _ready = false;
std::string _username;
};
Game();
~Game();

Expand All @@ -37,4 +29,5 @@ class Game {

std::vector<std::string> _availableGameName = {"Catan", "Test"};
std::vector<std::string> _availableGameDescription = {"The catan game", "test game"};
std::unique_ptr<IGame> _game;
};
16 changes: 16 additions & 0 deletions server/src/game/games/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Player.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ConfigUI.cpp
)

add_subdirectory(catan)
121 changes: 121 additions & 0 deletions server/src/game/games/ConfigUI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#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))
{
std::ifstream file(_filePath);
std::string all;
std::string bufferS;
std::vector<char> buffer(500, 0);

while (!file.eof()) {
bufferS.clear();
file.read(buffer.data(), buffer.size());
for (int i = 0; buffer[i] != '\0'; i++) {
bufferS.push_back(buffer[i]);
}
_fileChunk.push_back(bufferS);
all = all + bufferS;
}
_fileHash = std::hash<std::string>{}(all);
}

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

void ConfigUI::update()
{
for (auto &[key, pState] : _peersState) {
if (pState._ok) {
continue;
}
const auto &pEer = _peers[key];
while (network.hasPacket(pEer)) {
const auto message = network.receive(pEer);
if (!message.contains("type") || !message.at("type").is_string()) {
continue;
}
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()) {
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},
});
continue;
}
if (hash != _fileHash) {
pState._ok = false;
pState._nbChunk = _fileChunk.size();
pState._currentChunk = -1;
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()) {
continue;
}
network.send(pEer, {
{"type", "uiConfigHash"},
{"name", _identifier},
});
}
}
if (pState._ok) {
continue;
}
if (!pState._askedHash) {
network.send(_peers[key], {
{"type", "uiConfigHash"},
{"name", _identifier},
});
pState._askedHash = true;
continue;
}
if (pState._nbChunk == -1) {
continue;
}
if (pState._currentChunk == pState._nbChunk) {
continue;
}
pState._currentChunk += 1;
if (pState._currentChunk == pState._nbChunk) {
continue;
}
network.send(pEer, {
{"type", "uiConfig"},
{"name", _identifier},
{"nbChunk", pState._nbChunk},
{"chunkIndex", pState._currentChunk},
{"data", _fileChunk[pState._currentChunk]},
});
}
return;
}

bool ConfigUI::isPeersOk()
{
for (const auto &[_, pState] : _peersState) {
if (!pState._ok) {
return false;
}
}
return true;
}
33 changes: 33 additions & 0 deletions server/src/game/games/ConfigUI.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

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

class ConfigUI {
public:
ConfigUI(const std::string &identifier);

void addPeer(std::shared_ptr<INetwork::IPeer> peer);
bool isPeersOk();
void update();

private:

class ConfigState {
public:
bool _askedHash = false;
bool _ok = false;
int _nbChunk = -1;
int _currentChunk = -1;
std::string _hashClient;
};

std::string _identifier;
std::string _fileHash;
std::string _filePath;
std::vector<std::string> _fileChunk;

std::unordered_map<std::string, std::shared_ptr<INetwork::IPeer>> _peers;
std::unordered_map<std::string, ConfigState> _peersState;
};
13 changes: 13 additions & 0 deletions server/src/game/games/IGame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

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

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

Player::Player(std::shared_ptr<INetwork::IPeer> peer, const std::string &username)
: _peer(peer),
_username(username)
{
}
14 changes: 14 additions & 0 deletions server/src/game/games/Player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

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

class Player {
public:
Player(std::shared_ptr<INetwork::IPeer> peer, const std::string &username);
Player() = default;
std::shared_ptr<INetwork::IPeer> _peer;
bool _isDisconnected = false;
bool _ready = false;
std::string _username;
};
13 changes: 13 additions & 0 deletions server/src/game/games/catan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Catan.cpp
)
Loading

0 comments on commit 92559b9

Please sign in to comment.