Coverage for zombie_nomnom_api/graphql_app/resolvers.py: 100%
93 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-07 04:25 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-07 04:25 +0000
1from zombie_nomnom import Command, Die, DieBag, DieColor, Face, Player, RoundState
2from zombie_nomnom.engine import DrawDice, Score
3from zombie_nomnom_api.game import Game, GameMakerInterface
4from zombie_nomnom_api.graphql_app.dependencies import DIContainer
5from .schema import (
6 Query,
7 Mutation,
8 GameResource,
9 Move,
10 DieResource,
11 DieBagResource,
12 PlayerResource,
13 Round,
14)
15from .dependencies import bootstrap
18@Query.field("games")
19def games_resolver(_, __, id: str = None, dependencies: DIContainer = bootstrap()):
20 maker: GameMakerInterface = dependencies[GameMakerInterface]
21 if id is not None:
22 value = maker[id]
23 return [value] if value else []
24 return list(maker)
27@Mutation.field("createGame")
28def create_game_resolver(
29 _, __, players: list[str], dependencies: DIContainer = bootstrap()
30):
31 if len(players) == 0:
32 return {"errors": ["No players provided"], "game": None}
33 maker: GameMakerInterface = dependencies[GameMakerInterface]
34 game = maker.make_game(players)
35 return {"errors": [], "game": game}
38@Mutation.field("drawDice")
39def draw_dice_resolver(_, __, gameId: str, dependencies: DIContainer = bootstrap()):
41 if gameId is None:
42 return {"errors": ["No game id provided"], "round": None}
43 maker: GameMakerInterface = dependencies[GameMakerInterface]
44 dice: DrawDice = dependencies[DrawDice]
45 gameInstance: Game = maker[gameId]
46 if gameInstance is None:
47 return {"errors": [f"Game id not found: {gameId}"], "round": None}
48 return {"errors": [], "round": gameInstance.game.process_command(dice)}
51@Mutation.field("endRound")
52def end_round_resolver(_, __, gameId: str, dependencies: DIContainer = bootstrap()):
53 if gameId is None:
54 return {"errors": ["No game id provided"], "round": None}
55 maker: GameMakerInterface = dependencies[GameMakerInterface]
56 score: Score = dependencies[Score]
57 gameInstance: Game = maker[gameId]
58 if gameInstance is None:
59 return {"errors": [f"Game id not found: {gameId}"], "round": None}
60 return {"errors": [], "round": gameInstance.game.process_command(score)}
63@GameResource.field("gameOver")
64def game_over_resolver(game: Game, _):
65 instance = game.game
66 return instance.game_over
69@GameResource.field("winner")
70def game_winner_resolver(game: Game, _):
71 instance = game.game
72 if instance.game_over:
73 return instance.winner
74 return None
77@GameResource.field("round")
78def game_round_resolver(game: Game, _):
79 instance = game.game
80 return instance.round
83@GameResource.field("players")
84def game_players_resolver(game: Game, _):
85 instance = game.game
86 return instance.players
89@GameResource.field("moves")
90def game_moves_resolver(game: Game, _):
91 instance = game.game
92 return instance.commands
95@Move.field("name")
96def move_name_resolver(move: tuple[Command, RoundState], _):
97 [command, _] = move
98 return type(command).__name__
101@Move.field("player")
102def move_player_resolver(move: tuple[Command, RoundState], _):
103 [_, state] = move
104 return state.player
107@DieResource.field("sides")
108def die_sides_resolver(die: Die, _):
109 return die.faces
112@DieResource.field("color")
113def die_color_resolver(die: Die, _):
114 brains = len([face for face in die.faces if face == Face.BRAIN])
115 # this is a hack because the library does not provide a name directly.
116 if brains >= 3:
117 return DieColor.GREEN
118 elif brains == 2:
119 return DieColor.YELLOW
120 return DieColor.RED
123@DieResource.field("currentFace")
124def die_current_face_resolver(die: Die, _):
125 return die.current_face
128@DieBagResource.field("drawnDice")
129def die_bag_draw_die_resolver(die_bag: DieBag, _):
130 return die_bag.drawn_dice
133@PlayerResource.field("score")
134def player_score_resolver(player: Player, _):
135 return player.total_brains
138@Round.field("points")
139def round_points_resolver(round: RoundState, _):
140 # Might be better if the lib provided a count instead..
141 return len(round.player.brains)