-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
54 lines (42 loc) · 931 Bytes
/
handler.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
package waboorrt
import (
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/rpc/v2"
"github.com/flipdot/waboorrt-go/actions"
)
type Bot interface {
NextAction(state *GameState) actions.Action
Debug(msg string)
EnableDebug()
msgBuffer() []string
}
func Run(bot Bot) error {
s := rpc.NewServer()
c := newCustomCodec()
s.RegisterCodec(c, "application/json")
s.RegisterCodec(c, "application/json;charset=UTF-8")
if err := s.RegisterService(newWrapper(bot), "Bot"); err != nil {
return err
}
r := mux.NewRouter()
r.Handle("/jsonrpc", s)
return http.ListenAndServe(":4000", r)
}
type BotDebugger struct {
messages []string
debug bool
}
func (d *BotDebugger) EnableDebug() {
d.debug = true
}
func (d *BotDebugger) Debug(msg string) {
if d.debug {
d.messages = append(d.messages, msg)
}
}
func (d *BotDebugger) msgBuffer() []string {
ret := d.messages
d.messages = []string{}
return ret
}