Ejemplo n.º 1
0
def on_player_build_facility_button(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()

    if game.state not in ["initializing", "runturn", "waiting", "finished"]:
        if company.state in ["view"]:
            company.state = "build"
            shout_player_state(game.id, company.state, [company.player_number])
        elif company.state in ["build"]:
            company.state = "view"
            shout_player_state(game.id, company.state, [company.player_number])
        elif company.state in ["waiting", "ready"]:
            shout_players_message(
                game.id,
                "You can't toggle the build facility button while you are waiting for the next turn to run.",
                [company.player_number])
    else:
        shout_players_message(
            game.id,
            "You can't toggle the build facility button during this phase of the game.",
            [company.player_number])

    db.session.commit()
    return
Ejemplo n.º 2
0
def on_player_is_ready(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()
    company.state = "ready"
    db.session.commit()

    companies_not_ready = Company.query.filter(
        Company.id_game == game.id, Company.state != "ready",
        Company.player_type == "human").all()

    if len(companies_not_ready) == 0:
        game.state = "runturn"
        db.session.commit()
        shout_game_state(game.id, game.state)
        shout_player_state(game.id, "ready", players=[1, 2, 3, 4, 5])

        msg = "Running the next turn!"
        shout_players_message(game.id, msg)

        shout_run_game_turn(game.id)
        run_turn(game)

    return
Ejemplo n.º 3
0
def on_player_next_turn(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()

    if game.state == "runturn":
        shout_players_message(game.id, "The next turn is currently running.",
                              [company.player_number])
        return

    if company.state != "waiting":
        msg = f"Company, {company.name}, is ready for the next turn."
        company.state = "waiting"
        shout_players_message(game.id, msg)
    else:
        msg = f"Company, {company.name}, is NOT ready for the next turn."
        company.state = "view"
        shout_players_message(game.id, msg)

    db.session.commit()

    shout_player_state(game.id, company.state, [company.player_number])
    companies_not_waiting = Company.query.filter(
        Company.id_game == game.id, Company.state != "waiting",
        Company.player_type == "human").all()

    if len(companies_not_waiting) == 0:
        shout_ready_to_run_turn(game.id)
    else:
        msg = "The following companies are NOT ready for the next turn: " + ", ".join(
            [company.name for company in companies_not_waiting])
        shout_players_message(game.id, msg)

    return
Ejemplo n.º 4
0
def on_join_gameroom(data):
    data = json.loads(data)
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()
    company_serialized = CompanySchema().dump(company).data
    join_room("game" + str(data['gameId']))

    if not company.joined_game:
        company.joined_game = True
        db.session.commit()
        shout_players_message(data['gameId'],
                              "Welcome to Energize! A power grid simulator.",
                              [company.player_number])
        shout_company_joined_game(data['gameId'], company_serialized)

    return
Ejemplo n.º 5
0
def force_run_turn(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()

    game.state = "runturn"
    Company.query.filter_by(id_game=game.id).update(dict(state="ready"))

    db.session.commit()
    shout_game_state(game.id, game.state)
    shout_player_state(game.id, "ready", players=[1, 2, 3, 4, 5])

    msg = "Running the next turn!"
    shout_players_message(game.id, msg)

    shout_run_game_turn(game.id)
    run_turn(game)

    return
Ejemplo n.º 6
0
def on_cancel_run_turn(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()

    companies_updated = Company.query.filter_by(id_game=data['gameId']).update(
        dict(state='waiting'))
    company.state = "view"

    db.session.commit()
    companies = Company.query.filter_by(id_game=data['gameId']).all()

    for c in companies:
        shout_player_state(game.id, c.state, [c.player_number])

    shout_player_cancel_run_turn(game.id)
    msg = f"Company, {company.name}, is NOT ready for the next turn."
    shout_players_message(game.id, msg)

    return