Created
July 10, 2018 18:59
-
-
Save Pathen85/27b2651911deac63e7d8d07e896242dc to your computer and use it in GitHub Desktop.
WEB game logic example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// List with users that have set a username | |
let activePlayers = []; | |
// username of player that is currently drawing | |
let playerDrawing; | |
let word; | |
// This will execute everytime a new player connects | |
io.sockets.on('connection', function(socket) { | |
// username is not yet defined | |
let username; | |
// let the client know that a username is required (this can be left out) | |
socket.emit('require-username'); | |
// this will execute as soon as the user has set a username | |
socket.on('receive-username', function(name) { | |
// the name will be stored in the local variable | |
username = name; | |
// the client will be notified that the user has been set to active | |
socket.emit('player-active'); | |
// the user is added to the array of active players | |
activePlayers.push(name); | |
}); | |
// this is called when a user sends a message | |
socket.on('chat', function(message) { | |
// broadcast the message to all other users | |
socket.broadcast.emit('chat', message); | |
// check if word equals the content of the message and it's not sent by the player drawing | |
if (message === word && playerDrawing !== username) { | |
// if yes round is finished and the username of the winning user is send to everybody | |
io.sockets.emit('round-finished', username); | |
// reset variables | |
playerDrawing = null; | |
word = null; | |
// TODO: adjust highscores | |
} | |
}) | |
// this is called by the drawing user after he gets selected | |
socket.on('request-word', function() { | |
// the user receives his wordlist but only if he is really the one drawing | |
if (username === playerDrawing) { | |
socket.emit(word); | |
} | |
}) | |
// this will execute when the client loses connection (closer browser tab, switches to another site, etc.) | |
// and must not be called manually! | |
socket.on('disconnect', function() { | |
// find the user in the array | |
let user = activePlayers.findIndex(item => item === username); | |
// if he was found remove him from the array | |
if (user >= 0) { | |
activePlayers.splice(user, 1); | |
} | |
}) | |
}) | |
// this function recursively calls itself and checks if someone is drawing at the moment | |
function checkIfSomeoneIsDrawing() { | |
setTimeout(function() { | |
// Only if more than 2 players are active and noone is drawing a round can be started | |
if (activePlayers.length >= 2 && !playerDrawing) { | |
// select a random player to start drawing | |
let randomNumber = Math.random(); | |
randomNumber = randomNumber * activePlayers.length - 1; | |
playerDrawing = activePlayers[Math.round(randomNumber)]; | |
/* TODO: set word variable from wordlist */ | |
// Let everyone know who is drawing | |
io.sockets.emit('start-drawing', playerDrawing); | |
} | |
// function is calls itself infinitely | |
checkIfSomeoneIsDrawing(); | |
}, 1000) | |
} | |
checkIfSomeoneIsDrawing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment