/*
* Copyright (C) 2021 Thomas Van Acker
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*
* This project is hosted on https://git.bitscuit.be/bitscuit/Scrawl
*
*/
// Imports
const http = require("http");
const url = require("url");
const fs = require("fs");
const util = require("util");
// Vars
const hostname = "localhost"; // Enter your local IP address here
const port = 5000; // Enter a port number here
const STATE_LOBBY = 0;
const STATE_MANUAL = 10;
const STATE_WRITE = 20;
const STATE_PICK = 30;
const STATE_RESULT = 40;
const STATE_END = 50;
var state = STATE_LOBBY;
var userSockets = [];
var userNames = [];
var userOK = [];
var lines = [];
var lineNr = 0; // Current line
var linesTotal = 10; // Total nr of lines before game ends
var submissions = [];
var userWrite = [];
var userPick = [];
var userPoints = [];
// Create HTTP server
const server = http.createServer((req, res) => {
// Parse url
const requrl = url.parse(req.url);
var reqpath = requrl.pathname;
if(reqpath == "/") reqpath = "/index.html";
// Respond to requests
try{
// Return requested page
res.statusCode = 200;
res.write(fs.readFileSync("html"+reqpath));
res.end();
}catch(error){
// 404 if page not found
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain");
res.end("404 - Page Not Found");
}
});
// Create socket.io server
const io = require("socket.io")(server); // Can only be done when server is created
io.on("connection", (socket) => {
console.log("New user connected!");
// Init socket listeners
// Disconnect
socket.on("disconnect", () => {
console.log("User disconnected!");
// Remove from user lists
if(userSockets.indexOf(socket) != -1){
// When in users list
name = userNames[userSockets.indexOf(socket)];
userNames.splice(userSockets.indexOf(socket), 1);
userSockets.splice(userSockets.indexOf(socket), 1);
if(state == STATE_LOBBY){
// Send new lobby
userSockets.forEach(function(s){s.emit("LOBBY", userNames);});
}else{
// Send disconnect message
userSockets.forEach(function(s){s.emit("DISCONNECT", name);});
}
}
});
// JOIN
socket.on("JOIN", (username) => {
console.log("JOIN request for "+username);
// Check if can enter in lobby
if(state == STATE_LOBBY){
// Add user to lists
userSockets.push(socket);
userNames.push(username);
console.log(userNames);
// Send lobby data
userSockets.forEach(function(s){s.emit("LOBBY", userNames);});
}else{
// When can't enter
socket.emit("LOBBY_CLOSED");
}
});
// START
socket.on("START", () => {
console.log("START request");
// Send START to all users
userSockets.forEach(function(s){s.emit("START");});
// Set state
state = STATE_MANUAL;
// Reset vars
lines = [];
lineNr = 0;
userPoints = [];
for(i=0; i {
ok(socket);
});
// WRITE
socket.on("WRITE", (data) => {
// When received write from player
// Check if empty
if(data.write == ""){
socket.emit("WRITE_REPLY", {error:"Please enter some text!"});
return;
}
// Add to list
userWrite[userSockets.indexOf(socket)] = data.write;
// Perform ok
ok(socket);
});
// PICK
socket.on("PICK", (id) => {
// When received pick form player
userPick[userSockets.indexOf(socket)] = id;
// Perform ok
ok(socket);
});
});
// Start HTTP server
server.listen(port, hostname, () => {
console.log("Server running at http://"+hostname+":"+port);
});
// Game methods
function next(){
// Go to next step of game
// Send LOADING
userSockets.forEach(function(s){s.emit("LOADING");});
if(state == STATE_MANUAL || state == STATE_RESULT){
// Write
state = STATE_WRITE;
// Update vars
lineNr++;
userWrite = [];
for(i=0; i linesTotal){
// End
state = STATE_END;
// Gather data
var sorted = [];
for(i=0; i b.score-a.score);
console.log(sorted);
var ranks = [];
var r = 1;
var ls = sorted[0].score;
for(i=0; i lineVotes[bestLine]){
bestLine = i;
}
}
// Add bestLine to lines
lines.push(submissions[bestLine]);
// Send data to players
var data = {lineNr:lineNr, linesTotal:linesTotal, lines:lines, submissions:submissions, bestLine:bestLine, userPick:userPick, userWrite:userWrite, userPoints:userPoints, pointsDiff:pointsDiff, usernames:userNames};
userSockets.forEach(function(s){s.emit("ANSWER", data);});
console.log(data);
}
}
function ok(socket){
// Set OK for this player
userOK[userSockets.indexOf(socket)] = true;
// Check if everybody OK
var OK = true;
for(i=0; i