From ee09c56a8b63f75e7002240cda8a370e9c30c249 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Mon, 29 Apr 2019 15:09:06 -0500 Subject: [PATCH] some kind of communication works next: frontend, then debugging --- server/ball.js | 8 +++++++- server/constants.js | 1 + server/field.js | 6 +++--- server/game.js | 4 +++- server/gamestate.js | 31 +++++++++++++++++-------------- server/paddles.js | 13 +++++++++++-- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/server/ball.js b/server/ball.js index e0daf4e..44c6084 100644 --- a/server/ball.js +++ b/server/ball.js @@ -5,7 +5,7 @@ const Coord = require('./coord.js'); function Ball() { // generates a new ball with random direction and speed (within limits) - var coord = new Coord(0,0); + this.coord = new Coord(0,0); var angle = Math.random()*2*Math.PI; var speed = Math.random()*(c.MAX_INIT-c.MIN_INIT) + c.MIN_INIT; this.dx = speed*Math.cos(angle); @@ -25,6 +25,12 @@ Ball.prototype.get_angle = function() { // gets angle from the origin return Math.atan2(this.coord.y, this.coord.x); } + +Ball.prototype.reduce = function() { + // returns a minimally necessary object for use on the client + return this.coord; +} + Ball.prototype.radius = c.BALL_RADIUS; module.exports = Ball; diff --git a/server/constants.js b/server/constants.js index 53b6c2c..f01ff59 100644 --- a/server/constants.js +++ b/server/constants.js @@ -6,6 +6,7 @@ var c = { NUM_PLAYERS: 3, MS_PER_FRAME: 500, WAIT_TIME: 60000, // 1 minute + MAX_GAMES: 5, // the most games allowed to go on at once, to be tweaked as needed for purposes // gamestate DYING_TIME_IN_FRAMES: 100, BOARD_RADIUS: 10, diff --git a/server/field.js b/server/field.js index 3f63978..abd3124 100644 --- a/server/field.js +++ b/server/field.js @@ -16,7 +16,7 @@ var genEndpoints = function(n ,dead) { // so i'll start from a radius out a zero radians var endpoints = []; var players_length = n - dead.length; - for(var d in dead) { + for(var d of dead) { players_length += d.time/c.DYING_TIME_IN_FRAMES; } var theta = 0; @@ -64,7 +64,7 @@ var angles = function(n, dead, thresh) { // gives angle pairs for the thresholds and whatnot. var angs = []; var players_length = n - dead.length; - for(var d in dead) { + for(var d of dead) { players_length += d.time/c.DYING_TIME_IN_FRAMES; } var theta = 0; @@ -85,7 +85,7 @@ var angles = function(n, dead, thresh) { var t1 = theta; theta += dtheta/2; var t2 = theta; - angs.push(new anglepair(t1, t2, n)); + angs.push(new AnglePair(t1, t2, n)); } } return angs; diff --git a/server/game.js b/server/game.js index 70ed3ed..ac85f31 100644 --- a/server/game.js +++ b/server/game.js @@ -20,8 +20,10 @@ Game.prototype.getNextFrame = function() { var inputs = this.players.map(function(p) {return p.get_status();}); this.state.update(inputs); var state = this.state.getState(); + state.id = 0; for(var i=0; i ep.isDead); var walls = borders.concat(deadzones); // check for collisions - for(var ball in this.balls) { + for(var ball of this.balls) { // (check the fixt edges first, then the paddles I guess var collided = false; for(var i=0; (i !ep.isDead); - for(var lz in livingzones) { + for(var lz of livingzones) { // get corresponding paddle // should be guaranteed to find one.... so... var paddle = this.paddles.find(p => p.id ==lz.id); @@ -129,11 +127,11 @@ GameState.prototype.update = function(inputs) { // OK I'M HERE AFAICT // i don't need to check where any of the paddles are, I just need to check the spaces behind the paddles (±) var zero = new Coord(0,0); - var oobs = this.balls.filter(b => (b.dist2(zero)>(c.BOARD_RADIUS+c.OOB_THRESH))); + var oobs = this.balls.filter(b => (b.coord.dist2(zero)>(c.BOARD_RADIUS+c.OOB_THRESH))); var angs = field.angles(this.numPlayers, this.dead, c.ANGLE_THRESH); - for(var oob in oobs) { + for(var oob of oobs) { var oobth = oob.get_angle(); - for(var ang in angs) { + for(var ang of angs) { // normalise angle pair... // this gon be inefficient sigh var a1 = ang.f; @@ -162,7 +160,12 @@ GameState.prototype.update = function(inputs) { GameState.prototype.getState = function() { // returns a string suitable to be broadcast to all the players - return this.inputs.join(""); + // only need the dead, balls, and paddles, everything else can be determined + // dead don't need changing, all info is necessary + var newballs = this.balls.map(b => b.reduce()); + var newpads = this.paddles.map(p => p.reduce()); + var theobject = {dead: this.dead, paddles: newpads, balls: newballs}; + return theobject; } module.exports = GameState; diff --git a/server/paddles.js b/server/paddles.js index b8d7f6b..1d895e9 100644 --- a/server/paddles.js +++ b/server/paddles.js @@ -11,7 +11,7 @@ var Paddle = function(id) { this.direction = 'x'; } -Paddle.prototype.move(direction) { +Paddle.prototype.move = function(direction) { // direction is either 'u', 'd', or 'x', passed along from the inputs gathered elsewhere if((direction=='u') && (this.position < 1)) { this.position += c.DPADDLE; @@ -33,7 +33,7 @@ function dist(p1,p2) { } -Paddle.prototype.getEndpoints(enclosing) { +Paddle.prototype.getEndpoints = function(enclosing) { // returns an endpoints object for the paddle // given the desired width of said paddle and the enclosing endpoints var encl_len = dist(enclosing.f, enclosing.s); @@ -48,3 +48,12 @@ Paddle.prototype.getEndpoints(enclosing) { var second = new Coord(vector[0]*d, vector[1]*d); return new Endpoints(first, second, this.id); } + +Paddle.prototype.reduce = function() { + var theid = this.id; + var thepos = this.position; + return {id:theid, pos:thepos}; +} + + +module.exports = Paddle; -- 2.39.2