study: force variation WIP

This commit is contained in:
Thibault Duplessis 2018-09-28 12:14:17 +02:00
parent cd2c5c1076
commit f5fece87d3
6 changed files with 32 additions and 8 deletions

View file

@ -169,7 +169,8 @@ object BSONHandlers {
score = r.getO[Score]("e"),
crazyData = r.getO[Crazyhouse.Data]("z"),
clock = r.getO[Centis]("l"),
children = r.get[Node.Children]("n")
children = r.get[Node.Children]("n"),
forceVariation = r boolD "fv"
)
def writes(w: Writer, s: Node) = $doc(
"i" -> s.id,
@ -185,7 +186,8 @@ object BSONHandlers {
"e" -> s.score,
"l" -> s.clock,
"z" -> s.crazyData,
"n" -> (if (s.ply < Node.MAX_PLIES) s.children else Node.emptyChildren)
"n" -> (if (s.ply < Node.MAX_PLIES) s.children else Node.emptyChildren),
"fv" -> w.boolO(s.forceVariation)
)
}
import Node.Root

View file

@ -39,7 +39,8 @@ case class Node(
score: Option[Score] = None,
clock: Option[Centis],
crazyData: Option[Crazyhouse.Data],
children: Node.Children
children: Node.Children,
forceVariation: Boolean
) extends RootOrNode {
import Node.Children
@ -91,7 +92,8 @@ case class Node(
crazyData = n.crazyData orElse crazyData,
children = n.children.nodes.foldLeft(children) {
case (cs, c) => children addNode c
}
},
forceVariation = n.forceVariation || forceVariation
)
override def toString = s"$ply.${move.san} ${children.nodes}"
@ -305,6 +307,7 @@ object Node {
check = b.check,
crazyData = b.crazyData,
clock = b.clock,
children = Children(b.children.map(fromBranch)(scala.collection.breakOut))
children = Children(b.children.map(fromBranch)(scala.collection.breakOut)),
forceVariation = false
)
}

View file

@ -137,7 +137,8 @@ object PgnImport {
Node.Children {
makeNode(game, rest, annotator).fold(variations)(_ :: variations).toVector
}
}
},
forceVariation = false
).some
}
}

View file

@ -115,7 +115,8 @@ object ServerEval {
check = g.situation.check,
crazyData = g.situation.board.crazyData,
clock = none,
children = Node.emptyChildren
children = Node.emptyChildren,
forceVariation = false
)
}
}

View file

@ -453,6 +453,19 @@ final class StudyApi(
}
}
def forceVariation(studyId: Study.Id, position: Position.Ref, force: Boolean, uid: Uid): Funit =
sequenceStudyWithChapter(studyId, position.chapterId) { sc =>
sc.chapter.forceVariation(force, position.path) match {
case Some(newChapter) =>
studyRepo.updateNow(sc.study)
chapterRepo.forceVariation(newChapter, position.path, force) >>-
sendTo(sc.study, Socket.SetClock(position, clock, uid))
case None =>
fufail(s"Invalid setClock $position $clock") >>-
reloadUidBecauseOf(sc.study, uid, position.chapterId)
}
}
def explorerGame(userId: User.ID, studyId: Study.Id, data: actorApi.ExplorerGame, uid: Uid) = sequenceStudyWithChapter(studyId, data.position.chapterId) {
case Study.WithChapter(study, chapter) => Contribute(userId, study) {
if (data.insert) explorerGameHandler.insert(userId, study, Position(chapter, data.position.path), data.gameId) flatMap {

View file

@ -29,6 +29,7 @@ sealed trait Node {
def addChild(branch: Branch): Node
def dropFirstChild: Node
def clock: Option[Centis]
def forceVariation: Boolean
// implementation dependent
def idOption: Option[UciCharPair]
@ -62,6 +63,7 @@ case class Root(
def idOption = None
def moveOption = None
def comp = false
def forceVariation = false
def addChild(branch: Branch) = copy(children = children :+ branch)
def prependChild(branch: Branch) = copy(children = branch :: children)
@ -86,7 +88,8 @@ case class Branch(
opening: Option[FullOpening] = None,
comp: Boolean = false,
clock: Option[Centis] = None, // clock state after the move is played, and the increment applied
crazyData: Option[Crazyhouse.Data]
crazyData: Option[Crazyhouse.Data],
forceVariation: Boolean = false // cannot be mainline
) extends Node {
def idOption = Some(id)
@ -273,6 +276,7 @@ object Node {
.add("crazy", crazyData)
.add("comp", comp)
.add("children", if (alwaysChildren || children.nonEmpty) Some(children) else None)
.add("forceVariation", forceVariation)
} catch {
case e: StackOverflowError =>
e.printStackTrace