-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathFootball.scala
More file actions
179 lines (148 loc) · 4.69 KB
/
Football.scala
File metadata and controls
179 lines (148 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package implicits
import football.model.FootballMatchTrail
import model._
import pa._
import views.MatchStatus
import java.time.temporal.ChronoUnit
import java.time.{LocalDate, ZonedDateTime}
import scala.language.implicitConversions
trait Football {
implicit class MatchSeq2Sorted(matches: Seq[FootballMatch]) {
lazy val sortByDate = matches.sortBy(m => (m.date.toInstant.toEpochMilli, m.homeTeam.name))
}
implicit class Content2minByMin(c: ContentType) {
lazy val minByMin = c.tags.tags.exists(_.id == "tone/minutebyminute")
}
implicit class Content2matchReport(c: ContentType) {
lazy val matchReport = c.tags.tags.exists(_.id == "tone/matchreports")
}
implicit class Content2squadSheet(c: ContentType) {
lazy val squadSheet = c.tags.tags.exists(_.id == "football/series/squad-sheets")
}
implicit class Content2preview(c: ContentType) {
lazy val preview = c.tags.tags.exists(_.id == "football/series/match-previews")
}
implicit def match2Trail(m: FootballMatch): FootballMatchTrail = {
FootballMatchTrail.toTrail(m)
}
implicit class MatchHelpers(m: FootballMatch) {
lazy val statusSummary = StatusSummary(
s"${m.homeTeam.name} v ${m.awayTeam.name}",
MatchStatus(m.matchStatus).toString,
m.homeTeam.score,
m.awayTeam.score,
)
def isOn(date: LocalDate): Boolean = m.date.toLocalDate == date
def isAboutToStart: Boolean = {
val now = ZonedDateTime.now()
val upperBound = now.plusMinutes(5)
m.date.isAfter(now) && m.date.isBefore(upperBound)
}
// results and fixtures do not actually have a status field in the API
lazy val matchStatus = m match {
case f: Fixture => "Fixture"
case l: LiveMatch => l.status
case r: Result => "FT"
case m: MatchDay => m.matchStatus
}
lazy val isFixture = m match {
case f: Fixture => true
case m: MatchDay => m.matchStatus == "-" // yeah really even though its not in the docs
case _ => false
}
lazy val isResult = m match {
case r: Result => true
case m: MatchDay => m.result
case _ => false
}
lazy val hasStarted = m.isLive || m.isResult
val smartUrl: Option[String] = MatchUrl.smartUrl(m)
def hasTeam(teamId: String): Boolean = m.homeTeam.id == teamId || m.awayTeam.id == teamId
// England, Scotland, Wales, N. Ireland or Rep. Ireland
lazy val isHomeNationGame = {
val homeNations = Seq("497", "630", "964", "494", "499")
homeNations.contains(m.homeTeam.id) || homeNations.contains(m.awayTeam.id)
}
}
implicit class TeamHasScored(t: MatchDayTeam) {
lazy val hasScored = t.score.exists(_ != 0)
}
implicit class GhostTeam(t: MatchDayTeam) {
lazy val isGhostTeam = ghostTeamIds.contains(t.id)
lazy val knockoutName = {
if (isGhostTeam) ghostTeamNameMappings.foldLeft(t.name) { case (name, (from, to)) => name.replace(from, to) }
else t.name
}
// ghost team IDs correct for world-cups in 2014 and 2018, let's assume they are static and leave them here forever
// PA knockout placeholder teams
// e.g. "Winner Group A", "Wnr Gp G/R-Up Gp H", "Loser SF1"
private val ghostTeamIds = List(
"8158",
"8159",
"8162",
"8163",
"8160",
"8161",
"8164",
"8165",
"8166",
"8167",
"8172",
"8173",
"8170",
"8171",
"8174",
"8175",
"8204",
"8206",
"8200",
"8202",
"8205",
"8207",
"8201",
"8203",
"42624",
"42625",
"42626",
"42627",
"8176",
"8177",
"7357",
"7358",
// womens world cup 2019 (assumed the same for future women's world cups)
// round of 16 (TODO)
"64635",
"64636",
"64637",
"64638",
"64639",
"64640",
"64641",
"64642", // quarter final
"64643",
"64644",
"64645",
"64646", // semi final
"64647",
"64648", // 3rd place playoff
"64649",
"64650", // final
)
private val ghostTeamNameMappings = List(
"/" -> " / ",
"Q / F" -> "QF",
"Wnr Gp" -> "W",
"R-Up Gp" -> "RU",
"Winner Group" -> "Winner",
"Runner-up Group" -> "Runner-up",
)
}
// "700" is for world-cup 2014 - remove that entry when it is done (leave the impls for other tournaments)
val roundLinks = Map[String, Round => Option[String]]()
def groupTag(competitionId: String, round: Round): Option[String] = roundLinks.get(competitionId).flatMap(_(round))
}
object Football extends Football {
def hoursTillMatch(theMatch: FootballMatch): Long = {
ChronoUnit.HOURS.between(ZonedDateTime.now(), theMatch.date)
}
}