DAJKDJAKDBHAKJdbk_Server

This commit is contained in:
Alexander Bass 2022-06-21 21:51:09 -04:00
parent 3c12e33970
commit 5f5f759f69
33 changed files with 537 additions and 358 deletions

BIN
Pixerif.ttf Normal file

Binary file not shown.

175
index.js
View file

@ -1,175 +0,0 @@
const worldgen = require("./worldgen.js")
const util = require("./util.js")
const log = require("./log.js")
const {performance} = require('perf_hooks');
const gridSize = [32, 32];
const perlinScale = 4;
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);
}
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 (let i = 0; i < this.players.length; i++) {
ids.push(this.players[i].id)
}
return ids
}
getAllNames() {
let names = []
for (let 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(data){
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 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
io.to(client.game.name).emit('sync',{world: client.game.world})
})
});

10
index.mjs Normal file
View file

@ -0,0 +1,10 @@
import * as log from "./scripts/log.mjs"
import "./scripts/roomManager.mjs"
import "./scripts/server/netcode.mjs"
import {roomManager} from "./scripts/roomManager.mjs"
log.setMode(1);
setInterval(function(){ updateInfo()},5000)
function updateInfo() {
log.setInfo(roomManager.games)
}

120
package-lock.json generated
View file

@ -30,9 +30,9 @@
"integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw=="
},
"node_modules/@types/node": {
"version": "17.0.30",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.30.tgz",
"integrity": "sha512-oNBIZjIqyHYP8VCNAV9uEytXVeXG2oR0w9lgAXro20eugRQfY002qr3CUl6BAe+Yf/z3CRjPdz27Pu6WWtuSRw=="
"version": "18.0.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.0.tgz",
"integrity": "sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA=="
},
"node_modules/accepts": {
"version": "1.3.8",
@ -49,7 +49,7 @@
"node_modules/array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
},
"node_modules/base64id": {
"version": "2.0.0",
@ -137,7 +137,7 @@
"node_modules/cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
},
"node_modules/cors": {
"version": "2.8.5",
@ -179,12 +179,12 @@
"node_modules/ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
},
"node_modules/encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
"integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=",
"integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==",
"engines": {
"node": ">= 0.8"
}
@ -249,12 +249,12 @@
"node_modules/escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="
},
"node_modules/etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=",
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
"engines": {
"node": ">= 0.6"
}
@ -328,7 +328,7 @@
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=",
"integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==",
"engines": {
"node": ">= 0.6"
}
@ -339,13 +339,13 @@
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"node_modules/get-intrinsic": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.1.tgz",
"integrity": "sha512-kWZrnVM42QCiEA2Ig1bG8zjoIMOgxWwYCEeNdwY6Tv/cOSeGpcoX4pXHfKUxNKVoArnrEr2e9srnAxxGIraS9Q==",
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.2.tgz",
"integrity": "sha512-Jfm3OyCxHh9DJyc28qGk+JmfkpO41A4XkneDSujN9MDXrm4oDKdHvndhZ2dN94+ERNfkYJWDclW6k2L/ZGHjXA==",
"dependencies": {
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.1"
"has-symbols": "^1.0.3"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
@ -415,7 +415,7 @@
"node_modules/media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=",
"integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==",
"engines": {
"node": ">= 0.6"
}
@ -423,12 +423,12 @@
"node_modules/merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
"integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w=="
},
"node_modules/methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=",
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==",
"engines": {
"node": ">= 0.6"
}
@ -466,7 +466,7 @@
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/negotiator": {
"version": "0.6.3",
@ -479,15 +479,15 @@
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/object-inspect": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.0.tgz",
"integrity": "sha512-Ho2z80bVIvJloH+YzRmpZVQe87+qASmBUKZDWgx9cu+KDrX2ZDH/3tMy+gXbZETVGs2M8YdxObOh7XAtim9Y0g==",
"version": "1.12.2",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz",
"integrity": "sha512-z+cPxW0QGUp0mcqcsgQyLVRDoXFQbXOwBaqyF7VIgI4TWNQsDHrBpUQslRmIfAoYWdYzs6UlKJtB2XJpTaNSpQ==",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
@ -514,7 +514,7 @@
"node_modules/path-to-regexp": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
"integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ=="
},
"node_modules/proxy-addr": {
"version": "2.0.7",
@ -649,9 +649,9 @@
}
},
"node_modules/socket.io": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.5.0.tgz",
"integrity": "sha512-slTYqU2jCgMjXwresG8grhUi/cC6GjzmcfqArzaH3BN/9I/42eZk9yamNvZJdBfTubkjEdKAKs12NEztId+bUA==",
"version": "4.5.1",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.5.1.tgz",
"integrity": "sha512-0y9pnIso5a9i+lJmsCdtmTTgJFFSvNQKDnPQRz28mGNnxbmqYg2QPtJTLFxhymFZhAIn50eHAKzJeiNaKr+yUQ==",
"dependencies": {
"accepts": "~1.3.4",
"base64id": "~2.0.0",
@ -727,7 +727,7 @@
"node_modules/socket.js": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/socket.js/-/socket.js-0.1.4.tgz",
"integrity": "sha1-9CfaaFPXVnPsM2X2kYAMopQ2Cj4="
"integrity": "sha512-2JqBbfPOslz48eSRibPa98123V2iR5vxWBQt7w3pSxEgCMOHafNqmjpdZLpwcXe6y6H+Mj0MablR1VXfU4NN2Q=="
},
"node_modules/statuses": {
"version": "2.0.1",
@ -760,7 +760,7 @@
"node_modules/unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=",
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
"engines": {
"node": ">= 0.8"
}
@ -768,7 +768,7 @@
"node_modules/utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=",
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==",
"engines": {
"node": ">= 0.4.0"
}
@ -776,7 +776,7 @@
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
"engines": {
"node": ">= 0.8"
}
@ -819,9 +819,9 @@
"integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw=="
},
"@types/node": {
"version": "17.0.30",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.30.tgz",
"integrity": "sha512-oNBIZjIqyHYP8VCNAV9uEytXVeXG2oR0w9lgAXro20eugRQfY002qr3CUl6BAe+Yf/z3CRjPdz27Pu6WWtuSRw=="
"version": "18.0.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.0.tgz",
"integrity": "sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA=="
},
"accepts": {
"version": "1.3.8",
@ -835,7 +835,7 @@
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
},
"base64id": {
"version": "2.0.0",
@ -901,7 +901,7 @@
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
},
"cors": {
"version": "2.8.5",
@ -933,12 +933,12 @@
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
},
"encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
"integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k="
"integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w=="
},
"engine.io": {
"version": "6.2.0",
@ -985,12 +985,12 @@
"escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg=="
},
"express": {
"version": "4.18.1",
@ -1052,7 +1052,7 @@
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
"integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q=="
},
"function-bind": {
"version": "1.1.1",
@ -1060,13 +1060,13 @@
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"get-intrinsic": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.1.tgz",
"integrity": "sha512-kWZrnVM42QCiEA2Ig1bG8zjoIMOgxWwYCEeNdwY6Tv/cOSeGpcoX4pXHfKUxNKVoArnrEr2e9srnAxxGIraS9Q==",
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.2.tgz",
"integrity": "sha512-Jfm3OyCxHh9DJyc28qGk+JmfkpO41A4XkneDSujN9MDXrm4oDKdHvndhZ2dN94+ERNfkYJWDclW6k2L/ZGHjXA==",
"requires": {
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.1"
"has-symbols": "^1.0.3"
}
},
"has": {
@ -1115,17 +1115,17 @@
"media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
"integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ=="
},
"merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
"integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w=="
},
"methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w=="
},
"mime": {
"version": "1.6.0",
@ -1148,7 +1148,7 @@
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"negotiator": {
"version": "0.6.3",
@ -1158,12 +1158,12 @@
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg=="
},
"object-inspect": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.0.tgz",
"integrity": "sha512-Ho2z80bVIvJloH+YzRmpZVQe87+qASmBUKZDWgx9cu+KDrX2ZDH/3tMy+gXbZETVGs2M8YdxObOh7XAtim9Y0g=="
"version": "1.12.2",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz",
"integrity": "sha512-z+cPxW0QGUp0mcqcsgQyLVRDoXFQbXOwBaqyF7VIgI4TWNQsDHrBpUQslRmIfAoYWdYzs6UlKJtB2XJpTaNSpQ=="
},
"on-finished": {
"version": "2.4.1",
@ -1181,7 +1181,7 @@
"path-to-regexp": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
"integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ=="
},
"proxy-addr": {
"version": "2.0.7",
@ -1280,9 +1280,9 @@
}
},
"socket.io": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.5.0.tgz",
"integrity": "sha512-slTYqU2jCgMjXwresG8grhUi/cC6GjzmcfqArzaH3BN/9I/42eZk9yamNvZJdBfTubkjEdKAKs12NEztId+bUA==",
"version": "4.5.1",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.5.1.tgz",
"integrity": "sha512-0y9pnIso5a9i+lJmsCdtmTTgJFFSvNQKDnPQRz28mGNnxbmqYg2QPtJTLFxhymFZhAIn50eHAKzJeiNaKr+yUQ==",
"requires": {
"accepts": "~1.3.4",
"base64id": "~2.0.0",
@ -1340,7 +1340,7 @@
"socket.js": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/socket.js/-/socket.js-0.1.4.tgz",
"integrity": "sha1-9CfaaFPXVnPsM2X2kYAMopQ2Cj4="
"integrity": "sha512-2JqBbfPOslz48eSRibPa98123V2iR5vxWBQt7w3pSxEgCMOHafNqmjpdZLpwcXe6y6H+Mj0MablR1VXfU4NN2Q=="
},
"statuses": {
"version": "2.0.1",
@ -1364,17 +1364,17 @@
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ=="
},
"utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA=="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg=="
},
"ws": {
"version": "8.2.3",

View file

@ -2,7 +2,7 @@
"name": "empires",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

BIN
pixerif.woff2 Normal file

Binary file not shown.

1
scripts/env.mjs Normal file
View file

@ -0,0 +1 @@
export const port = 8082;

54
scripts/game.mjs Normal file
View file

@ -0,0 +1,54 @@
import {Player} from "./player.mjs"
import * as log from "./log.mjs"
import {roomManager} from "./roomManager.mjs"
import { World} from "./world/world.mjs"
export class Game {
constructor(name) {
this.name = name;
this.players = [];
this.world = new World([32,32])
}
addPlayer(id, name) {
if (this.getAllNames().includes(name)) return false
var color = 1
const player = new Player(id, name, color);
this.players.push(player);
return player
}
removePlayerByID(id) {
this.players = this.players.filter(obj => obj.id != id);
log.log("removed player - " + id)
if (this.players.length < 1) {
roomManager.removeGame(this.name);
}
}
getAllIDs() {
let ids = []
for (let i = 0; i < this.players.length; i++) {
ids.push(this.players[i].id)
}
return ids
}
getAllNames() {
let names = []
for (let i = 0; i < this.players.length; i++) {
names.push(this.players[i].name)
}
return names
}
getSettings() {
const settings =
{
mines: this.world.mines,
dimensions: this.world.dimensions,
name: this.name
}
return settings
}
}

View file

@ -1,14 +1,14 @@
const os = require("os")
import * as os from "os"
var mode = 0
var logs = []
var rightView = [0,1,2,3,4,5,6]
var info = [[],[]]
function setMode(type) {
export function setMode(type) {
mode = type
}
function updateLog() {
export function updateLog() {
if (mode) return
let templogs = logs;
@ -53,12 +53,9 @@ for (var i = 0; i < templogs.length; i++ ) {
}
}
function logRight(line, text) {
rightView[line] = text;
updateLog();
}
function log(string, color) {
export function log(string, color) {
if (mode) return console.log(string)
if (string == undefined) return;
var prefix = '';
@ -118,7 +115,7 @@ logs.push(newString)
updateLog();
}
function setInfo (players){
export function setInfo (players){
if (mode) return false
if (players != undefined) {
info[0] = players;
@ -126,5 +123,3 @@ if (players != undefined) {
}
}
module.exports = {log, updateLog, setInfo, setMode, logRight}

7
scripts/player.mjs Normal file
View file

@ -0,0 +1,7 @@
export class Player {
constructor(id, name, color) {
this.id = id;
this.name = name;
this.color = color;
}
}

64
scripts/roomManager.mjs Normal file
View file

@ -0,0 +1,64 @@
import {Game} from "./game.mjs"
import * as log from "./log.mjs"
import {IllegalAction} from "./server/illegalAction.mjs"
class RoomManager {
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, settings) {
const game = new Game(name, settings);
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 new IllegalAction(id, 22)
}
if (nick.length > 9) {
return new IllegalAction(id, 21)
}
const game = this.getGameByName(room, true);
const player = game.addPlayer(id, nick);
if (player === false) {
this.removeGame(game)
return new IllegalAction(id, 20)
}
client.join(game.name)
client.game = game;
client.player = player
return true
}
}
export var roomManager = new RoomManager();

View file

@ -0,0 +1,11 @@
import {io} from "./io.mjs"
export class IllegalAction {
constructor(id, action){
this.action = action
this.id = id
this.sendIllegalAction();
}
sendIllegalAction() {
io.to(this.id).emit('illegalAction', this.action)
}
}

4
scripts/server/io.mjs Normal file
View file

@ -0,0 +1,4 @@
import {webServer} from "./server.mjs"
import { Server } from "socket.io";
export const io = new Server(webServer, {});

View file

@ -0,0 +1,55 @@
import {IllegalAction} from "./illegalAction.mjs"
import * as log from "../log.mjs"
import {roomManager} from "../roomManager.mjs"
import {io} from "./io.mjs"
var oldWorld = 0
io.on('connection', function(client){
connected(client)
});
function connected(client) {
client.sync = function() {
const tempWorld = client.game.world.obfuscate();
io.to(client.game.name).emit('sync',{world: tempWorld})
}
client.sendMeta = function() {
const roomSettings = client.game.getSettings();
const roomPlayers = client.game.players; // Dont send client id's to everyone, TODO
const metadata = {players: roomPlayers, settings: roomSettings}
io.to(client.game.name).emit('metadata', metadata)
}
client.game = "";
client.player = "";
client.on('disconnect', function(){
if (!roomManager.getAllIDs().includes(client.id)) return new IllegalAction(client.id, 22)
log.log(client.id + ' disconnected.', 'FgCyan')
client.game.removePlayerByID(client.id)
client.sendMeta();
})
client.on('joinGame', function(data){
if(!roomManager.joinClientToGame(data.room, data.name, client.id, client)) return
client.sync()
log.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`, 'FgMagenta');
client.emit('inGame', true)
client.sendMeta()
})
client.on('leaveGame', function(data){
log.log(client.id + ' disconnected.')
client.game.removePlayerByID(client.id)
})
client.on('clickCanvas', function(data){
if (!roomManager.getAllIDs().includes(client.id)) return new IllegalAction(client.id, 1)
client.game.world.click([data.tilePosition[0],data.tilePosition[1]], data.mode, client.player)
client.sync()
})
}

10
scripts/server/server.mjs Normal file
View file

@ -0,0 +1,10 @@
import path from 'path';
import express from "express"
import { port } from "../env.mjs"
var app = express()
const __dirname = path.resolve(path.dirname(''));
app.use(express.static(__dirname + '/www/'));
export var webServer = app.listen(port, function () {
var port = webServer.address().port;
});

6
scripts/util.mjs Normal file
View file

@ -0,0 +1,6 @@
export function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
export function clamp(number, min, max) {
return Math.max(min, Math.min(number, max));
}

22
scripts/world/flagger.mjs Normal file
View file

@ -0,0 +1,22 @@
export function flag(data, location, player) {
let tile = data[location[0]][location[1]]
if (!tile.mask || tile.color === 0) return data
const color = player.color * 1
console.log(player)
switch (tile.flag) {
case (0):
tile.flag = color;
break;
case (color):
tile.flag = color + 16;
break;
default:
tile.flag = 0;
}
console.log(tile.flag)
data[location[0]][location[1]] = tile
return data;
}

View file

@ -0,0 +1,42 @@
import * as util from "../util.mjs"
const searchLoc =
// [
// [0, 2],
// [-1, 1], [0, 1], [1, 1],
// [-2,0], [-1, 0],/*Start*/[1, 0], [2,0],
// [-1,-1], [0,-1], [1,-1],
// [0,-2]
//
// ]
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
export function generate(world, avoidLocation){
var minesPlanted = 0;
while (minesPlanted < world.mines || !(minesPlanted <= world.dimensions[0]*world.dimensions[1]-15)) {
const x = util.randomNumber(0,world.dimensions[0])
const y = util.randomNumber(0,world.dimensions[1])
var suitable = true;
searchLoc.forEach(loc => {
const tempx = x + loc[0]
const tempy = y + loc[1]
if (tempx === avoidLocation[0] && tempy === avoidLocation[1]) {
suitable = false;
}
})
// console.log([x,y] + ":----:" + avoidLocation)
if (x == avoidLocation[0] && y == avoidLocation[1]) { suitable = false }
if (world.data[x][y].type == 5) { suitable = false }
if (suitable) {
world.data[x][y].type = 5
minesPlanted++;
}
}
world.isGenerated = true;
return world;
}

32
scripts/world/marker.mjs Normal file
View file

@ -0,0 +1,32 @@
const searchLoc =
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
// Place Numbers
export function mark(world) {
for(let x = 0; x < world.dimensions[0]; x++){
for(let y = 0; y < world.dimensions[1]; y++){
if (world.data[x][y].type === 1) {
var counter = 0;
searchLoc.forEach(location => {
const tempx = x + location[0]
const tempy = y + location[1]
if (tempx >= 0 && tempy >= 0 && tempx < world.dimensions[0] && tempy < world.dimensions[1]) {
if (world.data[tempx][tempy].type === 5) {
counter++;
}
}
});
if (counter !== 0) {
world.data[x][y].type = 15 + counter
}
}
}
}
world.isMarked = true;
return world;
}

View file

@ -0,0 +1,16 @@
export function obfuscate(world) {
let tempWorld = world;
for(let x = 0; x < tempWorld.dimensions[0]; x++){
for(let y = 0; y < tempWorld.dimensions[1]; y++){
if (tempWorld.data[x][y].mask === true) {
tempWorld.data[x][y].type = 0;
};
tempWorld.isObfuscated = true;
}
}
return tempWorld;
}

14
scripts/world/placer.mjs Normal file
View file

@ -0,0 +1,14 @@
import * as log from "../log.mjs"
import * as revealer from "./revealer.mjs"
export function place(data, location, id) {
let tile = data[location[0]][location[1]];
if (tile.mask === true) {
if (tile.mask && tile.flag === 0) {
tile.mask = false;
}
data[location[0]][location[1]] = tile;
revealer.reveal(data, location)
}
return data;
}

View file

@ -0,0 +1,43 @@
const searchLoc =
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
export function reveal(data, location) {
if (data[location[0]][location[1]].type !== 5) {
var toSearch = [];
var searchedLocations = [];
toSearch.push(location)
if (data[location[0]][location[1]].type === 1) {
while (toSearch.length > 0) {
const x = toSearch[0][0]
const y = toSearch[0][1]
searchedLocations.push(toSearch[0])
toSearch.shift()
searchLoc.forEach(loc => {
const tempx = x + loc[0]
const tempy = y + loc[1]
if (tempx >= 0 && tempy >= 0 && tempx < data.length && tempy < data[0].length) {
if (data[tempx][tempy].type === 1 && data[tempx][tempy].mask === true) {
if (!toSearch.includes([tempx,tempy]) && !searchedLocations.includes([tempx,tempy])) {
toSearch.push([tempx,tempy])
}
}
if (data[tempx][tempy].type !== 5) {
data[tempx][tempy].mask = false;
}
}
})
}
}
return data
}
}

55
scripts/world/world.mjs Normal file
View file

@ -0,0 +1,55 @@
import * as obfuscater from "./obfuscator.mjs";
import * as generator from "./generator.mjs"
import * as marker from "./marker.mjs"
import * as log from "../log.mjs"
import * as flagger from "./flagger.mjs"
import * as placer from "./placer.mjs"
export class World {
constructor(dimensions = [32,32], mines = 30) {
this.mines = 200
this.dimensions = dimensions;
this.isGenerated = false;
this.isObfuscated = false;
this.isMarked = false;
this.data = Array.from(Array(this.dimensions[0]), () => new Array(this.dimensions[1]));
for (let x = 0; x < this.dimensions[0]; x++){
for (let y = 0; y < this.dimensions[1]; y++){
this.data[x][y] = {type: 1, flag: 0, mask: true}
}
}
}
click (location, mode, player) {
if (this.isGenerated) {
if (mode === 2) {
this.data = flagger.flag(this.data, location, player)
}
if (mode === 0) {
this.data = placer.place(this.data, location, player)
}
} else {
this.generate(location).mark();
this.data = placer.place(this.data, location, player)
}
}
generate(location) {
if (this.isGenerated) return log.log("Already Generated");
return generator.generate(this, location);
}
mark() {
if (this.isMarked) return log.log("Already Marked!");
return marker.mark(this);
}
obfuscate() {
if (this.isObfuscated) return log.log("already done")
const tempWorld = JSON.parse(JSON.stringify(this));;
return obfuscater.obfuscate(tempWorld);
}
}

View file

@ -1,7 +0,0 @@
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function clamp(number, min, max) {
return Math.max(min, Math.min(number, max));
}
module.exports = {randomNumber, clamp}

View file

@ -1,77 +0,0 @@
const util = require("./util.js")
const log = require("./log.js")
const searchLoc =
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
function generateWorld(gridSize, perlinScale){
let x, y = 0
let tempWorld = Array.from(Array(gridSize[0]), () => new Array(gridSize[1]));
for (x = 0; x < gridSize[0]; x++){
for (y = 0; y < gridSize[1]; y++){
tempWorld[x][y] = {type: 0, structure: 0, flag: 0, mask: true}
}
}
// Generate board
for(let x = 0; x < gridSize[0]; x++){
for(let y = 0; y < gridSize[1]; y++){
const value = util.randomNumber(0, 6);
var n;
switch (value) {
case 0:
n = 0;
break;
case 1:
n = 0
break;
case 2:
n = 0
break;
case 3:
n = 0
break;
case 4:
n = 0
break;
case 5:
n = 5
break;
default:
log.log("OTHER")
n = 7
}
tempWorld[x][y].type = n
}
}
// Place Numbers
for(let x = 0; x < gridSize[0]; x++){
for(let y = 0; y < gridSize[1]; y++){
if (tempWorld[x][y].type === 0) {
var counter = 0;
searchLoc.forEach(location => {
const tempx = x + location[0]
const tempy = y + location[1]
if (tempx >= 0 && tempy >= 0 && tempx < gridSize[0] && tempy < gridSize[1]) {
if (tempWorld[tempx][tempy].type === 5) {
counter++;
}
}
});
if (counter !== 0) {
tempWorld[x][y].type = 15 + counter
}
}
}
}
return tempWorld;
}
module.exports = { generateWorld };

View file

@ -3,7 +3,7 @@
<head>
<title></title>
<script src="http://localhost:35729/livereload.js" charset="utf-8"></script>
<script type="text/javascript" src="socket.io.js"></script>
<script type="text/javascript" src="socket.io.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 64 KiB

View file

@ -16,28 +16,18 @@ export function renderTiles() { // DRAW THE IMAGE TO THE CANVAS.
for(y = 0; y < gridSize[1]; y++){
const xu = x*tileSize;
const yu = y*tileSize;
const tempWorld = game.world;
const tempWorld = game.world.data;
// Draw buildings
ctx.drawImage(tileArray[tempWorld[x][y].type], xu,yu)
// Draw Structures
switch (tempWorld[x][y].structure) {
case (1):
ctx.drawImage(tileArray[4], xu,yu)
break;
case (2):
ctx.drawImage(tileArray[32], xu,yu)
break;
case (3):
ctx.drawImage(tileArray[33], xu,yu)
break;
case (4):
ctx.drawImage(tileArray[34], xu,yu)
break;
}
const flag = tempWorld[x][y].flag
// console.log(flag)
if (flag !== 0) {
// console.log("FALAG")
ctx.drawImage(tileArray[flag + 7], xu,yu)
}
}
}

View file

@ -43,9 +43,10 @@ function getMousePosition(canvas, event) {
const y = event.clientY - rect.top;
const xu = cursor.x
const yu = cursor.y
const button = event.button
console.log("Click!")
console.log(xu,yu)
clickCanvas({tilePosition: [xu,yu], structure: getButton()*1})
clickCanvas({tilePosition: [xu,yu], mode: button})
}

View file

@ -40,7 +40,7 @@ socket.on('illegalAction', function(data){
})
socket.on('playerList', function(data){
socket.on('metadata', function(data){
console.log(data)
});

File diff suppressed because one or more lines are too long

7
www/socket.io.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -13,6 +13,12 @@ canvas {
border-width: 3px;
}
@font-face {
font-family: "serif";
src: url("pixserif.woff2") format('woff2');
}
.left{
display: block;
transform: translate(0, 50px);