parent
c0eb74bb43
commit
32d66cc7c4
@ -0,0 +1,97 @@ |
|||||||
|
// |
||||||
|
// ImportedPlayer+Extensions.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 24/04/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import Foundation |
||||||
|
|
||||||
|
extension ImportedPlayer: PlayerHolder { |
||||||
|
func getAssimilatedAsMaleRank() -> Int? { |
||||||
|
guard male == false else { return nil } |
||||||
|
return getRank()?.femaleInMaleAssimilation |
||||||
|
} |
||||||
|
|
||||||
|
var computedAge: Int? { nil } |
||||||
|
|
||||||
|
var tournamentPlayed: Int? { |
||||||
|
Int(tournamentCount) |
||||||
|
} |
||||||
|
|
||||||
|
func getPoints() -> Double? { |
||||||
|
self.points |
||||||
|
} |
||||||
|
|
||||||
|
func getFirstName() -> String { |
||||||
|
self.firstName ?? "prénom inconnu" |
||||||
|
} |
||||||
|
|
||||||
|
func getLastName() -> String { |
||||||
|
self.lastName ?? "nom inconnu" |
||||||
|
} |
||||||
|
|
||||||
|
func formattedLicense() -> String { |
||||||
|
if let license { return license.computedLicense } |
||||||
|
return "aucune licence" |
||||||
|
} |
||||||
|
|
||||||
|
func getRank() -> Int? { |
||||||
|
Int(rank) |
||||||
|
} |
||||||
|
|
||||||
|
func isUnranked() -> Bool { |
||||||
|
false |
||||||
|
} |
||||||
|
|
||||||
|
func formattedRank() -> String { |
||||||
|
rank.formatted() |
||||||
|
} |
||||||
|
|
||||||
|
func isMalePlayer() -> Bool { |
||||||
|
male |
||||||
|
} |
||||||
|
|
||||||
|
func hitForSearch(_ searchText: String) -> Int { |
||||||
|
var trimmedSearchText = searchText.lowercased().trimmingCharacters(in: .whitespaces).folding(options: .diacriticInsensitive, locale: .current) |
||||||
|
trimmedSearchText = trimmedSearchText.replaceCharactersFromSet(characterSet: .punctuationCharacters, replacementString: " ") |
||||||
|
trimmedSearchText = trimmedSearchText.replaceCharactersFromSet(characterSet: .symbols, replacementString: " ") |
||||||
|
|
||||||
|
if trimmedSearchText.isEmpty { return 0 } |
||||||
|
let tokens = trimmedSearchText.components(separatedBy: .whitespacesAndNewlines).filter { $0.isEmpty == false } |
||||||
|
if let license, trimmedSearchText.contains(license) { |
||||||
|
return 100 |
||||||
|
} |
||||||
|
|
||||||
|
let label = canonicalFullName! |
||||||
|
if tokens.count > 1 { |
||||||
|
var wordFound = 0 |
||||||
|
if trimmedSearchText.lowercased().components(separatedBy: .whitespacesAndNewlines).count > 1 { |
||||||
|
let searchFields: Set = Set([firstName!.canonicalVersion.components(separatedBy: .whitespacesAndNewlines), lastName!.canonicalVersion.components(separatedBy: .whitespacesAndNewlines)].flatMap { $0 }) |
||||||
|
let tokens: Set = Set(trimmedSearchText.components(separatedBy: .whitespacesAndNewlines)) |
||||||
|
wordFound = searchFields.intersection(tokens).count |
||||||
|
} |
||||||
|
|
||||||
|
if wordFound == 2 { |
||||||
|
if let first = tokens.pairs().first(where: { a,b in |
||||||
|
label.contains(a) && label.contains(b) |
||||||
|
}) { |
||||||
|
return 2 + first.0.count + first.1.count |
||||||
|
} |
||||||
|
} else { |
||||||
|
return wordFound * 10 |
||||||
|
} |
||||||
|
} else if let first = tokens.first { |
||||||
|
if label.contains(first) { |
||||||
|
return 1 |
||||||
|
} |
||||||
|
} |
||||||
|
return 0 |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fileprivate extension Int { |
||||||
|
var femaleInMaleAssimilation: Int { |
||||||
|
self + TournamentCatgory.femaleInMaleAssimilationAddition(self) |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
// |
||||||
|
// PlayerHolder.swift |
||||||
|
// PadelClub |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 24/04/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import Foundation |
||||||
|
|
||||||
|
protocol PlayerHolder { |
||||||
|
|
||||||
|
func getFirstName() -> String |
||||||
|
func getLastName() -> String |
||||||
|
func formattedRank() -> String |
||||||
|
func formattedLicense() -> String |
||||||
|
func getPoints() -> Double? |
||||||
|
func getRank() -> Int? |
||||||
|
func isUnranked() -> Bool |
||||||
|
var male: Bool { get } |
||||||
|
var tournamentPlayed: Int? { get } |
||||||
|
var clubName: String? { get } |
||||||
|
var ligueName: String? { get } |
||||||
|
var assimilation: String? { get } |
||||||
|
var computedAge: Int? { get } |
||||||
|
func getAssimilatedAsMaleRank() -> Int? |
||||||
|
} |
||||||
|
|
||||||
|
extension PlayerHolder { |
||||||
|
var isAssimilated: Bool { |
||||||
|
assimilation == "Oui" |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue