async def play(proc: ProcM, game_id: int, user=Depends(manager)): with db_session: game = Game.get(id=game_id) if game is None: raise HTTPException(status_code=404, detail="Game not found") current_player = Player.user_player(user, game_id) if game.status["phase"] == "minister play": if current_player["current_position"] != "minister": raise HTTPException(status_code=404, detail="This player is not the minister") cards = game.board.deck.split(',')[:3] if proc.card in cards: cards = game.board.deck.split(',') cards.remove(proc.card) game.board.deck = ','.join(cards) else: raise HTTPException( status_code=400, detail="The input card was not one of the options") msg = f'{proc.card} card discarded successfully' # PASS THE TURN ##################### game.status["phase"] = "headmaster play" ##################################### elif game.status["phase"] == "headmaster play": if current_player["current_position"] != "headmaster": raise HTTPException(status_code=404, detail="This player is not the headmaster") cards = game.board.deck.split(',')[:2] if proc.card in cards: cards = game.board.deck.split(',')[2:] game.board.deck = ','.join(cards) if proc.card == 'phoenix': game.board.po_proc += 1 else: game.board.de_proc += 1 # IMPORTANT! HERE GOES THE LOGIC FOR SPELL ACTIVATION # PASS THE TURN ########### spell_fields = game.board.spell_fields.split(",") spells = ["divination", "avadakedavra", "imperius", "crucio"] if game.board.de_proc != 0 and spell_fields[game.board.de_proc - 1] in spells: game.status["phase"] = "spell play" else: Player.reassign_minister(game) ##################################### msg = f'{proc.card} card played successfully' else: raise HTTPException( status_code=400, detail="The input card was not one of the options") else: raise HTTPException( status_code=400, detail="It is not a phase for playing a proclamation") return {"message": msg}
async def end_turn(game_id: int, user=Depends(manager)): with db_session: game = Game.get(id=game_id) if game is None: raise HTTPException(status_code=404, detail="Game not found") if not game.started: raise HTTPException(status_code=400, detail="Game is not started") Player.reassign_minister(game) return {"message": "Turn ended!"}
async def expelliarmus(in_vote: VoteM, game_id: int, user=Depends(manager)): with db_session: game = Game.get(id=game_id) current_player = Player.user_player(user, game_id) username = User.get(id=current_player["user"]).username detail = f"The player {username} has played expelliarmus!" if game is None: raise HTTPException(status_code=404, detail="Game not found") if not game.started: raise HTTPException(status_code=400, detail="Game is not started") if current_player["current_position"] != "minister" and current_player[ "current_position"] != "headmaster": raise HTTPException( status_code=400, detail="this player is not the minister nor the headmaster") if game.board.de_proc < 5 or game.status["phase"] != "headmaster play": raise HTTPException(status_code=400, detail="It is not time for expelliarmus!!!") if "headmaster_expelliarmus" not in game.status.keys(): if current_player["current_position"] == 'headmaster': game.status["headmaster_expelliarmus"] = in_vote.vote else: raise HTTPException( status_code=400, detail="The headmaster must play the expelliarmus first!") else: if current_player["current_position"] == 'minister': game.status["minister_expelliarmus"] = in_vote.vote else: raise HTTPException( status_code=400, detail="The minister must confirm the expelliarmus!") detail = "The expelliarmus has failed!" if game.status["minister_expelliarmus"] and game.status[ "headmaster_expelliarmus"]: cards = game.board.deck.split(',')[2:] game.board.deck = ','.join(cards) detail = "the expelliarmus was played succesfully!, cards discarded" Player.reassign_minister(game) return {detail}
async def kill_player(player_id: PlayerM, game_id: int, user=Depends(manager)): with db_session: game = Game.get(id=game_id) current_player = Player.user_player(user, game_id) victim_player = Player.select( lambda p: p.id == player_id.id and p.game.id == game_id).first() deck = game.board.spell_fields.split(",") if game is None: raise HTTPException(status_code=404, detail="Game not found") if game.status["phase"] != "spell play": raise HTTPException(status_code=400, detail="Its not time for playing spells!") if not game.board.de_proc or deck[game.board.de_proc - 1] != 'avadakedavra': raise HTTPException( status_code=400, detail="The avadakedavra spell is not available") if not victim_player: raise HTTPException( status_code=400, detail="The victim player does not belong to this game") if current_player["current_position"] != "minister": raise HTTPException(status_code=404, detail="This player is not the minister") victim_player.alive = False if victim_player.is_voldemort: game.status = { "info": "game ended", "winner": "Phoenix Order", "detail": "voldemort killed" } else: Player.reassign_minister(game) victim_user = User.select( lambda u: u.id == victim_player.user.id).first() return { "avadakedavra": "succeed!", "dead_player_id": player_id.id, "dead_player_alias": victim_user.useralias }
async def vote(in_vote: VoteM, game_id: int, user=Depends(manager)): with db_session: game = Game.get(id=game_id) if game is None: raise HTTPException(status_code=404, detail="Game not found") if game.status["phase"] != "vote": raise HTTPException(status_code=400, detail="It is not the vote phase") current_player = Player.user_player(user, game_id) vote_arr = [{ "player": current_player["id"], "user": user["id"], "username": user["username"], "vote": in_vote.vote }] if 'votes' in game.status.keys(): for v in game.status["votes"]: if v["user"] == user["id"]: raise HTTPException(status_code=400, detail="This player already voted") game.status["votes"] = game.status["votes"] + vote_arr else: game.status["votes"] = vote_arr pid = current_player["id"] username = user["username"] player_msg = f"Player: {pid} ({username}) successfully voted" general_msg = "election in progress" if len(game.status["votes"]) == game.players.select( lambda p: p.alive).count(): nox_votes = 0 lumos_votes = 0 for v in game.status["votes"]: if v["vote"]: lumos_votes += 1 else: nox_votes += 1 if lumos_votes > nox_votes: # PASS THE TURN ###################### game.board.caos = 0 game.status["phase"] = "minister play" general_msg = "election succeed" new_hm = Player.get(id=game.status["headmaster"]) if new_hm and (game.player_amount == 5 or game.player_amount == 6) \ and game.board.de_proc > 3 and new_hm.is_voldemort: game.status = { "info": "game ended", "winner": "Death Eaters", "detail": "voldemort headmaster" } return {"vote": player_msg, "election": general_msg} ###################################### else: # PASS THE TURN ##################### game.board.caos = game.board.caos + 1 general_msg = "election failed" # ACTIONS TO DO IF CAOS IS EQUAL TO 3 if game.board.caos == 3: deck = game.board.deck.split(',') first_card = deck[:1] if first_card[0] == 'death': game.board.de_proc += 1 else: game.board.po_proc += 1 if 'caos' in game.status.keys(): game.status["caos"] = game.status["caos"] + first_card else: game.status["caos"] = first_card game.board.deck = ','.join(deck[1:]) game.board.caos = 0 general_msg = "government caos" Player.reset_choosable() ##################################### Player.reassign_minister(game) return {"vote": player_msg, "election": general_msg}