-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 1.32 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"hackarena2-0-mono-tanks-go/args"
"hackarena2-0-mono-tanks-go/ws_client"
)
func main() {
app := args.NewCLIApp()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
parsedArgs, ok := app.Metadata["args"].(*args.Args)
if !ok || parsedArgs == nil {
log.Fatal("[System] 🌋 Error: Failed to retrieve parsed arguments")
}
fmt.Println("[System] 🚀 Starting bot...")
if err := startWebSocketClient(parsedArgs); err != nil {
log.Printf("[System] 🌋 Error: %v", err)
}
fmt.Println("[System] 🏁 Bot stopped")
}
func startWebSocketClient(parsedArgs *args.Args) error {
websocketClient := ws_client.NewWebSocketClient()
err := websocketClient.Connect(parsedArgs.Host, int(parsedArgs.Port), parsedArgs.Code, parsedArgs.Nickname)
if err != nil {
return fmt.Errorf("connecting to the server: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle interrupt signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
fmt.Println("\n[System] 🛑 Interrupt received, shutting down...")
cancel()
}()
if err := websocketClient.Run(ctx); err != nil {
return fmt.Errorf("running WebSocket client: %w", err)
}
return nil
}