From 05ccfe918d7e69436ccc0e32836ea68a7a6cd0a9 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 10 Mar 2025 19:45:42 +0100 Subject: [PATCH] port to contact4 --- plugins/plugin_toxic_games.cpp | 4 +- src/solanaceae/toxic_games/games/chess.cpp | 68 ++++++++++----------- src/solanaceae/toxic_games/games/chess.hpp | 26 +++++--- src/solanaceae/toxic_games/toxic_game_i.hpp | 5 +- src/solanaceae/toxic_games/toxic_games.cpp | 38 +++++++----- src/solanaceae/toxic_games/toxic_games.hpp | 12 ++-- 6 files changed, 86 insertions(+), 67 deletions(-) diff --git a/plugins/plugin_toxic_games.cpp b/plugins/plugin_toxic_games.cpp index 1bd8826..9e9c68f 100644 --- a/plugins/plugin_toxic_games.cpp +++ b/plugins/plugin_toxic_games.cpp @@ -32,14 +32,14 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) } try { - auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I); auto* t = PLUG_RESOLVE_INSTANCE(ToxI); auto* tep = PLUG_RESOLVE_INSTANCE(ToxEventProviderI); auto* tcm = PLUG_RESOLVE_INSTANCE(ToxContactModel2); // static store, could be anywhere tho // construct with fetched dependencies - g_tg = std::make_unique(*cr, *t, *tep, *tcm); + g_tg = std::make_unique(*cs, *t, *tep, *tcm); // register types PLUG_PROVIDE_INSTANCE(ToxicGames, plugin_name, g_tg.get()); diff --git a/src/solanaceae/toxic_games/games/chess.cpp b/src/solanaceae/toxic_games/games/chess.cpp index 05eb296..d589e2b 100644 --- a/src/solanaceae/toxic_games/games/chess.cpp +++ b/src/solanaceae/toxic_games/games/chess.cpp @@ -18,42 +18,40 @@ bool Chess::ChessInstance::allInvitesAccepted(void) { } void Chess::ChessInstance::onPacket(uint32_t from, const uint8_t* data, const uint32_t data_size) { - // test, auto resign - sendResign(); } -bool Chess::ChessInstance::sendResign(void) { - std::vector pkg; +//bool Chess::ChessInstance::sendResign(void) { + //std::vector pkg; - // resign (chess specific) - pkg.push_back(0xff); - return _game_type_static._tg.sendPacket( - static_cast(_opponent), - _game_type_static.getGameType(), - _id, - pkg.data(), pkg.size() - ); -} + //// resign (chess specific) + //pkg.push_back(0xff); + //return _game_type_static._tg.sendPacket( + //static_cast(_opponent), + //_game_type_static.getGameType(), + //_id, + //pkg.data(), pkg.size() + //); +//} -bool Chess::ChessInstance::sendMove(ChessCoords p0, ChessCoords p1) { - std::vector pkg; +//bool Chess::ChessInstance::sendMove(ChessCoords p0, ChessCoords p1) { + //std::vector pkg; - // move piece (chess specific) - pkg.push_back(0xfe); + //// move piece (chess specific) + //pkg.push_back(0xfe); - pkg.push_back(p0.L); - pkg.push_back(p0.N); + //pkg.push_back(p0.L); + //pkg.push_back(p0.N); - pkg.push_back(p1.L); - pkg.push_back(p1.N); + //pkg.push_back(p1.L); + //pkg.push_back(p1.N); - return _game_type_static._tg.sendPacket( - static_cast(_opponent), - _game_type_static.getGameType(), - _id, - pkg.data(), pkg.size() - ); -} + //return _game_type_static._tg.sendPacket( + //static_cast(_opponent), + //_game_type_static.getGameType(), + //_id, + //pkg.data(), pkg.size() + //); +//} Chess::Chess(ToxicGames& tg) : ToxicGameI(tg) { } @@ -66,18 +64,18 @@ std::unique_ptr Chess::createGame(std::vector with) } std::unique_ptr Chess::acceptInvite(uint32_t from, uint32_t game_id) { - sendAcceptInvite(from, game_id); + //sendAcceptInvite(from, game_id); auto new_instance = std::make_unique(*this, from, game_id); return new_instance; } -bool Chess::sendAcceptInvite(uint32_t to, uint32_t game_id) { - std::vector pkg; +//bool Chess::sendAcceptInvite(uint32_t to, uint32_t game_id) { + //std::vector pkg; - // init accept invite (chess specific) - pkg.push_back(0x02); - return _tg.sendPacket(static_cast(to), getGameType(), game_id, pkg.data(), pkg.size()); -} + //// init accept invite (chess specific) + //pkg.push_back(0x02); + //return _tg.sendPacket(static_cast(to), getGameType(), game_id, pkg.data(), pkg.size()); +//} diff --git a/src/solanaceae/toxic_games/games/chess.hpp b/src/solanaceae/toxic_games/games/chess.hpp index 4a2d3a8..db387a5 100644 --- a/src/solanaceae/toxic_games/games/chess.hpp +++ b/src/solanaceae/toxic_games/games/chess.hpp @@ -6,9 +6,14 @@ struct Chess final : public ToxicGameI { Chess(ToxicGames& tg); ~Chess(void); + // TODO: extract? + struct Coords { + int32_t x {}; + int32_t y {}; + }; struct ChessCoords { - char L; - uint8_t N; + char L {}; + uint8_t N {}; }; struct ChessInstance final : public ToxicGameI::InstanceI { @@ -30,9 +35,16 @@ struct Chess final : public ToxicGameI { // ?? //virtual void tick(); - private: - bool sendResign(void); - bool sendMove(ChessCoords p0, ChessCoords p1); + //private: + //bool sendResign(void); + //bool sendMove(ChessCoords p0, ChessCoords p1); + + // the toxic game stuff + int game_x_left_bound(void); + int game_x_right_bound(void); + int game_y_top_bound(void); + int game_y_bottom_bound(void); + int game_set_window_shape(/*shape*/); }; uint8_t getGameType(void) const override { return 1; }; @@ -40,7 +52,7 @@ struct Chess final : public ToxicGameI { std::unique_ptr createGame(std::vector with) override; std::unique_ptr acceptInvite(uint32_t from, uint32_t game_id) override; - private: - bool sendAcceptInvite(uint32_t to, uint32_t game_id); + //private: + //bool sendAcceptInvite(uint32_t to, uint32_t game_id); }; diff --git a/src/solanaceae/toxic_games/toxic_game_i.hpp b/src/solanaceae/toxic_games/toxic_game_i.hpp index 62c4e28..a02176b 100644 --- a/src/solanaceae/toxic_games/toxic_game_i.hpp +++ b/src/solanaceae/toxic_games/toxic_game_i.hpp @@ -4,8 +4,8 @@ #include #include -//#include -//enum class Contact3 : uint32_t {}; +//#include +//enum class Contact4 : uint32_t {}; // fwd class ToxicGames; @@ -35,6 +35,7 @@ struct ToxicGameI { // - invalid virtual bool allInvitesAccepted(void) = 0; + // TODO: contact virtual void onPacket(uint32_t from, const uint8_t* data, const uint32_t data_size) = 0; // ?? diff --git a/src/solanaceae/toxic_games/toxic_games.cpp b/src/solanaceae/toxic_games/toxic_games.cpp index 98c1ad0..eb0a7d7 100644 --- a/src/solanaceae/toxic_games/toxic_games.cpp +++ b/src/solanaceae/toxic_games/toxic_games.cpp @@ -1,8 +1,12 @@ #include "./toxic_games.hpp" #include +#include #include +#include +#include + #include #include @@ -11,12 +15,12 @@ // https://youtu.be/9tLCQQ5_ado ToxicGames::ToxicGames( - Contact3Registry& cr, + ContactStore4I& cs, ToxI& t, ToxEventProviderI& tep, ToxContactModel2& tcm ) : - _cr(cr), + _cs(cs), _t(t), _tep_sr(tep.newSubRef(this)), _tcm(tcm) @@ -41,16 +45,18 @@ ToxicGames::ToxicGames( //_game_instances[game_type][game_id] = std::move(instance); //} -void ToxicGames::createGame(uint8_t game_type, std::vector with) { +void ToxicGames::createGame(uint8_t game_type, std::vector with) { } -void ToxicGames::acceptInvite(Contact3 from, uint8_t game_type, uint32_t game_id) { - if (!_cr.valid(from)) { +void ToxicGames::acceptInvite(Contact4 from, uint8_t game_type, uint32_t game_id) { + const auto& cr = _cs.registry(); + + if (!cr.valid(from)) { return; } // online gaming - if (!_cr.all_of(from) && !_cr.all_of(from)) { + if (!cr.all_of(from) && !cr.all_of(from)) { return; } @@ -71,18 +77,20 @@ 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)) { +bool ToxicGames::sendPacket(Contact4 to, uint8_t game_type, uint32_t game_id, const uint8_t* data, const size_t data_size) { + const auto& cr = _cs.registry(); + + if (!cr.valid(to)) { return false; } // online gaming - if (!_cr.all_of(to) && !_cr.all_of(to)) { + if (!cr.all_of(to) && !cr.all_of(to)) { return false; } // friend - if (_cr.all_of(to)) { + if (cr.all_of(to)) { std::vector pkg; pkg.push_back(161); // game data pkg.push_back(0x01); // netver @@ -94,7 +102,7 @@ bool ToxicGames::sendPacket(Contact3 to, uint8_t game_type, uint32_t game_id, co pkg.insert(pkg.cend(), data, data+data_size); const auto send_err = _t.toxFriendSendLosslessPacket( - _cr.get(to).friend_number, + cr.get(to).friend_number, pkg ); @@ -102,7 +110,7 @@ bool ToxicGames::sendPacket(Contact3 to, uint8_t game_type, uint32_t game_id, co } // group peer - if (_cr.all_of(to)) { + if (cr.all_of(to)) { return false; // TODO } @@ -145,7 +153,7 @@ bool ToxicGames::onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) { if (_game_types.count(game_type)) { if (!_game_instances[game_type].count(game_id)) { - const Contact3 from = _tcm.getContactFriend(friend_number); + const auto from = _tcm.getContactFriend(friend_number); // HACK: auto accept std::cout << "TG: autoaccepting game ...\n"; @@ -163,8 +171,8 @@ bool ToxicGames::onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) { if (_game_types.count(game_type)) { if (_game_instances[game_type].count(game_id)) { - const Contact3 from = _tcm.getContactFriend(friend_number); - _game_instances.at(game_type).at(game_id)->onPacket(static_cast(from), nullptr, 0); + const auto from = _tcm.getContactFriend(friend_number); + _game_instances.at(game_type).at(game_id)->onPacket(static_cast(entt::to_integral(from.entity())), nullptr, 0); } else { // error, unk game std::cerr << "TG error: packet for unknown game id gt:" << (uint32_t)game_type << " id:" << game_id << "\n"; diff --git a/src/solanaceae/toxic_games/toxic_games.hpp b/src/solanaceae/toxic_games/toxic_games.hpp index cfda402..634e93b 100644 --- a/src/solanaceae/toxic_games/toxic_games.hpp +++ b/src/solanaceae/toxic_games/toxic_games.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include "./toxic_game_i.hpp" @@ -14,7 +14,7 @@ // got invite class ToxicGames : public ToxEventI { - Contact3Registry& _cr; + ContactStore4I& _cs; ToxI& _t; ToxEventProviderI::SubscriptionReference _tep_sr; ToxContactModel2& _tcm; @@ -24,7 +24,7 @@ class ToxicGames : public ToxEventI { public: ToxicGames( - Contact3Registry& cr, + ContactStore4I& cs, ToxI& t, ToxEventProviderI& tep, ToxContactModel2& tcm @@ -34,12 +34,12 @@ class ToxicGames : public ToxEventI { public: // TODO: a setup game, for configurability? - void createGame(uint8_t game_type, std::vector with); + void createGame(uint8_t game_type, std::vector with); // with (contact list) ? - void acceptInvite(Contact3 from, uint8_t game_type, uint32_t game_id); + void acceptInvite(Contact4 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); + bool sendPacket(Contact4 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;