Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #131 from oisdk/fix-sse-balances-error
Browse files Browse the repository at this point in the history
Fix sse KeyErrors in list comprehensions.
  • Loading branch information
Noel Bourke authored Mar 1, 2018
2 parents f36eb90 + ce717f1 commit 3aa8e21
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions backend/backend/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,32 @@ def generate_player_move_event(output_stream, old_positions, new_positions):
data: [[5, 4, 0]]
<BLANKLINE>
>>> import sys
>>> generate_player_move_event(
... sys.stdout,
... {3: 10},
... {5: 4, 3: 10})
event: playerMove
data: [[5, 4, 0]]
<BLANKLINE>
"""
# Send the event name to the client.
output_stream.write('event: playerMove\n')

# Send the JSON object which contains the elements that are not in common
# with the two dictionaries.
output_stream.write('data: ')
if not old_positions:
output_stream.write(json.dumps([
[uid, board_position, 0]
for uid, board_position in new_positions.items()]))
else:
output_stream.write(json.dumps([
[uid, board_position, old_positions[uid]]
for uid, board_position in new_positions.items()
if board_position != old_positions[uid]]))

data = []
for uid, new_position in new_positions.items():
if uid not in old_positions:
data.append([uid, new_position, 0])
else:
old_position = old_positions[uid]
if old_position != new_position:
data.append([uid, new_position, old_position])

output_stream.write(json.dumps(data))

# Standard SSE procedure to have two blank lines after data.
output_stream.write('\n\n')
Expand Down Expand Up @@ -396,6 +406,14 @@ def generate_player_balance_event(output_stream, old_balances, new_balances):
data: [[5, 200, 0]]
<BLANKLINE>
>>> import sys
>>> generate_player_balance_event(
... sys.stdout,
... {3: 100},
... {5: 200, 3: 100})
event: playerBalance
data: [[5, 200, 0]]
<BLANKLINE>
"""
# Send the event name to the client.
output_stream.write('event: playerBalance\n')
Expand All @@ -404,16 +422,16 @@ def generate_player_balance_event(output_stream, old_balances, new_balances):
# with the two dictionaries.
output_stream.write('data: ')

if not old_balances:
output_stream.write(json.dumps([
[uid, balance, 0]
for uid, balance in new_balances.items()]))
else:
output_stream.write(json.dumps([
[uid, balance, ((balance - old_balances[uid])
if old_balances[uid] else balance)]
for uid, balance in new_balances.items()
if balance != old_balances[uid]]))
data = []
for uid, balance in new_balances.items():
if uid in old_balances:
old = old_balances[uid]
if old != balance:
data.append([uid, balance, balance - old])
else:
data.append([uid, balance, 0])

output_stream.write(json.dumps(data))

# Standard SSE procedure to have two blank lines after data.
output_stream.write('\n\n')
Expand Down

0 comments on commit 3aa8e21

Please # to comment.