-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add random bot. Fix bug where cards run out.
- Loading branch information
1 parent
ad383b8
commit 4b6c0a9
Showing
5 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package examplebot | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/marianogappa/truco/server" | ||
"github.com/marianogappa/truco/truco" | ||
) | ||
|
||
func Bot(playerID int, address string) { | ||
// 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) | ||
} | ||
defer conn.Close() | ||
|
||
// Hello message is meant to tell the server who we are, and request game state. | ||
// Game could be in progress (this could be a reconnection). | ||
if err := server.WsSend(conn, server.NewMessageHello(playerID)); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// On each iteration | ||
for { | ||
clientGameState, err := server.WsReadMessage[truco.ClientGameState, server.MessageHeresGameState](conn, server.MessageTypeHeresGameState) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if clientGameState.IsGameEnded { | ||
return | ||
} | ||
|
||
if clientGameState.TurnPlayerID != playerID { | ||
continue | ||
} | ||
|
||
// Get a random element from clientGameState.PossibleActions. | ||
randomAction := clientGameState.PossibleActions[rand.Intn(len(clientGameState.PossibleActions))] | ||
|
||
// Send the action to the server. | ||
if err := server.WsSend(conn, server.MessageAction{WebsocketMessage: server.WebsocketMessage{Type: server.MessageTypeAction}, Action: randomAction}); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters