Working version before implementing rooms. Wish me luck.
This commit is contained in:
parent
96aa1df1fd
commit
165366c2b7
14
index.js
14
index.js
|
@ -30,9 +30,11 @@ class Game {
|
|||
}
|
||||
|
||||
addPlayer(id, name) {
|
||||
if (this.getAllNames().includes(name)) return false
|
||||
var color = "blue"
|
||||
const player = new Player(id, name, color);
|
||||
this.players.push(player);
|
||||
return true
|
||||
}
|
||||
|
||||
removePlayer(id) {
|
||||
|
@ -49,11 +51,16 @@ class Game {
|
|||
let ids = []
|
||||
for (i = 0; i < this.players.length; i++) {
|
||||
ids.push(this.players[i].id)
|
||||
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
getAllNames() {
|
||||
let names = []
|
||||
for (i = 0; i < this.players.length; i++) {
|
||||
names.push(this.players[i].name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
}
|
||||
var game = new Game();
|
||||
|
||||
|
@ -80,7 +87,7 @@ io.on('connection', function(client) {
|
|||
})
|
||||
|
||||
client.on('joinGame', function(data){
|
||||
game.addPlayer(client.id, data.name);
|
||||
if (game.addPlayer(client.id, data.name) == false) return client.illegalAction(20)
|
||||
sendMap(client)
|
||||
log.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`, 'FgMagenta');
|
||||
client.broadcast.emit('playerList', game.players)
|
||||
|
@ -94,7 +101,6 @@ io.on('connection', function(client) {
|
|||
|
||||
|
||||
client.on('clickCanvas', function(data){
|
||||
log.log(game.getAllIDs())
|
||||
if (!game.getAllIDs().includes(client.id)) return client.illegalAction(1)
|
||||
const xu = data.tilePosition[0]
|
||||
const yu = data.tilePosition[1]
|
||||
|
|
|
@ -37,6 +37,9 @@ socket.on('illegalAction', function(data){
|
|||
case 1:
|
||||
action = "You must be in game to do this."
|
||||
break;
|
||||
case 20:
|
||||
action = "That name is already taken."
|
||||
break;
|
||||
default:
|
||||
action = "Unknown action."
|
||||
|
||||
|
|
Loading…
Reference in a new issue