346 lines
10 KiB
Go
346 lines
10 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/rabbitmq/amqp091-go"
|
|
"samba/pkg/log"
|
|
"samba/pkg/servername"
|
|
"samba/proto"
|
|
"samba/util/model"
|
|
"samba/util/routingKey"
|
|
"samba/util/util"
|
|
)
|
|
|
|
func ntfUpdateResource(uid int64, clubId, resType int, num, amount int64) {
|
|
d := make(map[string]interface{})
|
|
d["a"] = proto.NtfUpdateResourceId
|
|
xd := make(map[string]interface{})
|
|
xd["u"] = fmt.Sprintf("%v", uid)
|
|
xd["d"] = &proto.NtfUpdateResource{
|
|
ClubId: clubId,
|
|
Type: fmt.Sprintf("%v", resType),
|
|
Num: num,
|
|
Amount: amount,
|
|
}
|
|
d["d"] = xd
|
|
|
|
p := make(map[string]interface{})
|
|
p["u"] = fmt.Sprintf("%v", uid)
|
|
p["d"] = d
|
|
|
|
data := make(map[string]interface{})
|
|
data["a"] = "user_transfer"
|
|
data["p"] = p
|
|
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
log.Debug(fmt.Sprintf("send msg:%v to exchange:%v, routKey:%v, data:%v", proto.NtfUpdateResourceId, util.Direct(servername.User),
|
|
routingKey.GateKey(uid), string(byteData)))
|
|
if err = gDBServices.Get(uid).Publish(util.Direct(servername.User), routingKey.GateKey(uid), byteData); err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
func ntfBatchUpdateResource(uid int64, ntfs *proto.NtfBatchUpdateResource) {
|
|
if len(ntfs.Data) <= 0 {
|
|
return
|
|
}
|
|
d := make(map[string]interface{})
|
|
d["a"] = proto.NtfUpdateResourceId
|
|
xd := make(map[string]interface{})
|
|
xd["u"] = fmt.Sprintf("%v", uid)
|
|
xd["d"] = ntfs
|
|
d["d"] = xd
|
|
|
|
p := make(map[string]interface{})
|
|
p["u"] = fmt.Sprintf("%v", uid)
|
|
p["d"] = d
|
|
|
|
data := make(map[string]interface{})
|
|
data["a"] = "user_transfer"
|
|
data["p"] = p
|
|
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
log.Debug(fmt.Sprintf("send msg:%v to exchange:%v, routKey:%v, data:%v", proto.NtfUpdateResourceId, util.Direct(servername.User),
|
|
routingKey.GateKey(uid), string(byteData)))
|
|
if err = gDBServices.Get(uid).Publish(util.Direct(servername.User), routingKey.GateKey(uid), byteData); err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
func addResource(uid int64, clubId int, req *proto.ReqAddResource) proto.RspAddResource {
|
|
if req.ResValue == 0 {
|
|
return proto.RspAddResource{Code: proto.BadParam, Msg: proto.BadParam.Error().Error()}
|
|
}
|
|
var value int64
|
|
var err error
|
|
switch req.ResType {
|
|
case model.ResCoins, model.ResDiamond, model.ResTakeCoins, model.ResClubCoins:
|
|
resOp := model.NewUserResourceOp()
|
|
value, err = resOp.Add(clubId, uid, req.ResValue, req.ResType)
|
|
case model.ResClubScore, model.ResClubUserFreeScore, model.ResClubUserInOutFreeScore:
|
|
resOp := model.NewUserClubInfoOp()
|
|
_, value, err = resOp.Add(uid, clubId, req.ResValue, req.ResType)
|
|
}
|
|
if err != nil {
|
|
return proto.RspAddResource{Code: proto.Fail, ResType: req.ResType, Msg: err.Error()}
|
|
}
|
|
resType, _ := model.ResToInt(req.ResType)
|
|
switch req.ResType {
|
|
case model.ResClubScore, model.ResClubUserFreeScore, model.ResClubUserInOutFreeScore:
|
|
if info, err := model.NewUserClubInfoOp().Load(req.UserId, req.ClubId); err == nil {
|
|
value = info.Score + info.FreeScore + info.InOutFreeScore
|
|
}
|
|
resType, _ = model.ResToInt(model.ResClubScore)
|
|
default:
|
|
}
|
|
if req.IsNotify {
|
|
ntfUpdateResource(req.UserId, req.ClubId, resType, req.ResValue, value)
|
|
}
|
|
srv := gDBServices.Get(uid)
|
|
model.NewResourceRecord(req, value).Flush(srv)
|
|
return proto.RspAddResource{Code: proto.Ok, ResType: req.ResType, ResValue: value}
|
|
}
|
|
|
|
func onAddResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqAddResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
|
|
response := make(map[string]interface{})
|
|
rsp := addResource(req.UserId, req.ClubId, req)
|
|
response["p"] = rsp
|
|
if rsp.Code == proto.Ok && req.IsNotify {
|
|
resType, _ := model.ResToInt(req.ResType)
|
|
ntfUpdateResource(req.UserId, req.ClubId, resType, req.ResValue, rsp.ResValue)
|
|
}
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
return
|
|
}
|
|
|
|
func onBatchAddResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqBatchAddResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
uid, clubId := req.UserId, req.ClubId
|
|
response := make(map[string]interface{})
|
|
rsps := make([]proto.RspAddResource, 0, len(req.ReqResources))
|
|
ntfs := make([]proto.NtfUpdateResource, 0, len(req.ReqResources))
|
|
for _, r := range req.ReqResources {
|
|
rsp := addResource(uid, clubId, &r)
|
|
rsps = append(rsps, rsp)
|
|
if rsp.Code != proto.Ok {
|
|
continue
|
|
}
|
|
resType, _ := model.ResToInt(r.ResType)
|
|
ntfs = append(ntfs, proto.NtfUpdateResource{
|
|
ClubId: clubId,
|
|
Type: fmt.Sprintf("%d", resType),
|
|
Num: r.ResValue,
|
|
Amount: rsp.ResValue,
|
|
})
|
|
}
|
|
if req.IsNotify {
|
|
ntfBatchUpdateResource(uid, &proto.NtfBatchUpdateResource{
|
|
Type: 305,
|
|
Data: ntfs,
|
|
ClubId: clubId,
|
|
Reason: 306,
|
|
})
|
|
}
|
|
|
|
response["p"] = proto.RspBatchAddResource{RspResources: rsps}
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
|
|
}
|
|
|
|
func onLoadResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqGetResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
response := make(map[string]interface{})
|
|
response["r"] = "0"
|
|
|
|
var value int64
|
|
switch req.ResType {
|
|
case model.ResCoins, model.ResDiamond, model.ResTakeCoins, model.ResClubCoins:
|
|
resOp := model.NewUserResourceOp()
|
|
value, err = resOp.Get(req.UserId, req.ResType)
|
|
case model.ResClubScore, model.ResClubUserFreeScore, model.ResClubUserInOutFreeScore:
|
|
resOp := model.NewUserClubInfoOp()
|
|
value, err = resOp.Get(req.UserId, req.ClubId, req.ResType)
|
|
}
|
|
if err == nil {
|
|
response["p"] = proto.RspGetResource{Code: proto.Ok, ResType: req.ResType, ResValue: value}
|
|
} else {
|
|
response["p"] = proto.RspGetResource{Code: proto.Fail, ResType: req.ResType, Msg: err.Error()}
|
|
}
|
|
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
}
|
|
|
|
func ntfUpdateClubResource(uid int64, clubId, resType int, num, amount int64) {
|
|
d := make(map[string]interface{})
|
|
d["a"] = proto.NtfUpdateClubResourceId
|
|
xd := make(map[string]interface{})
|
|
xd["u"] = fmt.Sprintf("%v", uid)
|
|
xd["d"] = &proto.NtfUpdateClubResource{
|
|
ClubId: clubId,
|
|
Type: fmt.Sprintf("%v", resType),
|
|
Num: num,
|
|
Amount: amount,
|
|
}
|
|
d["d"] = xd
|
|
|
|
p := make(map[string]interface{})
|
|
p["u"] = fmt.Sprintf("%v", uid)
|
|
p["d"] = d
|
|
|
|
data := make(map[string]interface{})
|
|
data["a"] = "user_transfer"
|
|
data["p"] = p
|
|
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
log.Debug(fmt.Sprintf("send msg:%v to exchange:%v, routKey:%v, data:%v", proto.NtfUpdateClubResourceId, util.Direct(servername.User),
|
|
routingKey.GateKey(uid), string(byteData)))
|
|
if err = gDBServices.Get(uid).Publish(util.Direct(servername.User), routingKey.GateKey(uid), byteData); err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
// 添加俱乐部资源,目前只有俱乐部本身的积分
|
|
func onAddClubResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqAddClubResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
|
|
response := make(map[string]interface{})
|
|
if req.ResValue == 0 {
|
|
response["p"] = proto.RspAddClubResource{Code: proto.BadParam, Msg: proto.BadParam.Error().Error()}
|
|
} else {
|
|
resOp := model.NewClubResourceOp()
|
|
|
|
var value int64
|
|
if value, err = resOp.Add(req.ClubId, req.ResValue, req.ResType); err == nil {
|
|
srv := gDBServices.Get(req.UserId)
|
|
model.NewClubResourceRecord(req, value).Flush(srv)
|
|
response["p"] = proto.RspAddClubResource{Code: proto.Ok, ResType: req.ResType, ResValue: value, ClubId: req.ClubId}
|
|
resType, _ := model.ClubResToInt(req.ResType)
|
|
if req.GameNo != "" {
|
|
ntfUpdateClubResource(req.UserId, req.ClubId, resType, req.ResValue, value)
|
|
}
|
|
|
|
} else {
|
|
response["p"] = proto.RspAddClubResource{Code: proto.Fail, ResType: req.ResType, Msg: err.Error()}
|
|
}
|
|
}
|
|
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
return
|
|
}
|
|
|
|
func onLoadClubResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqGetClubResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
response := make(map[string]interface{})
|
|
response["r"] = "0"
|
|
|
|
resOp := model.NewClubResourceOp()
|
|
if coins, err := resOp.Get(req.ResType, req.ClubId); err == nil {
|
|
response["p"] = proto.RspGetClubResource{Code: proto.Ok, ResType: req.ResType, ResValue: coins, ClubId: req.ClubId}
|
|
} else {
|
|
response["p"] = proto.RspGetClubResource{Code: proto.Fail, ResType: req.ResType, Msg: err.Error(), ClubId: req.ClubId}
|
|
}
|
|
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
}
|
|
|
|
// 获取玩家身上所有资源
|
|
func onGetUserAllResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqGetAllResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
response := make(map[string]interface{})
|
|
response["r"] = "0"
|
|
|
|
res := model.NewUserResourceOp().Load(req.UserId)
|
|
var resMap map[string]interface{}
|
|
if res == nil {
|
|
err = errors.New(fmt.Sprintf("user:%v is not exist", req.UserId))
|
|
} else {
|
|
resMap, err = util.StructToMap(res)
|
|
}
|
|
if err == nil {
|
|
response["p"] = proto.RspGetAllResource{Code: proto.Ok, UserId: req.UserId, Res: resMap}
|
|
} else {
|
|
response["p"] = proto.RspGetAllResource{Code: proto.Fail, Msg: err.Error()}
|
|
}
|
|
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
}
|
|
|
|
// 获取玩家身上所有资源
|
|
func onGetUserClubAllResource(d *amqp091.Delivery, msg map[string]interface{}) {
|
|
req, err := util.MapToStructT[proto.ReqGetUserClubAllResource](msg["p"])
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
response := make(map[string]interface{})
|
|
response["r"] = "0"
|
|
|
|
res, err := model.NewUserClubInfoOp().Load(req.UserId, req.ClubId)
|
|
var resMap map[string]interface{}
|
|
if err == nil {
|
|
resMap, err = util.StructToMap(res)
|
|
}
|
|
|
|
if err == nil {
|
|
response["p"] = proto.RspGetUserClubAllResource{Code: proto.Ok, UserId: req.UserId, ClubId: req.ClubId, Res: resMap}
|
|
} else {
|
|
response["p"] = proto.RspGetUserClubAllResource{Code: proto.Fail, Msg: err.Error()}
|
|
}
|
|
|
|
if data, err := json.Marshal(response); err == nil {
|
|
_ = gDBServices.Get(req.UserId).PublishRpc(d, data)
|
|
}
|
|
}
|