-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (133 loc) · 3.15 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
func main() {
_ = godotenv.Load()
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
delay := os.Getenv("TIMEOUT")
var timeout time.Duration
if delay == "" {
timeout = 60 * time.Minute
} else {
n, err := strconv.ParseInt(delay, 10, 64)
if err != nil {
log.Fatal(err)
}
timeout = time.Duration(n)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Infoln(conn.RemoteAddr().String(), "accepted")
go func(c net.Conn) {
defer closeConn(c)
c.Write([]byte("give me a name: "))
b := make([]byte, 64)
n, err := c.Read(b)
if err != nil {
log.Errorf("could not read name bytes: %v", err.Error())
return
}
name := toString(b, n)
log.Infof("name: %v %d", name, len(name))
if len(name) == 0 {
msg := "okok, privacy is nice but next time give me a name pls :3\n"
if _, err = c.Write([]byte(msg)); err != nil {
log.Errorf("could not write err name len bytes: %v", err.Error())
}
return
}
greet := fmt.Sprintf("Hi %s, lets play!\n", name)
n, err = c.Write([]byte(greet))
if err != nil {
log.Errorf("could not write greeting bytes: %v", err.Error())
return
}
log.Infoln("wrote", n, "bytes")
game, err := newGame(name)
if err != nil {
msg := fmt.Sprintf("%s\n", err.Error())
if _, err = c.Write([]byte(msg)); err != nil {
log.Error(err)
}
return
}
log.Infof("the game began")
if err := c.SetReadDeadline(time.Now().Add(timeout * time.Second)); err != nil {
log.Error(err)
return
}
for {
game.check()
// check if the game already ended
if game.Finished {
msg := ""
if game.Won {
msg = fmt.Sprintf("GG %s, here you have it flag{%s}\n", game.Player, game.Flag)
} else {
msg = "jaja noob, try again :3\n"
}
if _, err = c.Write([]byte(msg)); err != nil {
log.Error(err)
}
return
}
// if not bot's turn, update the state
if !game.PlayerMoves {
if err := game.counter(); err != nil {
log.Error(err)
return
}
continue
}
// print the current game state
game.Print(c)
// get input
c.Write([]byte("give me a position (comma separated): "))
b := make([]byte, 64)
n, err := c.Read(b)
if err != nil {
log.Errorf("could not read name bytes: %v", err.Error())
return
}
raw := toString(b, n)
pos := strings.Split(raw, ",")
if len(pos) != 2 {
if _, err = c.Write([]byte("wrong position format\n")); err != nil {
log.Error(err)
}
continue
}
x, errX := strconv.ParseInt(pos[0], 10, 64)
y, errY := strconv.ParseInt(pos[1], 10, 64)
if errX != nil || errY != nil {
if _, err = c.Write([]byte("wrong position format\n")); err != nil {
log.Error(err)
}
continue
}
msg, err := game.Move(int(x), int(y))
if err != nil {
if _, err = c.Write([]byte(msg)); err != nil {
log.Error(err)
}
continue
}
}
}(conn)
}
}