ice_gametop.go
3.98 KB
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
package sdkdb
import (
"errors"
"fmt"
"log"
"strconv"
"github.com/astaxie/beego/orm"
)
type IceGameTop struct {
UID string `orm:"size(36);pk;unique"`
GTID string `orm:"size(36);unique"`
GID string `orm:"size(36);unique"`
TopType string `orm:"size(1);unique"`
MyKey string `orm:"size(20)"`
}
func (self *IceGameTop) String() string {
return fmt.Sprintf("UID[%s] GTID[%s] GID[%s] TopType[%s] MyKey[%s]", self.UID, self.GTID, self.GID, self.TopType, self.MyKey)
}
func IceGameTopInit() {
// 注册模型
orm.RegisterModel(new(IceGameTop))
}
/**
* 查询所有数据
*/
func IceGameTopMysqlQueryTotal() (iceGameTops []*IceGameTop, err error) {
o := orm.NewOrm()
var datalist []orm.Params
_, err = o.Raw("SELECT * FROM ice_gametop").Values(&datalist)
iceGameTops = []*IceGameTop{}
for _, data := range datalist {
igt := new(IceGameTop)
igt.UID = data["UID"].(string)
igt.GTID = data["GTID"].(string)
igt.GID = data["GID"].(string)
igt.TopType = data["TOPTYPE"].(string)
igt.MyKey = data["MYKEY"].(string)
iceGameTops = append(iceGameTops, igt)
}
return
}
/**
* 查询一条数据
*/
func IceGameTopMysqlQueryOne(uid string, gtid string, gid string, toptype string) (iceGameTop *IceGameTop, num int64, err error) {
o := orm.NewOrm()
var datalist []orm.Params
num, err = o.Raw("SELECT * FROM ice_gametop where uid=? and gtid=? and gid=? and toptype=?", uid, gtid, gid, toptype).Values(&datalist)
if num != 0 {
iceGameTop = new(IceGameTop)
iceGameTop.UID = datalist[0]["UID"].(string)
iceGameTop.GTID = datalist[0]["GTID"].(string)
iceGameTop.GID = datalist[0]["GID"].(string)
iceGameTop.TopType = datalist[0]["TOPTYPE"].(string)
iceGameTop.MyKey = datalist[0]["MYKEY"].(string)
}
return
}
/**
* 提交分数,如果不存在则insert,如果存在则update
*/
func IceGameTopMysqlSubmitScore(uid string, gtid string, gid string, score string) (err error) {
toptypes, _ := IceGameTopSetQueryToptype(gtid, gid)
//update time 2017-09-07 15:30:17 update by 徐振浩
//update start
if len(toptypes) == 0 {
err = errors.New("gametop name or game name not exist")
return
}
toptype := toptypes[0]
// myorm, _ := DBBegin()
myorm := orm.NewOrm()
// 先获得原有总榜分数
igt, num, _ := IceGameTopMysqlQueryOne(uid, gtid, gid, toptype)
var value string
if num != 0 {
value = igt.MyKey // 总榜分
} else {
value = "0"
}
value_float64, _ := strconv.ParseFloat(value, 64) // 总榜原有分数
score_float64, _ := strconv.ParseFloat(score, 64)
//判断排行榜类型,如果为顺序排序,则高分刷新,如果为倒序排序,则低分刷新
sortType, err := IceGameTopSetQuerySortType(gtid, gid)
log.Println(sortType)
if err != nil {
return
}
if sortType == "0" {
if value_float64-score_float64 > 0.000001 || value == "0" {
sqlstr := "INSERT INTO ice_gametop (uid,gtid,gid,toptype,mykey) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE mykey=?"
res, err := myorm.Raw(sqlstr, uid, gtid, gid, toptype, score_float64, score_float64).Exec()
if err != nil {
log.Printf("IceGameTopInsertOrUpdate err[%v]", err)
}
num, _ := res.RowsAffected()
log.Printf("IceGameTopInsertOrUpdate affected nums[%d]", num)
}
} else {
if score_float64-value_float64 > 0.000001 {
sqlstr := "INSERT INTO ice_gametop (uid,gtid,gid,toptype,mykey) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE mykey=?"
res, err := myorm.Raw(sqlstr, uid, gtid, gid, toptype, score_float64, score_float64).Exec()
if err != nil {
log.Printf("IceGameTopInsertOrUpdate err[%v]", err)
}
num, _ := res.RowsAffected()
log.Printf("IceGameTopInsertOrUpdate affected nums[%d]", num)
}
}
//updat end
return
}
func IceGameTopCleanScore() (err error) {
myorm, _ := DBBegin()
sqlstr := "UPDATE ice_gametop set mykey=? where toptype <> 0"
res, err := myorm.Raw(sqlstr, 0).Exec()
if err != nil {
log.Printf("IceGameTopCleanScore err[%v]", err)
}
num, _ := res.RowsAffected()
log.Printf("IceGameTopCleanScore affected nums[%d]", num)
DBCommit(myorm)
return
}