unibutton/index.js

45 lines
No EOL
1 KiB
JavaScript

import express from "express";
import expressW from "express-ws";
import Game from "./game.js";
import Player from "./common/player.js";
var app = express();
expressW(app);
var game = new Game();
game.init();
app.use('/js', express.static('./common'));
app.use(express.static('./static'));
app.ws('/', function (ws, req) {
game.ws.push(ws);
let player = new Player();
let playerI = game.entities.length;
game.entities[playerI] = player;
ws.active = true;
ws.on('message', function message(msg) {
let data = {};
try {
data = JSON.parse(msg);
} catch (err) {
console.log(err);
data = {};
}
let { vel, dir, you } = data;
let data2 = { vel, dir, you };
game.entities[playerI] = Object.assign(game.entities[playerI], data2);
})
ws.on('close', function () {
ws.active = false;
player.health = -1;
})
console.log('socket', req.testing);
});
app.listen(process.env.PORT || 3069);