port to contact4

This commit is contained in:
Green Sky 2025-03-10 19:45:42 +01:00
parent bdf375a91c
commit 05ccfe918d
No known key found for this signature in database
GPG Key ID: DBE05085D874AB4A
6 changed files with 86 additions and 67 deletions

View File

@ -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<ToxicGames>(*cr, *t, *tep, *tcm);
g_tg = std::make_unique<ToxicGames>(*cs, *t, *tep, *tcm);
// register types
PLUG_PROVIDE_INSTANCE(ToxicGames, plugin_name, g_tg.get());

View File

@ -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<uint8_t> pkg;
//bool Chess::ChessInstance::sendResign(void) {
//std::vector<uint8_t> pkg;
// resign (chess specific)
pkg.push_back(0xff);
return _game_type_static._tg.sendPacket(
static_cast<Contact3>(_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<Contact3>(_opponent),
//_game_type_static.getGameType(),
//_id,
//pkg.data(), pkg.size()
//);
//}
bool Chess::ChessInstance::sendMove(ChessCoords p0, ChessCoords p1) {
std::vector<uint8_t> pkg;
//bool Chess::ChessInstance::sendMove(ChessCoords p0, ChessCoords p1) {
//std::vector<uint8_t> 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<Contact3>(_opponent),
_game_type_static.getGameType(),
_id,
pkg.data(), pkg.size()
);
}
//return _game_type_static._tg.sendPacket(
//static_cast<Contact3>(_opponent),
//_game_type_static.getGameType(),
//_id,
//pkg.data(), pkg.size()
//);
//}
Chess::Chess(ToxicGames& tg) : ToxicGameI(tg) {
}
@ -66,18 +64,18 @@ std::unique_ptr<Chess::InstanceI> Chess::createGame(std::vector<uint32_t> with)
}
std::unique_ptr<Chess::InstanceI> Chess::acceptInvite(uint32_t from, uint32_t game_id) {
sendAcceptInvite(from, game_id);
//sendAcceptInvite(from, game_id);
auto new_instance = std::make_unique<ChessInstance>(*this, from, game_id);
return new_instance;
}
bool Chess::sendAcceptInvite(uint32_t to, uint32_t game_id) {
std::vector<uint8_t> pkg;
//bool Chess::sendAcceptInvite(uint32_t to, uint32_t game_id) {
//std::vector<uint8_t> pkg;
// init accept invite (chess specific)
pkg.push_back(0x02);
return _tg.sendPacket(static_cast<Contact3>(to), getGameType(), game_id, pkg.data(), pkg.size());
}
//// init accept invite (chess specific)
//pkg.push_back(0x02);
//return _tg.sendPacket(static_cast<Contact3>(to), getGameType(), game_id, pkg.data(), pkg.size());
//}

View File

@ -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<InstanceI> createGame(std::vector<uint32_t> with) override;
std::unique_ptr<InstanceI> 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);
};

View File

@ -4,8 +4,8 @@
#include <memory>
#include <vector>
//#include <solanaceae/contact/contact_model3.hpp>
//enum class Contact3 : uint32_t {};
//#include <solanaceae/contact/fwd.hpp>
//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;
// ??

View File

@ -1,8 +1,12 @@
#include "./toxic_games.hpp"
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/contact/contact_store_i.hpp>
#include <solanaceae/tox_contacts/components.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/handle.hpp>
#include <cstdint>
#include <iostream>
@ -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<Contact3> with) {
void ToxicGames::createGame(uint8_t game_type, std::vector<Contact4> 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<Contact::Components::ToxFriendEphemeral>(from) && !_cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(from)) {
if (!cr.all_of<Contact::Components::ToxFriendEphemeral>(from) && !cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(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<Contact::Components::ToxFriendEphemeral>(to) && !_cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(to)) {
if (!cr.all_of<Contact::Components::ToxFriendEphemeral>(to) && !cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(to)) {
return false;
}
// friend
if (_cr.all_of<Contact::Components::ToxFriendEphemeral>(to)) {
if (cr.all_of<Contact::Components::ToxFriendEphemeral>(to)) {
std::vector<uint8_t> 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<Contact::Components::ToxFriendEphemeral>(to).friend_number,
cr.get<Contact::Components::ToxFriendEphemeral>(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<Contact::Components::ToxGroupPeerEphemeral>(to)) {
if (cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(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<uint32_t>(from), nullptr, 0);
const auto from = _tcm.getContactFriend(friend_number);
_game_instances.at(game_type).at(game_id)->onPacket(static_cast<uint32_t>(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";

View File

@ -5,7 +5,7 @@
#include <map>
#include <vector>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/contact/fwd.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#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<Contact3> with);
void createGame(uint8_t game_type, std::vector<Contact4> 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;