forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm.go
116 lines (97 loc) · 2.96 KB
/
gm.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
package web
import (
"context"
"net/http"
"strings"
"time"
"errors"
"github.com/lonnng/nex"
log "github.com/sirupsen/logrus"
"github.com/lonnng/nanoserver/cmd/mahjong/game"
"github.com/lonnng/nanoserver/cmd/mahjong/web/service/login"
"github.com/lonnng/nanoserver/internal/errutil"
"github.com/lonnng/nanoserver/db"
"github.com/lonnng/nanoserver/internal/protocol"
)
func authFilter(_ context.Context, r *http.Request) (context.Context, error) {
parts := strings.Split(r.RemoteAddr, ":")
if len(parts) < 2 {
return context.Background(), errutil.YXErrPermissionDenied
}
if parts[0] != "127.0.0.1" {
return context.Background(), errutil.YXErrPermissionDenied
}
return context.Background(), nil
}
func broadcast(query *nex.Form) (*protocol.StringMessage, error) {
message := strings.TrimSpace(query.Get("message"))
if message == "" || len(message) < 5 {
return nil, errors.New("消息不可小于5个字")
}
login.AddMessage(message)
game.BroadcastSystemMessage(message)
return protocol.SuccessMessage, nil
}
func resetPlayerHandler(query *nex.Form) (*protocol.StringMessage, error) {
uid := query.Int64OrDefault("uid", -1)
if uid <= 0 {
return nil, errutil.YXErrIllegalParameter
}
log.Infof("手动重置玩家数据: Uid=%d", uid)
game.Reset(uid)
return protocol.SuccessMessage, nil
}
func kickHandler(query *nex.Form) (*protocol.StringMessage, error) {
uid := query.Int64OrDefault("uid", -1)
if uid <= 0 {
return nil, errutil.YXErrIllegalParameter
}
log.Infof("踢玩家下线: Uid=%d", uid)
if err := game.Kick(uid); err != nil {
return nil, err
}
return protocol.SuccessMessage, nil
}
func onlineHandler(query *nex.Form) (interface{}, error) {
begin := query.Int64OrDefault("begin", 0)
end := query.Int64OrDefault("end", -1)
if end < 0 {
end = time.Now().Unix()
}
log.Infof("获取在线数据信息: begin=%s, end=%s", time.Unix(begin, 0).String(), time.Unix(end, 0).String())
return db.OnlineStats(begin, end)
}
func rechargeHandler(data *protocol.RechargeRequest) (*protocol.StringMessage, error) {
if data.Uid < 1 || data.Count < 1 {
return nil, errutil.YXErrIllegalParameter
}
u, err := db.QueryUser(data.Uid)
if err != nil {
return nil, err
}
u.Coin += data.Count
if err := db.UpdateUser(u); err != nil {
return nil, err
}
// 通知客户端
game.Recharge(u.Id, u.Coin)
log.Infof("给玩家充值: Uid=%d, end=%d", data.Uid, data.Count)
return protocol.SuccessMessage, nil
}
// http://127.0.0.1:12306/v1/gm/consume?consume="4/1,8/1,16/2"
func cardConsumeHandler(query *nex.Form) (*protocol.StringMessage, error) {
consume := query.Get("consume")
if consume == "" {
return nil, errutil.YXErrIllegalParameter
}
log.Infof("手动重置房卡消耗数据: %s", consume)
game.SetCardConsume(consume)
return protocol.SuccessMessage, nil
}
func userInfoHandler(query *nex.Form) (interface{}, error) {
id := query.Int64OrDefault("id", -1)
if id <= 0 {
return nil, errutil.YXErrIllegalParameter
}
return db.QueryUserInfo(id)
}