UNPKG

programming-game

Version:

The client for programming game, an mmorpg that you interact with entirely through code.

142 lines (141 loc) 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.connect = void 0; var package_json_1 = require("./package.json"); var base_client_1 = require("./base-client"); var InnerSocket = /** @class */ (function () { function InnerSocket(credentials) { this.closed = false; this.handlers = new Map(); this.reconnecting = false; this.reconnectTimer = null; this.credentials = credentials; this.socket = this.reconnect(); } InnerSocket.prototype.reconnect = function () { var _this = this; console.log("reconnect call..."); var socket = new WebSocket(process.env.NODE_ENV === "development" ? "ws://localhost:3001" : "wss://programming-game.com"); if (this.reconnectTimer) { clearTimeout(this.reconnectTimer); this.reconnecting = false; } this.reconnecting = true; socket.addEventListener("open", function () { _this.reconnecting = false; console.log("connected to server!"); socket.send(JSON.stringify({ type: "credentials", value: _this.credentials, version: package_json_1.version, })); }); var attemptReconnect = function () { if (_this.closed) return; if (_this.reconnecting) return; _this.reconnecting = true; _this.reconnectTimer = setTimeout(function () { console.log("attempting to reconnect..."); _this.socket = _this.reconnect(); }, 1000); }; socket.addEventListener("error", function (event) { _this.reconnecting = false; console.log("WebSocket error...", Date.now()); attemptReconnect(); }); // @ts-ignore socket.addEventListener("close", function (e, b) { console.log("WebSocket closed", e, b); _this.triggerHandlers("close", null); attemptReconnect(); }); socket.addEventListener("message", function (event) { try { var data = JSON.parse(event.data.toString()); switch (data.type) { case "version": { return _this.triggerHandlers("version", data.value); } case "events": { return _this.triggerHandlers("events", data.value); } default: { console.error("Unknown message type:", data.type); } } } catch (e) { console.log("Error parsing message:", e); } }); return socket; }; InnerSocket.prototype.triggerHandlers = function (event, data) { var _a; (_a = this.handlers.get(event)) === null || _a === void 0 ? void 0 : _a.forEach(function (handler) { handler(data); }); }; InnerSocket.prototype.emit = function (event, data) { this.socket.send(JSON.stringify({ type: event, value: data })); }; InnerSocket.prototype.on = function (event, callback) { var set = this.handlers.get(event) || new Set(); if (!this.handlers.has(event)) { this.handlers.set(event, set); } set.add(callback); }; InnerSocket.prototype.close = function () { this.closed = true; this.socket.close(); }; return InnerSocket; }()); var connect = function (_a) { var credentials = _a.credentials, onTick = _a.onTick, onEvent = _a.onEvent, _b = _a.tickInterval, tickInterval = _b === void 0 ? 300 : _b; if (typeof WebSocket === "undefined") { throw new Error("No global WebSocket, please upgrade to Node 22, play in the browser, or polyfill w/ a compatible library."); } var socket = new InnerSocket(credentials); var versionHandler = function (serverVersion) { var clientVersion = package_json_1.version; if (serverVersion !== clientVersion) { var lines = []; lines.push("There's an updated version of programming-game."); lines.push("Your version: ".concat(clientVersion, ", latest version: ").concat(serverVersion)); console.log("\n" + lines.join("\n") + "\n"); } else { console.log("\nYou're on the latest version of programming-game, enjoy!.\n"); } }; socket.on("version", versionHandler); var baseClient = new base_client_1.BaseClient({ onTick: onTick, onEvent: onEvent, setIntent: function (intent) { socket.emit("setIntent", intent); }, tickInterval: tickInterval, }); // @ts-ignore socket.on("error", function (error) { console.log("socket error", error); }); socket.on("events", baseClient.eventsHandler.bind(baseClient)); // @ts-ignore socket.on("close", function (error) { baseClient.clearState(); }); return function () { socket.close(); baseClient.clearState(); }; }; exports.connect = connect;