<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/danmane/abalone/go/api"
	"github.com/danmane/abalone/go/game"
)

var (
	aiPort1   = flag.String("aiPort1", "3423", "port for first ai")
	aiPort2   = flag.String("aiPort2", "3424", "port for second ai (if present)")
	timelimit = flag.Duration("timelimit", time.Second*2, "per-move time limit")
)

func main() {
	flag.Parse()
	fmt.Println("game operater operating!")
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	whiteAI := api.Player{}
	blackAI := api.Player{}
	whiteAgent := PlayerInstance{Player: whiteAI, Port: *aiPort1}
	blackAgent := PlayerInstance{Player: blackAI, Port: *aiPort2}
	start := game.Standard
	playAIGame(whiteAgent, blackAgent, start)
	// fmt.Println(result)
	return nil
}

type PlayerInstance struct {
	Player api.Player
	Port   string
}

func toMillisecondCount(d time.Duration) int64 {
	return int64(d / 1e6)
}

func gameFromAI(port string, state *game.State) (*game.State, error) {
	mr := api.MoveRequest{
		State:      *state,
		LimitMilli: toMillisecondCount(api.DefaultMoveLimit),
	}
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(&mr); err != nil {
		return nil, err
	}
	resp, err := http.Post("http://localhost:"+port+api.MovePath, "application/json", &buf)
	if err != nil {
		return nil, err
	}
	responseGame := &game.State{}
	if err := json.NewDecoder(resp.Body).Decode(responseGame); err != nil {
		return nil, err
	}
	resp.Body.Close()
	if !state.ValidFuture(responseGame) {
		return nil, fmt.Errorf("game parsed correctly, but isn't a valid future")
	}
	return responseGame, nil
}

func playAIGame(whiteAgent, blackAgent PlayerInstance, startState game.State) api.GameResult {
	states := []game.State{startState}
	currentGame := &startState
	victory := api.NoVictory
	outcome := game.NullOutcome
	var moves int = 0
	for !currentGame.GameOver() {
		var nextAI PlayerInstance
		fmt.Printf("move %v (%v)\n", moves, currentGame.NextPlayer.String())
		moves++
		if currentGame.NextPlayer == game.White {
			nextAI = whiteAgent<commit_msg>fix(operator) 'defer' closing of response body<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/danmane/abalone/go/api"
	"github.com/danmane/abalone/go/game"
)

var (
	aiPort1   = flag.String("aiPort1", "3423", "port for first ai")
	aiPort2   = flag.String("aiPort2", "3424", "port for second ai (if present)")
	timelimit = flag.Duration("timelimit", time.Second*2, "per-move time limit")
)

func main() {
	flag.Parse()
	fmt.Println("game operater operating!")
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	whiteAI := api.Player{}
	blackAI := api.Player{}
	whiteAgent := PlayerInstance{Player: whiteAI, Port: *aiPort1}
	blackAgent := PlayerInstance{Player: blackAI, Port: *aiPort2}
	start := game.Standard
	playAIGame(whiteAgent, blackAgent, start)
	// fmt.Println(result)
	return nil
}

type PlayerInstance struct {
	Player api.Player
	Port   string
}

func toMillisecondCount(d time.Duration) int64 {
	return int64(d / 1e6)
}

func gameFromAI(port string, state *game.State) (*game.State, error) {
	mr := api.MoveRequest{
		State:      *state,
		LimitMilli: toMillisecondCount(api.DefaultMoveLimit),
	}
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(&mr); err != nil {
		return nil, err
	}
	resp, err := http.Post("http://localhost:"+port+api.MovePath, "application/json", &buf)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	responseGame := &game.State{}
	if err := json.NewDecoder(resp.Body).Decode(responseGame); err != nil {
		return nil, err
	}
	if !state.ValidFuture(responseGame) {
		return nil, fmt.Errorf("game parsed correctly, but isn't a valid future")
	}
	return responseGame, nil
}

func playAIGame(whiteAgent, blackAgent PlayerInstance, startState game.State) api.GameResult {
	states := []game.State{startState}
	currentGame := &startState
	victory := api.NoVictory
	outcome := game.NullOutcome
	var moves int = 0
	for !currentGame.GameOver() {
		var nextAI PlayerInstance
		fmt.Printf("move %v (%v)\n", moves, currentGame.NextPlayer.String())
		moves++
		if currentGame.NextPlayer == game.White {
			nextAI = whiteAgent
{'fraction_non_alphanumeric': 0.14761350407450524, 'fraction_numerical': 0.010244470314318976, 'mean_word_length': 4.4587039390088945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4494405', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1533, 'n_words': 369}