<commit_before>package org.drooms.gui.swing

import java.io.File

import scala.xml.NodeSeq
import scala.xml.XML

import org.drooms.gui.swing.event.EventBusFactory

/**
 * Class that represents Drooms game report.
 *
 * Report can be loaded from XML file.
 */
class GameReport(
  val props: Map[String, String],
  val players: List[String],
  val playgroundHeight: Int,
  val playgroundWidth: Int,
  val playgroundInit: Set[Node],
  val wormInitPositions: Set[(String, List[Node])],
  val turns: List[GameTurn],
  val results: List[(String, Int)]) {

  /**
   * Creates list of {@code TurnState}s representing states for all game turns.
   * It enable the game to be moved into particular turn very easily.
   */
  def createTurnsStates(): List[TurnState] = {
    val initPlayground = new PlaygroundModel(playgroundWidth, playgroundHeight, EventBusFactory.getNoOp())
    initPlayground.emptyNodes(playgroundInit)
    initPlayground.initWorms(wormInitPositions)
    val initPlayers = players.map(_ -> 0).toMap
    val initState = new TurnState(initPlayground, initPlayers)
    var turnsStates = List[TurnState]()
    turnsStates ::= initState
    var prevState = initState
    for (turn <- turns) {
      val newState = new TurnState(updatePlaygroundModel(turn, prevState.playgroundModel), updatePlayers(turn, prevState.players))
      turnsStates ::= newState
      prevState = newState
    }
    turnsStates.reverse
  }

  private def updatePlaygroundModel(turn: GameTurn, model: PlaygroundModel): PlaygroundModel = {
    val newModel = model.clone()
    for (step <- turn.steps) {
      newModel.update(step)
    }
    newModel
  }

  private def updatePlayers(turn: GameTurn, players: Map[String, Int]): Map[String, Int] = {
    var newPlayers = players
    for (step <- turn.steps) {
      step match {
        case WormSurvived(playerName, points) =>
          newPlayers = newPlayers.updated(playerName, newPlayers(playerName) + points)
        case CollectibleCollected(playerName, collectible) =>
          newPlayers = newPlayers.updated(playerName, newPlayers(playerName) + collectible.points)
        case _ =>
      }
    }<commit_msg>swing-gui: initial player positions are not considered as turn state anymore<commit_after>package org.drooms.gui.swing

import java.io.File

import scala.xml.NodeSeq
import scala.xml.XML

import org.drooms.gui.swing.event.EventBusFactory

/**
 * Class that represents Drooms game report.
 *
 * Report can be loaded from XML file using {@link GameReportXmlParser}.
 */
class GameReport(
  val props: Map[String, String],
  val players: List[String],
  val playgroundHeight: Int,
  val playgroundWidth: Int,
  val playgroundInit: Set[Node],
  val wormInitPositions: Set[(String, List[Node])],
  val turns: List[GameTurn],
  val results: List[(String, Int)]) {

  /**
   * Creates list of {@code TurnState}s representing states for all game turns.
   * It enable the game to be moved into particular turn very easily.
   */
  def createTurnsStates(): List[TurnState] = {
    val initPlayground = new PlaygroundModel(playgroundWidth, playgroundHeight, EventBusFactory.getNoOp())
    initPlayground.emptyNodes(playgroundInit)
    initPlayground.initWorms(wormInitPositions)
    val initPlayers = players.map(_ -> 0).toMap
    val initState = new TurnState(initPlayground, initPlayers)
    var turnsStates = List[TurnState]()
    //turnsStates ::= initState
    var prevState = initState
    for (turn <- turns) {
      val newState = new TurnState(updatePlaygroundModel(turn, prevState.playgroundModel), updatePlayers(turn, prevState.players))
      turnsStates ::= newState
      prevState = newState
    }
    turnsStates.reverse
  }

  private def updatePlaygroundModel(turn: GameTurn, model: PlaygroundModel): PlaygroundModel = {
    val newModel = model.clone()
    for (step <- turn.steps) {
      newModel.update(step)
    }
    newModel
  }

  private def updatePlayers(turn: GameTurn, players: Map[String, Int]): Map[String, Int] = {
    var newPlayers = players
    for (step <- turn.steps) {
      step match {
        case WormSurvived(playerName, points) =>
          newPlayers = newPlayers.updated(playerName, newPlayers(playerName) + points)
        case CollectibleCollected(playerName, collectible) =>
          newPlayers = newPlayers.updated(playerName, newPlayers(playerName) + collectible.points)
        case _ =>
      }
    }
{'fraction_non_alphanumeric': 0.10134365748121157, 'fraction_numerical': 0.0004554771122751082, 'mean_word_length': 3.748108108108108, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '830117', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1274, 'n_words': 343}