Skip to content
Snippets Groups Projects
Commit 6f70d54b authored by atm2g19's avatar atm2g19
Browse files

Battleships WIP

parent cf1afba9
No related branches found
No related tags found
No related merge requests found
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(){
......
......@@ -9,5 +9,8 @@
<h1>
Battleships game
</h1>
<!-- <p>
Current running games: <br>
</!-->
</body>
</html>
\ No newline at end of file
......@@ -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
......@@ -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();
});
......
......@@ -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;
}
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment