Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Merge branch 'fix-boxxy'
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Van der Jeugt committed Apr 29, 2014
2 parents ae891a2 + 7b036cc commit ba5d2bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
40 changes: 24 additions & 16 deletions boxxy/src/boxxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function Boxxy() {

/* State */
this.frozen = false;
this.frozenScore = null;
this.notification = null;
this.circuitLength = 0;
this.startTime = null;
Expand All @@ -20,11 +21,22 @@ function Boxxy() {
}

Boxxy.prototype.putState = function(stateDelta) {
if(stateDelta.frozen != null) this.frozen = stateDelta.frozen;
if(stateDelta.notification != null) this.notification = stateDelta.notification;
if(stateDelta.circuitLength != null) this.circuitLength = stateDelta.circuitLength;
if(stateDelta.startTime != null) this.startTime = stateDelta.startTime;
if(stateDelta.stations != null) this.stations = stateDelta.stations;

// This signifies the change melted -> frozen
if(stateDelta.frozen != null && stateDelta.frozen && !this.frozen) {
this.frozenScore = this.teamsByScore();
this.frozen = true;
}

// This signifies the change frozen -> melted
if(stateDelta.frozen != null && !stateDelta.frozen && this.frozen) {
this.frozen = false;
}

if(!this.frozen && stateDelta.teams) this.teams = stateDelta.teams;
if(!this.frozen && stateDelta.laps) this.laps = stateDelta.laps;

Expand All @@ -33,26 +45,22 @@ Boxxy.prototype.putState = function(stateDelta) {
}

Boxxy.prototype.addLap = function(lap) {
if(!this.frozen) {
if(this.laps.length >= this.maxLaps) this.laps.pop();
this.laps = [lap].concat(this.laps);
if(this.laps.length >= this.maxLaps) this.laps.pop();
this.laps = [lap].concat(this.laps);

/* Just copy the total laps instead of calculating it, more robust. */
this.teams[lap.team].laps = lap.total;
this.teams[lap.team].updated = lap.timestamp;
/* Just copy the total laps instead of calculating it, more robust. */
this.teams[lap.team].laps = lap.total;
this.teams[lap.team].updated = lap.timestamp;

this.onAddLap(lap);
this.onUpdate();
}
this.onAddLap(lap);
this.onUpdate();
}

Boxxy.prototype.updatePosition = function(position) {
if(!this.frozen) {
this.teams[position.team].station = position.station;
this.teams[position.team].updated = position.timestamp;
this.onUpdatePosition(position);
this.onUpdate();
}
this.teams[position.team].station = position.station;
this.teams[position.team].updated = position.timestamp;
this.onUpdatePosition(position);
this.onUpdate();
}

Boxxy.prototype.teamsByScore = function() {
Expand Down
18 changes: 14 additions & 4 deletions boxxy/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ app.use(express.static('public')); // Serve the public dir as-is
var boxxyState = boxxy.initialize();

io.sockets.on('connection', function(socket) {
socket.emit('/state', boxxyState);
if (boxxyState.frozen) {
socket.emit('/state', boxxyState.frozenScore);
} else {
socket.emit('/state', boxxyState);
}
});

boxxyState.onPutState = function(stateDelta) {
io.sockets.emit('/state', stateDelta);
if (!boxxyState.frozen) {
io.sockets.emit('/state', stateDelta);
}
}

boxxyState.onAddLap = function(lap) {
io.sockets.emit('/lap', lap);
if (!boxxyState.frozen) {
io.sockets.emit('/lap', lap);
}
}

boxxyState.onUpdatePosition = function(position) {
io.sockets.emit('/position', position);
if (!boxxyState.frozen) {
io.sockets.emit('/position', position);
}
}

// count-von-count facing API
Expand Down

0 comments on commit ba5d2bc

Please sign in to comment.