#ifndef NETWORK_H #define NETWORK_H #include #include #include #include #include struct bufferevent; struct event; struct event_base; struct evconnlistener; struct sockaddr; #define check_trivially_copyable(T) static_assert(std::is_trivially_copyable::value == true && std::is_standard_layout::value == true, "not trivially copyable") namespace ygo { #ifdef _WIN32 using EventSocket = intptr_t; // evutil_socket_t #else using EventSocket = int; #endif constexpr int SIZE_NETWORK_BUFFER = 0x20000; constexpr int MAX_DATA_SIZE = UINT16_MAX - 1; struct HostInfo { uint32_t lflist{}; uint8_t rule{}; uint8_t mode{}; uint8_t duel_rule{}; uint8_t no_check_deck{}; uint8_t no_shuffle_deck{}; // byte padding[3] int32_t start_lp{}; uint8_t start_hand{}; uint8_t draw_count{}; uint16_t time_limit{}; }; check_trivially_copyable(HostInfo); static_assert(sizeof(HostInfo) == 20, "size mismatch: HostInfo"); struct HostPacket { uint16_t identifier{}; uint16_t version{}; uint16_t port{}; // byte padding[2] uint32_t ipaddr{}; uint16_t name[20]{}; HostInfo host; }; check_trivially_copyable(HostPacket); static_assert(sizeof(HostPacket) == 72, "size mismatch: HostPacket"); struct HostRequest { uint16_t identifier{}; }; check_trivially_copyable(HostRequest); static_assert(sizeof(HostRequest) == 2, "size mismatch: HostRequest"); struct CTOS_HandResult { uint8_t res{}; }; check_trivially_copyable(CTOS_HandResult); static_assert(sizeof(CTOS_HandResult) == 1, "size mismatch: CTOS_HandResult"); struct CTOS_TPResult { uint8_t res{}; }; check_trivially_copyable(CTOS_TPResult); static_assert(sizeof(CTOS_TPResult) == 1, "size mismatch: CTOS_TPResult"); struct CTOS_PlayerInfo { uint16_t name[20]{}; }; check_trivially_copyable(CTOS_PlayerInfo); static_assert(sizeof(CTOS_PlayerInfo) == 40, "size mismatch: CTOS_PlayerInfo"); struct CTOS_CreateGame { HostInfo info; uint16_t name[20]{}; uint16_t pass[20]{}; }; check_trivially_copyable(CTOS_CreateGame); static_assert(sizeof(CTOS_CreateGame) == 100, "size mismatch: CTOS_CreateGame"); struct CTOS_JoinGame { uint16_t version{}; // byte padding[2] uint32_t gameid{}; uint16_t pass[20]{}; }; check_trivially_copyable(CTOS_JoinGame); static_assert(sizeof(CTOS_JoinGame) == 48, "size mismatch: CTOS_JoinGame"); struct CTOS_Kick { uint8_t pos{}; }; check_trivially_copyable(CTOS_Kick); static_assert(sizeof(CTOS_Kick) == 1, "size mismatch: CTOS_Kick"); /* * CTOS_ExternalAddress * uint32_t real_ip; (IPv4 address, BE, alway 0 in normal client) * uint16_t hostname[256]; (UTF-16 string) */ constexpr int LEN_HOSTNAME = 256; // STOC struct STOC_ErrorMsg { uint8_t msg{}; // byte padding[3] uint32_t code{}; }; check_trivially_copyable(STOC_ErrorMsg); static_assert(sizeof(STOC_ErrorMsg) == 8, "size mismatch: STOC_ErrorMsg"); struct STOC_HandResult { uint8_t res1{}; uint8_t res2{}; }; check_trivially_copyable(STOC_HandResult); static_assert(sizeof(STOC_HandResult) == 2, "size mismatch: STOC_HandResult"); // reserved for STOC_CREATE_GAME struct STOC_CreateGame { uint32_t gameid{}; }; check_trivially_copyable(STOC_CreateGame); static_assert(sizeof(STOC_CreateGame) == 4, "size mismatch: STOC_CreateGame"); struct STOC_JoinGame { HostInfo info; }; check_trivially_copyable(STOC_JoinGame); static_assert(sizeof(STOC_JoinGame) == 20, "size mismatch: STOC_JoinGame"); struct STOC_TypeChange { uint8_t type{}; }; check_trivially_copyable(STOC_TypeChange); static_assert(sizeof(STOC_TypeChange) == 1, "size mismatch: STOC_TypeChange"); // reserved for STOC_LEAVE_GAME struct STOC_ExitGame { uint8_t pos{}; }; check_trivially_copyable(STOC_ExitGame); static_assert(sizeof(STOC_ExitGame) == 1, "size mismatch: STOC_ExitGame"); struct STOC_TimeLimit { uint8_t player{}; // byte padding[1] uint16_t left_time{}; }; check_trivially_copyable(STOC_TimeLimit); static_assert(sizeof(STOC_TimeLimit) == 4, "size mismatch: STOC_TimeLimit"); /* * STOC_Chat * uint16_t player_type; * uint16_t msg[256]; (UTF-16 string) */ constexpr int LEN_CHAT_PLAYER = 1; constexpr int LEN_CHAT_MSG = 256; constexpr int SIZE_STOC_CHAT = (LEN_CHAT_PLAYER + LEN_CHAT_MSG) * sizeof(uint16_t); #pragma pack(push, 1) struct STOC_HS_PlayerEnter { uint16_t name[20]{}; uint8_t pos{}; }; #pragma pack(pop) check_trivially_copyable(STOC_HS_PlayerEnter); static_assert(sizeof(STOC_HS_PlayerEnter) == 41, "size mismatch: STOC_HS_PlayerEnter"); struct STOC_HS_PlayerChange { //pos<<4 | state uint8_t status{}; }; check_trivially_copyable(STOC_HS_PlayerChange); static_assert(sizeof(STOC_HS_PlayerChange) == 1, "size mismatch: STOC_HS_PlayerChange"); struct STOC_HS_WatchChange { uint16_t watch_count{}; }; check_trivially_copyable(STOC_HS_WatchChange); static_assert(sizeof(STOC_HS_WatchChange) == 2, "size mismatch: STOC_HS_WatchChange"); // YGOL feature messages: fixed-size payloads; the STOC form is uint16_t player_type + the same bytes #pragma pack(push, 1) struct CTOS_Capability { uint16_t protocol{}; uint32_t flags{}; }; struct CTOS_Appearance { int32_t items[8]{}; uint16_t deck_name[48]{}; uint16_t character_id[64]{}; uint16_t deck_group[48]{}; }; struct CTOS_Dialogue { uint8_t version{}; uint8_t flags{}; int16_t index{}; uint32_t card_code{}; uint32_t target_code{}; uint16_t corpus_key[48]{}; uint16_t event_key[48]{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_Capability); static_assert(sizeof(CTOS_Capability) == 6, "size mismatch: CTOS_Capability"); check_trivially_copyable(CTOS_Appearance); static_assert(sizeof(CTOS_Appearance) == 352, "size mismatch: CTOS_Appearance"); check_trivially_copyable(CTOS_Dialogue); static_assert(sizeof(CTOS_Dialogue) == 204, "size mismatch: CTOS_Dialogue"); constexpr int SIZE_STOC_FEATURE = sizeof(uint16_t) + sizeof(CTOS_Appearance); constexpr uint16_t FEATURE_PROTOCOL = 12; // YGOL host seats: 1 -> 2; YGOL seat state: 2 -> 3; YGOL lobby after match: 3 -> 4; YGOL coin toss: 4 -> 5; YGOL deck lists: 5 -> 6; YGOL profile: 6 -> 7; YGOL attack preview: 7 -> 8; YGOL tag partner cards: 8 -> 9; YGOL tag two fields: 9 -> 10; YGOL tag shared extra: 10 -> 11; YGOL prompt context: 11 -> 12 constexpr uint32_t FEATURE_APPEARANCE = 0x1; constexpr uint32_t FEATURE_DIALOGUE = 0x2; constexpr uint16_t FEATURE_SENDER_SERVER = 0xff; // YGOL setup pairs: ordered ygol.= tokens after slot 11, never interpreted constexpr uint32_t FEATURE_SETUP_PAIRS = 0x4; constexpr size_t SETUP_PAIRS_MAX = 64; constexpr size_t SETUP_PAIR_MAX_LEN = 512; constexpr size_t SETUP_PAIRS_BLOCK_MAX = sizeof(uint16_t) + SETUP_PAIRS_MAX * (sizeof(uint16_t) + SETUP_PAIR_MAX_LEN); constexpr uint32_t REPLAY_YGOL_SETUP_PAIRS = 0x40000000; // replay flag: uint32_t length + the block, before the names // YGOL life points per side: the setup pair ygol.side.b.lp gives seat side B (seat 1; Tag seats 2 and 3) its own start LP constexpr uint32_t REPLAY_YGOL_SIDE_LP = 0x20000000; // replay flag: int32_t lp[0], int32_t lp[1] in duel player order, after opt extern std::vector setup_pairs; bool IsSetupPairToken(const char* token); bool AddSetupPair(const char* token); size_t WriteSetupPairsBlock(unsigned char* dst); // YGOL host seats: the host plans every seat (Open / Cpu); a joiner claims a Cpu seat with that seat's token #pragma pack(push, 1) struct CTOS_SeatPlan { uint8_t restart_id[16]{}; uint32_t deadline_ms{}; uint16_t revision{}; uint8_t seat_count{}; uint8_t kind[4]{}; uint8_t token[4][16]{}; }; struct CTOS_SeatRequest { uint16_t protocol{}; uint8_t restart_id[16]{}; uint8_t seat{}; uint8_t token[16]{}; }; struct CTOS_SeatSwap { uint8_t first{}; uint8_t second{}; }; struct CTOS_SeatMove { uint8_t from{}; uint8_t to{}; }; struct STOC_SeatPlan { uint8_t restart_id[16]{}; uint16_t revision{}; uint8_t seat_count{}; uint8_t kind[4]{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_SeatPlan); static_assert(sizeof(CTOS_SeatPlan) == 91, "size mismatch: CTOS_SeatPlan"); check_trivially_copyable(CTOS_SeatRequest); static_assert(sizeof(CTOS_SeatRequest) == 35, "size mismatch: CTOS_SeatRequest"); check_trivially_copyable(CTOS_SeatSwap); static_assert(sizeof(CTOS_SeatSwap) == 2, "size mismatch: CTOS_SeatSwap"); check_trivially_copyable(CTOS_SeatMove); static_assert(sizeof(CTOS_SeatMove) == 2, "size mismatch: CTOS_SeatMove"); check_trivially_copyable(STOC_SeatPlan); static_assert(sizeof(STOC_SeatPlan) == 23, "size mismatch: STOC_SeatPlan"); constexpr uint32_t FEATURE_SEAT_PLAN = 0x8; constexpr uint8_t SEAT_KIND_OPEN = 0; constexpr uint8_t SEAT_KIND_CPU = 1; constexpr uint8_t SEAT_WATCHERS = 0xff; constexpr int SEAT_TOKEN_LEN = 16; // YGOL rules restart: the host replaces its server on the same port; every human and CPU comes back to the same seat, unready #pragma pack(push, 1) struct CTOS_RestartPrepare { uint8_t restart_id[16]{}; uint32_t timeout_ms{}; uint8_t seat_count{}; uint8_t seat_token[4][16]{}; uint8_t observer_token[16]{}; }; struct STOC_RoomRestarting { uint8_t phase{}; uint8_t restart_id[16]{}; uint8_t seat{}; uint8_t token[16]{}; uint32_t timeout_ms{}; }; struct CTOS_RestartAck { uint8_t restart_id[16]{}; uint8_t phase{}; }; struct CTOS_RestartControl { uint8_t restart_id[16]{}; uint8_t action{}; }; struct STOC_RestartStatus { uint8_t phase{}; uint8_t restart_id[16]{}; uint8_t acked{}; uint8_t expected{}; uint8_t complete{}; uint8_t reason{}; }; struct CTOS_SeatResume { uint8_t restart_id[16]{}; uint8_t action{}; uint8_t reason{}; }; struct STOC_SeatResumeResult { uint8_t restart_id[16]{}; uint8_t accepted{}; uint8_t seat{}; uint8_t reason{}; }; struct CTOS_RestartOpen { uint8_t restart_id[16]{}; uint8_t observer_token[16]{}; }; struct STOC_RestartOpen { uint8_t restart_id[16]{}; uint8_t opened{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_RestartPrepare); static_assert(sizeof(CTOS_RestartPrepare) == 101, "size mismatch: CTOS_RestartPrepare"); check_trivially_copyable(STOC_RoomRestarting); static_assert(sizeof(STOC_RoomRestarting) == 38, "size mismatch: STOC_RoomRestarting"); check_trivially_copyable(CTOS_RestartAck); static_assert(sizeof(CTOS_RestartAck) == 17, "size mismatch: CTOS_RestartAck"); check_trivially_copyable(CTOS_RestartControl); static_assert(sizeof(CTOS_RestartControl) == 17, "size mismatch: CTOS_RestartControl"); check_trivially_copyable(STOC_RestartStatus); static_assert(sizeof(STOC_RestartStatus) == 21, "size mismatch: STOC_RestartStatus"); check_trivially_copyable(CTOS_SeatResume); static_assert(sizeof(CTOS_SeatResume) == 18, "size mismatch: CTOS_SeatResume"); check_trivially_copyable(STOC_SeatResumeResult); static_assert(sizeof(STOC_SeatResumeResult) == 19, "size mismatch: STOC_SeatResumeResult"); check_trivially_copyable(CTOS_RestartOpen); static_assert(sizeof(CTOS_RestartOpen) == 32, "size mismatch: CTOS_RestartOpen"); check_trivially_copyable(STOC_RestartOpen); static_assert(sizeof(STOC_RestartOpen) == 17, "size mismatch: STOC_RestartOpen"); constexpr uint32_t FEATURE_RULES_RESTART = 0x10; constexpr uint8_t SEAT_KIND_RESERVED = 2; constexpr uint8_t RESTART_PHASE_PREPARE = 1; constexpr uint8_t RESTART_PHASE_COMMIT = 2; constexpr uint8_t RESTART_PHASE_ABORT = 3; constexpr uint8_t RESTART_REASON_NONE = 0; constexpr uint8_t RESTART_REASON_INCOMPATIBLE = 1; constexpr uint8_t RESTART_REASON_ACK_TIMEOUT = 2; constexpr uint8_t RESTART_REASON_NOT_LOBBY = 3; constexpr uint8_t RESTART_REASON_NOT_HOST = 4; constexpr uint8_t RESTART_REASON_BUSY = 5; constexpr uint8_t RESTART_REASON_BAD_REQUEST = 6; constexpr uint8_t SEAT_RESUME_COMPLETE = 1; constexpr uint8_t SEAT_RESUME_RELEASE = 2; constexpr uint8_t RESUME_REASON_NO_RESTART = 1; constexpr uint8_t RESUME_REASON_NOT_RESTORING = 2; constexpr uint32_t RESTART_ACK_TIMEOUT_TICKS = 5; // YGOL seat state: what a seated duelist is doing right now; stored per seat and replayed to joiners like appearance #pragma pack(push, 1) struct CTOS_SeatState { uint8_t version{}; uint8_t state{}; uint16_t value{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_SeatState); static_assert(sizeof(CTOS_SeatState) == 4, "size mismatch: CTOS_SeatState"); constexpr uint32_t FEATURE_SEAT_STATE = 0x20; constexpr uint8_t SEAT_STATE_VERSION = 1; constexpr uint8_t SEAT_STATE_NONE = 0; constexpr uint8_t SEAT_STATE_PICKING_DECK = 1; constexpr uint8_t SEAT_STATE_MAX = SEAT_STATE_PICKING_DECK; // YGOL lobby after match: the match ended and the room stayed; the lobby every connection is back in (READY is false on every seat) #pragma pack(push, 1) struct STOC_MatchOver { uint8_t version{}; uint8_t seat_count{}; uint8_t occupied[4]{}; }; #pragma pack(pop) check_trivially_copyable(STOC_MatchOver); static_assert(sizeof(STOC_MatchOver) == 6, "size mismatch: STOC_MatchOver"); constexpr uint32_t FEATURE_LOBBY_AFTER_MATCH = 0x40; constexpr uint8_t MATCH_OVER_VERSION = 1; // YGOL coin toss: the server rolls the coin at the duel start and times the first-turn choice; rock-paper-scissors is gone #pragma pack(push, 1) struct STOC_CoinToss { uint8_t version{}; uint8_t chooser_seat{}; uint8_t face{}; uint8_t choose_seconds{}; }; #pragma pack(pop) check_trivially_copyable(STOC_CoinToss); static_assert(sizeof(STOC_CoinToss) == 4, "size mismatch: STOC_CoinToss"); constexpr uint32_t FEATURE_COIN_TOSS = 0x80; constexpr uint8_t COIN_TOSS_VERSION = 1; constexpr uint8_t COIN_FACE_HEADS = 0; constexpr uint8_t COIN_FACE_TAILS = 1; constexpr uint8_t COIN_FACE_NONE = 0xFF; constexpr uint8_t COIN_CHOOSE_SECONDS = 10; // YGOL deck lists: the deck's owner gets its remaining cards SORTED (the order never leaves the server); when SpectatorsSeeAll() // every observer gets the real order top first and the submitted Main Deck of the duelist whose deck is in the zone #pragma pack(push, 1) struct STOC_DeckList { uint8_t version{}; uint8_t side{}; uint8_t kind{}; uint16_t count{}; }; #pragma pack(pop) check_trivially_copyable(STOC_DeckList); static_assert(sizeof(STOC_DeckList) == 5, "size mismatch: STOC_DeckList"); constexpr uint32_t FEATURE_DECK_LIST = 0x100; constexpr uint8_t DECK_LIST_VERSION = 1; constexpr uint8_t DECK_LIST_SORTED = 0; constexpr uint8_t DECK_LIST_ORDERED = 1; constexpr uint8_t DECK_LIST_SUBMITTED = 2; constexpr int DECK_LIST_KINDS = 3; // YGOL profile: a seated duelist's avatar id and frame id; stored per seat and replayed to joiners like appearance #pragma pack(push, 1) struct CTOS_Profile { uint8_t version{}; uint8_t reserved{}; uint16_t avatar_id[32]{}; uint16_t frame_id[16]{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_Profile); static_assert(sizeof(CTOS_Profile) == 98, "size mismatch: CTOS_Profile"); constexpr uint32_t FEATURE_PROFILE = 0x200; constexpr uint8_t PROFILE_VERSION = 1; // YGOL attack preview: the seat whose battle prompt is open asks for pairs; the host's embedded core answers each one through ygol_preview_attack #pragma pack(push, 1) struct CTOS_AttackPreviewPair { uint8_t attacker_location{}; uint8_t attacker_sequence{}; uint8_t target_location{}; // ATTACK_PREVIEW_DIRECT = a direct attack uint8_t target_sequence{}; }; #pragma pack(pop) check_trivially_copyable(CTOS_AttackPreviewPair); static_assert(sizeof(CTOS_AttackPreviewPair) == 4, "size mismatch: CTOS_AttackPreviewPair"); constexpr uint32_t FEATURE_ATTACK_PREVIEW = 0x400; constexpr uint8_t ATTACK_PREVIEW_VERSION = 1; constexpr uint8_t ATTACK_PREVIEW_DIRECT = 0xFF; constexpr int ATTACK_PREVIEW_MAX_PAIRS = 64; constexpr int ATTACK_PREVIEW_ANSWER_SIZE = 24; // the ygol_preview_attack answer, echoed after the pair // YGOL tag two fields (proposal 45): in Tag on Master Rule 4/5 each partner has a Field slot (Spell/Trap sequence 5 and 6, the core's // field::has_two_fields); no message, the second field reaches a client as sequence 6. A Tag room refuses a joiner without the bit // (ERRMSG_VERERROR), a Single room does not care; 0x800 is reserved by proposal 44 constexpr uint32_t FEATURE_TAG_TWO_FIELDS = 0x1000; // YGOL tag partner cards: the waiting teammate's hand, Extra Deck and deck (ygol_query_tag_cards), sent to that side's two seats only constexpr uint32_t FEATURE_TAG_PARTNER_CARDS = 0x2000; // YGOL tag shared extra (proposal 53): with the setup pair ygol.tag.extra=shared a Tag team has one Extra Deck (option DUEL_TAG_SHARED_EXTRA) // and its Extra Deck refreshes carry QUERY_TAG_MEMBER; a Tag room refuses a joiner without the bit (ERRMSG_VERERROR) constexpr uint32_t FEATURE_TAG_SHARED_EXTRA = 0x4000; // YGOL prompt context (proposal 55): which card and which effect asks (ygol_query_prompt_context), sent to the answering seat right // before every relayed prompt but rock-paper-scissors; never to observers, never in a replay; a room does not refuse a joiner without it constexpr uint32_t FEATURE_PROMPT_CONTEXT = 0x8000; constexpr int PROMPT_CONTEXT_RECORD_SIZE = 23; // the ygol_query_prompt_context answer; the message adds the prompt's MSG id after it constexpr uint8_t TAG_PARTNER_CARDS_VERSION = 1; // the one-use bootstrap secret of a replacement server: a host-string token parsed apart from the ygol.* pairs, never stored anywhere else extern uint8_t bootstrap_secret[16]; extern bool bootstrap_closed; bool IsBootstrapToken(const char* token); void SetBootstrapSecret(const char* token); bool WatchersAllowed(); bool SpectatorsSeeAll(); // YGOL spectators see all int32_t SideBStartLp(int32_t fallback); // YGOL life points per side: ygol.side.b.lp, else fallback bool TagSharedExtra(); // YGOL tag shared extra: ygol.tag.extra=shared inline bool IsZeroId(const uint8_t* id) { for(int i = 0; i < 16; ++i) if(id[i]) return false; return true; } class DuelMode; struct DuelPlayer { uint16_t name[20]{}; DuelMode* game{}; uint8_t type{}; uint8_t state{}; bufferevent* bev{}; bool has_capability{}; // YGOL feature messages CTOS_Capability capability{}; bool has_seat_request{}; // YGOL host seats CTOS_SeatRequest seat_request{}; bool ygol_cpu{}; uint8_t restart_acked{}; // YGOL rules restart: the phase this connection acknowledged bool ygol_restoring{}; // seated through a restart claim: no auto-ready until SeatResume Complete bool ygol_bootstrap{}; // the replacement server's host, authenticated by the bootstrap secret }; inline unsigned int GetPosition(unsigned char* qbuf, size_t offset) { unsigned int info = 0; std::memcpy(&info, qbuf + offset, sizeof info); return info >> 24; } class DuelMode { public: DuelMode() = default; virtual ~DuelMode() = default; virtual void Chat(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL feature messages virtual void Capability(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void Appearance(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void Dialogue(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL host seats virtual void SeatPlan(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void SeatSwap(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void SeatMove(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL rules restart virtual void RestartPrepare(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void RestartAck(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void RestartControl(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void SeatResume(DuelPlayer* dp, unsigned char* pdata, int len) = 0; virtual void RestartOpen(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL seat state virtual void SeatState(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL profile virtual void Profile(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL attack preview virtual void AttackPreview(DuelPlayer* dp, unsigned char* pdata, int len) = 0; // YGOL coin toss virtual void ChooseTimeout() = 0; virtual void JoinGame(DuelPlayer* dp, unsigned char* pdata, bool is_creater) = 0; virtual void LeaveGame(DuelPlayer* dp) = 0; virtual void ToDuelist(DuelPlayer* dp) = 0; virtual void ToObserver(DuelPlayer* dp) = 0; virtual void PlayerReady(DuelPlayer* dp, bool is_ready) = 0; virtual void PlayerKick(DuelPlayer* dp, unsigned char pos) = 0; virtual void UpdateDeck(DuelPlayer* dp, unsigned char* pdata, unsigned int len) = 0; virtual void StartDuel(DuelPlayer* dp) = 0; virtual void HandResult(DuelPlayer* dp, unsigned char res) = 0; virtual void TPResult(DuelPlayer* dp, unsigned char tp) = 0; virtual void Process() = 0; virtual int Analyze(unsigned char* msgbuffer, unsigned int len) = 0; virtual void Surrender(DuelPlayer* dp) = 0; virtual void GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len) = 0; virtual void TimeConfirm(DuelPlayer* dp) = 0; #ifdef YGOPRO_SERVER_MODE virtual void RequestField(DuelPlayer* dp) = 0; #endif virtual void TimerTick() = 0; virtual void EndDuel() = 0; virtual void OnPlayerDisconnected(DuelPlayer* dp) = 0; #ifdef YGOPRO_SERVER_MODE virtual void TestCard(int code) = 0; #endif public: DuelPlayer* host_player{ nullptr }; HostInfo host_info; int duel_stage{}; intptr_t pduel{}; wchar_t name[20]{}; wchar_t pass[20]{}; std::vector registry_dump; }; } #define NETWORK_SERVER_ID 0x7428 #define NETWORK_CLIENT_ID 0xdef6 #define NETPLAYER_TYPE_PLAYER1 0 #define NETPLAYER_TYPE_PLAYER2 1 #define NETPLAYER_TYPE_PLAYER3 2 #define NETPLAYER_TYPE_PLAYER4 3 #define NETPLAYER_TYPE_PLAYER5 4 #define NETPLAYER_TYPE_PLAYER6 5 #define NETPLAYER_TYPE_OBSERVER 7 #define CTOS_RESPONSE 0x1 // byte array #define CTOS_UPDATE_DECK 0x2 // uint32_t mainc, uint32_t sidec, uint32_t[mainc+sidec] #define CTOS_HAND_RESULT 0x3 // CTOS_HandResult #define CTOS_TP_RESULT 0x4 // CTOS_TPResult #define CTOS_PLAYER_INFO 0x10 // CTOS_PlayerInfo #define CTOS_CREATE_GAME 0x11 // CTOS_CreateGame #define CTOS_JOIN_GAME 0x12 // CTOS_JoinGame #define CTOS_LEAVE_GAME 0x13 // no data #define CTOS_SURRENDER 0x14 // no data #define CTOS_TIME_CONFIRM 0x15 // no data #define CTOS_CHAT 0x16 // uint16_t array #define CTOS_EXTERNAL_ADDRESS 0x17 // CTOS_ExternalAddress #define CTOS_HS_TODUELIST 0x20 // no data #define CTOS_HS_TOOBSERVER 0x21 // no data #define CTOS_HS_READY 0x22 // no data #define CTOS_HS_NOTREADY 0x23 // no data #define CTOS_HS_KICK 0x24 // CTOS_Kick #define CTOS_HS_START 0x25 // no data #define CTOS_REQUEST_FIELD 0x30 // YGOL feature messages #define CTOS_CAPABILITY 0x40 // CTOS_Capability #define CTOS_APPEARANCE 0x41 // CTOS_Appearance #define CTOS_DIALOGUE 0x42 // CTOS_Dialogue // YGOL host seats #define CTOS_SEAT_PLAN 0x50 // CTOS_SeatPlan, host only #define CTOS_SEAT_REQUEST 0x51 // CTOS_SeatRequest, before JoinGame #define CTOS_SEAT_SWAP 0x52 // CTOS_SeatSwap, host only #define CTOS_SEAT_MOVE 0x53 // CTOS_SeatMove // YGOL rules restart #define CTOS_RESTART_PREPARE 0x54 // CTOS_RestartPrepare, host only #define CTOS_RESTART_ACK 0x55 // CTOS_RestartAck #define CTOS_RESTART_CONTROL 0x56 // CTOS_RestartControl, host only #define CTOS_SEAT_RESUME 0x57 // CTOS_SeatResume #define CTOS_RESTART_OPEN 0x58 // CTOS_RestartOpen, bootstrap host only // YGOL seat state #define CTOS_SEAT_STATE 0x59 // CTOS_SeatState, own seat only // YGOL profile #define CTOS_PROFILE 0x5D // CTOS_Profile, own seat only // YGOL attack preview #define CTOS_ATTACK_PREVIEW 0x5E // uint8_t version, uint8_t count, CTOS_AttackPreviewPair[count]; own open battle prompt only inline bool IsFeatureMessage(unsigned char pktType) { return pktType == CTOS_CAPABILITY || pktType == CTOS_APPEARANCE || pktType == CTOS_DIALOGUE || pktType == CTOS_SEAT_PLAN || pktType == CTOS_SEAT_REQUEST || pktType == CTOS_SEAT_SWAP || pktType == CTOS_SEAT_MOVE // YGOL host seats || pktType == CTOS_RESTART_PREPARE || pktType == CTOS_RESTART_ACK || pktType == CTOS_RESTART_CONTROL || pktType == CTOS_SEAT_RESUME || pktType == CTOS_RESTART_OPEN // YGOL rules restart || pktType == CTOS_SEAT_STATE // YGOL seat state || pktType == CTOS_PROFILE // YGOL profile || pktType == CTOS_ATTACK_PREVIEW; // YGOL attack preview } #define STOC_GAME_MSG 0x1 // byte array #define STOC_ERROR_MSG 0x2 // STOC_ErrorMsg #define STOC_SELECT_HAND 0x3 // no data #define STOC_SELECT_TP 0x4 // no data #define STOC_HAND_RESULT 0x5 // STOC_HandResult #define STOC_TP_RESULT 0x6 // reserved #define STOC_CHANGE_SIDE 0x7 // no data #define STOC_WAITING_SIDE 0x8 // no data #define STOC_DECK_COUNT 0x9 // int16_t[6] #define STOC_CREATE_GAME 0x11 // reserved #define STOC_JOIN_GAME 0x12 // STOC_JoinGame #define STOC_TYPE_CHANGE 0x13 // STOC_TypeChange #define STOC_LEAVE_GAME 0x14 // reserved #define STOC_DUEL_START 0x15 // no data #define STOC_DUEL_END 0x16 // no data #define STOC_REPLAY 0x17 // ExtendedReplayHeader + byte array #define STOC_TIME_LIMIT 0x18 // STOC_TimeLimit #define STOC_CHAT 0x19 // uint16_t + uint16_t array #define STOC_HS_PLAYER_ENTER 0x20 // STOC_HS_PlayerEnter #define STOC_HS_PLAYER_CHANGE 0x21 // STOC_HS_PlayerChange #define STOC_HS_WATCH_CHANGE 0x22 // STOC_HS_WatchChange #define STOC_TEAMMATE_SURRENDER 0x23 // no data #define STOC_FIELD_FINISH 0x30 #define STOC_SRVPRO_ROOMLIST 0x31 // YGOL feature messages: uint16_t player_type + the CTOS payload #define STOC_CAPABILITY 0x40 // uint16_t + CTOS_Capability #define STOC_APPEARANCE 0x41 // uint16_t + CTOS_Appearance #define STOC_DIALOGUE 0x42 // uint16_t + CTOS_Dialogue #define STOC_SETUP_PAIRS 0x43 // uint16_t (0xff) + uint16_t count, then per pair uint16_t length + "key=value" #define STOC_PROMPT_CONTEXT 0x44 // the 23-byte ygol_query_prompt_context record + uint8_t prompt MSG id, no sender stamp, to the answering seat only (YGOL prompt context) #define STOC_SEAT_PLAN 0x50 // uint16_t (0xff) + STOC_SeatPlan (YGOL host seats) // YGOL rules restart: uint16_t (0xff) + the struct #define STOC_ROOM_RESTARTING 0x54 // STOC_RoomRestarting, to each participant with its own seat and token #define STOC_RESTART_STATUS 0x56 // STOC_RestartStatus, host only #define STOC_SEAT_RESUME_RESULT 0x57 // STOC_SeatResumeResult, to the claimant #define STOC_RESTART_OPEN 0x58 // STOC_RestartOpen, to the bootstrap host #define STOC_SEAT_STATE 0x59 // uint16_t + CTOS_SeatState (YGOL seat state) #define STOC_MATCH_OVER 0x5A // uint16_t 0xFF + STOC_MatchOver (YGOL lobby after match) #define STOC_COIN_TOSS 0x5B // STOC_CoinToss, no sender stamp (YGOL coin toss) #define STOC_DECK_LIST 0x5C // STOC_DeckList + uint32_t codes[count], no sender stamp (YGOL deck lists) #define STOC_PROFILE 0x5D // uint16_t + CTOS_Profile (YGOL profile) #define STOC_ATTACK_PREVIEW 0x5E // uint8_t version, uint8_t count, count x (CTOS_AttackPreviewPair + 24-byte answer), no sender stamp, to the asker only (YGOL attack preview) #define STOC_TAG_PARTNER_CARDS 0x5F // uint8_t side + the ygol_query_tag_cards bytes, no sender stamp, to that side's two seats only (YGOL tag partner cards) // STOC_GAME_MSG header #define MSG_WAITING 3 #define MSG_START 4 #define MSG_UPDATE_DATA 6 // flag=0: clear #define MSG_UPDATE_CARD 7 // flag=QUERY_CODE, code=0: clear #define MSG_REQUEST_DECK 8 #define MSG_REFRESH_DECK 34 #define MSG_CARD_SELECTED 80 #define MSG_UNEQUIP 95 #define MSG_BE_CHAIN_TARGET 121 #define MSG_CREATE_RELATION 122 #define MSG_RELEASE_RELATION 123 #define PLAYERCHANGE_OBSERVE 0x8 #define PLAYERCHANGE_READY 0x9 #define PLAYERCHANGE_NOTREADY 0xa #define PLAYERCHANGE_LEAVE 0xb #define ERRMSG_JOINERROR 0x1 #define ERRMSG_DECKERROR 0x2 #define ERRMSG_SIDEERROR 0x3 #define ERRMSG_VERERROR 0x4 #define DECKERROR_LFLIST 0x1U #define DECKERROR_OCGONLY 0x2U #define DECKERROR_TCGONLY 0x3U #define DECKERROR_UNKNOWNCARD 0x4U #define DECKERROR_CARDCOUNT 0x5U #define DECKERROR_MAINCOUNT 0x6U #define DECKERROR_EXTRACOUNT 0x7U #define DECKERROR_SIDECOUNT 0x8U #define DECKERROR_NOTAVAIL 0x9U #define MODE_SINGLE 0x0 #define MODE_MATCH 0x1 #define MODE_TAG 0x2 #define DUEL_STAGE_BEGIN 0 #define DUEL_STAGE_FINGER 1 #define DUEL_STAGE_FIRSTGO 2 #define DUEL_STAGE_DUELING 3 #define DUEL_STAGE_SIDING 4 #define DUEL_STAGE_END 5 #endif //NETWORK_H