// nice-harness.js // Copyright (C) 2022 Rob Colbert // License: Apache-2.0 /* * This module provides the most basic runtime environment for testing your game * in an environment similar to what will be provided when hosted in the arcade. * * You should not alter this script at all. None of your changes made will be * available in the final environment(s). */ 'use strict'; import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line import { createRequire } from 'module'; const require = createRequire(import.meta.url); // jshint ignore:line import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; import WEBPACK_CONFIG from './webpack.config.js'; import express from 'express'; const APP_CONFIG = { pkg: require('./package.json'), niceGameSdk: require('./node_modules/dtp-nice-game/package.json'), }; async function getHomeView (req, res) { res.locals.gameModuleUrl = '/dist/game-app.bundle.js'; res.render('game-view'); } (async ( ) => { const harness = { app: express(), }; harness.app.locals.config = APP_CONFIG; harness.app.locals.pkg = APP_CONFIG.pkg; harness.app.locals.niceGameSdk = APP_CONFIG.niceGameSdk; harness.app.set('view engine', 'pug'); harness.app.set('views', path.join(__dirname, 'game', 'views')); /* * Nice Game SDK static asset routes are served directly from the * ./game/assets directory in dev mode. */ harness.app.use('/dist/assets', express.static(path.join(__dirname, 'game', 'assets'))); harness.app.use('/platform', express.static(path.join(__dirname, 'platform'))); /* * Webpack integration */ harness.compiler = webpack(WEBPACK_CONFIG); /* * Webpack dev server middleware */ harness.webpackDevMiddleware = webpackDevMiddleware(harness.compiler, { publicPath: WEBPACK_CONFIG.output.publicPath, }); harness.app.use(harness.webpackDevMiddleware); /* * This is not a complex web app, it's a dev harness that runs a packaged * application in the browser with live updates. The intent is to provide a * development harness in which Nice Game SDK games and other apps can be * built. All this "app" does is serve out the SDK app as if it is being * loaded in Nice Arcade. */ harness.app.get('/', getHomeView); /* * Start the ExpressJS server */ const port = parseInt(process.env.ARCADE_HTTP_PORT || '3000', 10); const host = process.env.ARCADE_HTTP_BIND || '127.0.0.1'; console.log('Starting game server harness', { host, port }); harness.app.listen(port, host, ( ) => { console.log('CyberEgg 2077 is alive'); }); })();