Ejemplo n.º 1
0
def new_game(game_id):
    """Save new game results to database"""

    game = Game()
    game.game_id = game_id
    game.game_host = 'https://xgs8.c.bytro.com/'

    supremacy = Supremacy(game.game_id, game.game_host)

    while True:
        try:
            result = supremacy.game()
        except ServerChangeError as exception:
            new_server = str(exception)
            game.game_host = new_server
            supremacy.url = new_server
            continue
        break

    _update_game(game, result)
    game.start_at = datetime.fromtimestamp(result["startOfGame"])
    game.password = result["password"]
    game.scenario = result["scenarioID"]
    game.ranked = result["ranked"]
    game.gold_round = result["goldRound"]
    game.ai_level = result["aiLevel"]
    game.country_selection = result["countrySelection"]
    game.time_scale = result["timeScale"]
    # game.team_setting = result["teamSettings"]
    game.victory_points = result["victoryPoints"]
    game.research_days_offset = result["researchDaysOffset"]
    if "researchTimeScale" in result:
        game.research_time_scale = result["researchTimeScale"]
    else:
        game.research_time_scale = 1.0
    game.team_victory_points = result["teamVictoryPoints"]

    game_map = Map.query.filter(Map.map_id == result["mapID"]).first()
    if game_map is None:
        game_map = Map()
        game_map.map_id = result["mapID"]
        game_map.name = result["mapID"]
        game_map.slots = result["openSlots"] + result["numberOfPlayers"]

        db.session.add(game_map)
        db.session.commit()

    game.map_id = game_map.id

    db.session.add(game)
    db.session.commit()

    return game
Ejemplo n.º 2
0
def get_game(game_id):
    """Get results from game"""
    payload = PAYLOAD_SAMPLE
    payload["gameID"] = game_id
    payload["stateType"] = 12

    game = Game.query.filter(Game.game_id == game_id).first()
    if game is None:
        game = Game()
        game.game_id = game_id
        game.game_host = URL

        db.session.add(game)
        db.session.commit()

    request = requests.post(game.game_host, headers=HEADERS, json=payload)

    text = json.loads(request.text)
    if not check_response(game_id, text):
        return get_game(game_id)

    return text["result"]