study: brutally switch chapter by reloading all clients

pull/1834/head
Thibault Duplessis 2016-04-24 16:38:35 +07:00
parent 5c1ddd8f9a
commit 4e02e82e01
10 changed files with 47 additions and 10 deletions

View File

@ -11,9 +11,7 @@ case class Chapter(
setup: Chapter.Setup,
root: Node.Root,
order: Int,
createdAt: DateTime) {
def id = _id
createdAt: DateTime) extends Chapter.Like {
def updateRoot(f: Node.Root => Option[Node.Root]) =
f(root) map { newRoot =>
@ -30,6 +28,13 @@ object Chapter {
type ID = String
sealed trait Like {
val _id: Chapter.ID
val name: String
val setup: Chapter.Setup
def id = _id
}
case class Setup(
gameId: Option[String],
variant: Variant,
@ -38,7 +43,7 @@ object Chapter {
case class Metadata(
_id: Chapter.ID,
name: String,
setup: Chapter.Setup)
setup: Chapter.Setup) extends Like
val idSize = 8

View File

@ -25,8 +25,6 @@ final class ChapterRepo(coll: Coll) {
"order"
) map { order => ~order + 1 }
def exists(id: Chapter.ID) = coll.exists($id(id))
def insert(s: Chapter): Funit = coll.insert(s).void
def update(c: Chapter): Funit = coll.update($id(c.id), c).void

View File

@ -49,9 +49,11 @@ private final class Socket(
"w" -> who(uid)
), Messadata())
case ReloadMembers(members) => notifyVersion("members", members, Messadata())
case ReloadMembers(members) => notifyVersion("members", members, Messadata())
case ReloadChapters(chapters) => notifyVersion("chapters", chapters, Messadata())
case ReloadChapters(chapters) => notifyVersion("chapters", chapters, Messadata())
case ReloadAll(study, chapters) => notifyVersion("reload", JsNull, Messadata())
case ReloadShapes(shapes, uid) => notifyVersion("shapes", Json.obj(
"s" -> shapes,
@ -130,6 +132,7 @@ private object Socket {
case class ReloadMembers(members: StudyMembers)
case class ReloadShapes(shapes: List[Shape], uid: Uid)
case class ReloadChapters(chapters: List[Chapter.Metadata])
case class ReloadAll(study: Study, chapters: List[Chapter.Metadata])
case class Messadata(trollish: Boolean = false)
case object NotifyCrowd

View File

@ -127,6 +127,11 @@ private[study] final class SocketHandler(
name <- d str "name"
} api.addChapter(byUserId, studyId, name)
case ("setChapter", o) => for {
byUserId <- member.userId
chapterId <- o str "d"
} api.setChapter(byUserId, studyId, chapterId)
}
private def reading[A](o: JsValue)(f: A => Unit)(implicit reader: Reads[A]): Unit =

View File

@ -22,7 +22,7 @@ case class Study(
def canContribute(id: User.ID) = isOwner(id) || members.get(id).exists(_.canContribute)
def withChapter(c: Chapter) = copy(
def withChapter(c: Chapter.Like) = copy(
position = position.copy(chapterId = c.id)
)
}

View File

@ -135,6 +135,16 @@ final class StudyApi(
}
}
def setChapter(byUserId: User.ID, studyId: Study.ID, chapterId: Chapter.ID) = sequenceStudy(studyId) { study =>
(study.position.chapterId != chapterId) ?? {
chapterRepo.byId(chapterId) flatMap {
_.filter(_.studyId == study.id) ?? { chapter =>
studyRepo.update(study withChapter chapter) >>- reloadAll(study)
}
}
}
}
private def reloadUid(study: Study, uid: Uid) =
sendTo(study.id, Socket.ReloadUid(uid))
@ -150,6 +160,11 @@ final class StudyApi(
sendTo(study.id, Socket.ReloadChapters(chapters))
}
private def reloadAll(study: Study) =
chapterRepo.orderedMetadataByStudy(study.id).foreach { chapters =>
sendTo(study.id, Socket.ReloadAll(study, chapters))
}
private def reloadShapes(study: Study, uid: Uid) =
studyRepo.getShapes(study.id).foreach { shapes =>
sendTo(study.id, Socket.ReloadShapes(shapes, uid))

View File

@ -17,6 +17,8 @@ private final class StudyRepo(coll: Coll) {
def insert(s: Study): Funit = coll.insert(s).void
def update(s: Study): Funit = coll.update($id(s.id), s).void
def membersById(id: Study.ID): Fu[Option[StudyMembers]] =
coll.primitiveOne[StudyMembers]($id(id), "members")

View File

@ -113,6 +113,9 @@
.study_box .list div.config .kick {
margin-top: 15px;
}
.study_box .chapters .chapter {
cursor: pointer;
}
.study_box .chapters .status i {
color: #3893E8;
opacity: 1;

View File

@ -70,7 +70,10 @@ module.exports = {
chapter: true,
active: active,
confing: confing
})
}),
onclick: function() {
ctrl.setChapter(chapter.id);
}
};
return [
m('div', attrs, [

View File

@ -71,6 +71,9 @@ module.exports = {
path: path
}));
},
setChapter: function(id) {
send("setChapter", id);
},
onShowGround: function() {
updateShapes();
},