const worldgen = require("./worldgen.js") const util = require("./util.js") const log = require("./log.js") const {performance} = require('perf_hooks'); const gridSize = [18, 18]; const perlinScale = 4; const startTime = performance.now() var express = require('express'); log.setMode(0); process.title = "Server" setInterval(function(){ updateInfo()},5000) function updateInfo() { log.setInfo(server.games) } class Player { constructor(id, name, color) { this.id = id; this.name = name; this.color = color; } } class Server { constructor(){ this.games = []; } getGameByName(name, create) { const game = this.games.filter(obj => obj.name == name)[0] if (game === undefined && create) { this.addGame(name); // Oh no.... This cant be good code. // Coming back to it two months later. Horrible, but I don't know how to fix it. Maybe I'll leave it as an ancient artifact. return this.getGameByName(name); } return game } addGame(name) { const game = new Game(name); this.games.push(game); return game } removeGame(name) { this.games = this.games.filter(obj => obj.name != name); log.log("removed game - " + name) // Broken? } getAllIDs() { let ids = [] for (let i = 0; i < this.games.length; i++) { for (let j = 0; j < this.games[i].players.length; j++) { ids.push(this.games[i].players[j].id) } } return ids } joinClientToGame(room, nick, id, client) { if (this.getAllIDs().includes(id)) { return client.illegalAction(22) } if (nick.length > 9) { return client.illegalAction(21) } const game = this.getGameByName(room, true); if (game.addPlayer(id, nick) == false) { this.removeGame(game) return client.illegalAction(20) } client.join(game.name) client.game = game; return true } } class Game { constructor(name) { this.name = name; this.players = []; this.world = worldgen.generateWorld(gridSize, perlinScale); } 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) { this.players = this.players.filter(obj => obj.id != id); log.log("removed player - " + id) if (this.players.length < 1) { server.removeGame(this.name); } } getPlayerByID(id) { const player = this.players.filter(obj => obj.id == id)[0] return player } getAllIDs() { 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 server = new Server(); var app = express() //Static resources server app.use(express.static(__dirname + '/www/')); var webServer = app.listen(8082, function () { var port = webServer.address().port; log.logRight(0, `Server running at port ${port}`, "bright"); }); var io = require('socket.io')(webServer);/* Connection events */ io.on('connection', function(client) { client.illegalAction = function(action) { client.emit('illegalAction', action) } log.log('User connected', 'FgGreen'); client.on('disconnect', function(){ if (!server.getAllIDs().includes(client.id)) return client.illegalAction(22) log.log(client.id + ' disconnected.', 'FgCyan') client.game.removePlayer(client.id) }) client.on('joinGame', function(data){ if(!server.joinClientToGame(data.room, data.name, client.id, client)) return io.to(client.game.name).emit('sync',{world: client.game.world}) log.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`, 'FgMagenta'); client.emit('inGame', true) }) client.on('leaveGame', function(tank){ log.log(client.id + ' disconnected.') client.game.removePlayer(client.id) io.to(client.game.name).emit('playerList', client.game.players) }) client.on('clickCanvas', function(data){ if (!server.getAllIDs().includes(client.id)) return client.illegalAction(1) const room = client.game.name const xu = util.clamp(data.tilePosition[0], 0, gridSize[0]) const yu = util.clamp(data.tilePosition[1], 0, gridSize[1]) if (!Number.isInteger(xu) || !Number.isInteger(yu)) return client.illegalAction(23) client.game.world[xu][yu].structure = data.structure client.game.world[xu][yu].owner = client.game.getPlayerByID(client.id).color; io.to(client.game.name).emit('sync',{world: client.game.world}) // client.emit('sync',{world: client.game.world}) }) });