MultiplayerMinesweeper/index.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-04-20 18:53:36 +00:00
const worldgen = require("./worldgen.js")
const util = require("./util.js")
const log = require("./log.js")
2022-04-30 20:18:05 +00:00
const gridSize = [18, 18];
2022-04-20 04:32:17 +00:00
const perlinScale = 3;
2022-04-30 20:18:05 +00:00
var express = require('express');
2022-04-20 18:53:36 +00:00
2022-04-30 21:49:20 +00:00
process.title = "Server"
setInterval(function(){ updateInfo()},5000)
function updateInfo() {
const players = game.players;
log.setInfo(players)
}
2022-04-21 03:23:51 +00:00
2022-04-30 20:18:05 +00:00
class Player {
2022-04-30 21:49:20 +00:00
constructor(id, name, color) {
2022-04-30 20:18:05 +00:00
this.id = id;
2022-04-30 21:49:20 +00:00
this.name = name;
2022-04-30 20:18:05 +00:00
this.color = color;
}
2022-04-20 18:53:36 +00:00
}
class Game {
2022-04-30 20:18:05 +00:00
constructor() {
this.players = [{id: "adfhad", name: "bobbert"}];
2022-04-30 20:18:05 +00:00
this.world = worldgen.generateWorld(gridSize, perlinScale);
2022-04-20 18:53:36 +00:00
}
2022-04-30 21:49:20 +00:00
addPlayer(id, name) {
if (this.getAllNames().includes(name)) return false
var color = "blue"
2022-04-30 21:49:20 +00:00
const player = new Player(id, name, color);
2022-04-20 18:53:36 +00:00
this.players.push(player);
return true
2022-04-20 18:53:36 +00:00
}
removePlayer(id) {
this.players = this.players.filter(obj => obj.id != id);
log.log("removed player - " + id)
2022-04-20 18:53:36 +00:00
}
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
}
2022-04-20 18:53:36 +00:00
}
2022-04-30 20:18:05 +00:00
var game = new Game();
2022-04-20 18:53:36 +00:00
2022-04-20 04:32:17 +00:00
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");
2022-04-20 04:32:17 +00:00
});
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');
2022-04-20 18:53:36 +00:00
client.on('disconnect', function(){
log.log(client.id + ' disconnected.', 'FgCyan')
2022-04-30 20:18:05 +00:00
game.removePlayer(client.id)
client.broadcast.emit('playerList', game.players)
2022-04-20 18:53:36 +00:00
})
2022-04-30 21:49:20 +00:00
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');
2022-04-30 20:18:05 +00:00
client.broadcast.emit('playerList', game.players)
client.emit('playerList', game.players)
2022-04-20 18:53:36 +00:00
})
client.on('leaveGame', function(tank){
log.log(client.id + ' disconnected.')
2022-04-30 20:18:05 +00:00
game.removePlayer(client.id)
client.broadcast.emit('playerList', game.players)
2022-04-20 04:32:17 +00:00
})
2022-04-20 04:32:17 +00:00
client.on('clickCanvas', function(data){
if (!game.getAllIDs().includes(client.id)) return client.illegalAction(1)
2022-04-30 20:18:05 +00:00
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))
2022-04-30 20:18:05 +00:00
client.broadcast.emit('sync',{world: game.world})
client.emit('sync',{world: game.world})
2022-04-20 04:32:17 +00:00
})
});
2022-04-30 20:18:05 +00:00
function sendMap(client) {
client.broadcast.emit('sync',{world: game.world.map})
client.emit('sync',{world: game.world.map})
}