Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Hedbanz/app2.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
560 lines (520 sloc)
12.8 KB
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
var express = require('express'); | |
var app = express(); | |
var fs = require('fs'); | |
var server = require('http').createServer(app); | |
app.get('/',function(req, res) { | |
res.sendFile(__dirname + '/client/index.html'); | |
}); | |
app.use('/client',express.static(__dirname + '/client')); | |
console.log("Server started."); | |
SOCKET_LIST = {}; | |
PLAYER_WORDS = {}; | |
PLAYER_NAMES = {}; | |
TURN_ORDER = {}; | |
WINNERS = {}; | |
var results = {}; | |
var namesInUse = {}; | |
var playerDict = {}; | |
var words = ["cat", "sun", "cup", | |
"ghost", "flower", "pie", | |
"cow", "banana", "snowflake", | |
"bug", "book", "jar", | |
"snake", "light", "tree", | |
"lips", "apple", "slide", | |
"socks", "smile", "swing", | |
"coat", "shoe", "water", | |
"heart", "hat", "ocean", | |
"kite", "dog", "mouth", | |
"milk", "duck", "eyes", | |
"skateboard", "bird", "boy", | |
"apple", "person", "girl", | |
"mouse", "ball", "house", | |
"star", "nose", "bed", | |
"whale", "jacket", "shirt", | |
"hippo", "beach", "egg", | |
"face", "cookie", "cheese", | |
"ice cream", "drums", "circle", | |
"spoon", "worm", "spider web", | |
"horse", "door", "song", | |
"rainbow", "cello", "bomb", | |
"camera", "treasure", "garbage", | |
"park", "pirate", "ski", | |
"state", "whistle", "palace", | |
"baseball", "coal", "queen", | |
"dominoes", "photograph", "computer", | |
"hockey", "airplane", "hot dog", | |
"salt", "key", "ipad", | |
"vodka", "frog", "lawnmower", | |
"mattress", "air", "cake", | |
"circus", "battery", "mailman", | |
"cowboy", "password", "bicycle", | |
"skate", "electricity", "lightsaber", | |
"thief", "teapot", "rain", | |
"spring", "nature", "garden", | |
"toast", "outdoors", "America", | |
"ticket", "gingerbread man", "bowtie", | |
"chalk", "scale", "wax", | |
"light bulb", "platypus", "music"]; | |
var numWords = 120; | |
var wordsToUse = 1; | |
var currentStandings = 0; | |
var playerCount = 0; | |
var votesIn = 0; | |
var yesCount = 0; | |
var noCount = 0; | |
var maybeCount = 0; | |
var whosTurn = 0; | |
var isGameOver = false; | |
var guessWasMade = false; | |
var inGame = false; | |
var votesToRestart = 0; | |
// load anime words from txt file | |
var animeWords = fs.readFileSync('animedb_actual.txt').toString().split(/\n|\r/g); | |
var numAnimeWords = animeWords.length - 1; | |
// socket stuff | |
var io = require('socket.io')(server); | |
io.sockets.on('connection', function(socket){ | |
console.log('new user!'); | |
for (var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', "A spectator has joined."); | |
} | |
var socketId = Math.random(); | |
SOCKET_LIST[socketId] = socket; | |
playerDict[socket.id] = socketId; | |
socket.canGuess = false; | |
socket.emit('switchWordsButton', wordsToUse); | |
socket.emit('addToChat', "Welcome! You must set a name to play."); | |
socket.on('sendMsgToServer',function(data){ | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
if ((socket.canGuess == true) && (data !== "")) | |
{ | |
socket.canGuess = false; | |
console.log('someone sent a message!'); | |
var strToSend; | |
if (data.slice(-1) == "?") | |
{ | |
strToSend = data; | |
} | |
else | |
{ | |
strToSend = data.concat("?"); | |
} | |
for(var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + ": " + strToSend); | |
} | |
var guess = data.toLowerCase(); | |
var correctWord = PLAYER_WORDS[playerDict[socket.id]].toLowerCase(); | |
var isIt = "is it "; | |
if ((guess == correctWord) || (guess == correctWord.concat("?")) || (guess == isIt.concat(correctWord)) || (guess == isIt.concat(correctWord.concat("?")))) | |
{ | |
// they guessed the word correctly | |
WINNERS[playerDict[socket.id]] = 1; | |
results[currentStandings] = playerDict[socket.id]; | |
currentStandings += 1; | |
// let everyone know | |
for (var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " guessed their word and finished in place " + currentStandings); | |
} | |
if ((currentStandings == playerCount - 1) || (currentStandings == 3)) | |
{ | |
// game over | |
if (currentStandings !== 3) | |
{ | |
for (var i in PLAYER_NAMES) | |
{ | |
if (WINNERS[i]) | |
{ | |
console.log("ur good"); | |
} | |
else | |
{ | |
results[currentStandings] = i; | |
} | |
} | |
} | |
setTimeout(gameOver, 500); | |
} | |
else | |
{ | |
socket.emit('addToChat', "Congrats, but you must still vote until the game is over."); | |
startTurn(); | |
} | |
} | |
else | |
{ | |
for (var i in PLAYER_NAMES) | |
{ | |
// enable voting | |
if (i !== TURN_ORDER[whosTurn - 1]) | |
{ | |
SOCKET_LIST[i].emit('enableVote'); | |
} | |
} | |
} | |
} | |
else | |
{ | |
socket.emit('enableGuess'); | |
} | |
} | |
else | |
{ | |
SOCKET_LIST[playerDict[socket.id]].emit('addToChat', "You must set a name to chat!"); | |
} | |
}); | |
socket.on('assignWords',function(){ | |
if (playerCount < 2) | |
{ | |
socket.emit('addToChat', "At least 2 players needed to start."); | |
} | |
else | |
{ | |
if (inGame) | |
{ | |
socket.emit('disableNewGame'); | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
votesToRestart += 1; | |
var votesNeeded = Math.floor(playerCount / 2) + 1; | |
for (var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " voted to restart."); | |
SOCKET_LIST[i].emit('addToChat', "Votes to restart: " + votesToRestart + ". Votes needed: " + votesNeeded + "."); | |
} | |
if (votesToRestart == votesNeeded) | |
{ | |
gameOver(); | |
} | |
} | |
else | |
{ | |
socket.emit('addToChat', "A game is in progress. Spectators can't vote to restart."); | |
} | |
} | |
else | |
{ | |
inGame = true; | |
for(var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', "The game has begun! Assigning words..."); | |
if (PLAYER_NAMES[i]) | |
{ | |
var playerWord; | |
var isValidWord = false; | |
var repeats = 0; | |
while (!isValidWord) | |
{ | |
if (wordsToUse == 0) | |
{ | |
playerWord = words[Math.floor(Math.random() * numWords)] | |
} | |
else | |
{ | |
playerWord = animeWords[Math.floor(Math.random() * numAnimeWords)]; | |
} | |
for (var j in PLAYER_WORDS) | |
{ | |
if (PLAYER_WORDS[j] == playerWord) | |
{ | |
repeats += 1; | |
} | |
} | |
if (repeats !== 0) | |
{ | |
repeats = 0; | |
} | |
else | |
{ | |
isValidWord = true; | |
} | |
} | |
PLAYER_WORDS[i] = playerWord; | |
} | |
else | |
{ | |
SOCKET_LIST[i].emit('disableNewGame'); | |
} | |
} | |
for(var i in SOCKET_LIST){ | |
for (var j in SOCKET_LIST) | |
{ | |
if ((j != i) && (PLAYER_NAMES[i])) | |
{ | |
SOCKET_LIST[j].emit('addToChat', PLAYER_NAMES[i] + "'s word is: " + PLAYER_WORDS[i]); | |
} | |
} | |
} | |
startGame(); | |
} | |
} | |
}); | |
socket.on('setUsername',function(data){ | |
if ((data != "") && (!inGame)) | |
{ | |
if (namesInUse[data.toLowerCase()]) | |
{ | |
socket.emit('addToChat', data + " is already taken. Pick a different name."); | |
} | |
else if (data.length > 12) | |
{ | |
socket.emit('addToChat', "Your name must be no longer than 12 characters."); | |
} | |
else | |
{ | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
if (PLAYER_NAMES[playerDict[socket.id]].toLowerCase() == data.toLowerCase()) | |
{ | |
socket.emit('addToChat', "You already have that name."); | |
} | |
else | |
{ | |
delete namesInUse[PLAYER_NAMES[playerDict[socket.id]]]; | |
for (var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " changed their name to " + data); | |
} | |
} | |
} | |
else | |
{ | |
for (var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', data + " has joined the game"); | |
} | |
playerCount += 1; | |
console.log("Player count: " + playerCount); | |
} | |
PLAYER_NAMES[playerDict[socket.id]] = data; | |
namesInUse[data] = 1; | |
} | |
} | |
else if (inGame) | |
{ | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
socket.emit('addToChat', "You can't change your name during a game."); | |
} | |
else | |
{ | |
socket.emit('addToChat', "Please wait until the game ends to join."); | |
} | |
} | |
}); | |
socket.on('getVote', function(data){ | |
console.log("got a vote."); | |
if (data == 1) | |
{ | |
yesCount += 1; | |
} | |
else if (data == 2) | |
{ | |
noCount += 1; | |
} | |
else | |
{ | |
maybeCount += 1; | |
} | |
votesIn += 1; | |
console.log(playerCount + ' ' + votesIn + ' ' + noCount); | |
if (playerCount - 1 - votesIn == 0) | |
{ | |
console.log('all votes are in'); | |
// all votes are in | |
for (var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', "Results-"); | |
SOCKET_LIST[i].emit('addToChat', "Yes: " + yesCount); | |
SOCKET_LIST[i].emit('addToChat', "No: " + noCount); | |
SOCKET_LIST[i].emit('addToChat', "Maybe: " + maybeCount); | |
} | |
votesIn = 0; | |
yesCount = 0; | |
noCount = 0; | |
maybeCount = 0; | |
startTurn(); | |
} | |
}); | |
socket.on('switchWords', function(data){ | |
if (inGame) | |
{ | |
socket.emit('addToChat', "You can't switch the word set during a game!"); | |
} | |
else | |
{ | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
if (data == 0) | |
{ | |
wordsToUse = 0; | |
} | |
else | |
{ | |
wordsToUse = 1; | |
} | |
for (var i in SOCKET_LIST) | |
{ | |
if (data == 1) | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " set the word set to Anime."); | |
SOCKET_LIST[i].emit('switchWordsButton', 1); | |
} | |
else | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " set the word set to Classic."); | |
SOCKET_LIST[i].emit('switchWordsButton', 0); | |
} | |
} | |
} | |
else | |
{ | |
socket.emit('addToChat', "Spectators can't switch the word set. Join by setting a name."); | |
} | |
} | |
}); | |
socket.on('disconnect',function(){ | |
console.log('disconnected user' + playerDict[socket.id]); | |
delete PLAYER_WORDS[playerDict[socket.id]]; | |
delete SOCKET_LIST[playerDict[socket.id]]; | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
for (var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[playerDict[socket.id]] + " has left the game."); | |
SOCKET_LIST[i].emit('addToChat', "Votes to restart has been reset to 0."); | |
votesToRestart = 0; | |
if (PLAYER_NAMES[i]) | |
{ | |
SOCKET_LIST[i].emit('enableNewGame'); | |
} | |
} | |
} | |
else | |
{ | |
for (var i in SOCKET_LIST){ | |
SOCKET_LIST[i].emit('addToChat', "A spectator has left."); | |
} | |
} | |
if (PLAYER_NAMES[playerDict[socket.id]]) | |
{ | |
playerCount -= 1; | |
} | |
delete namesInUse[PLAYER_NAMES[playerDict[socket.id]]]; | |
delete PLAYER_NAMES[playerDict[socket.id]]; | |
delete playerDict[socket.id]; | |
if ((playerCount == 1) && inGame) | |
{ | |
for (var i in PLAYER_NAMES) | |
{ | |
results[0] = i; | |
} | |
currentStandings = 0; | |
gameOver(); | |
} | |
else if ((playerCount == 0) && inGame) | |
{ | |
currentStandings = -1; | |
gameOver(); | |
} | |
}); | |
}); | |
server.listen(4141); | |
function startGame() | |
{ | |
console.log("game started"); | |
// reset all variables first | |
inGame = true; | |
whosTurn = 0; | |
yesCount = 0; | |
noCount = 0; | |
maybeCount = 0; | |
votesIn = 0; | |
votesToRestart = 0; | |
currentStandings = 0; | |
for (var i in WINNERS) | |
{ | |
delete WINNERS[i]; | |
} | |
for (var i in results) | |
{ | |
delete results[i]; | |
} | |
var x = 0; | |
for (var i in PLAYER_NAMES) | |
{ | |
TURN_ORDER[x] = i; | |
x += 1; | |
} | |
startTurn(); | |
} | |
function startTurn() | |
{ | |
var isValidPlayer = false; | |
while (!isValidPlayer) | |
{ | |
if (TURN_ORDER[whosTurn]) | |
{ | |
if (PLAYER_NAMES[TURN_ORDER[whosTurn]]) | |
{ | |
if (WINNERS[TURN_ORDER[whosTurn]]) | |
{ | |
whosTurn += 1; | |
} | |
else | |
{ | |
isValidPlayer = true; | |
} | |
} | |
else | |
{ | |
whosTurn += 1; | |
} | |
} | |
else | |
{ | |
whosTurn = 0; | |
} | |
} | |
for (var i in SOCKET_LIST) | |
{ | |
if (i != TURN_ORDER[whosTurn]) | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[TURN_ORDER[whosTurn]] + "'s turn. Their word is: " + PLAYER_WORDS[TURN_ORDER[whosTurn]] + "."); | |
} | |
else | |
{ | |
SOCKET_LIST[i].emit('addToChat', PLAYER_NAMES[TURN_ORDER[whosTurn]] + "'s turn."); | |
} | |
} | |
SOCKET_LIST[TURN_ORDER[whosTurn]].emit('addToChat', "It's your turn. Ask a yes/no question to try and figure out your word."); | |
SOCKET_LIST[TURN_ORDER[whosTurn]].emit('addToChat', "You can also guess the word if you think you know it."); | |
SOCKET_LIST[TURN_ORDER[whosTurn]].canGuess = true; | |
SOCKET_LIST[TURN_ORDER[whosTurn]].emit('enableGuess'); | |
whosTurn += 1; | |
} | |
function gameOver() | |
{ | |
for (var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', "Game over!"); | |
} | |
if (currentStandings == 0) | |
{ | |
currentStandings = -1; | |
} | |
setTimeout(displayStandings, 500); | |
} | |
function displayStandings() | |
{ | |
for (var i in SOCKET_LIST) | |
{ | |
SOCKET_LIST[i].emit('addToChat', "Final Standings-"); | |
var j; | |
for (j = 0; j < currentStandings + 1; j++) | |
{ | |
SOCKET_LIST[i].emit('addToChat', (j + 1) + ": " + PLAYER_NAMES[results[j]]); | |
} | |
SOCKET_LIST[i].emit('addToChat', "-----------------"); | |
inGame = false; | |
SOCKET_LIST[i].emit('enableNewGame'); | |
} | |
} |