Skip to content
Browse files

ADD extract DrawQueue from Player

and add path simplification
  • Loading branch information...
1 parent 68ccf8f commit f843942e5d81a1cd0e25c7943366fc2523b43592 @oelmekki committed
View
6 server_side/lib/Board.js
@@ -56,7 +56,11 @@
return this.change();
},
change: (function() {
- return this.fireEvent('change', [Object.values(this.players)]);
+ return this.fireEvent('change', [
+ Object.values(this.players).map(function(player) {
+ return player.toJson();
+ })
+ ]);
}).protect(),
noCollision: (function(current_id, current_player) {
var collide,
View
29 server_side/lib/DrawQueue.js
@@ -0,0 +1,29 @@
+(function() {
+
+ exports.DrawQueue = new Class({
+ initialize: function() {
+ this.queue = [];
+ return this;
+ },
+ addPosition: function(new_position) {
+ this.queue.push(new_position);
+ return this.simplify();
+ },
+ reset: function() {
+ return this.queue = [];
+ },
+ simplify: (function() {
+ var positions, start_index;
+ if (this.queue.length > 2) {
+ start_index = this.queue.length - 3;
+ positions = this.queue.slice(start_index, start_index + 3);
+ if (positions.length > 2) {
+ if ((positions[2].x === positions[1].x && positions[2].x === positions[0].x) || (positions[2].y === positions[1].y && positions[2].y === positions[0].y)) {
+ return this.queue.splice(start_index, 3, positions[0], positions[2]);
+ }
+ }
+ }
+ }).protect()
+ });
+
+}).call(this);
View
28 server_side/lib/Player.js
@@ -2,8 +2,15 @@
var __hasProp = Object.prototype.hasOwnProperty;
exports.Player = new Class({
- initialize: function() {
+ Extends: require('./Base').Base,
+ options: {
+ dependencies: {
+ DrawQueue: require('./DrawQueue').DrawQueue
+ }
+ },
+ initialize: function(options) {
var attr, value, _ref;
+ this.parent(options);
this.step = configuration.step;
this.run_step = configuration.run_step;
_ref = configuration.initial;
@@ -27,7 +34,7 @@
},
create: function() {
this.color = 'rgb( ' + Number.random(10, 255) + ', ' + Number.random(10, 255) + ', ' + Number.random(10, 255) + ' )';
- return this.drawn = [];
+ return this.draw_queue = new this.DrawQueue();
},
findNewPosition: function() {
var new_position, step;
@@ -58,9 +65,12 @@
if (this.isOnSafeZone(new_position)) {
this.x = new_position.x;
this.y = new_position.y;
- if (this.drawing) return this.drawn = [];
+ if (this.drawing) {
+ this.draw_queue.reset();
+ return this.drawing = false;
+ }
} else if (this.drawing) {
- this.drawn.push({
+ this.draw_queue.addPosition({
x: new_position.x,
y: new_position.y
});
@@ -75,6 +85,16 @@
if (options.draw) return this.drawing = true;
}
},
+ toJson: function() {
+ var resp;
+ return resp = {
+ x: this.x,
+ y: this.y,
+ width: this.width,
+ height: this.height,
+ drawn: this.draw_queue.queue
+ };
+ },
isOnSafeZone: (function(position) {
if (position.y === 0 && position.x >= 0 && position.x <= configuration.area_width) {
return true;
View
1 server_side/spec/Board.spec.coffee
@@ -8,6 +8,7 @@ describe 'Board', ->
create: true
findNewPosition: true
move: true
+ toJson: true
}
@board = new Board( dependencies: { Player: @Player })
View
33 server_side/spec/DrawQueue.spec.coffee
@@ -0,0 +1,33 @@
+DrawQueue = require( '../src/DrawQueue' ).DrawQueue
+
+describe 'DrawQueue', ->
+ beforeEach ->
+ @draw_queue = new DrawQueue()
+
+ describe 'addPosition()', ->
+ describe 'with empty list', ->
+ beforeEach ->
+ @draw_queue.addPosition( x: 10, y: 20 )
+
+ it 'should add new position', ->
+ expect( @draw_queue.queue.getLast() ).toEqual( x: 10, y: 20 )
+
+
+ describe 'with already populated list', ->
+ beforeEach ->
+ @draw_queue.queue = [ { x: 10, y: 20 }, { x: 10, y: 25 } ]
+ @draw_queue.addPosition( x: 10, y: 30 )
+
+ it 'should simplify paths', ->
+ expect( @draw_queue.queue ).toEqual( [ { x: 10, y: 20 }, { x: 10, y: 30 } ] )
+
+ describe 'reset()', ->
+ beforeEach ->
+ @draw_queue.queue = [ { x: 10, y: 20 }, { x: 20, y: 30 } ]
+ @draw_queue.reset()
+
+ it 'should empty position list', ->
+ expect( @draw_queue.queue ).toEqual( [] )
+
+
+
View
23 server_side/spec/Player.spec.coffee
@@ -1,8 +1,15 @@
-Player = require( '../src/Player' ).Player
+Player = require( '../src/Player' ).Player
+DrawQueue = require( '../src/DrawQueue' ).DrawQueue
describe 'Player', ->
beforeEach ->
- @player = new Player()
+ @DrawQueue = getMock 'DrawQueue', {
+ initialize: true
+ addPosition: true
+ reset: true
+ }
+
+ @player = new Player( dependencies: { DrawQueue: @DrawQueue } )
describe 'positionateRandom()', ->
beforeEach ->
@@ -23,7 +30,7 @@ describe 'Player', ->
expect( @player.color ).toMatch( /rgb\( \d+, \d+, \d+ \)/ )
it "should create draw queue", ->
- expect( @player.drawn ).toEqual( [] )
+ expect( @DrawQueue.prototype.initialize.used ).toEqual( 1 )
describe 'findNewPosition()', ->
@@ -41,6 +48,9 @@ describe 'Player', ->
describe 'move()', ->
+ beforeEach ->
+ @player.draw_queue = new @DrawQueue()
+
describe 'moving along the edges', ->
describe 'when not drawing', ->
beforeEach ->
@@ -58,7 +68,6 @@ describe 'Player', ->
@player.x = 0
@player.y = 1
@player.drawing = true
- @player.drawn = [ { x: 0, y: 4 }, { x: 0, y: 3 }, { x: 0, y: 2 } ]
@player.move x: 0, y: 0
it 'should move player', ->
@@ -66,7 +75,7 @@ describe 'Player', ->
expect( @player.y ).toBe( 0 )
it 'should reset draw queue', ->
- expect( @player.drawn ).toEqual( [] )
+ expect( @player.draw_queue.reset.used ).toBe( 1 )
describe 'moving somewhere else than edges', ->
@@ -86,7 +95,6 @@ describe 'Player', ->
@player.x = 0
@player.y = 1
@player.drawing = true
- @player.drawn = []
@player.move x: 1, y: 1
it 'should move player', ->
@@ -94,8 +102,7 @@ describe 'Player', ->
expect( @player.y ).toBe( 1 )
it 'should add new position to draw queue', ->
- expect( @player.drawn[0].x ).toBe( 1 )
- expect( @player.drawn[0].y ).toBe( 1 )
+ expect( @player.draw_queue.addPosition.used ).toBe( 1 )
describe 'changeDirection()', ->
View
2 server_side/src/Board.coffee
@@ -56,7 +56,7 @@ exports.Board = new Class {
change: ( ->
- @fireEvent 'change', [ Object.values( @players ) ]
+ @fireEvent 'change', [ Object.values( @players ).map( ( player )-> ( player.toJson() ) ) ]
).protect()
View
29 server_side/src/DrawQueue.coffee
@@ -0,0 +1,29 @@
+exports.DrawQueue = new Class {
+ initialize: ->
+ @queue = []
+ @
+
+
+ addPosition: ( new_position ) ->
+ @queue.push( new_position )
+ @simplify()
+
+
+ reset: ->
+ @queue = []
+
+
+ # Protected
+
+
+ simplify: ( ->
+ if @queue.length > 2
+ start_index = @queue.length - 3
+ positions = @queue.slice( start_index, start_index + 3 )
+
+ if positions.length > 2
+ if ( positions[2].x == positions[1].x and positions[2].x == positions[0].x ) or ( positions[2].y == positions[1].y and positions[2].y == positions[0].y )
+ @queue.splice( start_index, 3, positions[0], positions[2] )
+
+ ).protect()
+}
View
33 server_side/src/Player.coffee
@@ -1,7 +1,15 @@
exports.Player = new Class {
- initialize: ->
- @step = configuration.step
- @run_step = configuration.run_step
+ Extends: require( './Base' ).Base
+
+ options:
+ dependencies:
+ DrawQueue: require( './DrawQueue' ).DrawQueue
+
+ initialize: ( options ) ->
+ @parent( options )
+
+ @step = configuration.step
+ @run_step = configuration.run_step
for own attr, value of configuration.initial
@[ attr ] = value
@@ -23,8 +31,8 @@ exports.Player = new Class {
create: ->
- @color = 'rgb( ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ' )'
- @drawn = []
+ @color = 'rgb( ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ', ' + Number.random( 10, 255 ) + ' )'
+ @draw_queue = new @DrawQueue()
findNewPosition: ->
@@ -58,10 +66,12 @@ exports.Player = new Class {
if @isOnSafeZone new_position
@x = new_position.x
@y = new_position.y
- @drawn = [] if @drawing
+ if @drawing
+ @draw_queue.reset()
+ @drawing = false
else if @drawing
- @drawn.push({ x: new_position.x, y: new_position.y })
+ @draw_queue.addPosition( x: new_position.x, y: new_position.y )
@x = new_position.x
@y = new_position.y
@@ -73,6 +83,15 @@ exports.Player = new Class {
@drawing = true if options.draw
+ toJson: ->
+ resp =
+ x: @x
+ y: @y
+ width: @width
+ height: @height
+ drawn: @draw_queue.queue
+
+
# Protected

0 comments on commit f843942

Please sign in to comment.
Something went wrong with that request. Please try again.