import multiple games into study - closes #6842

pull/6882/head
Thibault Duplessis 2020-06-27 12:40:57 +02:00
parent 1c6df4ff98
commit 49ac5dd2e9
4 changed files with 43 additions and 26 deletions

View File

@ -211,7 +211,17 @@ private[study] object ChapterMaker {
orientation: String = "white",
mode: String = ChapterMaker.Mode.Normal.key,
initial: Boolean = false
) extends ChapterData
) extends ChapterData {
def manyGames =
game
.??(_.linesIterator.take(Study.maxChapters).toList)
.map(_.trim)
.filter(_.nonEmpty)
.map { g => copy(game = g.some) }
.some
.filter(_.size > 1)
}
case class EditData(
id: Chapter.Id,

View File

@ -569,27 +569,34 @@ final class StudyApi(
}
}
def addChapter(studyId: Study.Id, data: ChapterMaker.Data, sticky: Boolean)(who: Who) =
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.countByStudyId(study.id) flatMap { count =>
if (count >= Study.maxChapters) funit
else
chapterRepo.nextOrderByStudy(study.id) flatMap { order =>
chapterMaker(study, data, order, who.u) flatMap { chapter =>
data.initial ?? {
chapterRepo.firstByStudy(study.id) flatMap {
_.filter(_.isEmptyInitial) ?? chapterRepo.delete
}
} >> doAddChapter(study, chapter, sticky, who)
} addFailureEffect {
case ChapterMaker.ValidationException(error) =>
sendTo(study.id)(_.validationError(error, who.sri))
case u => println(u)
}
}
def addChapter(studyId: Study.Id, data: ChapterMaker.Data, sticky: Boolean)(who: Who): Funit =
data.manyGames match {
case Some(datas) =>
lila.common.Future.applySequentially(datas) { data =>
addChapter(studyId, data, sticky)(who)
}
case _ =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.countByStudyId(study.id) flatMap { count =>
if (count >= Study.maxChapters) funit
else
chapterRepo.nextOrderByStudy(study.id) flatMap { order =>
chapterMaker(study, data, order, who.u) flatMap { chapter =>
data.initial ?? {
chapterRepo.firstByStudy(study.id) flatMap {
_.filter(_.isEmptyInitial) ?? chapterRepo.delete
}
} >> doAddChapter(study, chapter, sticky, who)
} addFailureEffect {
case ChapterMaker.ValidationException(error) =>
sendTo(study.id)(_.validationError(error, who.sri))
case u => println(u)
}
}
}
}
}
}
}
def importPgns(studyId: Study.Id, datas: List[ChapterMaker.Data], sticky: Boolean)(who: Who) =

View File

@ -94,16 +94,16 @@
<string name="startFromInitialPosition">Start from initial position</string>
<string name="editor">Editor</string>
<string name="startFromCustomPosition">Start from custom position</string>
<string name="loadAGameByUrl">Load a game by URL</string>
<string name="loadAGameByUrl">Load games by URLs</string>
<string name="loadAPositionFromFen">Load a position from FEN</string>
<string name="loadAGameFromPgn">Load a game from PGN</string>
<string name="loadAGameFromPgn">Load games from PGN</string>
<string name="automatic">Automatic</string>
<plurals name="pasteYourPgnTextHereUpToNbGames">
<item quantity="one">Paste your PGN text here, up to %s game</item>
<item quantity="other">Paste your PGN text here, up to %s games</item>
</plurals>
<string name="urlOfTheGame">URL of the game</string>
<string name="loadAGameFromXOrY">Load a game from %1$s or %2$s</string>
<string name="urlOfTheGame">URL of the games, one per line</string>
<string name="loadAGameFromXOrY">Load games from %1$s or %2$s</string>
<string name="createChapter">Create chapter</string>
<string name="configureLiveBroadcast">Configure live broadcast</string>
<string name="createStudy">Create study</string>

View File

@ -196,7 +196,7 @@ export function view(ctrl: StudyChapterNewFormCtrl): VNode {
h('label.form-label', {
attrs: { 'for': 'chapter-game' }
}, trans('loadAGameFromXOrY', 'lichess.org', 'chessgames.com')),
h('input#chapter-game.form-control', {
h('textarea#chapter-game.form-control', {
attrs: { placeholder: noarg('urlOfTheGame') }
})
]) : null,