Exemplo n.º 1
0
func run() error {
	whiteAgent := operator.RemotePlayerInstance{
		APIPlayer: api.Player{},
		Host:      *aiHost1,
	}
	blackAgent := operator.RemotePlayerInstance{
		APIPlayer: api.Player{},
		Host:      *aiHost2,
	}
	result := operator.ExecuteGame(&whiteAgent, &blackAgent, operator.Config{
		Start: game.Standard,
		Limit: *timelimit,
	})

	switch result.VictoryReason {
	case api.TimelimitExceeded:
		fmt.Println(result.VictoryReason, fmt.Sprintln("(limit: %s)", *timelimit))
	default:
		fmt.Println(result.VictoryReason)
	}

	var winner operator.RemotePlayerInstance
	switch result.Outcome {
	case game.WhiteWins:
		winner = whiteAgent
	case game.BlackWins:
		winner = blackAgent
	}
	switch result.Outcome {
	case game.WhiteWins, game.BlackWins:
		fmt.Printf("%s (port %s) wins in %d move(s)", result.Outcome.Winner(), winner.Host, len(result.States))
	default:
		fmt.Println("tie game!")
	}

	return nil
}
Exemplo n.º 2
0
func run(matches *matchesDB, g api.Game) error {

	var white api.Player
	if err := matches.DB.First(&white, g.WhiteId).Error; err != nil {
		return err
	}
	var black api.Player
	if err := matches.DB.First(&black, g.BlackId).Error; err != nil {
		return err
	}
	whiteAgent, err := operator.NewPlayerProcessInstance(white, path.Join(matches.FilestoragePath, white.Path), matches.Ports)
	if err != nil {
		return err
	}
	blackAgent, err := operator.NewPlayerProcessInstance(black, path.Join(matches.FilestoragePath, black.Path), matches.Ports)
	if err != nil {
		return err
	}
	result := operator.ExecuteGame(whiteAgent, blackAgent, operator.Config{
		Start: game.Standard,
		Limit: api.DefaultMoveLimit,
		GameHadState: func(s *game.State) error {
			//get count
			var count int
			err := matches.DB.Table("records").Where("game_id = ?", g.ID).Count(&count).Error
			if err != nil {
				return err
			}
			//jsonify state
			var buf bytes.Buffer
			if err := json.NewEncoder(&buf).Encode(s); err != nil {
				return err
			}

			////make and save record
			r := api.Record{
				GameID:  g.ID,
				TurnNum: int64(count + 1),
				State:   buf.String(),
			}
			if err := matches.DB.Create(&r).Error; err != nil {
				return err
			}
			return nil
		},
	})

	var status api.GameStatus
	switch result.Outcome {
	case game.WhiteWins:
		status = api.GameWhiteWins
	case game.BlackWins:
		status = api.GameBlackWins
	case game.Tie:
		status = api.GameDraw
	default:
		return fmt.Errorf("unhandled case. TODO %s", result.Outcome)
	}
	result.VictoryReason.String()

	updates := map[string]interface{}{
		"status": status.String(),
		"reason": result.VictoryReason.String(),
	}
	if err := matches.DB.First(new(api.Game), g.ID).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}