gamestart

This commit is contained in:
Flummi 2018-08-10 17:46:32 +02:00
parent baa1b00839
commit 2773c89e28
2 changed files with 45 additions and 30 deletions

View File

@ -5,11 +5,12 @@ import http from "http";
import sio from "socket.io";
import path from "path";
let deck = () => [
function deck() {
return [
...[...Array(12)].map((_, i) => new Array(12).fill(++i)).reduce((a,b)=>[...b,...a])
, ...[...Array(18).fill(13)]
].map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]);
//const player = new Map();
].map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]);
}
const rooms = new Map();
function cards() {
this.hand = [];
@ -17,18 +18,11 @@ function cards() {
this.stock = [];
}
const fillHand = _player => {
_player = player.get(_player);
_player.hand.push(...deck.splice(0, cfg.iters.hand - _player.hand.length));
_player.hand.sort((a,b)=>a>b);
const fillHand = (room, player) => {
player.hand.push(...room.deck.splice(0, cfg.iters.hand - player.hand.length));
player.hand.sort((a, b) => a > b);
};
/*const begin = () => {
player.forEach((cards, _player) => {
fillHand(_player);
//cards.stock.push(...deck.splice(0, cfg.iters.stock - cards.stock.length));
});
};
begin();*/
const begin = room => room.player.forEach(player => fillHand(room, player.cards));
const app = express();
const server = http.Server(app);
@ -45,10 +39,6 @@ app
io.on("connection", socket => {
socket.emit("auth"); // initial ping
socket.on("test", blah => {
console.log(blah);
io.in(socket.room).emit("test", blah);
});
socket.on("player", data => { // playername, gameroom
socket.playername = data.playername;
socket.room = data.gameroom;
@ -57,7 +47,8 @@ io.on("connection", socket => {
if(!rooms.has(data.gameroom)) {
rooms.set(data.gameroom, {
player: new Map(Object.entries({ [data.playername]: { cards: new cards, leader: true }})),
mainstack: []//deck()
deck: new deck,
mainstack: []
});
}
@ -65,19 +56,30 @@ io.on("connection", socket => {
rooms.get(data.gameroom).player.set(data.playername, { cards: new cards, leader: false });
}
console.log(rooms);
io.in(socket.room).emit("playerlist", [...rooms.get(data.gameroom).player.keys()]);
});
socket.on("disconnect", data => {
const room = socket.room;
const roomname = socket.room;
const player = socket.playername;
const room = rooms.get(socket.room);
socket.disconnect(true);
rooms.get(room).player.delete(player);
io.in(room).emit("playerlist", [...rooms.get(room).player.keys()]);
if(rooms.get(room).player.size === 1)
rooms.get(room).player.values().next().leader = true;
if(rooms.get(room).player.size === 0)
rooms.delete(room);
room.player.delete(player);
io.in(roomname).emit("playerlist", [...room.player.keys()]);
if(room.player.size === 1)
room.player.get( [...room.player.keys()][0] ).leader = true;
if(room.player.size === 0)
rooms.delete(roomname);
});
socket.on("start", () => {
const room = rooms.get(socket.room);
if( room.player.get(socket.playername).leader ) {
begin(room);
io.in(socket.room).emit("gamestart", {
deck: room.deck,
mainstack: room.mainstack,
player: [...room.player]
});
}
});
});

View File

@ -1,4 +1,5 @@
import { msgBox, removeAllmsgBox } from "./tpl.js";
import * as render from "./render.js";
let socket = false;
let _playername;
@ -28,6 +29,18 @@ const socketevents = () => {
removeAllmsgBox();
msgBox("waitforPlayer");
socket.on("playerlist", player => {
document.querySelector("span#wfp_players").innerHTML = player.join("<br />");
document.querySelector("span#wfp_players").innerHTML = player.join("<br />") + `<a href="#" id="start">start</a>`;
document.querySelector("a#start").addEventListener("click", () => {
socket.emit("start");
});
});
socket.on("gamestart", game => {
removeAllmsgBox();
document.querySelector("#menu").style.display = "none";
document.querySelector("#black").style.display = "none";
game.player.forEach((player, i) => {
//stackables
});
});
};