Skip to content

Commit ae14793

Browse files
committed
Adding doc strings
1 parent 93586a6 commit ae14793

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

core/services.py

+100
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,31 @@
1010

1111

1212
class GameService:
13+
"""Service class to handle game logic."""
14+
1315
@staticmethod
1416
def initialize_cells(game):
17+
"""
18+
Create the cells of the game, place the mines, and calculate adjacencies.
19+
20+
Args:
21+
game (Game): The game instance for which cells are being initialized.
22+
"""
1523
cells = GameService._create_cells(game)
1624
GameService._place_mines(game, cells)
1725
GameService._calculate_adjacencies(cells)
1826

1927
@staticmethod
2028
def _create_cells(game):
29+
"""
30+
Create game cells using bulk_create to optimize database access.
31+
32+
Args:
33+
game (Game): The game instance for which cells are being created.
34+
35+
Returns:
36+
QuerySet: A queryset of the created cells.
37+
"""
2138
cells = [
2239
Cell(game=game, row=row, column=col)
2340
for row in range(game.rows)
@@ -28,13 +45,26 @@ def _create_cells(game):
2845

2946
@staticmethod
3047
def _place_mines(game, cells):
48+
"""
49+
Randomly place mines in the game cells.
50+
51+
Args:
52+
game (Game): The game instance for which mines are being placed.
53+
cells (QuerySet): The queryset of cells in the game.
54+
"""
3155
mine_cells = sample(list(cells), game.mines)
3256
for mine in mine_cells:
3357
mine.is_mine = True
3458
Cell.objects.bulk_update(mine_cells, ["is_mine"])
3559

3660
@staticmethod
3761
def _calculate_adjacencies(cells):
62+
"""
63+
Calculate the number of adjacent mines for each cell.
64+
65+
Args:
66+
cells (QuerySet): The queryset of cells in the game.
67+
"""
3868
updates = []
3969
for cell in cells:
4070
if not cell.is_mine:
@@ -44,18 +74,49 @@ def _calculate_adjacencies(cells):
4474

4575
@staticmethod
4676
def _calculate_adjacent_mines(cell):
77+
"""
78+
Calculate the number of mines adjacent to a given cell.
79+
80+
Args:
81+
cell (Cell): The cell for which adjacent mines are being calculated.
82+
83+
Returns:
84+
int: The number of adjacent mines.
85+
"""
4786
neighbors = Cell.objects.get_neighbors(cell)
4887
return sum(1 for neighbor in neighbors if neighbor.is_mine)
4988

5089
@staticmethod
5190
def _get_cell(game, row, column):
91+
"""
92+
Retrieve a cell by its row and column in a given game.
93+
94+
Args:
95+
game (Game): The game instance.
96+
row (int): The row number of the cell.
97+
column (int): The column number of the cell.
98+
99+
Returns:
100+
Cell or None: The cell if found, otherwise None.
101+
"""
52102
try:
53103
return Cell.objects.get(game=game, row=row, column=column)
54104
except Cell.DoesNotExist:
55105
return None
56106

57107
@staticmethod
58108
def reveal_cell(game, row, column):
109+
"""
110+
Reveal a cell and handle game logic for revealing cells.
111+
112+
Args:
113+
game (Game): The game instance.
114+
row (int): The row number of the cell to reveal.
115+
column (int): The column number of the cell to reveal.
116+
117+
Returns:
118+
tuple: A tuple containing the response data and HTTP status code.
119+
"""
59120
cell = GameService._get_cell(game, row, column)
60121
if not cell:
61122
return CELL_NOT_FOUND, HTTP_404_NOT_FOUND
@@ -77,6 +138,12 @@ def reveal_cell(game, row, column):
77138

78139
@staticmethod
79140
def _reveal_cells(cell):
141+
"""
142+
Recursively reveal cells starting from the given cell.
143+
144+
Args:
145+
cell (Cell): The cell to start revealing from.
146+
"""
80147
if cell.is_revealed:
81148
return
82149
cell.is_revealed = True
@@ -89,13 +156,26 @@ def _reveal_cells(cell):
89156

90157
@staticmethod
91158
def _end_game(game, status):
159+
"""
160+
End the game with a given status and reveal all cells.
161+
162+
Args:
163+
game (Game): The game instance.
164+
status (GameStatus): The status to set for the game.
165+
"""
92166
game.status = status
93167
game.finished_at = now()
94168
game.save()
95169
GameService._reveal_all_cells(game)
96170

97171
@staticmethod
98172
def _reveal_all_cells(game):
173+
"""
174+
Reveal all cells in the game.
175+
176+
Args:
177+
game (Game): The game instance.
178+
"""
99179
cells = Cell.objects.filter(game=game)
100180
with transaction.atomic():
101181
for cell in cells:
@@ -104,12 +184,32 @@ def _reveal_all_cells(game):
104184

105185
@staticmethod
106186
def _check_win_condition(game):
187+
"""
188+
Check if the win condition is met for the game.
189+
190+
Args:
191+
game (Game): The game instance.
192+
193+
Returns:
194+
bool: True if the win condition is met, False otherwise.
195+
"""
107196
return not Cell.objects.filter(
108197
game=game, is_revealed=False, is_mine=False
109198
).exists()
110199

111200
@staticmethod
112201
def toggle_flag(game, row, column):
202+
"""
203+
Toggle the flag status of a cell.
204+
205+
Args:
206+
game (Game): The game instance.
207+
row (int): The row number of the cell to flag/unflag.
208+
column (int): The column number of the cell to flag/unflag.
209+
210+
Returns:
211+
tuple: A tuple containing the response data and HTTP status code.
212+
"""
113213
cell = GameService._get_cell(game, row, column)
114214
if not cell:
115215
return CELL_NOT_FOUND, HTTP_404_NOT_FOUND

core/views.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class GameViewSet(ModelViewSet):
1414
serializer_class = GameSerializer
1515

1616
def perform_create(self, serializer):
17+
"""Initialize the cells of the game after creation."""
1718
game = serializer.save()
1819
GameService.initialize_cells(game)
1920

2021
def _process_cell_action(self, request, cell_action):
22+
"""Process a cell action (flag or reveal) on a game."""
2123
row = request.data.get("row")
2224
column = request.data.get("column")
2325
game = self.get_object()
@@ -30,10 +32,12 @@ def _process_cell_action(self, request, cell_action):
3032

3133
@action(detail=True, methods=["post"])
3234
def flag(self, request, pk=None):
35+
"""Action to flag a cell in the game."""
3336
data, status_code = self._process_cell_action(request, GameService.toggle_flag)
3437
return Response(data, status=status_code)
3538

3639
@action(detail=True, methods=["post"])
3740
def reveal(self, request, pk=None):
41+
"""Action to reveal a cell in the game."""
3842
data, status_code = self._process_cell_action(request, GameService.reveal_cell)
3943
return Response(data, status=status_code)

0 commit comments

Comments
 (0)