-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a44767
commit 92559b9
Showing
18 changed files
with
323 additions
and
24 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 @@ | ||
*.json |
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,9 @@ | ||
{ | ||
"name": "<configName>", | ||
"assets": { | ||
}, | ||
"page": [ | ||
], | ||
"popups": { | ||
} | ||
} |
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,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) |
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,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; | ||
} |
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,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; | ||
}; |
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,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; | ||
}; |
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,7 @@ | ||
#include "Player.hpp" | ||
|
||
Player::Player(std::shared_ptr<INetwork::IPeer> peer, const std::string &username) | ||
: _peer(peer), | ||
_username(username) | ||
{ | ||
} |
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,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; | ||
}; |
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,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 | ||
) |
Oops, something went wrong.