bgammon/cmd/bgammon-server/main.go

97 lines
2.5 KiB
Go
Raw Normal View History

2023-07-31 23:46:28 +00:00
package main
2023-09-09 07:01:01 +00:00
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
2023-11-30 21:17:30 +00:00
"code.rocket9labs.com/tslocum/bgammon/pkg/server"
2023-09-09 07:01:01 +00:00
)
2023-09-08 03:06:15 +00:00
2023-07-31 23:46:28 +00:00
func main() {
2023-09-12 05:10:04 +00:00
var (
tcpAddress string
wsAddress string
2023-11-22 18:12:06 +00:00
tz string
2023-11-22 00:16:33 +00:00
dataSource string
2023-12-14 03:04:02 +00:00
mailServer string
passwordSalt string
resetSalt string
2023-12-21 20:52:56 +00:00
verbose bool
debug int
2023-11-16 17:25:26 +00:00
debugCommands bool
rollStatistics bool
2023-09-12 05:10:04 +00:00
)
2023-09-09 07:01:01 +00:00
flag.StringVar(&tcpAddress, "tcp", "localhost:1337", "TCP listen address")
2023-09-12 05:10:04 +00:00
flag.StringVar(&wsAddress, "ws", "localhost:1338", "WebSocket listen address")
2023-11-22 18:12:06 +00:00
flag.StringVar(&tz, "tz", "", "Time zone used when calculating statistics")
2023-11-22 00:16:33 +00:00
flag.StringVar(&dataSource, "db", "", "Database data source (postgres://username:password@localhost:5432/database_name")
2023-12-14 03:04:02 +00:00
flag.StringVar(&mailServer, "smtp", "", "SMTP server address")
2023-12-21 20:52:56 +00:00
flag.BoolVar(&verbose, "verbose", false, "Print all client messages")
2023-09-09 07:01:01 +00:00
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
2023-11-16 17:25:26 +00:00
flag.BoolVar(&debugCommands, "debug-commands", false, "allow players to use restricted commands")
flag.BoolVar(&rollStatistics, "statistics", false, "print dice roll statistics and exit")
2023-09-08 03:06:15 +00:00
flag.Parse()
2023-08-02 06:05:33 +00:00
if dataSource == "" {
dataSource = os.Getenv("BGAMMON_DB")
}
2023-12-14 03:04:02 +00:00
if mailServer == "" {
mailServer = os.Getenv("BGAMMON_SMTP")
}
passwordSalt = os.Getenv("BGAMMON_SALT_PASSWORD")
resetSalt = os.Getenv("BGAMMON_SALT_RESET")
if rollStatistics {
printRollStatistics()
return
}
2023-09-12 05:10:04 +00:00
if tcpAddress == "" && wsAddress == "" {
log.Fatal("Error: A TCP and/or WebSocket listen address must be specified.")
}
2023-09-09 07:01:01 +00:00
if debug > 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debug), nil))
}()
}
2023-12-21 20:52:56 +00:00
s := server.NewServer(tz, dataSource, mailServer, passwordSalt, resetSalt, false, verbose || debug > 0, debugCommands)
2023-09-12 05:10:04 +00:00
if tcpAddress != "" {
2023-11-30 21:17:30 +00:00
s.Listen("tcp", tcpAddress)
2023-09-12 05:10:04 +00:00
}
if wsAddress != "" {
2023-11-30 21:17:30 +00:00
s.Listen("ws", wsAddress)
2023-09-12 05:10:04 +00:00
}
2023-08-02 06:05:33 +00:00
select {}
2023-07-31 23:46:28 +00:00
}
func printRollStatistics() {
var oneSame, doubles int
var lastroll1, lastroll2 int
total := 1000000
for i := 0; i < total; i++ {
2023-11-30 21:17:30 +00:00
roll1 := server.RandInt(6) + 1
roll2 := server.RandInt(6) + 1
if roll1 == lastroll1 || roll1 == lastroll2 || roll2 == lastroll1 || roll2 == lastroll2 {
oneSame++
}
if roll1 == roll2 {
doubles++
}
lastroll1, lastroll2 = roll1, roll2
}
log.Printf("total: %d, one same: %d (%.0f%%), doubles: %d (%.0f%%)", total, oneSame, float64(oneSame)/float64(total)*100, doubles, float64(doubles)/float64(total)*100)
}