Adding completed_at and winners to g ames
This commit is contained in:
@@ -114,6 +114,8 @@ class Game(db.Model):
|
||||
current_question_index = db.Column(db.Integer, default=0) # Track current question
|
||||
is_template = db.Column(db.Boolean, default=False) # Mark as reusable template
|
||||
current_turn_team_id = db.Column(db.Integer, db.ForeignKey('teams.id'), nullable=True) # Track whose turn it is
|
||||
completed_at = db.Column(db.DateTime, nullable=True) # When game ended
|
||||
winners = db.Column(db.JSON, nullable=True) # [{"team_name": str, "score": int}, ...]
|
||||
|
||||
# Relationships
|
||||
teams = db.relationship('Team', back_populates='game', cascade='all, delete-orphan',
|
||||
@@ -146,7 +148,9 @@ class Game(db.Model):
|
||||
'total_questions': len(self.game_questions),
|
||||
'is_template': self.is_template,
|
||||
'current_turn_team_id': self.current_turn_team_id,
|
||||
'current_turn_team_name': self.current_turn_team.name if self.current_turn_team else None
|
||||
'current_turn_team_name': self.current_turn_team.name if self.current_turn_team else None,
|
||||
'completed_at': self.completed_at.isoformat() if self.completed_at else None,
|
||||
'winners': self.winners
|
||||
}
|
||||
|
||||
if include_questions:
|
||||
@@ -194,7 +198,7 @@ class Team(db.Model):
|
||||
phone_a_friend_count = db.Column(db.Integer, default=5) # Number of phone-a-friend lifelines
|
||||
|
||||
# Relationships
|
||||
game = db.relationship('Game', back_populates='teams')
|
||||
game = db.relationship('Game', back_populates='teams', foreign_keys=[game_id])
|
||||
scores = db.relationship('Score', back_populates='team', cascade='all, delete-orphan')
|
||||
|
||||
@property
|
||||
|
||||
@@ -4,6 +4,16 @@ from backend.models import db, Team
|
||||
bp = Blueprint('teams', __name__, url_prefix='/api/teams')
|
||||
|
||||
|
||||
@bp.route('/past-names', methods=['GET'])
|
||||
def get_past_team_names():
|
||||
"""Get distinct team names from past games"""
|
||||
past_names = db.session.query(Team.name)\
|
||||
.distinct()\
|
||||
.order_by(Team.name)\
|
||||
.all()
|
||||
return jsonify([name[0] for name in past_names]), 200
|
||||
|
||||
|
||||
@bp.route('/<int:team_id>', methods=['DELETE'])
|
||||
def delete_team(team_id):
|
||||
"""Delete a team"""
|
||||
|
||||
@@ -203,20 +203,33 @@ def reset_timer(game, socketio_instance):
|
||||
|
||||
|
||||
def end_game(game, socketio_instance):
|
||||
"""End/deactivate a game"""
|
||||
"""End/deactivate a game and record winners"""
|
||||
from datetime import datetime
|
||||
|
||||
# Calculate winners (team(s) with highest score)
|
||||
if game.teams:
|
||||
team_scores = [(team.name, team.total_score) for team in game.teams]
|
||||
if team_scores:
|
||||
max_score = max(score for _, score in team_scores)
|
||||
winners = [
|
||||
{"team_name": name, "score": score}
|
||||
for name, score in team_scores
|
||||
if score == max_score
|
||||
]
|
||||
game.winners = winners
|
||||
|
||||
game.is_active = False
|
||||
game.completed_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
# Emit game_ended event to all rooms
|
||||
socketio_instance.emit('game_ended', {
|
||||
# Emit game_ended event with winners
|
||||
end_data = {
|
||||
'game_id': game.id,
|
||||
'game_name': game.name
|
||||
}, room=f'game_{game.id}_contestant')
|
||||
|
||||
socketio_instance.emit('game_ended', {
|
||||
'game_id': game.id,
|
||||
'game_name': game.name
|
||||
}, room=f'game_{game.id}_admin')
|
||||
'game_name': game.name,
|
||||
'winners': game.winners
|
||||
}
|
||||
socketio_instance.emit('game_ended', end_data, room=f'game_{game.id}_contestant')
|
||||
socketio_instance.emit('game_ended', end_data, room=f'game_{game.id}_admin')
|
||||
|
||||
|
||||
def restart_game(game, socketio_instance):
|
||||
|
||||
Reference in New Issue
Block a user