-
Notifications
You must be signed in to change notification settings - Fork 55
/
commands.go
74 lines (63 loc) · 1.66 KB
/
commands.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
package main
import (
"log"
"code.google.com/p/go.net/websocket"
"github.com/bitly/go-simplejson"
)
type commandHandler func(this *player, command string, param *simplejson.Json)
type jsonReturn struct {
Code int
Message interface{}
}
type jsonResponse struct {
Command string
Return jsonReturn
}
func responseJson(command string, code int, message interface{}) string {
msg := jsonResponse{
Command : command,
Return : jsonReturn{
Code : code,
Message : message,
},
}
return makeJson(msg)
}
// handler map for "Command"
var cmHandlers = map[string]commandHandler{
"CM_REGISTER" : cmRegisterHander,
"CM_LOGIN" : cmLoginHander,
"CM_CHAR_CREATE" : cmCharCreateHander,
"CM_CHAR_GET" : cmCharGetHander,
"CM_CARDS_GET" : cmCardsGetHander,
"CM_RAID" : cmRaidHander,
}
func commandDispatcher(this *player, js *simplejson.Json) {
rtnCode := 0
command := ""
// defer need to be placed at header of func
defer func() {
// only send error message here
if 0 != rtnCode {
rtnMsg, _ := errCodes[rtnCode]
rtnJson := responseJson(command, rtnCode, rtnMsg)
if err := websocket.Message.Send(this.ws, rtnJson); err != nil {
log.Printf("Send fail for commandDispatcher")
}
}
}()
command, err := js.Get("Command").String()
if err != nil {
rtnCode = 1
return
}
param, ok := js.CheckGet("Param")
if ok {
handler, ok := cmHandlers[command]
if ok {
handler(this, command, param)
return
}
}
rtnCode = 1
}