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 sio from "socket.io";
import path from "path"; import path from "path";
let deck = () => [ function deck() {
...[...Array(12)].map((_, i) => new Array(12).fill(++i)).reduce((a,b)=>[...b,...a]) return [
, ...[...Array(18).fill(13)] ...[...Array(12)].map((_, i) => new Array(12).fill(++i)).reduce((a,b)=>[...b,...a])
].map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]); , ...[...Array(18).fill(13)]
//const player = new Map(); ].map(a => [Math.random(), a]).sort((a, b) => a[0] - b[0]).map(a => a[1]);
}
const rooms = new Map(); const rooms = new Map();
function cards() { function cards() {
this.hand = []; this.hand = [];
@ -17,18 +18,11 @@ function cards() {
this.stock = []; this.stock = [];
} }
const fillHand = _player => { const fillHand = (room, player) => {
_player = player.get(_player); player.hand.push(...room.deck.splice(0, cfg.iters.hand - player.hand.length));
_player.hand.push(...deck.splice(0, cfg.iters.hand - _player.hand.length)); player.hand.sort((a, b) => a > b);
_player.hand.sort((a,b)=>a>b);
}; };
/*const begin = () => { const begin = room => room.player.forEach(player => fillHand(room, player.cards));
player.forEach((cards, _player) => {
fillHand(_player);
//cards.stock.push(...deck.splice(0, cfg.iters.stock - cards.stock.length));
});
};
begin();*/
const app = express(); const app = express();
const server = http.Server(app); const server = http.Server(app);
@ -45,10 +39,6 @@ app
io.on("connection", socket => { io.on("connection", socket => {
socket.emit("auth"); // initial ping 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.on("player", data => { // playername, gameroom
socket.playername = data.playername; socket.playername = data.playername;
socket.room = data.gameroom; socket.room = data.gameroom;
@ -57,7 +47,8 @@ io.on("connection", socket => {
if(!rooms.has(data.gameroom)) { if(!rooms.has(data.gameroom)) {
rooms.set(data.gameroom, { rooms.set(data.gameroom, {
player: new Map(Object.entries({ [data.playername]: { cards: new cards, leader: true }})), 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 }); 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()]); io.in(socket.room).emit("playerlist", [...rooms.get(data.gameroom).player.keys()]);
}); });
socket.on("disconnect", data => { socket.on("disconnect", data => {
const room = socket.room; const roomname = socket.room;
const player = socket.playername; const player = socket.playername;
const room = rooms.get(socket.room);
socket.disconnect(true); socket.disconnect(true);
rooms.get(room).player.delete(player); room.player.delete(player);
io.in(room).emit("playerlist", [...rooms.get(room).player.keys()]); io.in(roomname).emit("playerlist", [...room.player.keys()]);
if(rooms.get(room).player.size === 1) if(room.player.size === 1)
rooms.get(room).player.values().next().leader = true; room.player.get( [...room.player.keys()][0] ).leader = true;
if(rooms.get(room).player.size === 0) if(room.player.size === 0)
rooms.delete(room); 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 { msgBox, removeAllmsgBox } from "./tpl.js";
import * as render from "./render.js";
let socket = false; let socket = false;
let _playername; let _playername;
@ -28,6 +29,18 @@ const socketevents = () => {
removeAllmsgBox(); removeAllmsgBox();
msgBox("waitforPlayer"); msgBox("waitforPlayer");
socket.on("playerlist", player => { 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
});
}); });
}; };