122 lines
2.8 KiB
Go
122 lines
2.8 KiB
Go
package comm
|
||
|
||
import (
|
||
"context"
|
||
"crypto/rand"
|
||
"fmt"
|
||
"go_dreamfactory/pb"
|
||
"math/big"
|
||
"reflect"
|
||
"strings"
|
||
|
||
"go_dreamfactory/lego/core"
|
||
|
||
"google.golang.org/protobuf/proto"
|
||
"google.golang.org/protobuf/types/known/anypb"
|
||
)
|
||
|
||
// 服务网关组件接口定义
|
||
type ISC_GateRouteComp interface {
|
||
core.IServiceComp
|
||
ReceiveMsg(ctx context.Context, args *pb.AgentMessage, reply *pb.RPCMessageReply) error
|
||
RegisterRoute(methodName string, comp reflect.Value, msg reflect.Type, handle reflect.Method)
|
||
GetUserSession(udata *pb.CacheUser) (session IUserSession)
|
||
PutUserSession(session IUserSession)
|
||
}
|
||
|
||
//游戏类资源类型
|
||
const (
|
||
AttrType = "attr" //用户属性资源 例如货币 经验 之类的
|
||
ItemType = "item" //道具物品资源
|
||
HeroType = "hero" //卡片资源 例如英雄卡,检验卡
|
||
EquipmentType = "equi" //武器资源
|
||
)
|
||
|
||
type Autogenerated struct {
|
||
ID string `json:"ID,omitempty" bson:"_id"`
|
||
UID string `json:"uid"`
|
||
Act string `json:"act"` // insert update delete
|
||
D []interface{}
|
||
}
|
||
|
||
const (
|
||
HeroStarLvRatio int32 = 10 // 卡牌等级上限系数(星级*10)
|
||
)
|
||
|
||
//Api Check 错误返回结构
|
||
type ErrorCode struct {
|
||
Code pb.ErrorCode
|
||
Data interface{}
|
||
}
|
||
|
||
//用户会话
|
||
type IUserSession interface {
|
||
SetSession(ip, sessionId, stag, sid, uid string)
|
||
GetSessionId() string
|
||
GetUserId() string
|
||
GetIP() string
|
||
GetServiecTag() string
|
||
GetGatewayServiceId() string
|
||
IsLogin() bool
|
||
Bind(uid string, wokerId string) (err error)
|
||
UnBind() (err error)
|
||
SendMsg(mainType, subType string, msg proto.Message) (err error)
|
||
Polls() []*pb.UserMessage
|
||
Push() (err error) //警告 api传递过来的会话禁用此接口
|
||
Close() (err error)
|
||
Reset()
|
||
ToString() string
|
||
}
|
||
|
||
//对protobuf格式的数据进行反序列化
|
||
func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) {
|
||
err := msg.Data.UnmarshalTo(req)
|
||
if err != nil {
|
||
fmt.Printf("UnmarshalTo %s.%s %v\n", msg.MainType, msg.SubType, err)
|
||
return
|
||
}
|
||
return true
|
||
}
|
||
|
||
//对protobuf格式的数据进行序列化
|
||
func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
|
||
any, err := anypb.New(rsp)
|
||
if err != nil {
|
||
fmt.Printf("Any New %s.%s %v", msg.MainType, msg.SubType, err)
|
||
return
|
||
}
|
||
msg.Data = any
|
||
return true
|
||
}
|
||
|
||
/// 参数 权重数组 返回值 数组下标
|
||
func GetRandW(sz []int32) int32 {
|
||
if len(sz) > 0 {
|
||
var _totalW int64 // 总权重
|
||
var _tmpW int64 // 临时权重
|
||
for _, v := range sz {
|
||
_totalW += int64(v)
|
||
}
|
||
// 随机权重
|
||
n, _ := rand.Int(rand.Reader, big.NewInt(_totalW))
|
||
for i, v := range sz {
|
||
_tmpW += int64(v)
|
||
if n.Int64() < _tmpW {
|
||
return int32(i)
|
||
}
|
||
}
|
||
}
|
||
return 0
|
||
}
|
||
|
||
///通过uid获取用户所在区服
|
||
func UidToSTag(uid string) (stag string, err error) {
|
||
s := strings.SplitN(uid, "_", 2)
|
||
if len(s) < 2 {
|
||
err = fmt.Errorf("uid 格式异常:%v", uid)
|
||
return
|
||
}
|
||
stag = s[0]
|
||
return
|
||
}
|