Commit 084add1b by Alexander Bazo

Handle gaze data from clients

parent 6e825baf
...@@ -14,7 +14,7 @@ var app = express(), ...@@ -14,7 +14,7 @@ var app = express(),
server = http.createServer(app), server = http.createServer(app),
gameServer; gameServer;
Logger.enable(); // Logger.enable();
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
...@@ -29,7 +29,9 @@ gameServer.define(Config.getGameRoomName(), StarGazerRoom); ...@@ -29,7 +29,9 @@ gameServer.define(Config.getGameRoomName(), StarGazerRoom);
app.use(Config.getSocialRoute(), socialRoutes); app.use(Config.getSocialRoute(), socialRoutes);
app.use(Config.getMonitorRoute(), monitor(gameServer)); app.use(Config.getMonitorRoute(), monitor(gameServer));
// Prrobably shoud be set after game server is created // Prrobably shoud be set after game server is created
app.use(Config.getGameRoute(), express.static(Config.getGamePath())); app.use(Config.getGameRoute(), express.static(Config.getGamePath(), {
maxAge: 100
}));
gameServer.listen(Config.getPort()); gameServer.listen(Config.getPort());
Logger.log(`Listening on ws://localhost:${ Config.getPort() }`, "Server"); Logger.log(`Listening on ws://localhost:${ Config.getPort() }`, "Server");
\ No newline at end of file
...@@ -3,11 +3,18 @@ const Schema = schema.Schema; ...@@ -3,11 +3,18 @@ const Schema = schema.Schema;
class GazePoint extends Schema { class GazePoint extends Schema {
constructor(screenX, screenY) { constructor(screenX, screenY, createdAt, player, id) {
super();
this.screenX = screenX; this.screenX = screenX;
this.screenY = screenY; this.screenY = screenY;
this.createdAt = Date.now(); this.createdAt = createdAt;
this.id = this.createdAt; this.player = player;
this.id = id;
this.relativeAge = 0;
}
static fromClientData(data) {
return new GazePoint(data.screenX, data.screenY, data.createdAt, data.player, data.id);
} }
} }
...@@ -16,7 +23,8 @@ schema.defineTypes(GazePoint, { ...@@ -16,7 +23,8 @@ schema.defineTypes(GazePoint, {
screenX: "number", screenX: "number",
screenY: "number", screenY: "number",
createdAt: "number", createdAt: "number",
player: "text", relativeAge: "number",
player: "string",
id: "number", id: "number",
}); });
......
...@@ -22,6 +22,10 @@ class StarGazerState extends Schema { ...@@ -22,6 +22,10 @@ class StarGazerState extends Schema {
this.lastEnemySpawn = 0; this.lastEnemySpawn = 0;
} }
addGazePoint(gazePoint) {
this.gazePoints.push(gazePoint);
}
update(deltaTime) { update(deltaTime) {
let now = Date.now(), let now = Date.now(),
delta = now - this.lastUpdate; delta = now - this.lastUpdate;
...@@ -32,7 +36,7 @@ class StarGazerState extends Schema { ...@@ -32,7 +36,7 @@ class StarGazerState extends Schema {
updateGazePoints(now) { updateGazePoints(now) {
for (let i = this.gazePoints.length - 1; i >= 0; i--) { for (let i = this.gazePoints.length - 1; i >= 0; i--) {
let age = now - gazePoints[i].createdAt; let age = now - this.gazePoints[i].createdAt;
if (age > GameConfig.getMaxGazePointAge()) { if (age > GameConfig.getMaxGazePointAge()) {
this.gazePoints.splice(i, 1); this.gazePoints.splice(i, 1);
} else { } else {
......
const colyseus = require("colyseus"), const colyseus = require("colyseus"),
Logger = require("../utils/Logger.js");
GazePoint = require("../game/GazePoint.js");
StarGazerState = require("../game/StarGazerState.js"); StarGazerState = require("../game/StarGazerState.js");
var deltaTime; var deltaTime;
...@@ -6,26 +8,28 @@ var deltaTime; ...@@ -6,26 +8,28 @@ var deltaTime;
class StarGazerRoom extends colyseus.Room { class StarGazerRoom extends colyseus.Room {
onCreate(options) { onCreate(options) {
console.log("in: onCreate"); Logger.log("Creating StarGazerRoom", "Game Room");
console.log(this);
this.setSimulationInterval(this.update.bind(this)); this.setSimulationInterval(this.update.bind(this));
this.setState(new StarGazerState()); this.setState(new StarGazerState());
} }
onJoin(client, options) { onJoin(client, options) {
console.log("in: onJoin"); Logger.log(`Player ${client.sessionId} joined StarGazerRoom`, "Game Room");
} }
onMessage(client, message) { onMessage(client, message) {
console.log("in: onMessage"); if(message.type === "gaze") {
message.data.player = client.id;
this.state.addGazePoint(GazePoint.fromClientData(message.data));
}
} }
onLeave(client, consented) { onLeave(client, consented) {
console.log("in: onLeave"); Logger.log(`Player ${client.sessionId} left StarGazerRoom`, "Game Room");
} }
onDispose() { onDispose() {
console.log("in: onDispose"); Logger.log("Disposing StarGazerRoom", "Game Room");
} }
startGame() { startGame() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment