const http = require('http');
const path = require('path');
const bodyParser = require('body-parser');
const express = require('express');
const WebSocket = require('ws');
const shortid = require('shortid');
const broadcast = require('./broadcast');

/**
 * Main store of all game rooms
 * Is used as in-memory database
 *
 * Structure:
 * {
 *   [id]: {
 *     players: [],
 *     maxScore: Number,
 *     playing: Boolean,
 *     ended: Boolean
 *   }
 * }
 * @type {Object}
 */
const games = {};

const port = process.env.PORT || 3000;

const app = express()
  .use(bodyParser.urlencoded({ extended: false }))
  .use(express.static(path.join(__dirname, 'public')))
  .set('view engine', 'ejs')
  .set('views', path.join(__dirname, 'views'))
  .get('/', getHome)
  .post('/', createRoom);

const server = http.createServer(app);
const webSocketServer = new WebSocket.Server({ server });

webSocketServer.broadcast = broadcast;

server.listen(port, function onListen() {
  console.log('Server started at port ' + port);
});

function getHome(req, res) {
  res.render('index', { games });
}

function createRoom(req, res) {
  const id = shortid.generate();
  games[id] = new Game();
  res.redirect('/game/' + id);
}

function Game() {
  return {
    players: {},
    maxScore: 10,
    playing: false,
    ended: false
  }
}

@@ -1023,73 +1023,382 @@
 );%0A%0A
-function getHome(req, res) %7B%0A  res.render('index', %7B games %7D);%0A%7D%0A
+/**%0A * %5BGET%5D / handler%0A * @param  %7BObject%7D req Express request object%0A * @param  %7BObject%7D res Express response object%0A */%0Afunction getHome(req, res) %7B%0A  res.render('index', %7B games %7D);%0A%7D%0A%0A/**%0A * %5BPOST%5D / handler%0A * Creates new game in store and redirects user to new gameroom%0A * @param  %7BObject%7D req Express request object%0A * @param  %7BObject%7D res Express response object%0A */
 %0Afun
@@ -1519,16 +1519,77 @@
 id);%0A%7D%0A%0A
+/**%0A * Game object-creator%0A * Sets defaults for new game%0A */%0A
 function
@@ -1604,46 +1604,42 @@
 %7B%0A  
-return %7B%0A    
+this.
 players
-: %7B%7D,%0A    
+ = %7B%7D;%0A  this.
 maxScore
 : 10
@@ -1638,53 +1638,59 @@
 core
-: 10,%0A    
+ = 10;%0A  this.
 playing
-:
+ =
  false
-,%0A    
+;%0A  this.
 ended
-:
+ =
  false
-%0A  %7D
+;
 %0A%7D%0A

Add comments and fix game creator function