func fightN(f1, f2 Factory, n int) []MatchResult { aiA := &aiAdapter{factory: f1, games: make(map[string]gameState)} aiB := &aiAdapter{factory: f2, games: make(map[string]gameState)} a1, a2 := net.Pipe() b1, b2 := net.Pipe() ca1, ca2 := rpc.StreamTransport(a1), rpc.StreamTransport(a2) cb1, cb2 := rpc.StreamTransport(b1), rpc.StreamTransport(b2) // Server-side srvA := botapi.Ai_ServerToClient(aiA) srvB := botapi.Ai_ServerToClient(aiB) serverConnA := rpc.NewConn(ca1, rpc.MainInterface(srvA.Client)) serverConnB := rpc.NewConn(cb1, rpc.MainInterface(srvB.Client)) defer serverConnA.Wait() defer serverConnB.Wait() // Client-side ctx := context.Background() clientConnA := rpc.NewConn(ca2) clientConnB := rpc.NewConn(cb2) defer clientConnA.Close() defer clientConnB.Close() clientA := localAI{botapi.Ai{Client: clientConnA.Bootstrap(ctx)}} clientB := localAI{botapi.Ai{Client: clientConnB.Bootstrap(ctx)}} matchRes := make([]MatchResult, n) // Run the game for i := 0; i < n; i++ { b := engine.EmptyBoard(engine.DefaultConfig) b.InitBoard(engine.DefaultConfig) for !b.IsFinished() { turnCtx, _ := context.WithTimeout(ctx, 30*time.Second) resA, _ := clientA.takeTurn(turnCtx, strconv.Itoa(i), b, engine.P1Faction) resB, _ := clientB.takeTurn(turnCtx, strconv.Itoa(i), b, engine.P2Faction) b.Update(resA, resB) } matchRes[i] = MatchResult{ P1Score: b.BotCount(1), P2Score: b.BotCount(2), } } return matchRes }
// RegisterAI adds an AI implementation for the token given by the website. // The AI factory function will be called for each new game encountered. func (c *Client) RegisterAI(name, token string, factory Factory) error { a := botapi.Ai_ServerToClient(&aiAdapter{ factory: factory, games: make(map[string]gameState), }) _, err := c.connector.Connect(context.TODO(), func(r botapi.ConnectRequest) error { creds, err := r.NewCredentials() if err != nil { return err } err = creds.SetBotName(name) if err != nil { return err } err = creds.SetSecretToken(token) if err != nil { return err } r.SetAi(a) return nil }).Struct() return err }