Skip to content

Commit

Permalink
reuse previous broadcast round settings and members for next one
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 28, 2021
1 parent 6df016d commit 4e74c08
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 69 deletions.
7 changes: 6 additions & 1 deletion app/views/relay/roundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ object roundForm {

private def inner(form: Form[Data], url: play.api.mvc.Call, t: RelayTour)(implicit ctx: Context) =
postForm(cls := "form3", action := url)(
div(cls := "form-group")(bits.howToUse),
div(cls := "form-group")(
bits.howToUse,
p(dataIcon := "", cls := "text")(
"The new round will have the same members and contributors as the previous one."
)
),
form3.globalError(form),
form3.group(form("name"), roundName())(form3.input(_)(autofocus)),
form3.group(
Expand Down
55 changes: 34 additions & 21 deletions modules/relay/src/main/RelayApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class RelayApi(
def activeTourNextRound(tour: RelayTour): Fu[Option[RelayRound]] = tour.active ??
roundRepo.coll
.find($doc("tourId" -> tour.id, "finished" -> false))
.sort(roundRepo.chronoSort)
.sort(roundRepo.sort.chrono)
.one[RelayRound]

def tourLastRound(tour: RelayTour): Fu[Option[RelayRound]] =
Expand Down Expand Up @@ -102,7 +102,7 @@ final class RelayApi(
)
),
$doc("$addFields" -> $doc("sync.log" -> $arr())),
$doc("$sort" -> roundRepo.chronoSort),
$doc("$sort" -> roundRepo.sort.chrono),
$doc("$limit" -> 1)
)
)
Expand Down Expand Up @@ -149,26 +149,39 @@ final class RelayApi(
def tourUpdate(tour: RelayTour, data: RelayTourForm.Data, user: User): Funit =
tourRepo.coll.update.one($id(tour.id), data.update(tour, user)).void

def create(data: RelayRoundForm.Data, user: User, tour: RelayTour): Fu[RelayRound] = {
val relay = data.make(user, tour)
roundRepo.coll.insert.one(relay) >>
studyApi.importGame(
StudyMaker.ImportGame(
id = relay.studyId.some,
name = Study.Name(relay.name).some,
settings = Settings.init
.copy(
chat = Settings.UserSelection.Everyone,
sticky = false
def create(data: RelayRoundForm.Data, user: User, tour: RelayTour): Fu[RelayRound] =
roundRepo.lastByTour(tour) flatMap {
_ ?? { last => studyRepo.byId(last.studyId) }
} flatMap { lastStudy =>
import lila.study.{ StudyMember, StudyMembers }
val relay = data.make(user, tour)
roundRepo.coll.insert.one(relay) >>
studyApi.create(
StudyMaker.ImportGame(
id = relay.studyId.some,
name = Study.Name(relay.name).some,
settings = lastStudy
.fold(
Settings.init
.copy(
chat = Settings.UserSelection.Everyone,
sticky = false
)
)(_.settings)
.some,
from = Study.From.Relay(none).some
),
user,
_.copy(members =
lastStudy.fold(StudyMembers.empty)(_.members) + StudyMember(
id = user.id,
role = StudyMember.Role.Write
)
.some,
from = Study.From.Relay(none).some
),
user
) >>
tourRepo.setActive(tour.id, true) >>
studyApi.addTopics(relay.studyId, List("Broadcast")) inject relay
}
)
) >>
tourRepo.setActive(tour.id, true) >>
studyApi.addTopics(relay.studyId, List("Broadcast")) inject relay
}

def requestPlay(id: RelayRound.Id, v: Boolean): Funit =
WithRelay(id) { relay =>
Expand Down
50 changes: 10 additions & 40 deletions modules/relay/src/main/RelayRoundRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,25 @@ final private class RelayRoundRepo(val coll: Coll)(implicit ec: scala.concurrent

def lookup(local: String, foreign: String) = $lookup.simple(coll, "tour", local, foreign)

// def scheduled =
// coll
// .find($doc(selectors scheduled true))
// .sort($sort asc "startsAt")
// .cursor[Relay]()
// .list()

// def ongoing =
// coll
// .find($doc(selectors ongoing true))
// .sort($sort asc "startedAt")
// .cursor[Relay]()
// .list()

// private[relay] def officialCursor(batchSize: Int): AkkaStreamCursor[Relay] =
// coll
// .find(selectors officialOption true)
// .sort($sort desc "startsAt")
// .batchSize(batchSize)
// .cursor[Relay](ReadPreference.secondaryPreferred)

def byTourOrdered(tour: RelayTour): Fu[List[RelayRound]] =
coll
.find(selectors.tour(tour.id))
.sort(chronoSort)
.sort(sort.chrono)
.cursor[RelayRound]()
.list(RelayTour.maxRelays)

val chronoSort = $doc("createdAt" -> 1)
def lastByTour(tour: RelayTour): Fu[Option[RelayRound]] =
coll
.find(selectors tour tour.id)
.sort(sort.reverseChrono)
.one[RelayRound]

val reverseChronoSort = $doc("createdAt" -> -1)
private[relay] object sort {
val chrono = $doc("createdAt" -> 1)
val reverseChrono = $doc("createdAt" -> -1)
}

private[relay] object selectors {
def tour(id: RelayTour.Id) = $doc("tourId" -> id)
// def scheduled(official: Boolean) =
// officialOption(official) ++ $doc(
// "startsAt" $gt DateTime.now.minusHours(1),
// "startedAt" $exists false
// )
// def ongoing(official: Boolean) =
// officialOption(official) ++ $doc(
// "startedAt" $exists true,
// "finished" -> false
// )
def finished(official: Boolean) =
// officialOption(official) ++
$doc(
"startedAt" $exists true,
"finished" -> true
)
}
}
31 changes: 31 additions & 0 deletions modules/relay/src/main/RelayTourRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,40 @@ final private class RelayTourRepo(val coll: Coll)(implicit ec: scala.concurrent.

def lookup(local: String) = $lookup.simple(coll, "tour", local, "_id")

// def scheduled =
// coll
// .find($doc(selectors scheduled true))
// .sort($sort asc "startsAt")
// .cursor[Relay]()
// .list()

// def ongoing =
// coll
// .find($doc(selectors ongoing true))
// .sort($sort asc "startedAt")
// .cursor[Relay]()
// .list()

// private[relay] def officialCursor(batchSize: Int): AkkaStreamCursor[Relay] =
// coll
// .find(selectors officialOption true)
// .sort($sort desc "startsAt")
// .batchSize(batchSize)
// .cursor[Relay](ReadPreference.secondaryPreferred)

private[relay] object selectors {
val official = $doc("official" -> true)
val active = $doc("active" -> true)
val inactive = $doc("active" -> false)
// def scheduled(official: Boolean) =
// officialOption(official) ++ $doc(
// "startsAt" $gt DateTime.now.minusHours(1),
// "startedAt" $exists false
// )
// def ongoing(official: Boolean) =
// officialOption(official) ++ $doc(
// "startedAt" $exists true,
// "finished" -> false
// )
}
}
22 changes: 15 additions & 7 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ final class StudyApi(

def importGame(data: StudyMaker.ImportGame, user: User): Fu[Option[Study.WithChapter]] =
(data.form.as match {
case StudyForm.importGame.AsNewStudy =>
studyMaker(data, user) flatMap { res =>
studyRepo.insert(res.study) >>
chapterRepo.insert(res.chapter) >>-
indexStudy(res.study) >>-
scheduleTimeline(res.study.id) inject res.some
}
case StudyForm.importGame.AsNewStudy => create(data, user)
case StudyForm.importGame.AsChapterOf(studyId) =>
byId(studyId) flatMap {
case Some(study) if study.canContribute(user.id) =>
Expand All @@ -127,6 +121,20 @@ final class StudyApi(
}
}

def create(
data: StudyMaker.ImportGame,
user: User,
transform: Study => Study = identity
): Fu[Option[Study.WithChapter]] =
studyMaker(data, user) map { sc =>
sc.copy(study = transform(sc.study))
} flatMap { sc =>
studyRepo.insert(sc.study) >>
chapterRepo.insert(sc.chapter) >>-
indexStudy(sc.study) >>-
scheduleTimeline(sc.study.id) inject sc.some
}

def clone(me: User, prev: Study): Fu[Option[Study]] =
Settings.UserSelection.allows(prev.settings.cloneable, prev, me.id.some) ?? {
val study1 = prev.cloneFor(me)
Expand Down

0 comments on commit 4e74c08

Please sign in to comment.