-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (59 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build !tinygo
// +build !tinygo
package main
import (
"fmt"
"os"
"strconv"
"github.com/marianogappa/truco/botclient"
"github.com/marianogappa/truco/examplebot/newbot"
"github.com/marianogappa/truco/exampleclient"
"github.com/marianogappa/truco/server"
)
func main() {
if len(os.Args) < 2 {
usage()
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
cmd := os.Args[1]
address := fmt.Sprintf("localhost:%v", port)
if len(os.Args) >= 4 {
address = os.Args[3]
}
var (
playerNum int
err error
)
if cmd == "player" || cmd == "bot" {
playerNum, err = strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Invalid player number. Please provide a number.")
usage()
}
}
switch cmd {
case "server":
server.New(port).Start()
case "player":
exampleclient.Player(playerNum-1, address)
case "bot":
botclient.Bot(playerNum-1, address, newbot.New(newbot.WithDefaultLogger))
default:
fmt.Println("Invalid argument. Please provide either server or client.")
}
}
func usage() {
fmt.Println("usage: truco server")
fmt.Println("usage: truco player %number [address]")
fmt.Println("usage: truco bot %number [address]")
fmt.Println("usage: e.g. truco player 1")
fmt.Println("usage: e.g. truco player 2")
fmt.Println("usage: e.g. truco player 1 localhost:8080")
fmt.Println("usage: e.g. truco bot 1 localhost:8080")
fmt.Println("usage: e.g. truco bot 2")
fmt.Println("Define the PORT environment variable for truco server to change the default port (8080).")
os.Exit(1)
}