예제 #1
0
 def post(self, **kwargs):
     pin = str(json.loads(self.request.get('json'))['pin'])
     game = PreGame.query().filter(PreGame.pin == pin).get()
     if game is None:
         self.error(404)
     else:
         if not game.can_update:
             self.response.write(
                 json.dumps({
                     'game_id': game.key.urlsafe(),
                     'status': 'started',
                     'version': json.loads(game.game_json)['version']
                 }))
             self.error(410)
         else:
             game.device_ids.append(self.device_id)
             key_db = game.put()
             game_struct = json.loads(game.game_json)
             CurrentGame.set_current_game(self.device_id, key_db.urlsafe())
             response_struct = {
                 "id":
                 key_db.urlsafe(),
                 "version":
                 game_struct['version'],
                 "game":
                 json.loads(
                     PreGame.delete_last_updates_from_json(game.game_json))
             }
             self.response.write(json.dumps(response_struct))
예제 #2
0
파일: pregame.py 프로젝트: Sibyx/thehat
 def get(self, **kwargs):
     #TODO: but if we have incorrect game_id?
     key_db = ndb.Key(urlsafe=kwargs.get('game_id'))
     game = key_db.get()
     game_struct = json.loads(game.game_json)
     if self.device_id in game.device_ids:
         self.response.write(json.dumps({
             'id': key_db.urlsafe(),
             'version': game_struct['version'],
             'game': json.loads(PreGame.delete_last_updates_from_json(game.game_json))
         }))
     else:
         self.error(403)
예제 #3
0
 def get(self, **kwargs):
     #TODO: but if we have incorrect game_id?
     key_db = ndb.Key(urlsafe=kwargs.get('game_id'))
     game = key_db.get()
     game_struct = json.loads(game.game_json)
     if self.device_id in game.device_ids:
         self.response.write(
             json.dumps({
                 'id':
                 key_db.urlsafe(),
                 'version':
                 game_struct['version'],
                 'game':
                 json.loads(
                     PreGame.delete_last_updates_from_json(game.game_json))
             }))
     else:
         self.error(403)
예제 #4
0
파일: pregame.py 프로젝트: Sibyx/thehat
 def post(self, **kwargs):
     pin = str(json.loads(self.request.get('json'))['pin'])
     game = PreGame.query().filter(PreGame.pin == pin).get()
     if game is None:
         self.error(404)
     else:
         if not game.can_update:
             self.response.write(json.dumps({
                 'game_id': game.key.urlsafe(),
                 'status': 'started',
                 'version': json.loads(game.game_json)['version']
             }))
             self.error(410)
         else:
             game.device_ids.append(self.device_id)
             key_db = game.put()
             game_struct = json.loads(game.game_json)
             CurrentGame.set_current_game(self.device_id, key_db.urlsafe())
             response_struct = {"id": key_db.urlsafe(),
                                "version": game_struct['version'],
                                "game": json.loads(PreGame.delete_last_updates_from_json(game.game_json))}
             self.response.write(json.dumps(response_struct))
예제 #5
0
파일: pregame.py 프로젝트: Sibyx/thehat
 def post(self, **kwargs):
     #TODO: but if we have incorrect game_id?
     key_db = ndb.Key(urlsafe=kwargs.get('game_id'))
     game = key_db.get()
     if self.device_id not in game.device_ids:
         self.error(403)
     elif not game.can_update:
         self.response.write(json.dumps({
             'game_id': key_db.urlsafe(),
             'status': 'started',
             'version': json.loads(game.game_json)['version']
         }))
         self.error(410)
     else:
         update = json.loads(self.request.get('json'))
         game_struct = json.loads(game.game_json)
         game_struct['version'] += 1
         if 'updated_words' in update:
             game_struct['words_last_update'] = game_struct['version']
             game_struct['words'] = update['updated_words']
         if 'updated_meta' in update:
             game_struct['meta'] = update['updated_meta']
             game_struct['meta']['last_update'] = game_struct['version']
         if 'updated_players' in update:
             for player in update['updated_players']:
                 found = False
                 for where in xrange(len(game_struct['players'])):
                     if player['id'] == game_struct['players'][where]['id']:
                         game_struct['players'][where] = player
                         game_struct['players'][where]['last_update'] = game_struct['version']
                         found = True
                 if not found:
                     game_struct['players'].append(player)
                     game_struct['players'][-1]['last_update'] = game_struct['version']
         if 'players_delete' in update:
             for player in update['players_delete']:
                 for was_player in game_struct['players']:
                     if player == was_player['id']:
                         game_struct['players'].remove(was_player)
                         game_struct['players_deleted'].append({'id': player,
                                                                'last_update': game_struct['version']})
                         break
         if 'updated_order' in update:
             game_struct['order_last_update'] = game_struct['version']
             game_struct['order'] = update['updated_order']
         changed = False
         for player_id in game_struct['order']:
             found = False
             for player in game_struct['players']:
                 if player['id'] == player_id:
                     found = True
             if not found:
                 changed = True
                 game_struct['order'].remove(player_id)
         for player in game_struct['players']:
             if player['id'] not in game_struct['order']:
                 game_struct['order'].append(player['id'])
                 changed = True
         if changed:
             game_struct['order_last_update'] = game_struct['version']
         game.game_json = json.dumps(game_struct)
         game.put()
         self.response.write(json.dumps({
             'id': key_db.urlsafe(),
             'version': game_struct['version'],
             'game': json.loads(PreGame.delete_last_updates_from_json(game.game_json))
         }))
예제 #6
0
 def post(self, **kwargs):
     #TODO: but if we have incorrect game_id?
     key_db = ndb.Key(urlsafe=kwargs.get('game_id'))
     game = key_db.get()
     if self.device_id not in game.device_ids:
         self.error(403)
     elif not game.can_update:
         self.response.write(
             json.dumps({
                 'game_id': key_db.urlsafe(),
                 'status': 'started',
                 'version': json.loads(game.game_json)['version']
             }))
         self.error(410)
     else:
         update = json.loads(self.request.get('json'))
         game_struct = json.loads(game.game_json)
         game_struct['version'] += 1
         if 'updated_words' in update:
             game_struct['words_last_update'] = game_struct['version']
             game_struct['words'] = update['updated_words']
         if 'updated_meta' in update:
             game_struct['meta'] = update['updated_meta']
             game_struct['meta']['last_update'] = game_struct['version']
         if 'updated_players' in update:
             for player in update['updated_players']:
                 found = False
                 for where in xrange(len(game_struct['players'])):
                     if player['id'] == game_struct['players'][where]['id']:
                         game_struct['players'][where] = player
                         game_struct['players'][where][
                             'last_update'] = game_struct['version']
                         found = True
                 if not found:
                     game_struct['players'].append(player)
                     game_struct['players'][-1][
                         'last_update'] = game_struct['version']
         if 'players_delete' in update:
             for player in update['players_delete']:
                 for was_player in game_struct['players']:
                     if player == was_player['id']:
                         game_struct['players'].remove(was_player)
                         game_struct['players_deleted'].append({
                             'id':
                             player,
                             'last_update':
                             game_struct['version']
                         })
                         break
         if 'updated_order' in update:
             game_struct['order_last_update'] = game_struct['version']
             game_struct['order'] = update['updated_order']
         changed = False
         for player_id in game_struct['order']:
             found = False
             for player in game_struct['players']:
                 if player['id'] == player_id:
                     found = True
             if not found:
                 changed = True
                 game_struct['order'].remove(player_id)
         for player in game_struct['players']:
             if player['id'] not in game_struct['order']:
                 game_struct['order'].append(player['id'])
                 changed = True
         if changed:
             game_struct['order_last_update'] = game_struct['version']
         game.game_json = json.dumps(game_struct)
         game.put()
         self.response.write(
             json.dumps({
                 'id':
                 key_db.urlsafe(),
                 'version':
                 game_struct['version'],
                 'game':
                 json.loads(
                     PreGame.delete_last_updates_from_json(game.game_json))
             }))