forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticeProgress.scala
59 lines (43 loc) · 1.52 KB
/
PracticeProgress.scala
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
package lila.practice
import org.joda.time.DateTime
import lila.user.User
import lila.study.{ Study, Chapter }
case class PracticeProgress(
_id: PracticeProgress.Id,
chapters: PracticeProgress.ChapterNbMoves,
createdAt: DateTime,
updatedAt: DateTime
) {
import PracticeProgress.NbMoves
def id = _id
def apply(chapterId: Chapter.Id): Option[NbMoves] =
chapters get chapterId
def withNbMoves(chapterId: Chapter.Id, nbMoves: PracticeProgress.NbMoves) = copy(
chapters = chapters - chapterId + {
chapterId -> NbMoves(math.min(chapters.get(chapterId).fold(999)(_.value), nbMoves.value))
},
updatedAt = DateTime.now
)
def countDone(chapterIds: List[Chapter.Id]): Int =
chapterIds count chapters.contains
def firstOngoingIn(metas: List[Chapter.Metadata]): Option[Chapter.Metadata] =
metas.find { c =>
!chapters.contains(c.id) && !PracticeStructure.isChapterNameCommented(c.name)
} orElse metas.find { c =>
!PracticeStructure.isChapterNameCommented(c.name)
}
}
object PracticeProgress {
case class Id(value: String) extends AnyVal
case class NbMoves(value: Int) extends AnyVal
implicit val nbMovesIso = lila.common.Iso.int[NbMoves](NbMoves.apply, _.value)
case class OnComplete(userId: User.ID, studyId: Study.Id, chapterId: Chapter.Id)
type ChapterNbMoves = Map[Chapter.Id, NbMoves]
def empty(id: Id) = PracticeProgress(
_id = id,
chapters = Map.empty,
createdAt = DateTime.now,
updatedAt = DateTime.now
)
def anon = empty(Id("anon"))
}