<commit_before>package com.mogproject.mogami.playground.controller.mode

import com.mogproject.mogami.{BranchNo, _}
import com.mogproject.mogami.GameStatus._
import com.mogproject.mogami.core.move.Resign
import com.mogproject.mogami.core.state.State.PromotionFlag
import com.mogproject.mogami.{Game, Square}
import com.mogproject.mogami.playground.controller.{Configuration, Controller, Cursor}
import com.mogproject.mogami.core.state.StateCache.Implicits._
import com.mogproject.mogami.playground.view.renderer.Renderer


/**
  * Play mode
  */
case class PlayModeController(renderer: Renderer,
                              config: Configuration,
                              game: Game,
                              displayBranchNo: BranchNo,
                              displayPosition: Int
                             ) extends GameController with CursorAdjustable {
  val mode: Mode = Playing

  override def copy(config: Configuration, game: Game, displayBranchNo: BranchNo, displayPosition: Int): GameController =
    PlayModeController(renderer, config, game, displayBranchNo, displayPosition)

  /**
    * Initialization
    */
  override def initialize(): Unit = {
    super.initialize()
    renderer.showActionSection()
    renderer.showBranchEditMenu()
  }

  override def terminate(): Unit = {
    super.terminate()
    renderer.hideActionSection()
    renderer.hideBranchEditMenu()
  }

  override def renderAll(): Unit = {
    super.renderAll()
    renderer.updateActionSection(config.messageLang, canResign)
  }

  override def canActivate(cursor: Cursor): Boolean = !cursor.isBox

  override def canSelect(cursor: Cursor): Boolean = cursor match {
    case Cursor(Some(sq), None, None, None) => selectedState.board.get(sq).exists(selectedState.turn == _.owner)
    case Cursor(None, Some(h), None, None) => h.owner == selectedState.turn && selectedState.hand.get(h).exists(_ > 0)
    case _ => false
  }

  private[this] def canResign: Boolean = (displayBranch.finalAction, displayBranch.status, displayPosition - currentMoves.length) match {
    case (Some(_), _, n) => n <= 0
    case (_, Mated | GameStatus.Playing, _) => true
    case (_, _, n) => n < 0
  }

  /**
    * Move action in Play Mode
    *
    * @param selected from
    * @param invoked  to
    */
  override def invokeCursor(selected: Cursor, invoked: Cursor, isFlipped: Boolean): Option[ModeController] = {
    if (invoked.isPlayer) {
      renderer.showGameInfoModal(config, game.gameInfo)
      None
    } else {
      val from = selected.moveFrom

      def f(to: Square, promote: Boolean): Option[GameController] = {
        MoveBuilderSfen(from, to, promote).toMove(selectedState, getLastMove.map(_.to)).flatMap { mv =>
          if (renderer.getIsNewBranchMode) {
            // New Branch Mode
            game.getMove(gamePosition) match {
              case Some(m) if m == mv => Some(this.copy(displayPosition = displayPosition + 1)) // move next
              case Some(_) => game.getForks(gamePosition).find(_._1 == mv) match {
                case Some((_, br)) => moveToBranch(br)
                case None => createNewBranch(mv)
              }
              case None => makeMoveOnCurrentBranch(mv)<commit_msg>fixed branch creation<commit_after>package com.mogproject.mogami.playground.controller.mode

import com.mogproject.mogami.{BranchNo, _}
import com.mogproject.mogami.GameStatus._
import com.mogproject.mogami.core.move.Resign
import com.mogproject.mogami.core.state.State.PromotionFlag
import com.mogproject.mogami.{Game, Square}
import com.mogproject.mogami.playground.controller.{Configuration, Controller, Cursor}
import com.mogproject.mogami.core.state.StateCache.Implicits._
import com.mogproject.mogami.playground.view.renderer.Renderer


/**
  * Play mode
  */
case class PlayModeController(renderer: Renderer,
                              config: Configuration,
                              game: Game,
                              displayBranchNo: BranchNo,
                              displayPosition: Int
                             ) extends GameController with CursorAdjustable {
  val mode: Mode = Playing

  override def copy(config: Configuration, game: Game, displayBranchNo: BranchNo, displayPosition: Int): GameController =
    PlayModeController(renderer, config, game, displayBranchNo, displayPosition)

  /**
    * Initialization
    */
  override def initialize(): Unit = {
    super.initialize()
    renderer.showActionSection()
    renderer.showBranchEditMenu()
  }

  override def terminate(): Unit = {
    super.terminate()
    renderer.hideActionSection()
    renderer.hideBranchEditMenu()
  }

  override def renderAll(): Unit = {
    super.renderAll()
    renderer.updateActionSection(config.messageLang, canResign)
  }

  override def canActivate(cursor: Cursor): Boolean = !cursor.isBox

  override def canSelect(cursor: Cursor): Boolean = cursor match {
    case Cursor(Some(sq), None, None, None) => selectedState.board.get(sq).exists(selectedState.turn == _.owner)
    case Cursor(None, Some(h), None, None) => h.owner == selectedState.turn && selectedState.hand.get(h).exists(_ > 0)
    case _ => false
  }

  private[this] def canResign: Boolean = (displayBranch.finalAction, displayBranch.status, displayPosition - currentMoves.length) match {
    case (Some(_), _, n) => n <= 0
    case (_, Mated | GameStatus.Playing, _) => true
    case (_, _, n) => n < 0
  }

  /**
    * Move action in Play Mode
    *
    * @param selected from
    * @param invoked  to
    */
  override def invokeCursor(selected: Cursor, invoked: Cursor, isFlipped: Boolean): Option[ModeController] = {
    if (invoked.isPlayer) {
      renderer.showGameInfoModal(config, game.gameInfo)
      None
    } else {
      val from = selected.moveFrom

      def f(to: Square, promote: Boolean): Option[GameController] = {
        MoveBuilderSfen(from, to, promote).toMove(selectedState, getLastMove.map(_.to)).flatMap { mv =>
          if (renderer.getIsNewBranchMode) {
            // New Branch Mode
            /** @note compare moves regardless of elapsed time */
            game.getMove(gamePosition).map(_.copy(elapsedTime = None)) match {
              case Some(m) if m == mv => Some(this.copy(displayPosition = displayPosition + 1)) // move next
              case Some(_) => game.getForks(gamePosition).find(_._1 == mv) match {
                case Some((_, br)) => moveToBranch(br)
                case None => createNewBranch(mv)
              }
              case None => makeMoveOnCurrentBranch(mv)
{'fraction_non_alphanumeric': 0.11235096300825435, 'fraction_numerical': 0.001528584530724549, 'mean_word_length': 2.899284862932062, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5664516', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1870, 'n_words': 421}