Coverage for models/live_match.py: 98.98%

98 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 15:03 +0000

1""" 

2Live Match Pydantic models. 

3 

4Models for managing live match state, clock, and events. 

5""" 

6 

7from datetime import datetime 

8 

9from pydantic import BaseModel, Field 

10 

11 

12class LiveMatchClock(BaseModel): 

13 """Model for clock management actions during a live match.""" 

14 

15 action: str = Field( 

16 ..., 

17 description="Clock action: start_first_half, start_halftime, etc.", 

18 ) 

19 half_duration: int | None = Field( 

20 None, 

21 ge=20, 

22 le=60, 

23 description="Duration of each half in minutes (only for start_first_half)", 

24 ) 

25 occurred_at: datetime | None = Field( 

26 None, 

27 description="When the action actually happened (offline clients syncing late); defaults to now", 

28 ) 

29 

30 @property 

31 def valid_actions(self) -> list[str]: 

32 return [ 

33 "start_first_half", 

34 "start_halftime", 

35 "cancel_halftime", 

36 "start_second_half", 

37 "cancel_second_half", 

38 "end_match", 

39 ] 

40 

41 

42class GoalEvent(BaseModel): 

43 """Model for recording a goal event.""" 

44 

45 team_id: int = Field(..., description="ID of the team that scored") 

46 player_name: str | None = Field(None, max_length=200, description="Name of the goal scorer (legacy)") 

47 player_id: int | None = Field(None, description="ID of the goal scorer from roster (preferred)") 

48 assist_player_id: int | None = Field(None, description="ID of the assisting player from roster") 

49 message: str | None = Field(None, max_length=500, description="Optional description") 

50 match_minute: int | None = Field( 

51 None, ge=0, le=130, description="Minute when scored (offline clients); defaults to clock-derived" 

52 ) 

53 extra_time: int | None = Field(None, ge=0, le=30, description="Stoppage time minutes (with match_minute)") 

54 client_event_id: str | None = Field( 

55 None, max_length=36, description="Client-generated UUID for idempotent retries" 

56 ) 

57 

58 

59class LiveCardEvent(BaseModel): 

60 """Model for recording a card event during a live match.""" 

61 

62 team_id: int = Field(..., description="ID of the team the player belongs to") 

63 player_id: int | None = Field(None, description="ID of the carded player from roster (preferred)") 

64 player_name: str | None = Field( 

65 None, max_length=200, description="Free-text player name/number when no roster is available" 

66 ) 

67 card_type: str = Field(..., pattern="^(yellow_card|red_card)$", description="Type of card: yellow_card or red_card") 

68 message: str | None = Field(None, max_length=500, description="Optional description (e.g., reason for card)") 

69 match_minute: int | None = Field( 

70 None, ge=0, le=130, description="Minute of the card (offline clients); defaults to clock-derived" 

71 ) 

72 extra_time: int | None = Field(None, ge=0, le=30, description="Stoppage time minutes (with match_minute)") 

73 client_event_id: str | None = Field( 

74 None, max_length=36, description="Client-generated UUID for idempotent retries" 

75 ) 

76 

77 

78class LiveSubstitutionEvent(BaseModel): 

79 """Model for recording a substitution during a live match.""" 

80 

81 team_id: int = Field(..., description="ID of the team making the substitution") 

82 player_in_id: int = Field(..., description="ID of the player coming on") 

83 player_out_id: int = Field(..., description="ID of the player coming off") 

84 match_minute: int | None = Field( 

85 None, ge=0, le=130, description="Minute of the sub (offline clients); defaults to clock-derived" 

86 ) 

87 extra_time: int | None = Field(None, ge=0, le=30, description="Stoppage time minutes (with match_minute)") 

88 client_event_id: str | None = Field( 

89 None, max_length=36, description="Client-generated UUID for idempotent retries" 

90 ) 

91 

92 

93class LivePositionChangeEvent(BaseModel): 

94 """Model for recording a player changing position during a live match (SB-1351).""" 

95 

96 team_id: int = Field(..., description="ID of the team the player belongs to") 

97 player_id: int = Field(..., description="ID of the player changing position") 

98 position: str = Field( 

99 ..., min_length=1, max_length=10, description="Formation position code the player moves to (e.g. ST, RW, LCB)" 

100 ) 

101 match_minute: int | None = Field( 

102 None, ge=0, le=130, description="Minute of the change (offline clients); defaults to clock-derived" 

103 ) 

104 extra_time: int | None = Field(None, ge=0, le=30, description="Stoppage time minutes (with match_minute)") 

105 client_event_id: str | None = Field( 

106 None, max_length=36, description="Client-generated UUID for idempotent retries" 

107 ) 

108 

109 

110class MessageEvent(BaseModel): 

111 """Model for posting a chat message.""" 

112 

113 message: str = Field(..., min_length=1, max_length=500, description="Message content") 

114 client_event_id: str | None = Field( 

115 None, max_length=36, description="Client-generated UUID for idempotent retries" 

116 ) 

117 

118 

119class GoalEventUpdate(BaseModel): 

120 """Model for updating a goal event (admin corrections).""" 

121 

122 match_minute: int | None = Field(None, ge=0, le=120, description="Match minute when goal was scored") 

123 extra_time: int | None = Field(None, ge=0, le=30, description="Extra/stoppage time minutes") 

124 player_name: str | None = Field(None, max_length=200, description="Name of the goal scorer") 

125 player_id: int | None = Field(None, description="ID of the goal scorer from roster") 

126 assist_player_id: int | None = Field(None, description="ID of the assisting player from roster") 

127 

128 

129class MatchEventResponse(BaseModel): 

130 """Response model for a match event.""" 

131 

132 id: int 

133 match_id: int 

134 event_type: str 

135 team_id: int | None = None 

136 player_name: str | None = None 

137 player_id: int | None = None 

138 player_out_id: int | None = None 

139 position: str | None = None 

140 assist_player_id: int | None = None 

141 assist_player_name: str | None = None 

142 match_minute: int | None = None 

143 extra_time: int | None = None 

144 message: str 

145 created_by: str | None = None 

146 created_by_username: str | None = None 

147 created_at: datetime 

148 is_deleted: bool = False 

149 client_event_id: str | None = None 

150 

151 

152class LiveMatchState(BaseModel): 

153 """Full live match state returned to clients.""" 

154 

155 match_id: int 

156 match_status: str 

157 home_score: int | None = None 

158 away_score: int | None = None 

159 

160 # Clock timestamps 

161 kickoff_time: datetime | None = None 

162 halftime_start: datetime | None = None 

163 second_half_start: datetime | None = None 

164 match_end_time: datetime | None = None 

165 half_duration: int = 45 # Minutes per half (default 45 for 90-min games) 

166 

167 # Team info 

168 home_team_id: int 

169 home_team_name: str 

170 away_team_id: int 

171 away_team_name: str 

172 

173 # Match metadata 

174 match_date: str 

175 age_group_name: str | None = None 

176 match_type_name: str | None = None 

177 division_name: str | None = None 

178 

179 # Recent events (last N) 

180 recent_events: list[MatchEventResponse] = [] 

181 

182 # All substitution + position_change events, oldest first, unpaginated 

183 # and chat-free (SB-1369). Populated only by GET /api/matches/{id}/live; 

184 # the write endpoints that echo state back (clock, goal) carry neither 

185 # this nor recent_events. 

186 pitch_events: list[MatchEventResponse] = [] 

187 

188 

189class LiveMatchSummary(BaseModel): 

190 """Summary info for the live matches list (for tab polling).""" 

191 

192 match_id: int 

193 match_status: str 

194 home_team_name: str 

195 away_team_name: str 

196 home_score: int | None = None 

197 away_score: int | None = None 

198 kickoff_time: datetime | None = None 

199 match_date: str