Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.20 on 2025-05-20 03:05

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("chat", "0002_livechatmessagereaction"),
]

operations = [
migrations.AlterField(
model_name="livechatmessagereaction",
name="content",
field=models.CharField(help_text="Up to 10 emoji characters", max_length=10),
),
]
3 changes: 2 additions & 1 deletion src/chigame/games/fixtures/checkers-fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@
1,
0
]
]
],
"current_turn_player": 1
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.20 on 2025-05-20 03:05

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("games", "0038_merge_20250519_0011"),
]

operations = [
migrations.AddField(
model_name="checkersboard",
name="current_turn_player",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="current_turn_boards",
to="games.player",
),
),
]
4 changes: 4 additions & 0 deletions src/chigame/games/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ class CheckersBoard(models.Model):
"""

state = models.JSONField() # store positions/pieces as a 2D array
current_turn_player = models.ForeignKey(
"games.Player", on_delete=models.CASCADE, null=True, blank=True, related_name="current_turn_boards"
)
# state_bits = models.IntegerField() # stores positions as bits

def __str__(self):
return f"Board {self.id}"
Expand Down
22 changes: 15 additions & 7 deletions src/chigame/games/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,7 @@ def checkers_game_view(request, pk):
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
]
# Save first turn
board = CheckersBoard.objects.create(state=default_state)
board = CheckersBoard.objects.create(state=default_state, current_turn_player=game.player_1)
CheckersTurn.objects.create(game=game, board=board, turn_number=1, player=game.player_1)

# Determine player ID for frontend
Expand All @@ -1686,6 +1685,7 @@ def checkers_game_view(request, pk):
"board_id": board.id,
"player_id": player.id,
"turn_number": turn_number,
"current_turn_player_id": board.current_turn_player.id if board.current_turn_player else None,
},
)

Expand All @@ -1695,25 +1695,33 @@ def checkers_game_update_board_state(request, board_id):
try:
board = CheckersBoard.objects.get(pk=board_id)
new_state = request.data.get("state")
next_player_id = request.data.get("next_player_id")

if new_state is None:
return Response({"error": "Missing 'state'"}, status=status.HTTP_400_BAD_REQUEST)
if new_state is None or next_player_id is None:
return Response({"error": "Missing 'state' or 'next_player_id'"}, status=status.HTTP_400_BAD_REQUEST)

board.state = new_state
board.current_turn_player_id = next_player_id
board.save()

return Response({"success": True})

except CheckersBoard.DoesNotExist:
return Response({"error": "Board not found"}, status=status.HTTP_404_NOT_FOUND)
return Response({"error": "Board not found"}, status=404)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({"error": str(e)}, status=500)


@api_view(["GET"])
def checkers_game_get_board_state(request, board_id):
try:
board = CheckersBoard.objects.get(pk=board_id)
return Response({"state": board.state})
return Response(
{
"state": board.state,
"current_turn_player_id": board.current_turn_player.id if board.current_turn_player else None,
}
)
except CheckersBoard.DoesNotExist:
return Response({"error": "Board not found"}, status=404)

Expand Down
Loading
Loading