gameworks 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8af89aede594bc27285d2a3c08d9e02257a6b184
4
- data.tar.gz: d12c91d8de8d29b506f03c1450c0e68059a73854
3
+ metadata.gz: 10a8ee8b5768eec3e315cf623d160995d9b321c7
4
+ data.tar.gz: 12daacb5259d91a5b299c1289c2ebfc77981a46c
5
5
  SHA512:
6
- metadata.gz: 8cc7293bf7d3b9db2bfc1b0876124d0bf96dcf6484a3b00d965ae0852179a1fb4dc8734d206b2361445eebc3ad0acc0c4a1030f58da3d50bc45cceb85b814e9c
7
- data.tar.gz: 51bf5e753319b8726d6fd22d4838703d0466fc4126037eec5b5bb3c6fd2ed646dc46ac8823ca04d3975ebd402d33110e5222c368176d756b5678dafdedf08c3c
6
+ metadata.gz: b606bae8289aef66caf94c308b27a94cfeb02f1bb999d4a8e91b93cea38061cee95be9b3a880a368f25526a054a7c5efae6147847a726f01735a122bd67eff17
7
+ data.tar.gz: 41cb36e64d04fb488324834bdba461a2eda9de821b8d3fe8b0dc8d847143d27881bfdfc669ed615a7c06446eede468827a1afe7ecfc027ee0e33b7b2cb7697a0
@@ -12,7 +12,7 @@ module Gameworks
12
12
  STATE_IN_PLAY = 'in play'
13
13
  STATE_COMPLETED = 'completed'
14
14
 
15
- attr_reader :players, :state, :id, :delay_time, :notifier
15
+ attr_reader :players, :state, :id, :delay_time, :moves, :notifier
16
16
 
17
17
  def initialize(payload={})
18
18
  @notifier = Notifier.new
@@ -23,6 +23,7 @@ module Gameworks
23
23
  @observers = {}
24
24
  @fuses = {}
25
25
  @id = generate_uuid.split('-').first
26
+ @moves = {}
26
27
  end
27
28
 
28
29
  def register_observer(cb)
@@ -37,7 +38,7 @@ module Gameworks
37
38
  cb.call(delta(snapshot).to_json)
38
39
  cb.succeed if end_game
39
40
  end
40
- notifier.push(self, delta(initial_snapshot).to_json)
41
+ notifier.push(self, to_json)
41
42
  end
42
43
 
43
44
  def snapshot_for_observer(token)
@@ -49,16 +50,13 @@ module Gameworks
49
50
  snapshot
50
51
  end
51
52
 
52
- def initial_snapshot
53
- @initial_snapshot ||= snapshot
54
- end
55
-
56
53
  def player_changed?(player, snapshot, for_player=nil)
57
54
  player.score != snapshot.player_scores[player]
58
55
  end
59
56
 
60
57
  def delta(snapshot, for_player=nil)
61
- { :players => @players.select{ |p| player_changed?(p, snapshot, for_player) }.map{ |p| p.as_json(for_player) },
58
+ { :id => id,
59
+ :players => @players.select{ |p| player_changed?(p, snapshot, for_player) }.map{ |p| p.as_json(for_player) },
62
60
  :state => @state }
63
61
  end
64
62
 
@@ -107,6 +105,7 @@ module Gameworks
107
105
  end
108
106
 
109
107
  def signal_turns
108
+ moves.clear
110
109
  active_players.each do |player|
111
110
  token = generate_uuid
112
111
  @turns[token] = player
@@ -127,13 +126,16 @@ module Gameworks
127
126
 
128
127
  move, error = build_move(payload, player)
129
128
  return false, error unless move
129
+
130
130
  legal, error = move_legal?(move, player)
131
131
  return false, error unless legal
132
- legal, error = process_move(move, player)
133
- return false, error unless legal
132
+
133
+ moves[player] = move
134
+ process_move(move, player)
135
+
134
136
  ignore_disqualified_players
135
137
  update_observers
136
- return true
138
+ true
137
139
  end
138
140
 
139
141
  def build_move(payload, player)
@@ -148,15 +150,19 @@ module Gameworks
148
150
  raise NotImplementedError
149
151
  end
150
152
 
153
+ def waiting_on_moves?
154
+ (active_players - moves.keys).any?
155
+ end
156
+
151
157
  def disqualify(player)
152
158
  player.disqualify!
153
159
  update_observers
154
160
  if @players.select { |p| !p.disqualified? }.size < 2
155
161
  end_game
156
- else
157
- update_observers
158
- end_turn
162
+ elsif !waiting_on_moves?
163
+ signal_turns
159
164
  end
165
+ # else do nothing; waiting for other players
160
166
  end
161
167
 
162
168
  def end_game
@@ -175,7 +181,8 @@ module Gameworks
175
181
  end
176
182
 
177
183
  def as_json(for_player=nil)
178
- { :players => @players.map{ |player| player.as_json(for_player) },
184
+ { :id => id,
185
+ :players => @players.map{ |player| player.as_json(for_player) },
179
186
  :state => @state }
180
187
  end
181
188
 
@@ -1,7 +1,9 @@
1
1
  require 'eventmachine'
2
+ require 'securerandom'
2
3
 
3
4
  module Gameworks
4
5
  class Player
6
+ attr_reader :id
5
7
  attr_reader :name
6
8
  attr_accessor :score
7
9
  attr_reader :disqualified
@@ -14,6 +16,7 @@ module Gameworks
14
16
  return
15
17
  end
16
18
 
19
+ @id = SecureRandom.uuid.to_s.split('-').first
17
20
  @name = payload['name']
18
21
  @score = 0
19
22
  @disqualified = false
@@ -43,7 +46,8 @@ module Gameworks
43
46
  end
44
47
 
45
48
  def as_json(for_player=nil)
46
- { name: name,
49
+ { id: id,
50
+ name: name,
47
51
  score: score,
48
52
  disqualified: disqualified }
49
53
  end
@@ -14,6 +14,7 @@ module Gameworks
14
14
  player.wait_for_turn do |turn_token|
15
15
  request[:async_cb].call [ 200, {
16
16
  'Content-Type' => 'application/json',
17
+ 'X-Player-ID' => player.id,
17
18
  'X-Turn-Token' => turn_token
18
19
  }, [game.to_json(player)] ]
19
20
  end
@@ -1,3 +1,3 @@
1
1
  module Gameworks
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gameworks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Palmer
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2015-07-09 00:00:00.000000000 Z
15
+ date: 2015-07-14 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rake