update relay clocks when changed upstream

pull/3627/head
Thibault Duplessis 2017-09-20 14:53:52 -05:00
parent 10f481fa90
commit 1a58e4c924
2 changed files with 25 additions and 2 deletions

View File

@ -26,8 +26,20 @@ private final class RelaySync(
game.root.mainline.foldLeft(Path.root -> none[Node]) {
case ((parentPath, None), gameNode) =>
val path = parentPath + gameNode
if (chapter.root.nodeAt(path).isDefined) path -> none
else (parentPath -> gameNode.some)
chapter.root.nodeAt(path) match {
case None => parentPath -> gameNode.some
case Some(existing) =>
gameNode.clock.filter(c => !existing.clock.has(c)) ?? { c =>
studyApi.doSetClock(
userId = chapter.ownerId,
study = study,
position = Position(chapter, path),
clock = c.some,
uid = socketUid
)
}
path -> none
}
case (found, _) => found
} match {
case (_, None) => funit

View File

@ -304,6 +304,17 @@ final class StudyApi(
}
}
def doSetClock(userId: User.ID, study: Study, position: Position, clock: Option[Centis], uid: Uid) =
position.chapter.setClock(clock, position.path) match {
case Some(newChapter) =>
studyRepo.updateNow(study)
chapterRepo.setClock(newChapter, position.path, clock) >>-
sendTo(study, Socket.SetClock(position.ref, clock, uid))
case None =>
fufail(s"Invalid setClock $position $clock") >>-
reloadUidBecauseOf(study, uid, position.chapter.id)
}
def setTag(userId: User.ID, studyId: Study.Id, setTag: actorApi.SetTag, uid: Uid) = sequenceStudy(studyId) { study =>
Contribute(userId, study) {
chapterRepo.byIdAndStudy(setTag.chapterId, studyId) flatMap {