Ton of work with the server Errors to client, custom UI. Boom.
This commit is contained in:
parent
95da35ee00
commit
96aa1df1fd
69
index.js
69
index.js
|
@ -1,10 +1,19 @@
|
|||
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) {
|
||||
|
@ -16,45 +25,35 @@ class Player {
|
|||
|
||||
class Game {
|
||||
constructor() {
|
||||
this.players = [];
|
||||
this.players = [{id: "adfhad", name: "bobbert"}];
|
||||
this.world = worldgen.generateWorld(gridSize, perlinScale);
|
||||
}
|
||||
|
||||
addPlayer(id, name) {
|
||||
var color
|
||||
switch(util.randomNumber(1,4)) {
|
||||
case 1:
|
||||
color = "red";
|
||||
break;
|
||||
|
||||
case 2:
|
||||
color = "aquamarine";
|
||||
break;
|
||||
|
||||
case 3:
|
||||
color = "green";
|
||||
break;
|
||||
|
||||
case 4:
|
||||
color = "yellow";
|
||||
break;
|
||||
}
|
||||
var color = "blue"
|
||||
const player = new Player(id, name, color);
|
||||
this.players.push(player);
|
||||
}
|
||||
|
||||
removePlayer(id) {
|
||||
this.players = this.players.filter(obj => obj.id != id);
|
||||
console.log("removed player - " + id)
|
||||
log.log("removed player - " + id)
|
||||
}
|
||||
|
||||
getPlayerByID(id) {
|
||||
console.log(this.players)
|
||||
console.log(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
|
||||
}
|
||||
|
||||
}
|
||||
var game = new Game();
|
||||
|
||||
|
@ -62,40 +61,46 @@ 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;
|
||||
console.log('Server running at port %s', port);
|
||||
log.log(`Server running at port ${port}`, "bright");
|
||||
});
|
||||
|
||||
var io = require('socket.io')(server);/* Connection events */
|
||||
io.on('connection', function(client) {
|
||||
console.log('User connected');
|
||||
|
||||
client.illegalAction = function(action) {
|
||||
client.emit('illegalAction', action)
|
||||
}
|
||||
|
||||
log.log('User connected', 'FgGreen');
|
||||
|
||||
client.on('disconnect', function(){
|
||||
console.log(client.id + ' disconnected.')
|
||||
log.log(client.id + ' disconnected.', 'FgCyan')
|
||||
game.removePlayer(client.id)
|
||||
client.broadcast.emit('playerList', game.players)
|
||||
})
|
||||
|
||||
client.on('joinGame', function(data){
|
||||
game.addPlayer(client.id);
|
||||
console.log(data)
|
||||
console.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`);
|
||||
client.emit('gameVars', {gridSize: gridSize, world: game.world})
|
||||
game.addPlayer(client.id, data.name);
|
||||
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){
|
||||
console.log(client.id + ' disconnected.')
|
||||
log.log(client.id + ' disconnected.')
|
||||
game.removePlayer(client.id)
|
||||
client.broadcast.emit('playerList', game.players)
|
||||
})
|
||||
|
||||
|
||||
client.on('clickCanvas', function(data){
|
||||
console.log(data)
|
||||
log.log(game.getAllIDs())
|
||||
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;
|
||||
// console.log(world[xu][yu].owner = game.getPlayerbyID(client.id))
|
||||
// log.log(world[xu][yu].owner = game.getPlayerbyID(client.id))
|
||||
|
||||
client.broadcast.emit('sync',{world: game.world})
|
||||
client.emit('sync',{world: game.world})
|
||||
|
|
101
log.js
Normal file
101
log.js
Normal file
|
@ -0,0 +1,101 @@
|
|||
const os = require("os")
|
||||
|
||||
var logs = []
|
||||
var info = [[],[]]
|
||||
|
||||
function updateLog() {
|
||||
console.clear()
|
||||
const columns = process.stdout.columns;
|
||||
const rows = process.stdout.rows;
|
||||
const vertSplit = Math.round(rows/4)
|
||||
|
||||
for (var i = 0; i < vertSplit; i++) {
|
||||
process.stdout.cursorTo(Math.round(columns/2),i);
|
||||
process.stdout.clearLine();
|
||||
process.stdout.write(`|`)
|
||||
}
|
||||
for (var i = 0; i < columns; i++) {
|
||||
process.stdout.cursorTo(i, vertSplit)
|
||||
process.stdout.write(`=`)
|
||||
}
|
||||
|
||||
process.stdout.cursorTo(0,0)
|
||||
process.stdout.write("Players:")
|
||||
const players = info[0]
|
||||
for (var i = 0; i < players.length; i++) {
|
||||
process.stdout.cursorTo(0,i+1)
|
||||
process.stdout.write(`Name: \x1b[1m${players[i].name}\x1b[0m -\x1b[1m ID:${players[i].id}\x1b[0m`)
|
||||
}
|
||||
for (var i = 0; i < logs.length; i++ ) {
|
||||
process.stdout.cursorTo(0,vertSplit+1+i)
|
||||
process.stdout.write(`${logs[i]}${os.EOL}` )
|
||||
}
|
||||
|
||||
}
|
||||
function log(string, color) {
|
||||
if (string == undefined) return;
|
||||
var prefix = '';
|
||||
switch (color) {
|
||||
case "reset":
|
||||
prefix = "\x1b[0m"
|
||||
break;
|
||||
case "bright":
|
||||
prefix = "\x1b[1m"
|
||||
break;
|
||||
case "dim":
|
||||
prefix = "\x1b[2m"
|
||||
break;
|
||||
case "underscore":
|
||||
prefix = "\x1b[4m"
|
||||
break;
|
||||
case "reverse":
|
||||
prefix = "\x1b[7m"
|
||||
break;
|
||||
case "FgBlack":
|
||||
prefix = "\x1b[30m"
|
||||
break;
|
||||
case "FgRed":
|
||||
prefix = "\x1b[31m"
|
||||
break;
|
||||
case "FgGreen":
|
||||
prefix = "\x1b[32m"
|
||||
break;
|
||||
case "FgYellow":
|
||||
prefix = "\x1b[33m"
|
||||
break;
|
||||
case "FgBlue":
|
||||
prefix = "\x1b[34m"
|
||||
break;
|
||||
case "FgMagenta":
|
||||
prefix = "\x1b[35m"
|
||||
break;
|
||||
case "FgCyan":
|
||||
prefix = "\x1b[36m"
|
||||
break;
|
||||
case "FgWhite":
|
||||
prefix = "\x1b[37m"
|
||||
break;
|
||||
}
|
||||
|
||||
// BgBlack = "\x1b[40m"
|
||||
// BgRed = "\x1b[41m"
|
||||
// BgGreen = "\x1b[42m"
|
||||
// BgYellow = "\x1b[43m"
|
||||
// BgBlue = "\x1b[44m"
|
||||
// BgMagenta = "\x1b[45m"
|
||||
// BgCyan = "\x1b[46m"
|
||||
// BgWhite = "\x1b[47m"
|
||||
const newString = prefix + string + "\x1b[0m"
|
||||
logs.push(newString)
|
||||
updateLog();
|
||||
}
|
||||
|
||||
function setInfo (players){
|
||||
if (players != undefined) {
|
||||
info[0] = players;
|
||||
updateLog()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {log, updateLog, setInfo}
|
9
tt.js
9
tt.js
|
@ -1 +1,10 @@
|
|||
|
||||
let os = require('os');
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
var jh = 0
|
||||
|
||||
|
||||
process.stdout.write('Hello World' + os.EOL);
|
||||
console.log(Boolean(process.stdout.isTTY))
|
||||
process.title = "Server"
|
||||
|
|
|
@ -18,10 +18,6 @@
|
|||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="right">
|
||||
<p id="cash">$0</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="menu" id="menu">
|
||||
|
|
|
@ -31,6 +31,20 @@ socket.on('disconnect', function(data){
|
|||
updateConnectionStatus();
|
||||
})
|
||||
|
||||
socket.on('illegalAction', function(data){
|
||||
let action
|
||||
switch (data) {
|
||||
case 1:
|
||||
action = "You must be in game to do this."
|
||||
break;
|
||||
default:
|
||||
action = "Unknown action."
|
||||
|
||||
}
|
||||
console.log(`Illegal Action. ${action}`)
|
||||
alert(`Illegal Action, ${action}`)
|
||||
})
|
||||
|
||||
function updateConnectionStatus() {
|
||||
document.getElementById("status").textContent = `Server Connection: ${SERVER_CONNECTION}`
|
||||
}
|
||||
|
@ -51,11 +65,6 @@ window.onbeforeunload = function(){
|
|||
world = []
|
||||
}
|
||||
|
||||
socket.on('gameVars', function(tank){
|
||||
gridSize = tank.gridSize;
|
||||
world = tank.world;
|
||||
});
|
||||
|
||||
socket.on('playerList', function(data){
|
||||
console.log(data)
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue