From 6f70d54b9414d2708aa1463df173de0d8bd6456d Mon Sep 17 00:00:00 2001
From: Alex <atm2g19@soton.ac.uk>
Date: Thu, 2 Apr 2020 19:32:48 +0100
Subject: [PATCH] Battleships WIP

---
 public/games/battleships/battleships.js | 65 ++++++++++++++++++++++++-
 public/games/battleships/index.html     |  3 ++
 public/index.html                       |  5 ++
 src/games.ts                            |  3 +-
 src/games/battleships.ts                |  2 +-
 src/server.ts                           |  7 +++
 6 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/public/games/battleships/battleships.js b/public/games/battleships/battleships.js
index 2a03058..3e2a470 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 8e61495..1a1d18f 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 3e9d740..805522a 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 3d5e90a..a33261a 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 5822cb0..89d4dbf 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 be87400..3f89ef3 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);
 
 
-- 
GitLab