From 57c1f7d22d6faa2af6c53dfa9f9f89205754dcd7 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 26 Nov 2023 01:27:40 +0100 Subject: [PATCH] more sketching --- src/CMakeLists.txt | 5 ++ src/solanaceae/toxic_games/games/chess.cpp | 16 ++++++ src/solanaceae/toxic_games/games/chess.hpp | 29 ++++++++++ src/solanaceae/toxic_games/toxic_game_i.hpp | 55 ++++++++++++++++++ src/solanaceae/toxic_games/toxic_games.cpp | 62 +++++++++++++++++++++ src/solanaceae/toxic_games/toxic_games.hpp | 24 ++++++++ 6 files changed, 191 insertions(+) create mode 100644 src/solanaceae/toxic_games/games/chess.cpp create mode 100644 src/solanaceae/toxic_games/games/chess.hpp create mode 100644 src/solanaceae/toxic_games/toxic_game_i.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed29161..62713fb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,6 +31,11 @@ endif() add_library(solanaceae_toxic_games ./solanaceae/toxic_games/toxic_games.hpp ./solanaceae/toxic_games/toxic_games.cpp + + ./solanaceae/toxic_games/toxic_game_i.hpp + + ./solanaceae/toxic_games/games/chess.hpp + ./solanaceae/toxic_games/games/chess.cpp ) target_link_libraries(solanaceae_toxic_games PUBLIC diff --git a/src/solanaceae/toxic_games/games/chess.cpp b/src/solanaceae/toxic_games/games/chess.cpp new file mode 100644 index 0000000..5b210fd --- /dev/null +++ b/src/solanaceae/toxic_games/games/chess.cpp @@ -0,0 +1,16 @@ +#include "./chess.hpp" + +Chess::Chess(ToxicGames& tg) : ToxicGameI(tg) { +} + +Chess::~Chess(void) { +} + +std::unique_ptr Chess::createGame(std::vector with) { + return nullptr; +} + +std::unique_ptr Chess::acceptInvite(uint32_t from, uint32_t id) { + return nullptr; +} + diff --git a/src/solanaceae/toxic_games/games/chess.hpp b/src/solanaceae/toxic_games/games/chess.hpp new file mode 100644 index 0000000..411b741 --- /dev/null +++ b/src/solanaceae/toxic_games/games/chess.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "../toxic_game_i.hpp" + +struct Chess final : public ToxicGameI { + Chess(ToxicGames& tg); + ~Chess(void); + + struct ChessInstance final : public ToxicGameI::InstanceI { + ~ChessInstance(void) {} + + // TODO: just destructor? + // needs to send quit to peers? + void quit(void) override; + + bool allInvitesAccepted(void) override; + + void onPacket(uint32_t from, const uint8_t* data, const uint32_t data_size) override; + + // ?? + //virtual void tick(); + }; + + uint8_t getGameType(void) const override { return 1; }; + + std::unique_ptr createGame(std::vector with) override; + std::unique_ptr acceptInvite(uint32_t from, uint32_t id) override; +}; + diff --git a/src/solanaceae/toxic_games/toxic_game_i.hpp b/src/solanaceae/toxic_games/toxic_game_i.hpp new file mode 100644 index 0000000..62c4e28 --- /dev/null +++ b/src/solanaceae/toxic_games/toxic_game_i.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + +//#include +//enum class Contact3 : uint32_t {}; + +// fwd +class ToxicGames; + +// static state/info + dynamic factory +struct ToxicGameI { + ToxicGames& _tg; + + ToxicGameI(ToxicGames& tg) : _tg(tg) {} + virtual ~ToxicGameI(void) {} + + struct InstanceI { + uint32_t _id; + + virtual ~InstanceI(void) {} + + // TODO: just destructor? + // needs to send quit to peers? + virtual void quit(void) = 0; + + // use state instead? + // toxic game states: + // - none + // - paused + // - running + // - finished + // - invalid + virtual bool allInvitesAccepted(void) = 0; + + virtual void onPacket(uint32_t from, const uint8_t* data, const uint32_t data_size) = 0; + + // ?? + //virtual void tick(); + }; + + virtual uint8_t getGameType(void) const = 0; + + // with (contact list, (in turn order?)) + // sends out invites + virtual std::unique_ptr createGame(std::vector with) = 0; + + // from + // game id + // with (contact list) + virtual std::unique_ptr acceptInvite(uint32_t from, uint32_t id) = 0; +}; + diff --git a/src/solanaceae/toxic_games/toxic_games.cpp b/src/solanaceae/toxic_games/toxic_games.cpp index 5db21c2..45d0eaf 100644 --- a/src/solanaceae/toxic_games/toxic_games.cpp +++ b/src/solanaceae/toxic_games/toxic_games.cpp @@ -1,8 +1,15 @@ #include "./toxic_games.hpp" +#include +#include + #include #include +#include "./games/chess.hpp" + +// https://youtu.be/9tLCQQ5_ado + ToxicGames::ToxicGames( Contact3Registry& cr, ToxI& t, @@ -15,12 +22,67 @@ ToxicGames::ToxicGames( _tcm(tcm) { + { // chess + auto tmp_game = std::make_unique(*this); + const auto game_type = tmp_game->getGameType(); + _game_types[game_type] = std::move(tmp_game); + } + // register custom packet handlers _tep.subscribe(this, Tox_Event::TOX_EVENT_FRIEND_LOSSLESS_PACKET); _tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PACKET); _tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET); } +//void ToxicGames::addGameInstance(uint8_t game_type, uint32_t game_id, std::unique_ptr instance) { + //// TODO: error checking + //_game_instances[game_type][game_id] = std::move(instance); +//} + +void ToxicGames::createGame(uint8_t game_type, std::vector with) { +} + +void ToxicGames::acceptInvite(Contact3 from, uint8_t game_type, uint32_t game_id) { +} + +bool ToxicGames::sendPacket(Contact3 to, uint8_t game_type, uint32_t game_id, const uint8_t* data, const size_t data_size) { + if (!_cr.valid(to)) { + return false; + } + + // online gaming + if (!_cr.all_of(to) && !_cr.all_of(to)) { + return false; + } + + // friend + if (_cr.all_of(to)) { + std::vector pkg; + pkg.push_back(161); // game data + pkg.push_back(0x01); // netver + pkg.push_back(game_type); + pkg.push_back((game_id >> 24) & 0xff); + pkg.push_back((game_id >> 16) & 0xff); + pkg.push_back((game_id >> 8) & 0xff); + pkg.push_back((game_id >> 0) & 0xff); + pkg.insert(pkg.cend(), data, data+data_size); + + const auto send_err = _t.toxFriendSendLosslessPacket( + _cr.get(to).friend_number, + pkg + ); + + return send_err == TOX_ERR_FRIEND_CUSTOM_PACKET_OK; + } + + // group peer + if (_cr.all_of(to)) { + return false; // TODO + } + + return false; +} + bool ToxicGames::onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) { //CUSTOM_PACKET_GAME_INVITE = 160, //CUSTOM_PACKET_GAME_DATA = 161, diff --git a/src/solanaceae/toxic_games/toxic_games.hpp b/src/solanaceae/toxic_games/toxic_games.hpp index 73731ff..7f3a9f0 100644 --- a/src/solanaceae/toxic_games/toxic_games.hpp +++ b/src/solanaceae/toxic_games/toxic_games.hpp @@ -1,14 +1,27 @@ #pragma once +#include +#include +#include +#include + #include #include +#include "./toxic_game_i.hpp" + +// TODO events: +// got invite + class ToxicGames : public ToxEventI { Contact3Registry& _cr; ToxI& _t; ToxEventProviderI& _tep; ToxContactModel2& _tcm; + std::map> _game_types; + std::map>> _game_instances; + public: ToxicGames( Contact3Registry& cr, @@ -17,6 +30,17 @@ class ToxicGames : public ToxEventI { ToxContactModel2& tcm ); + ~ToxicGames(void) {} + + public: + // TODO: a setup game, for configurability? + void createGame(uint8_t game_type, std::vector with); + // with (contact list) ? + void acceptInvite(Contact3 from, uint8_t game_type, uint32_t game_id); + + public: // internal, for games (TODO: extract?) + bool sendPacket(Contact3 to, uint8_t game_type, uint32_t game_id, const uint8_t* data, const size_t data_size); + private: // tox events bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override; bool onToxEvent(const Tox_Event_Group_Custom_Packet* e) override;