Working version before implementing rooms. Wish me luck.

This commit is contained in:
A Bass 2022-04-30 20:01:40 -04:00
parent 96aa1df1fd
commit 165366c2b7
2 changed files with 14 additions and 5 deletions

View file

@ -30,9 +30,11 @@ class Game {
} }
addPlayer(id, name) { addPlayer(id, name) {
if (this.getAllNames().includes(name)) return false
var color = "blue" var color = "blue"
const player = new Player(id, name, color); const player = new Player(id, name, color);
this.players.push(player); this.players.push(player);
return true
} }
removePlayer(id) { removePlayer(id) {
@ -49,11 +51,16 @@ class Game {
let ids = [] let ids = []
for (i = 0; i < this.players.length; i++) { for (i = 0; i < this.players.length; i++) {
ids.push(this.players[i].id) ids.push(this.players[i].id)
} }
return ids 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(); var game = new Game();
@ -80,7 +87,7 @@ io.on('connection', function(client) {
}) })
client.on('joinGame', function(data){ 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) sendMap(client)
log.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`, 'FgMagenta'); log.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`, 'FgMagenta');
client.broadcast.emit('playerList', game.players) client.broadcast.emit('playerList', game.players)
@ -94,8 +101,7 @@ io.on('connection', function(client) {
client.on('clickCanvas', function(data){ client.on('clickCanvas', function(data){
log.log(game.getAllIDs()) if (!game.getAllIDs().includes(client.id)) return client.illegalAction(1)
if (!game.getAllIDs().includes(client.id)) return client.illegalAction(1)
const xu = data.tilePosition[0] const xu = data.tilePosition[0]
const yu = data.tilePosition[1] const yu = data.tilePosition[1]
game.world[xu][yu].structure = data.structure game.world[xu][yu].structure = data.structure

View file

@ -37,6 +37,9 @@ socket.on('illegalAction', function(data){
case 1: case 1:
action = "You must be in game to do this." action = "You must be in game to do this."
break; break;
case 20:
action = "That name is already taken."
break;
default: default:
action = "Unknown action." action = "Unknown action."