Skip to content

Commit 88519be

Browse files
committed
完善经典端中活动奖励接口
1 parent 0b2ef29 commit 88519be

File tree

5 files changed

+192
-16
lines changed

5 files changed

+192
-16
lines changed

bhandler/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
packetTypeLogout byte = 0xA4
1111
packetTypeKeep byte = 0xA6
1212
packetTypeKick byte = 0xA9
13+
packetTypePrizeFetch byte = 0xC3
1314
packetTypeCostLog byte = 0xC5
1415
packetTypePrizeCard byte = 0xC6
1516
packetTypeConvertPoint byte = 0xE1

bhandler/prize_fetch_handler.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package bhandler
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/liuguangw/billing_go/common"
7+
"github.com/liuguangw/billing_go/models"
8+
"github.com/liuguangw/billing_go/services"
9+
"go.uber.org/zap"
10+
"golang.org/x/text/encoding/simplifiedchinese"
11+
)
12+
13+
// PrizeFetchHandler 活动奖励领取(仅经典), 脚本ID: 808062
14+
type PrizeFetchHandler struct {
15+
Resource *common.HandlerResource
16+
}
17+
18+
// GetType 可以处理的消息类型
19+
func (*PrizeFetchHandler) GetType() byte {
20+
return packetTypePrizeFetch
21+
}
22+
23+
// GetResponse 根据请求获得响应
24+
func (h *PrizeFetchHandler) GetResponse(request *common.BillingPacket) *common.BillingPacket {
25+
response := request.PrepareResponse()
26+
packetReader := services.NewPacketDataReader(request.OpData)
27+
//用户名
28+
usernameLength := packetReader.ReadByteValue()
29+
tmpLength := int(usernameLength)
30+
username := packetReader.ReadBytes(tmpLength)
31+
//登录IP
32+
tmpLength = int(packetReader.ReadByteValue())
33+
loginIP := string(packetReader.ReadBytes(tmpLength))
34+
//角色名
35+
tmpLength = int(packetReader.ReadByteValue())
36+
charNameGbkData := packetReader.ReadBytes(tmpLength)
37+
gbkDecoder := simplifiedchinese.GBK.NewDecoder()
38+
charName, err := gbkDecoder.Bytes(charNameGbkData)
39+
if err != nil {
40+
h.Resource.Logger.Error("decode char name failed: " + err.Error())
41+
charName = []byte("?")
42+
}
43+
//标记在线
44+
clientInfo := &common.ClientInfo{
45+
IP: loginIP,
46+
CharName: string(charName),
47+
}
48+
markOnline(h.Resource.LoginUsers, h.Resource.OnlineUsers, h.Resource.MacCounters, string(username), clientInfo)
49+
50+
//角色id
51+
charguid := packetReader.ReadInt()
52+
//等级
53+
charLv := packetReader.ReadUint16()
54+
//订单号
55+
prizeSerial := packetReader.ReadBytes(21)
56+
//初始化result
57+
var fetchResult byte
58+
prizeList, err := models.FindAccountPrizeList(h.Resource.Db, string(username), 0, 0, 30)
59+
if err != nil {
60+
fetchResult = 3
61+
h.Resource.Logger.Error("FindAccountPrizeList failed: " + err.Error())
62+
}
63+
//记录条数
64+
prizeCount := len(prizeList)
65+
if prizeCount == 0 {
66+
//没有奖励可以领取
67+
fetchResult = 5
68+
}
69+
//Packets::LBLNewPrize
70+
opDataLength := int(usernameLength) + 23
71+
if fetchResult == 0 {
72+
opDataLength += (21*prizeCount + 1)
73+
}
74+
opData := make([]byte, 0, opDataLength)
75+
opData = append(opData, usernameLength)
76+
opData = append(opData, username...)
77+
opData = append(opData, prizeSerial...)
78+
opData = append(opData, fetchResult)
79+
if fetchResult == 0 {
80+
opData = append(opData, byte(prizeCount))
81+
var itemIdList []int64
82+
for _, prizeItem := range prizeList {
83+
//id
84+
itemIdList = append(itemIdList, prizeItem.ID)
85+
//itemid
86+
itemIdData := make([]byte, 20)
87+
itemIdStrData := []byte("item " + strconv.Itoa(prizeItem.ItemID))
88+
copy(itemIdData, itemIdStrData)
89+
opData = append(opData, itemIdData...)
90+
//itemNum
91+
itemNum := prizeItem.ItemNum
92+
opData = append(opData, byte(itemNum&0xFF))
93+
h.Resource.Logger.Info("add prize item for "+string(charName)+": ",
94+
zap.Int64("id", prizeItem.ID),
95+
zap.Uint16("charLv", charLv),
96+
zap.String("username", string(username)),
97+
zap.Int("charguid", charguid),
98+
zap.Int("itemID", prizeItem.ItemID),
99+
zap.Int("itemNum", prizeItem.ItemNum),
100+
)
101+
}
102+
//标记为已使用
103+
if err := models.MarkGetAccountPrize(h.Resource.Db, itemIdList); err != nil {
104+
h.Resource.Logger.Error("MarkGetAccountPrize failed: " + err.Error())
105+
}
106+
}
107+
response.OpData = opData
108+
return response
109+
}

bhandler/prize_handler.go

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bhandler
22

33
import (
4+
"strconv"
5+
46
"github.com/liuguangw/billing_go/common"
57
"github.com/liuguangw/billing_go/models"
68
"github.com/liuguangw/billing_go/services"
@@ -30,7 +32,10 @@ func (*PrizeHandler) GetType() byte {
3032
func (h *PrizeHandler) GetResponse(request *common.BillingPacket) *common.BillingPacket {
3133
response := request.PrepareResponse()
3234
packetReader := services.NewPacketDataReader(request.OpData)
33-
prizeType := packetReader.ReadByteValue()
35+
var prizeType byte
36+
if h.BillType == common.BillTypeHuaiJiu {
37+
prizeType = packetReader.ReadByteValue()
38+
}
3439
//用户名
3540
usernameLength := packetReader.ReadByteValue()
3641
tmpLength := int(usernameLength)
@@ -53,23 +58,65 @@ func (h *PrizeHandler) GetResponse(request *common.BillingPacket) *common.Billin
5358
CharName: string(charName),
5459
}
5560
markOnline(h.Resource.LoginUsers, h.Resource.OnlineUsers, h.Resource.MacCounters, string(username), clientInfo)
56-
//世界id
57-
worldId := packetReader.ReadLeUint16()
58-
//角色id
59-
charguid := packetReader.ReadLeInt()
60-
if prizeType == prizeTypeCheck {
61-
h.processCheck(username, worldId, charguid, response)
62-
} else if prizeType == prizeTypeFetch {
63-
//领取类型
64-
fetchType := packetReader.ReadByteValue()
65-
h.processFetch(fetchType, charName, username, worldId, charguid, response)
61+
if h.BillType == common.BillTypeHuaiJiu {
62+
//世界id
63+
worldId := packetReader.ReadLeUint16()
64+
//角色id
65+
charguid := packetReader.ReadLeInt()
66+
if prizeType == prizeTypeCheck {
67+
h.processCheck(username, worldId, charguid, response)
68+
} else if prizeType == prizeTypeFetch {
69+
//领取类型
70+
fetchType := packetReader.ReadByteValue()
71+
h.processFetch(fetchType, charName, username, worldId, charguid, response)
72+
}
73+
} else {
74+
h.processCheck1(username, response)
6675
}
67-
//debug
68-
//h.Resource.Logger.Info("dump response\n" + response.String())
6976
return response
7077
}
7178

72-
// processCheck 处理查询奖励
79+
// processCheck1 处理查询奖励(经典端)
80+
func (h *PrizeHandler) processCheck1(username []byte, response *common.BillingPacket) {
81+
var checkResult byte
82+
prizeList, err := models.FindAccountPrizeList(h.Resource.Db, string(username), 0, 0, 30)
83+
if err != nil {
84+
checkResult = 3
85+
h.Resource.Logger.Error("FindAccountPrizeList failed: " + err.Error())
86+
}
87+
//记录条数
88+
prizeCount := len(prizeList)
89+
if prizeCount == 0 {
90+
//没有奖励可以领取
91+
checkResult = 5
92+
}
93+
usernameLength := len(username)
94+
opDataLength := usernameLength + 2
95+
if checkResult == 0 {
96+
opDataLength += (21*prizeCount + 1)
97+
}
98+
//Packets::LBLNewCheckPrize
99+
opData := make([]byte, 0, opDataLength)
100+
opData = append(opData, byte(usernameLength))
101+
opData = append(opData, username...)
102+
opData = append(opData, checkResult)
103+
if checkResult == 0 {
104+
opData = append(opData, byte(prizeCount))
105+
for _, prizeItem := range prizeList {
106+
//itemid
107+
itemIdData := make([]byte, 20)
108+
itemIdStrData := []byte("item " + strconv.Itoa(prizeItem.ItemID))
109+
copy(itemIdData, itemIdStrData)
110+
opData = append(opData, itemIdData...)
111+
//itemNum
112+
itemNum := prizeItem.ItemNum
113+
opData = append(opData, byte(itemNum&0xFF))
114+
}
115+
}
116+
response.OpData = opData
117+
}
118+
119+
// processCheck 处理怀旧端查询奖励
73120
func (h *PrizeHandler) processCheck(username []byte, worldId uint16, charguid int, response *common.BillingPacket) {
74121
usernameLength := len(username)
75122
opData := make([]byte, 0, usernameLength+15)
@@ -102,7 +149,7 @@ func (h *PrizeHandler) processCheck(username []byte, worldId uint16, charguid in
102149
response.OpData = opData
103150
}
104151

105-
// processFetch 处理领取奖励
152+
// processFetch 处理怀旧端领取奖励
106153
func (h *PrizeHandler) processFetch(fetchType byte, charName, username []byte, worldId uint16, charguid int, response *common.BillingPacket) {
107154
inputWorldId := 0
108155
inputCharguid := 0
@@ -128,7 +175,7 @@ func (h *PrizeHandler) processFetch(fetchType byte, charName, username []byte, w
128175
if prizeCount > 0 {
129176
opDataLength += (22*prizeCount + 1)
130177
}
131-
opData := make([]byte, 0, usernameLength+3)
178+
opData := make([]byte, 0, opDataLength)
132179
opData = append(opData, prizeTypeFetchRet, byte(usernameLength))
133180
opData = append(opData, username...)
134181
var fetchResult byte

models/account_prize.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import (
66
)
77

88
// AccountPrize 对应奖励表
9+
//
10+
// 创建表的SQL语句
11+
/*
12+
CREATE TABLE account_prize (
13+
id bigint(20) NOT NULL AUTO_INCREMENT,
14+
account varchar(50) NOT NULL COMMENT '账号',
15+
world int(11) NOT NULL DEFAULT '0' COMMENT '世界ID',
16+
charguid int(10) unsigned NOT NULL DEFAULT '0' COMMENT '玩家GUID',
17+
itemid int(10) unsigned NOT NULL DEFAULT '0' COMMENT '物品ID',
18+
itemnum int(11) NOT NULL COMMENT '物品数量',
19+
isget smallint(6) NOT NULL COMMENT '是否领取了',
20+
validtime int(11) NOT NULL COMMENT '有效期,时间格式为unix时间',
21+
PRIMARY KEY (id) USING BTREE,
22+
UNIQUE KEY id (id) USING BTREE
23+
)
24+
*/
925
type AccountPrize struct {
1026
ID int64
1127
Account string

services/billing/load_handlers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func (s *Server) loadHandlers(cancel context.CancelFunc) {
5151
Resource: resource,
5252
},
5353
&bhandler.KickHandler{},
54+
&bhandler.PrizeFetchHandler{
55+
Resource: resource,
56+
},
5457
&bhandler.CostLogHandler{
5558
Resource: resource,
5659
},

0 commit comments

Comments
 (0)