-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (157 loc) · 3.47 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
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/rpc"
"os"
"time"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/encoding"
"github.com/kelseyhightower/envconfig"
"0x539.lol/rtc/board"
"0x539.lol/rtc/display"
"0x539.lol/rtc/game"
"0x539.lol/rtc/util"
)
//Config encapsulates runtime config of RTC application for use by envconfig
type Config struct {
HostA string `default:"localhost"`
PortA string `default:"1234"`
HostB string `default:"localhost"`
PortB string `default:"4321"`
ID string `default:"A"`
Debug bool `default:"false"`
}
type GameEngine struct {
ID string
game *game.Game
rpc *game.GameRPC
listener net.Listener
}
type ExitCode int
const (
Success ExitCode = iota
Failure
)
func main() {
var c Config
err := envconfig.Process("rtc", &c)
if err != nil {
log.Fatal(err.Error())
}
os.Exit(int(entrypoint(c)))
}
func setup(c Config) (GameEngine, error) {
var b *board.Board
var remotehost string
var remoteport string
var localhost string
var localport string
if c.ID == "A" {
localhost = c.HostA
localport = c.PortA
remotehost = c.HostB
remoteport = c.PortB
b = board.New(board.White)
} else {
localhost = c.HostB
localport = c.PortB
remotehost = c.HostA
remoteport = c.PortA
b = board.New(board.Black)
}
g, gr := game.New(b)
//register RPC
rpc.Register(gr)
mux := http.NewServeMux()
http.DefaultServeMux = mux
rpc.HandleHTTP()
l, e := net.Listen("tcp", localhost+":"+localport)
if e != nil {
log.Fatal("listen error:", e)
}
go http.Serve(l, nil)
util.Write(fmt.Sprintf("Listening with listener: %v", l))
dialCount := 1
for {
//init client for peer
util.Write(fmt.Sprintf("Dial #%d", dialCount))
client, e := rpc.DialHTTP("tcp", remotehost+":"+remoteport)
if e != nil {
util.Write(fmt.Sprintf("error attempting to dial %s:%s: %s", remotehost, remoteport, e))
if dialCount > 10 {
return GameEngine{}, e
}
} else {
g.SetClient(client)
break
}
dialCount = dialCount + 1
/*
switch ev := s.PollEvent().(type) {
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
s.Fini()
return Success
}
}
*/
time.Sleep(time.Duration(1) * time.Second)
}
return GameEngine{c.ID, g, gr, l}, nil
}
func entrypoint(c Config) ExitCode {
util.Debug = c.Debug
util.Write(fmt.Sprintf("Config: %v", c))
//setup display
encoding.Register()
s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
return Failure
}
if e := s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
return Failure
}
defStyle := tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)
display.Loading(s)
engine, e := setup(c)
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
return Failure
}
defer engine.listener.Close()
go func(g *game.Game, s tcell.Screen) {
t := time.NewTicker(10 * time.Millisecond)
for range t.C {
display.Render(g, s)
}
}(engine.game, s)
for {
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
s.Sync()
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
s.Fini()
return Failure
} else {
util.Write(fmt.Sprintf("Event registered: %v", ev))
move := engine.game.RecieveEvent(ev)
util.Write(fmt.Sprintf("Move generated: %v", move))
var rpcExitCode int
if move.Seq > 0 {
engine.game.SendMove(move)
engine.rpc.DoMove(move, &rpcExitCode)
}
display.Render(engine.game, s)
}
}
}
}