Beispiel #1
0
// Backup the current trn and 2h files for this game
func (game *Game) Backup(turnNumber int, force bool) error {
	current2hPath := game.TwohFile.Fullpath

	target2hPath, err := game.TwohFile.BackupFilepath(turnNumber)
	if err != nil {
		return err
	}

	currentTrnPath := game.TrnFile.Fullpath

	targetTrnPath, err := game.TrnFile.BackupFilepath(turnNumber)
	if err != nil {
		return err
	}

	if !force && (utility.FileExists(target2hPath) || utility.FileExists(targetTrnPath)) {
		return errors.New(fmt.Sprintf("Backup for turn %v already exists in %v, not forcing", turnNumber, game.Directory))
	}

	err = utility.Cp(current2hPath, target2hPath)
	if err != nil {
		return err
	}

	err = utility.Cp(currentTrnPath, targetTrnPath)
	if err != nil {
		return err
	}

	return nil
}
Beispiel #2
0
// Restore backed up trn and 2h file for this game
func (game *Game) Restore(turnNumber int) error {
	current2hPath, err := game.Current2hFilepath()
	if err != nil {
		return err
	}

	currentTrnPath, err := game.CurrentTrnFilepath()
	if err != nil {
		return err
	}

	// It's fine if only the 2h or the trn file have backups we'll just restore the one that exists.
	// But if neither file exists, we error out.
	backup2hPath, err := game.TwohFile.BackupFilepath(turnNumber)
	backup2hExists := err == nil

	backupTrnPath, err := game.TrnFile.BackupFilepath(turnNumber)
	backupTrnExists := err == nil

	if !(backup2hExists || backupTrnExists) {
		return errors.New(fmt.Sprintf("Neither trn nor 2h backups exist for turn %v in %v", turnNumber, game.Directory))
	}

	if backup2hExists {
		err = utility.Cp(backup2hPath, current2hPath)
		if err != nil {
			return err
		}
	}

	if backupTrnExists {
		err = utility.Cp(backupTrnPath, currentTrnPath)
		if err != nil {
			return err
		}
	}

	return nil
}
Beispiel #3
0
func (c *ReplayCommand) run(parseContext *kingpin.ParseContext) error {
	game, err := c.Meta.RunContext.GameInstallation.AvailableGames.FindGameByName(c.GameName)
	if err != nil {
		return err
	}

	var endTurn int
	lastSavedTurn := game.SortedTrnBackupKeys[len(game.SortedTrnBackupKeys)-1]

	if c.TurnCount > 0 {
		endTurn = c.StartTurn + c.TurnCount - 1

		if endTurn > lastSavedTurn {
			endTurn = lastSavedTurn
		}
	} else {
		endTurn = lastSavedTurn
	}

	if c.Destroy {
		c.Ui.Output(fmt.Sprintf("Replaying turns for %v, starting at %v, ending at %v", game.Name, c.StartTurn, endTurn))

		for turn := c.StartTurn; turn <= endTurn; turn++ {

			replayGameName := game.ReplayName(turn)
			replayGame, err := c.Meta.RunContext.GameInstallation.AvailableGames.FindGameByName(replayGameName)

			if err != nil {
				c.Ui.Output(fmt.Sprintf("No game found for turn %v", turn))
			} else {
				c.Ui.Output(fmt.Sprintf("Deleting %v", replayGame.Name))
				replayGame.Delete()
			}
		}
	} else {
		c.Ui.Output(fmt.Sprintf("Deleting replays for %v, starting at %v, ending at %v", game.Name, c.StartTurn, endTurn))

		for turn := c.StartTurn; turn <= endTurn; turn++ {
			trnFile, ok := game.TrnBackups[turn]
			if !ok {
				c.Ui.Error(fmt.Sprintf("No .trn file found for turn %v, skipping", turn))
				continue
			}

			twohFile, ok := game.TwohBackups[turn]
			if !ok {
				c.Ui.Error(fmt.Sprintf("No .2h file found for turn %v, skipping", turn))
				continue
			}

			newGameName := game.ReplayName(turn)
			newGameCmd := CreateCommand{Meta: c.Meta, Force: c.Force, NewGameName: newGameName}
			newGameCmd.run(parseContext)
			newGame, err := c.Meta.RunContext.GameInstallation.AvailableGames.FindGameByName(newGameName)
			if err != nil {
				return err
			}

			backupTrnBasename, err := trnFile.BackupBasename()
			if err != nil {
				return err
			}

			backupTwohBasename, err := twohFile.BackupBasename()
			if err != nil {
				return err
			}

			err = utility.Cp(trnFile.Fullpath, filepath.Join(newGame.Directory, backupTrnBasename))
			if err != nil {
				return err
			}

			err = utility.Cp(twohFile.Fullpath, filepath.Join(newGame.Directory, backupTwohBasename))
			if err != nil {
				return err
			}
		}
	}

	return nil
}