more sketching

This commit is contained in:
Green Sky 2023-11-26 01:27:40 +01:00
parent 0219d24647
commit 57c1f7d22d
No known key found for this signature in database
6 changed files with 191 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,16 @@
#include "./chess.hpp"
Chess::Chess(ToxicGames& tg) : ToxicGameI(tg) {
}
Chess::~Chess(void) {
}
std::unique_ptr<Chess::InstanceI> Chess::createGame(std::vector<uint32_t> with) {
return nullptr;
}
std::unique_ptr<Chess::InstanceI> Chess::acceptInvite(uint32_t from, uint32_t id) {
return nullptr;
}

View File

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

View File

@ -0,0 +1,55 @@
#pragma once
#include <cstdint>
#include <memory>
#include <vector>
//#include <solanaceae/contact/contact_model3.hpp>
//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<InstanceI> createGame(std::vector<uint32_t> with) = 0;
// from
// game id
// with (contact list)
virtual std::unique_ptr<InstanceI> acceptInvite(uint32_t from, uint32_t id) = 0;
};

View File

@ -1,8 +1,15 @@
#include "./toxic_games.hpp"
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/tox_contacts/components.hpp>
#include <cstdint>
#include <iostream>
#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<Chess>(*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<ToxicGameI::InstanceI> instance) {
//// TODO: error checking
//_game_instances[game_type][game_id] = std::move(instance);
//}
void ToxicGames::createGame(uint8_t game_type, std::vector<Contact3> 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<Contact::Components::ToxFriendEphemeral>(to) && !_cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(to)) {
return false;
}
// friend
if (_cr.all_of<Contact::Components::ToxFriendEphemeral>(to)) {
std::vector<uint8_t> 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<Contact::Components::ToxFriendEphemeral>(to).friend_number,
pkg
);
return send_err == TOX_ERR_FRIEND_CUSTOM_PACKET_OK;
}
// group peer
if (_cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(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,

View File

@ -1,14 +1,27 @@
#pragma once
#include <cstdint>
#include <memory>
#include <map>
#include <vector>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include "./toxic_game_i.hpp"
// TODO events:
// got invite
class ToxicGames : public ToxEventI {
Contact3Registry& _cr;
ToxI& _t;
ToxEventProviderI& _tep;
ToxContactModel2& _tcm;
std::map<uint8_t, std::unique_ptr<ToxicGameI>> _game_types;
std::map<uint8_t, std::map<uint32_t, std::unique_ptr<ToxicGameI::InstanceI>>> _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<Contact3> 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;