You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClub/PadelClub/Data/TeamScore.swift

70 lines
1.8 KiB

//
// TeamScore.swift
// Padel Tournament
//
// Created by razmig on 10/03/2024.
//
import Foundation
import LeStorage
@Observable
class TeamScore: ModelObject, Storable {
static func resourceName() -> String { "team-scores" }
static func tokenExemptedMethods() -> [HTTPMethod] { return [] }
var id: String = Store.randomId()
var match: String
var teamRegistration: String?
var playerRegistrations: [String] = []
var score: String?
var walkOut: Int?
var luckyLoser: Int?
init(match: String, teamRegistration: String? = nil, playerRegistrations: [String], score: String? = nil, walkOut: Int? = nil, luckyLoser: Int? = nil) {
self.match = match
self.teamRegistration = teamRegistration
self.playerRegistrations = playerRegistrations
self.score = score
self.walkOut = walkOut
self.luckyLoser = luckyLoser
}
init(match: String, team: TeamRegistration?) {
self.match = match
if let team {
self.teamRegistration = team.id
self.playerRegistrations = team.players().map { $0.id }
}
self.score = nil
self.walkOut = nil
self.luckyLoser = nil
}
func isWalkOut() -> Bool {
walkOut != nil
}
func matchObject() -> Match? {
Store.main.findById(match)
}
var team: TeamRegistration? {
guard let teamRegistration else {
return nil
}
return DataStore.shared.teamRegistrations.findById(teamRegistration)
}
enum CodingKeys: String, CodingKey {
case _id = "id"
case _match = "match"
case _teamRegistration = "teamRegistration"
case _playerRegistrations = "playerRegistrations"
case _score = "score"
case _walkOut = "walkOut"
case _luckyLoser = "luckyLoser"
}
}