import { GameOptions } from './game/objects'; import { MainDisplayController } from './display/engine'; import { GameWorld } from './backend/types'; import { BackendClient, RequestMetod } from './backend/api-helper'; import { EliasDBGraphQLClient } from './backend/eliasdb-graphql'; import { MainGameController } from './game/game-controller'; import { generateRandomName, getURLParams, setURLParam } from './helper'; export default { start: async function (canvasId: string) { // Initial values const host = `${window.location.hostname}:${window.location.port}`; const gameName = 'main'; // Create backend client to send game specific requests const bc = new BackendClient(host, gameName); // Get option details const options = new GameOptions(); try { let res = await bc.req( '/game', { gameName }, RequestMetod.Get ); const gm = res.gameworld as GameWorld; // Set game world related options options.backdrop = null; // TODO Asset load: gm.backdrop options.screenWidth = gm.screenWidth; options.screenHeight = gm.screenHeight; options.screenElementWidth = gm.screenElementWidth; options.screenElementHeight = gm.screenElementHeight; } catch (e) { throw new Error(`Could not register: ${e}`); } const ec = new EliasDBGraphQLClient(host, gameName); const mdc = new MainDisplayController(canvasId, options); // TODO: Register the player and let the game controller subscribe to the state let params = getURLParams(); if (!params.player) { setURLParam('player', generateRandomName()); params = getURLParams(); } const playerName = params.player; try { await bc.req('/player', { player: playerName, gameName }); const gc = new MainGameController( gameName, playerName, mdc, bc, ec ); await gc.start(); } catch (e) { throw new Error(`Could not register: ${e}`); } } };