Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete earlier moves#12986 WIP #14486

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/views/analyse/jsI18n.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ object jsI18n:
trans.deleteFromHere,
trans.forceVariation,
trans.copyVariationPgn,
trans.deleteEarlierMoves,
// practice (also uses checkmate, draw)
trans.practiceWithComputer,
trans.puzzle.goodMove,
Expand Down
1 change: 1 addition & 0 deletions app/views/board/userAnalysisI18n.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ object userAnalysisI18n:
trans.deleteFromHere,
trans.forceVariation,
trans.copyVariationPgn,
trans.deleteEarlierMoves,
// practice (also uses checkmate, draw)
trans.practiceWithComputer,
trans.puzzle.goodMove,
Expand Down
1 change: 1 addition & 0 deletions modules/i18n/src/main/I18nKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package lila.i18n
// format: OFF
object I18nKeys:
val `playWithAFriend` = I18nKey("playWithAFriend")
val `deleteEarlierMoves` = I18nKey("deleteEarlierMoves")
val `playWithTheMachine` = I18nKey("playWithTheMachine")
val `toInviteSomeoneToPlayGiveThisUrl` = I18nKey("toInviteSomeoneToPlayGiveThisUrl")
val `gameOver` = I18nKey("gameOver")
Expand Down
16 changes: 16 additions & 0 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ final class StudyApi(
def deleteNodeAt(studyId: StudyId, position: Position.Ref)(who: Who) =
sequenceStudyWithChapter(studyId, position.chapterId):
case Study.WithChapter(study, chapter) =>
"in deleteNodeAt".pp;
Contribute(who.u, study):
chapter.updateRoot { root =>
root.withChildren(_.deleteNodeAt(position.path))
Expand All @@ -284,6 +285,21 @@ final class StudyApi(
fufail(s"Invalid delNode $studyId $position") andDo
reloadSriBecauseOf(study, who.sri, chapter.id)

def deleteEarlierMoves(studyId: StudyId, position: Position.Ref)(who: Who) =
sequenceStudyWithChapter(studyId, position.chapterId):
case Study.WithChapter(study, chapter) =>
"in deleteEarlierMoves".pp;
Contribute(who.u, study):
chapter.updateRoot { root =>
root.withChildren(_.deleteEarlierMoves(position.path))
} match
case Some(newChapter) =>
chapterRepo.update(newChapter) andDo
sendTo(study.id)(_.deleteEarlierMoves(position, who))
case None =>
fufail(s"Invalid delEarlierMoves $studyId $position") andDo
reloadSriBecauseOf(study, who.sri, chapter.id)

def clearAnnotations(studyId: StudyId, chapterId: StudyChapterId)(who: Who) =
sequenceStudyWithChapter(studyId, chapterId):
case Study.WithChapter(study, chapter) =>
Expand Down
13 changes: 13 additions & 0 deletions modules/study/src/main/StudySocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ final private class StudySocket(
applyWho(api.setPath(studyId, position.ref.withPath(jumpTo)))
applyWho(api.deleteNodeAt(studyId, position.ref))

case "deleteEarlierMoves" =>
"here".pp
reading[AtPosition](o): position =>
(o \ "d" \ "jumpTo")
.asOpt[UciPath]
.foreach: jumpTo =>
applyWho(api.deleteEarlierMoves(studyId, position.ref.withPath(jumpTo)))

case "promote" =>
reading[AtPosition](o): position =>
(o \ "d" \ "toMainline")
Expand Down Expand Up @@ -310,6 +318,11 @@ final private class StudySocket(
.add("relay", relay)
)
def deleteNode(pos: Position.Ref, who: Who) = version("deleteNode", Json.obj("p" -> pos, "w" -> who))

def deleteEarlierMoves(pos: Position.Ref, who: Who) =
pp("deleteEarlierMoves", pos, who)
version("deleteEarlierMoves", Json.obj("p" -> pos, "w" -> who))

def promote(pos: Position.Ref, toMainline: Boolean, who: Who) =
version(
"promote",
Expand Down
8 changes: 8 additions & 0 deletions modules/tree/src/main/tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ case class Branches(nodes: List[Branch]) extends AnyVal:
case (head, tail) => updateChildren(head, _.deleteNodeAt(tail))
}

// TODO: ? Refactor to be like AnalyseCtrl.deleteEarlierMovesAt
def deleteEarlierMoves(path: UciPath): Option[Branches] =
path.split flatMap {
case (head, p) if p.isEmpty && hasNode(head) => Branches(nodes.filterNot(_.id == head)).some
case (_, p) if p.isEmpty => none
case (head, tail) => updateChildren(head, _.deleteEarlierMoves(tail))
}

def promoteToMainlineAt(path: UciPath): Option[Branches] =
path.split match
case None => this.some
Expand Down
1 change: 1 addition & 0 deletions translation/source/site.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="playWithAFriend">Play with a friend</string>
<string name="deleteEarlierMoves">Delete earlier moves</string>
<string name="playWithTheMachine">Play with the computer</string>
<string name="toInviteSomeoneToPlayGiveThisUrl">To invite someone to play, give this URL</string>
<string name="gameOver">Game Over</string>
Expand Down
9 changes: 9 additions & 0 deletions ui/analyse/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ export default class AnalyseCtrl {
if (this.study) this.study.deleteNode(path);
}

deleteEarlierMoves(path: Tree.Path): void {
const node = this.tree.nodeAtPath(path);
if (!node) return;
this.tree = makeTree(node);
console.log('in ui/analyse/analyseCtrl.deleteEarlierMoves');
console.log('this.tree', this.tree);
this.redraw();
}

promote(path: Tree.Path, toMainline: boolean): void {
this.tree.promoteAt(path, toMainline);
this.jump(path);
Expand Down
1 change: 1 addition & 0 deletions ui/analyse/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface GameUpdate {
export type StudySocketSendParams =
| [t: 'setPath', d: ReqPosition]
| [t: 'deleteNode', d: ReqPosition & { jumpTo: string }]
| [t: 'deleteEarlierMoves', d: ReqPosition & { jumpTo: string }]
| [t: 'promote', d: ReqPosition & { toMainline: boolean }]
| [t: 'forceVariation', d: ReqPosition & { force: boolean }]
| [t: 'shapes', d: ReqPosition & { shapes: Tree.Shape[] }]
Expand Down
25 changes: 25 additions & 0 deletions ui/analyse/src/study/studyCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface Handlers {
addNode(
d: WithWhoAndPos & { d: string; n: Tree.Node; o: Opening; s: boolean; relay?: StudyChapterRelay },
): void;
deleteEarlierMoves(d: WithWhoAndPos): void;
deleteNode(d: WithWhoAndPos): void;
promote(d: WithWhoAndPos & { toMainline: boolean }): void;
liking(d: WithWho & { l: { likes: number; me: boolean } }): void;
Expand Down Expand Up @@ -486,6 +487,18 @@ export default class StudyCtrl {
jumpTo: this.ctrl.path,
}),
);

deleteEarlierMoves = (path: Tree.Path) => {
console.log('In ui/analyse/studyCtrl.deleteEarlierMoves');
console.log('value of path: Tree.Path is:', path);
this.makeChange(
'deleteEarlierMoves',
this.addChapterId({
path: path.toLocaleUpperCase(),
jumpTo: this.ctrl.path,
}),
);
};
promote = (path: Tree.Path, toMainline: boolean) =>
this.makeChange(
'promote',
Expand Down Expand Up @@ -601,6 +614,18 @@ export default class StudyCtrl {
if (this.vm.mode.sticky) this.ctrl.jump(this.ctrl.path);
this.redraw();
},
deleteEarlierMoves: d => {
const position = d.p,
who = d.w;
console.log('in studyCtrl.deleteEarlierMoves');
this.setMemberActive(who);
if (this.wrongChapter(d)) return;
if (who && who.s === lichess.sri) return;
if (!this.ctrl.tree.pathExists(d.p.path)) return this.xhrReload();
this.ctrl.tree.deleteNodeAt(position.path);
if (this.vm.mode.sticky) this.ctrl.jump(position.path);
this.redraw();
},
promote: d => {
const position = d.p,
who = d.w;
Expand Down
3 changes: 3 additions & 0 deletions ui/analyse/src/treeView/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ function view(opts: Opts, coords: Coords): VNode {
action(licon.Clipboard, trans('copyVariationPgn'), () =>
navigator.clipboard.writeText(renderVariationPgn(opts.root.tree.getNodeList(opts.path))),
),
//* Create a button labelled 'study from here' that creates a study from the current node
// strange issue whereby the moves cannot be played after the studyFromHere button is clicked
action(licon.StudyBoard, trans('deleteEarlierMoves'), () => ctrl.deleteEarlierMoves(opts.path)),
],
);
}
Expand Down
Loading