async def confirm_game_handler(websocket, gid): try: gdb = db.pull(G_DB) if (gdb[gid]["status"] == "DELETED"): await websocket.send( '{"header":"ERROR", "content":"Room has been deleted"}') return game = Game.from_dict(gdb[gid]) # Disable for testing # if len(game.players) < game.min_players: # await websocket.send('{"header":"ERROR", "content":"Not enough players"}') # return game.assignTeam() game.status = "PREPARE" gdb[gid] = game.to_dict() db.push(gdb, G_DB) game = gdb[gid] room_info = {"status": game["status"], "players": game["players"]} await broadcast(gid, "ROOM_INFO", room_info) except Exception as e: print(e) await websocket.send( '{"header": "ERROR", "content": "Confirmation fail"}')
def gp_delete(gpid): gpdb = db.pull(GP_DB) try: del gpdb[gpid] except KeyError: return "GameParameter not found", 404 db.push(gpdb, GP_DB)
async def join_game_handler(websocket, _dict): try: gid = _dict["gid"] player = Player.from_dict(_dict["player"]) except KeyError: await websocket.send( '{"header":"ERROR", "content":"Join Game - Wrong Format"}') return try: gdb = db.pull(G_DB) if (gdb[gid]["status"] == "DELETED"): await websocket.send( '{"header":"ERROR", "content":"Room has been deleted"}') return game = Game.from_dict(gdb[gid]) if len(game.players) < game.max_players: game.players.append(player) gdb[gid] = game.to_dict() else: await websocket.send( '{"header":"ERROR", "content":"Room is fulled"}') return db.push(gdb, G_DB) await websocket.send('{"header": "JOINED", "content":' + json.dumps(gdb[gid]) + '}') await register(websocket, gid) game = gdb[gid] room_info = {"status": game["status"], "players": game["players"]} await broadcast(gid, "ROOM_INFO", room_info) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Game not found"}' )
async def add_handler(websocket, _dict): try: gdb = db.pull(G_DB) game = Game.from_dict(gdb[_dict["gid"]]) except Exception as e: print(e) await websocket.send( '{"header": "ERROR", "content": "Cannot find game"}') return try: game.incrementLevel(int(_dict["cid"]), _dict["team"]) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Incremnet fail"}' ) return try: await broadcast(_dict["gid"], "GAME_INFO", game.keyInfo) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Broadcast fail"}' ) return try: gdb[_dict["gid"]] = game.to_dict() db.push(gdb, G_DB) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Save game fail"}' )
async def save_checkpoints_handler(websocket, _dict): try: gid = _dict["gid"] cps = list() for cp in _dict["checkpoints"]: cps.append(Checkpoint.from_dict(cp)) except: await websocket.send( '{"header":"ERROR", "content":"Save checkpoints- Wrong Format"}') return try: gdb = db.pull(G_DB) if (gdb[gid]["status"] == "DELETED"): await websocket.send( '{"header":"ERROR", "content":"Room has been deleted"}') return game = Game.from_dict(gdb[gid]) game.checkpoints = cps gdb[gid] = game.to_dict() db.push(gdb, G_DB) await websocket.send('{"header": "CPS_SAVED", "content":true}') game = gdb[gid] room_info = { "status": game["status"], "checkpoints": game["checkpoints"] } await broadcast(gid, "ROOM_INFO", room_info) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Game not found"}' )
def gs_read_time(gsid, time): gsdb = db.pull(GS_DB) try: gs = GameSnapshot.from_dict(gsdb[gsid]) gs.fast_forward(time) gsdb[gsid] = gs.to_dict() except KeyError: return "GameSnapshot not found", 404 db.push(gsdb, GS_DB)
def gp_remove_player(gpid, pid): try: gpdb = db.pull(GP_DB) gp = GameParameters.from_dict(gpdb[gpid]) gp.players = [player for player in gp.players if player.pid != pid] gpdb[gpid] = gp.to_dict() db.push(gpdb, GP_DB) return "Player removed" except KeyError: return "GameParameter not found", 404
def gp_create(gpid): try: _dict = request.get_json(force=True) _dict["gpid"] = gpid gp = GameParameters.from_dict(_dict) except KeyError: return "GPID: Wrong Format", 400 gpdb = db.pull(GP_DB) gpdb[gpid] = gp.to_dict() db.push(gpdb, GP_DB) return "GameParameter successfully created", 200
def gs_process_movement(gsid): gsdb = db.pull(GS_DB) try: gs = GameSnapshot.from_dict(gsdb[gsid]) except KeyError: return "GameSnapshot not found", 404 try: _dict = request.get_json(force=True) movement = CheckpointMovement.from_dict(_dict) except KeyError: return "CheckpointMovement: Format Error", 400 gs.fast_forward(movement.time, (movement, )) gsdb[gsid] = gs.to_dict() db.push(gsdb, GS_DB)
def handle(job, *args, **kwargs): print 'handle', args, kwargs task = json.loads(job) url = task["url"] domain = tldextracter.extract_domain(url) status, content = fetch(url, use_proxy=False) try: url = url.encode('utf8') urlhash = cityhash.CityHash64(url) except: return (url, None, status, domain, content) logger.info('%s|%s' % (url, status)) if magic.from_buffer(content, mime=True) != 'text/html': return (url, urlhash, status, domain, content) _, content = encoding.html_to_unicode('', content) if status != 200: db.push(url, detail=False) return (url, urlhash, status, domain, content) return (url, urlhash, status, domain, content)
def gp_add_player(gpid): try: _dict = request.get_json(force=True) player = Player.from_dict(_dict) except KeyError: return "Player: Format Error", 400 try: gpdb = db.pull(GP_DB) gp = GameParameters.from_dict(gpdb[gpid]) if len(gp.players) < gp.max_players: gp.players.append(player) gpdb[gpid] = gp.to_dict() else: return "Room is full", 409 db.push(gpdb, GP_DB) except KeyError: return "GameParameter not found", 404
async def player_stats_handler(websocket, _dict): try: gdb = db.pull(G_DB) game = Game.from_dict(gdb[_dict["gid"]]) except Exception as e: print(e) await websocket.send( '{"header": "ERROR", "content": "Cannot find game"}') return try: flag = game.setPlayerStats(int(_dict["key"]), _dict["points"], _dict["dist"]) except Exception as e: print(e) await websocket.send( '{"header": "ERROR", "content": "Updatae stats fail"}') return try: gdb[_dict["gid"]] = game.to_dict() db.push(gdb, G_DB) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Save game fail"}' ) try: if flag: [distMVP, pointMVP] = game.findMVPs() game = gdb[_dict["gid"]] await broadcast( _dict["gid"], "ACCOUNT_FINISHED", { "players": game["players"], "winTeam": game["winTeam"], "distMVP": distMVP, "pointMVP": pointMVP, "startTime": game["startTime"], "endTime": game["endTime"] }) except Exception as e: print(e) await websocket.send('{"header": "ERROR", "content": "Broadcast fail"}' ) return
def gp_to_game(gpid): try: gpdb = db.pull(GP_DB) gp = GameParameters.from_dict(gpdb[gpid]) except KeyError: return "GameParameter not found", 404 if not gp.min_players <= len(gp.players) <= gp.max_players: return "Player count not fulfilled", 409 try: _dict = request.get_json(force=True) _dict["parameters"] = gp.to_dict() game = Game.from_dict(_dict) except KeyError: return "Game: Format Error", 400 del gpdb[gpid] db.push(gpdb, GP_DB) gdb = db.pull(G_DB) gdb[gpid] = game.to_dict() db.push(gdb, G_DB) gsdb = db.pull(GS_DB) gs = GameSnapshot.from_game(game) gsdb[gpid] = gs.to_dict() db.push(gsdb, GS_DB) return "Game Successfully Started", 200
async def create_game_handler(websocket, _dict): game = None try: gid = 0 gdb = db.pull(G_DB) assigned = False while (not assigned): gid = random.randint(100000, 999999) if gid not in gdb: _dict["gid"] = gid assigned = True game = Game.new_game(_dict) except Exception as e: print(e) await websocket.send( '{"header": "ERROR", "content": "Create game - Wrong Format"}') return gdb[gid] = game.to_dict() db.push(gdb, G_DB) await websocket.send('{"header": "CREATED", "content":' + json.dumps(gdb[gid]) + '}') await register(websocket, str(gid))