rooms lol

This commit is contained in:
Flummi 2018-08-10 14:16:16 +02:00
parent e19453dc66
commit baa1b00839
4 changed files with 65 additions and 42 deletions

View File

@ -38,7 +38,10 @@ server.listen(8080);
app
.use(express.static(path.resolve("src", "static")))
.get("/rooms", (req, res) => {
res.setHeader("Content-Type", "application/json");
res.send([...rooms.keys()]);
});
io.on("connection", socket => {
socket.emit("auth"); // initial ping
@ -46,22 +49,37 @@ io.on("connection", socket => {
console.log(blah);
io.in(socket.room).emit("test", blah);
});
socket.on("player", data => { // playername, roomname
socket.on("player", data => { // playername, gameroom
socket.playername = data.playername;
socket.room = data.roomname;
socket.room = data.gameroom;
socket.join(data.gameroom);
socket.join(data.roomname);
if(!rooms.has(data.roomname)) {
rooms.set(data.roomname, {
player: new Map(Object.entries({ [data.playername]: { cards: new cards }})),
mainstack: deck()
if(!rooms.has(data.gameroom)) {
rooms.set(data.gameroom, {
player: new Map(Object.entries({ [data.playername]: { cards: new cards, leader: true }})),
mainstack: []//deck()
});
}
if(!rooms.get(data.gameroom).player.has(data.playername)) {
rooms.get(data.gameroom).player.set(data.playername, { cards: new cards, leader: false });
}
console.log(rooms);
socket.emit("done");
io.in(socket.room).emit("playerlist", [...rooms.get(data.gameroom).player.keys()]);
});
socket.on("disconnect", data => {
const room = socket.room;
const player = socket.playername;
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);
});
});
//repl.start("> ");
const r = repl.start("> ");
r.context.rooms = rooms;

View File

@ -1,5 +1,5 @@
import * as render from "./render.js";
import { tpl, msgBox } from "./tpl.js";
import { msgBox } from "./tpl.js";
//let socket = io();
function card({ type = "normal", val = "" }) {

View File

@ -1,45 +1,33 @@
import { msgBox, removeAllmsgBox } from "./tpl.js";
let socket = false;
let _playername;
let _roomname;
let _gameroom;
export const connect = ({ playername, roomname = "default" }) => {
export const connect = ({ playername, gameroom = "default" }) => {
// checks
if(playername < 3 || roomname < 3)
if(playername < 3 || gameroom < 3)
return false;
[_playername, _roomname] = [playername, roomname];
[_playername, _gameroom] = [playername, gameroom];
if(socket) {
socket.disconnect();
socket = false;
}
socket = io();
socketevents();
};
const socketevents = () => {
socket.on("auth", () => {
});
};
/*const socketevents = () => {
socket.on("auth", () => {
socket.emit("player", {
roomname: _roomname,
playername: _playername
playername: _playername,
gameroom: _gameroom
});
});
socket.on("done", () => {
socket.emit("test", "lul");
removeAllmsgBox();
msgBox("waitforPlayer");
socket.on("playerlist", player => {
document.querySelector("span#wfp_players").innerHTML = player.join("<br />");
});
socket.on("test", blah => {
console.log(blah);
});
};
export const connect = ({ playername, roomname = "default" }) => {
[_playername, _roomname] = [playername, roomname];
if(!socket)
socket = io();
else {
socket.disconnect(true);
socket = io();
}
socketevents();
};*/
};

View File

@ -101,6 +101,18 @@ export const tpl = {
removemsgBox(e.target.parentNode.parentNode.parentNode);
}
}]
},
waitforPlayer: {
title: "waiting for players",
content: `
actually connected: <span id="wfp_players">1</span>
`,
buttons: [{
tpl: `<input class="msgButton" type="button" value="Cancel" />`,
event: e => {
removemsgBox(e.target.parentNode.parentNode.parentNode);
}
}]
}
}
};
@ -110,6 +122,11 @@ const removemsgBox = node => {
if(document.querySelectorAll(".msgBox").length === 0 && document.querySelector("div#menu").style.display === "none")
document.querySelector("#black").style.display = "none";
};
export const removeAllmsgBox = () => {
document.querySelectorAll(".msgbox").forEach(msgbox => {
removemsgBox(msgbox);
});
};
export const msgBox = type => {
if(document.querySelector("#black").style.display === "none")