diff --git a/public/games/battleships/battleships.js b/public/games/battleships/battleships.js index 2a030585e8534907356a9104db4eb8aaa726d56e..3e2a470332d0a65dbd4c3e1e0aa22a0b07572ce6 100644 --- a/public/games/battleships/battleships.js +++ b/public/games/battleships/battleships.js @@ -1,8 +1,71 @@ -let socket; +/// <reference path="../../../p5.global-mode.d.ts"/> + +let socket; +let GamesList; function setup(){ + noCanvas(); socket = io.connect("http://localhost:8080"); + + let newGame = createButton("New Game"); + newGame.mouseClicked(createGame); + let refresh = createButton("Refresh"); + refresh.mouseClicked(refreshList); + + + + GamesList = createDiv(); + createP("Current running games: ").parent(GamesList); + GamesList = createElement("ul").parent(GamesList); + + refreshList(); + +} + +function refreshList(){ + + let children = GamesList.child(); + while(children.length > 0){ + children[0].remove(); + } + + fetch("/games/api/list/battleships") + .then(response => { + console.log(response); + return response.json(); + }) + .then(json => { + console.log(json); + + + for(let game of json){ + createElement("li", `${JSON.stringify(game)}`).parent(GamesList); + } + }) + .catch(err =>{ + console.log(err); + }); +} + +function createGame(){ + + let name = prompt("Please enter game name", "name"); + let password = prompt("Enter password, \nor leave blank for open game."); + let data = { + name : name, + password : password, + }; + + fetch("/games/api/create/battleships", { + method : "POST", + body : JSON.stringify(data), + headers : { + 'Content-Type' : 'application/json' + } + }).then(() => { + refreshList(); + }) } function draw(){ diff --git a/public/games/battleships/index.html b/public/games/battleships/index.html index 8e614951604ef39c8fcba54d9809a8db454c3dcb..1a1d18f646d625f3db01aa7bbe9801ae123231d2 100644 --- a/public/games/battleships/index.html +++ b/public/games/battleships/index.html @@ -9,5 +9,8 @@ <h1> Battleships game </h1> + <!-- <p> + Current running games: <br> + </!--> </body> </html> \ No newline at end of file diff --git a/public/index.html b/public/index.html index 3e9d740e68fc0d492aece56777e49daa109c60a6..805522aa32fe15f0134dcde2a1c6000b6dae0316 100644 --- a/public/index.html +++ b/public/index.html @@ -7,5 +7,10 @@ <h1> Test web-server </h1> + <p> + <li> + <a href="games/">Games</a> + </li> + </p> </body> </html> \ No newline at end of file diff --git a/src/games.ts b/src/games.ts index 3d5e90a676ae4944de24aa2e648990d6a4e1906b..a33261ad468685554724361e76f6db2f7d3042fd 100644 --- a/src/games.ts +++ b/src/games.ts @@ -207,7 +207,8 @@ function createGame(game : string ,req : express.Request, res : express.Response } game_prom.then((game : Game) => { debug(`Creating game by Alex. CHANGE`); - game.create("Alex").then(active_game_id => { + console.log(req.body); + game.create(req.body.name, req.body.password).then(active_game_id => { res.send(`Created new ${game_name} game by ${"Alex"} with id ${active_game_id}`); res.end(); }); diff --git a/src/games/battleships.ts b/src/games/battleships.ts index 5822cb02b1a0fbea73772c457470cded4a25ca2b..89d4dbf43860fd43ee02b51884532ad9ebf63263 100644 --- a/src/games/battleships.ts +++ b/src/games/battleships.ts @@ -21,7 +21,7 @@ const battleships : GameConstructor = class battleships implements Game{ .then(results => { console.log(results.results); resolve(results.results.insertId || -1); - let json_data : any = {name: "Alex's battleships V2", players : []}; + let json_data : any = {name: creator, players : [], password : password}; if(password){ json_data.password = password; } diff --git a/src/server.ts b/src/server.ts index be874003ca90e784e447799de64f267fa128ca9b..3f89ef3b7019ff4cc3cfcb2dca8b4c59626f97db 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5,16 +5,23 @@ import {Log} from "./log" import * as error from "./error" import {staticServe as serve} from "./sendFile" import {game_api} from "./games" +import * as bodyParser from "body-parser"; +import multer = require("multer"); +const upload = multer(); const app : express.Application = express(); const PORT : number = parseInt(process.env.PORT) || 8080; let server = app.listen(PORT, () => {console.log(`Listening on port ${PORT}`)}); const GAMES = game_api(server); +app.use(bodyParser.json()); +// app.post('*', upload.array()) + app.use(Log); app.use(GAMES.API); + app.use(autoredir);