Browse Source

Push new status immediately if possible instead of invalidating and forcing a reload.

feature/long-poll-status
Daniel Perelman 1 year ago
parent
commit
96c41150f9
1 changed files with 20 additions and 10 deletions
  1. +20
    -10
      fear_tracker/views.py

+ 20
- 10
fear_tracker/views.py View File

@@ -159,12 +159,6 @@ def game_status_object(game, current_phase=None, players_with_fear=None):
status_hash = h.hexdigest()
status_obj['hash'] = status_hash

async_to_sync(get_channel_layer().group_send)(
"%s_status" % game.access_code, {
"type": "fear_tracker.hashcode_seen",
"hashcode": status_hash,
})

return status_obj


@@ -290,10 +284,6 @@ def handle_game_request(request, game, update):
for player in players.values():
player.ready = True

async_to_sync(get_channel_layer().group_send)(
"%s_status" % game.access_code,
{"type": "fear_tracker.invalidate_status"})

if update:
if errors:
res = {
@@ -304,12 +294,22 @@ def handle_game_request(request, game, update):
res['value'] = current_value
else:
res = {'success': True}
async_to_sync(get_channel_layer().group_send)(
"%s_status" % game.access_code,
{"type": "fear_tracker.invalidate_status"})
return HttpResponse(json.dumps(res))

players = get_players_with_fear(game, current_phase, players)
status_obj = game_status_object(game, current_phase, players)
status_string = json.dumps(status_obj)

async_to_sync(get_channel_layer().group_send)(
"%s_status" % game.access_code, {
"type": "fear_tracker.hashcode_seen",
"hashcode": status_obj['hash'],
"status_string": status_string,
})

for player in players.values():
info = player.fear
if not info:
@@ -340,6 +340,12 @@ def status(request, game, hashcode=None):
return HttpResponse(status=HTTPStatus.NOT_MODIFIED)
else:
status_string = json.dumps(status_obj)
async_to_sync(get_channel_layer().group_send)(
"%s_status" % game.access_code, {
"type": "fear_tracker.hashcode_seen",
"hashcode": status_obj['hash'],
"status_string": status_string,
})
return HttpResponse(status_string)


@@ -372,6 +378,10 @@ class StatusLongPollConsumer(AsyncHttpConsumer):

async def fear_tracker_hashcode_seen(self, event):
if self.hashcode != event["hashcode"]:
if event["status_string"]:
body = event["status_string"].encode('utf-8')
await self.send_response(200, body)
await self.disconnect()
await self.channel_layer.group_send(
"%s_status" % self.access_code, {
"type": "fear_tracker.invalidate_status",


Loading…
Cancel
Save