UNPKG

shoehive

Version:

WebSocket-based multiplayer game framework for real-time, event-driven gameplay

115 lines (114 loc) 6.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGameServer = exports.Hand = exports.Deck = exports.CardRank = exports.CardSuit = exports.EVENTS = exports.LOBBY_EVENTS = exports.TABLE_EVENTS = exports.PLAYER_EVENTS = exports.CLIENT_MESSAGE_TYPES = exports.CLIENT_COMMAND_TYPES = exports.Lobby = exports.GameManager = exports.MessageRouter = exports.EventBus = exports.WebSocketManager = exports.TableFactory = exports.TableState = exports.Seat = exports.Table = exports.Player = void 0; const Player_1 = require("./core/Player"); Object.defineProperty(exports, "Player", { enumerable: true, get: function () { return Player_1.Player; } }); const Table_1 = require("./core/Table"); Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return Table_1.Table; } }); Object.defineProperty(exports, "TableState", { enumerable: true, get: function () { return Table_1.TableState; } }); const index_1 = require("./core/card/index"); Object.defineProperty(exports, "CardSuit", { enumerable: true, get: function () { return index_1.CardSuit; } }); Object.defineProperty(exports, "CardRank", { enumerable: true, get: function () { return index_1.CardRank; } }); Object.defineProperty(exports, "Deck", { enumerable: true, get: function () { return index_1.Deck; } }); Object.defineProperty(exports, "Hand", { enumerable: true, get: function () { return index_1.Hand; } }); const Seat_1 = require("./core/Seat"); Object.defineProperty(exports, "Seat", { enumerable: true, get: function () { return Seat_1.Seat; } }); const TableFactory_1 = require("./core/TableFactory"); Object.defineProperty(exports, "TableFactory", { enumerable: true, get: function () { return TableFactory_1.TableFactory; } }); const WebSocketManager_1 = require("./core/WebSocketManager"); Object.defineProperty(exports, "WebSocketManager", { enumerable: true, get: function () { return WebSocketManager_1.WebSocketManager; } }); const EventBus_1 = require("./events/EventBus"); Object.defineProperty(exports, "EventBus", { enumerable: true, get: function () { return EventBus_1.EventBus; } }); const MessageRouter_1 = require("./events/MessageRouter"); Object.defineProperty(exports, "MessageRouter", { enumerable: true, get: function () { return MessageRouter_1.MessageRouter; } }); const GameManager_1 = require("./core/GameManager"); Object.defineProperty(exports, "GameManager", { enumerable: true, get: function () { return GameManager_1.GameManager; } }); const Lobby_1 = require("./core/Lobby"); Object.defineProperty(exports, "Lobby", { enumerable: true, get: function () { return Lobby_1.Lobby; } }); const index_2 = require("./core/commands/index"); Object.defineProperty(exports, "CLIENT_COMMAND_TYPES", { enumerable: true, get: function () { return index_2.CLIENT_COMMAND_TYPES; } }); Object.defineProperty(exports, "CLIENT_MESSAGE_TYPES", { enumerable: true, get: function () { return index_2.CLIENT_MESSAGE_TYPES; } }); // Import all events-related exports const events_1 = require("./events"); Object.defineProperty(exports, "PLAYER_EVENTS", { enumerable: true, get: function () { return events_1.PLAYER_EVENTS; } }); Object.defineProperty(exports, "TABLE_EVENTS", { enumerable: true, get: function () { return events_1.TABLE_EVENTS; } }); Object.defineProperty(exports, "LOBBY_EVENTS", { enumerable: true, get: function () { return events_1.LOBBY_EVENTS; } }); Object.defineProperty(exports, "EVENTS", { enumerable: true, get: function () { return events_1.EVENTS; } }); function createGameServer(server, authModule, serverTransportModule, options) { const eventBus = new EventBus_1.EventBus(); const messageRouter = new MessageRouter_1.MessageRouter(eventBus); const tableFactory = new TableFactory_1.TableFactory(eventBus); const gameManager = new GameManager_1.GameManager(eventBus, tableFactory); const lobby = new Lobby_1.Lobby(eventBus, gameManager, tableFactory); const wsManager = new WebSocketManager_1.WebSocketManager(server, eventBus, messageRouter, gameManager, authModule, options?.reconnectionTimeoutMs || 600000, lobby, tableFactory); // Register default Lobby message handlers /** * Table commands */ messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.GET_STATE, (player, data) => { if (!data.tableId) return; const table = gameManager.getAllTables().find(t => t.id === data.tableId); if (table) { player.sendMessage({ type: index_2.CLIENT_MESSAGE_TYPES.TABLE.STATE, state: table.getState() }); } }); messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.JOIN, (player, data) => { if (!data.tableId) return; const table = gameManager.getAllTables().find(t => t.id === data.tableId); if (table) { table.addPlayer(player); } }); messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.CREATE, (player, data) => { if (!data.gameId) return; const table = lobby.createTable(data.gameId, data.options); if (table) { table.addPlayer(player); } }); messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.LEAVE, (player, data) => { const table = player.getTable(); if (table) { table.removePlayer(player.id); } }); /** * Seat commands */ messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.SEAT_SIT, (player, data) => { if (typeof data.seatIndex !== 'number') return; const table = player.getTable(); if (table) { table.sitPlayerAtSeat(player.id, data.seatIndex); } }); messageRouter.registerCommandHandler(index_2.CLIENT_COMMAND_TYPES.TABLE.SEAT_STAND, (player, data) => { if (typeof data.seatIndex !== 'number') return; const table = player.getTable(); if (table) { table.removePlayerFromSeat(data.seatIndex); } }); return { eventBus, messageRouter, tableFactory, gameManager, lobby, wsManager, // Add transport modules to the returned object transport: { auth: authModule, server: serverTransportModule } }; } exports.createGameServer = createGameServer;