122 lines
3.2 KiB
JavaScript
122 lines
3.2 KiB
JavaScript
const worldgen = require("./worldgen.js")
|
|
const util = require("./util.js")
|
|
const log = require("./log.js")
|
|
const gridSize = [18, 18];
|
|
const perlinScale = 3;
|
|
|
|
var express = require('express');
|
|
|
|
process.title = "Server"
|
|
setInterval(function(){ updateInfo()},5000)
|
|
|
|
function updateInfo() {
|
|
const players = game.players;
|
|
|
|
log.setInfo(players)
|
|
}
|
|
|
|
class Player {
|
|
constructor(id, name, color) {
|
|
this.id = id;
|
|
this.name = name;
|
|
this.color = color;
|
|
}
|
|
}
|
|
|
|
class Game {
|
|
constructor() {
|
|
this.players = [{id: "adfhad", name: "bobbert"}];
|
|
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)
|
|
}
|
|
|
|
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 game = new Game();
|
|
|
|
|
|
var app = express() //Static resources server
|
|
app.use(express.static(__dirname + '/www/'));var server = app.listen(8082, function () {
|
|
var port = server.address().port;
|
|
log.log(`Server running at port ${port}`, "bright");
|
|
});
|
|
|
|
var io = require('socket.io')(server);/* Connection events */
|
|
io.on('connection', function(client) {
|
|
|
|
client.illegalAction = function(action) {
|
|
client.emit('illegalAction', action)
|
|
}
|
|
|
|
log.log('User connected', 'FgGreen');
|
|
|
|
client.on('disconnect', function(){
|
|
log.log(client.id + ' disconnected.', 'FgCyan')
|
|
game.removePlayer(client.id)
|
|
client.broadcast.emit('playerList', game.players)
|
|
})
|
|
|
|
client.on('joinGame', function(data){
|
|
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)
|
|
client.emit('playerList', game.players)
|
|
})
|
|
client.on('leaveGame', function(tank){
|
|
log.log(client.id + ' disconnected.')
|
|
game.removePlayer(client.id)
|
|
client.broadcast.emit('playerList', game.players)
|
|
})
|
|
|
|
|
|
client.on('clickCanvas', function(data){
|
|
if (!game.getAllIDs().includes(client.id)) return client.illegalAction(1)
|
|
const xu = data.tilePosition[0]
|
|
const yu = data.tilePosition[1]
|
|
game.world[xu][yu].structure = data.structure
|
|
game.world[xu][yu].owner = game.getPlayerByID(client.id).color;
|
|
// log.log(world[xu][yu].owner = game.getPlayerbyID(client.id))
|
|
|
|
client.broadcast.emit('sync',{world: game.world})
|
|
client.emit('sync',{world: game.world})
|
|
})
|
|
|
|
|
|
});
|
|
|
|
function sendMap(client) {
|
|
client.broadcast.emit('sync',{world: game.world.map})
|
|
client.emit('sync',{world: game.world.map})
|
|
}
|