Skip to content

Commit

Permalink
Implement comprehensive changes to enable UI client.
Browse files Browse the repository at this point in the history
- Add bot interface and bot implementation.
- Replace turn-based concept to available actions concept.
- Add "confirm_round_end" action to simplify UI.
- Modify client logic to always accept actions.
  • Loading branch information
marianogappa committed Jul 13, 2024
1 parent 48c46aa commit a2fd933
Show file tree
Hide file tree
Showing 25 changed files with 1,840 additions and 224 deletions.
18 changes: 10 additions & 8 deletions examplebot/main.go → botclient/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//go:build !tinygo
// +build !tinygo

package examplebot
package botclient

import (
"encoding/json"
"fmt"
"log"
"math/rand"
"time"

"github.com/gorilla/websocket"
"github.com/marianogappa/truco/server"
"github.com/marianogappa/truco/truco"
)

func Bot(playerID int, address string) {
func Bot(playerID int, address string, bot truco.Bot) {
// Open the WebSocket connection, and send a hello message.

conn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://%v/ws", address), nil)
if err != nil {
log.Fatalf("Failed to connect to WebSocket server: %v", err)
Expand All @@ -39,15 +39,17 @@ func Bot(playerID int, address string) {
return
}

if clientGameState.TurnPlayerID != playerID {
botAction := bot.ChooseAction(*clientGameState)

if botAction == nil {
time.Sleep(1 * time.Second)
continue
}

// Get a random element from clientGameState.PossibleActions.
randomAction := clientGameState.PossibleActions[rand.Intn(len(clientGameState.PossibleActions))]
bs, _ := json.Marshal(botAction)

// Send the action to the server.
if err := server.WsSend(conn, server.MessageAction{WebsocketMessage: server.WebsocketMessage{Type: server.MessageTypeAction}, Action: randomAction}); err != nil {
if err := server.WsSend(conn, server.MessageAction{WebsocketMessage: server.WebsocketMessage{Type: server.MessageTypeAction}, Action: bs}); err != nil {
log.Fatal(err)
}
}
Expand Down
Loading

0 comments on commit a2fd933

Please sign in to comment.