-
Notifications
You must be signed in to change notification settings - Fork 55
/
cmChar.go
118 lines (100 loc) · 2.81 KB
/
cmChar.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
package main
import (
"log"
"encoding/json"
"code.google.com/p/go.net/websocket"
"github.com/bitly/go-simplejson"
)
func queryCharInfo(this *player) int {
rows, err := gamedb.Query(`SELECT cname, clevel, vitality, scene, stage, cards
FROM charinfo WHERE cid=?`, this.charID)
if err != nil {
return 2
}
var cards string
for rows.Next() {
err = rows.Scan(&this.character.CharName, &this.character.Level,
&this.character.Vitality,&this.character.Scene,
&this.character.Stage, &cards)
if err != nil {
return 2
}
// decode cards
err = json.Unmarshal([]byte(cards), &this.character.cards)
if err != nil {
panic(err)
}
}
return 0
}
func cmCharCreateHander(this *player, command string, param *simplejson.Json) {
rtnCode := 0
defer func() {
var rtnMsg interface{}
if 0 == rtnCode {
rtnMsg = this.character
} else {
rtnMsg, _ = errCodes[rtnCode]
}
rtnJson := responseJson(command, rtnCode, rtnMsg)
if err := websocket.Message.Send(this.ws, rtnJson); err != nil {
log.Printf("Send fail for cmCharCreateHander")
}
}()
//check if login
if this.userID == 0 {
rtnCode = 6
return
}
// get charname from json
charname, err := param.Get("CharName").String()
if err != nil {
rtnCode = 1
return
}
// insert to table charinfo
stmt, err := gamedb.Prepare("INSERT charinfo SET uid=?, cname=?, cards=?")
if err != nil {
rtnCode = 2
return
}
// TODO: read default vaule from config
cards := `[{"ID":1, "Level":1, "Status":1},{"ID":2, "Level":1, "Status":1}]`//json array
res, err := stmt.Exec(this.userID, charname, cards)
if err != nil {
log.Print(err)
rtnCode = 5
return
}
cid, err := res.LastInsertId()
if err != nil {
rtnCode = 2
log.Printf("Error: database not support LastInsertId()")
return
}
// safe cast for mysql INT
this.charID = uint32(cid)
// query that we jsut insert
rtnCode = queryCharInfo(this)
}
func cmCharGetHander(this *player, command string, param *simplejson.Json) {
rtnCode := 0
defer func() {
var rtnMsg interface{}
if 0 == rtnCode {
rtnMsg = this.character
} else {
rtnMsg, _ = errCodes[rtnCode]
}
rtnJson := responseJson(command, rtnCode, rtnMsg)
if err := websocket.Message.Send(this.ws, rtnJson); err != nil {
log.Printf("Send fail for cmCharGetHander")
}
}()
//check if login
if this.userID == 0 || this.charID == 0{
rtnCode = 6
return
}
rtnCode = queryCharInfo(this)
}