165 lines
4.4 KiB
Go
165 lines
4.4 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"samba/pkg/log"
|
|
"samba/pkg/servername"
|
|
"samba/pkg/service"
|
|
"samba/proto"
|
|
"samba/util/routingKey"
|
|
"samba/util/util"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// 服务启动时间
|
|
var ServiceStartTime = time.Now().Unix()
|
|
|
|
func ParseMsg(msg map[string]interface{}) (msgId string, roomId int, uid int64, data interface{}) {
|
|
var v int64
|
|
msgId, _ = msg["a"].(string)
|
|
if iv, ok := msg["r"]; ok {
|
|
tv := reflect.TypeOf(iv)
|
|
switch tv.Kind() {
|
|
case reflect.Float64:
|
|
roomId = int(iv.(float64))
|
|
case reflect.Int:
|
|
roomId = iv.(int)
|
|
case reflect.Int64:
|
|
roomId = int(iv.(int64))
|
|
case reflect.String:
|
|
v, _ = strconv.ParseInt(msg["r"].(string), 10, 64)
|
|
roomId = int(v)
|
|
default:
|
|
log.Error(fmt.Sprintf("map:%+v, room:%v type:%v", msg, iv, tv.Kind()))
|
|
}
|
|
}
|
|
if iv, ok := msg["uid"]; ok {
|
|
tv := reflect.TypeOf(iv)
|
|
switch tv.Kind() {
|
|
case reflect.Float64:
|
|
uid = int64(iv.(float64))
|
|
case reflect.Int64:
|
|
uid = iv.(int64)
|
|
default:
|
|
log.Error(fmt.Sprintf("map:%+v, uid:%v type:%v", msg, iv, tv.Kind()))
|
|
}
|
|
}
|
|
|
|
data = msg["p"]
|
|
|
|
if msgId == proto.ReqLeaveRoomId {
|
|
req, err := util.MapToStructT[proto.ReqLeaveRoom](data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
roomId = req.RoomId
|
|
}
|
|
return
|
|
}
|
|
|
|
func SendMsg(s service.IService, exchange, routingKey string, roomId int, token string, uid int64, msgId string, msg interface{}) {
|
|
data := make(map[string]interface{})
|
|
data["a"] = msgId
|
|
data["r"] = roomId
|
|
data["k"] = token
|
|
data["uid"] = uid
|
|
data["p"] = msg
|
|
byteData, err := json.Marshal(data)
|
|
//log.Debug(fmt.Sprintf("exchange:%v routerKer:%v msg%v", exchange, routingKey, string(byteData)))
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("msgId:%v send msg error:%v", msgId, err))
|
|
return
|
|
}
|
|
err = s.Publish(exchange, routingKey, byteData)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("msgId:%v send msg error:%v", msgId, err))
|
|
return
|
|
}
|
|
}
|
|
|
|
func SendMsgToDb(s service.IService, routingKey string, uid int64, msgId string, msg interface{}) {
|
|
data := service.Message{
|
|
MsgId: msgId,
|
|
Data: msg,
|
|
Uid: uid,
|
|
}
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg to db error:%v", uid, err))
|
|
return
|
|
}
|
|
log.Debug(fmt.Sprintf("send msg:%v to exchange:%v, routKey:%v, data:%v", msgId, util.Direct(servername.Money), routingKey, string(byteData)))
|
|
if err = s.Publish(util.Direct(servername.Money), routingKey, byteData); err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg to db error:%v", uid, err))
|
|
}
|
|
}
|
|
|
|
func SendMsgToOther(s service.IService, toSrvName, routingKey string, uid int64, msgId string, msg interface{}) {
|
|
data := MakeMessage(msgId, msg, uid, 0)
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg to %v error:%v", uid, toSrvName, err))
|
|
return
|
|
}
|
|
log.Debug(fmt.Sprintf("send msg:%v to exchange:%v, routKey:%v, data:%v", msgId, util.Direct(toSrvName), routingKey, string(byteData)))
|
|
if err = s.Publish(util.Direct(toSrvName), routingKey, byteData); err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg to %v error:%v", uid, toSrvName, err))
|
|
}
|
|
}
|
|
|
|
func SendMsgToGate(s service.IService, uid int64, msgId string, msg interface{}) {
|
|
data := service.RspMessage{
|
|
Fun: "user_transfer",
|
|
Data: service.RspData{
|
|
User: fmt.Sprintf("%v", uid),
|
|
Data: service.MsgData{
|
|
MsgId: msgId,
|
|
Data: msg,
|
|
},
|
|
},
|
|
}
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg error:%v", uid, err))
|
|
return
|
|
}
|
|
// `{"a":"user_transfer","p":{"u":"1008","d":{"t":0,"c":3,"p":[[413,113,301],[]],"d":1}}}`
|
|
//log.Debug(fmt.Sprintf("send msg:%v to gate:%v", msgId, string(byteData)))
|
|
err = s.Publish(util.Direct(servername.User), routingKey.GateKey(uid), byteData)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("uid:%v send msg error:%v", uid, err))
|
|
return
|
|
}
|
|
}
|
|
|
|
// BroadcastMsg 广播消息
|
|
func BroadcastMsg(s service.IService, msgId string, msg any) {
|
|
data := service.RspMessage{
|
|
Fun: "system_transfer",
|
|
Data: service.RspData{
|
|
Data: service.MsgData{
|
|
MsgId: msgId,
|
|
Data: msg,
|
|
},
|
|
},
|
|
}
|
|
byteData, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
err = s.Publish(util.Direct(servername.User), routingKey.SystemPublish, byteData)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
func MakeMessage(msgId string, req interface{}, uid int64, roomId int) map[string]interface{} {
|
|
return util.MakeMessage(msgId, req, uid, roomId)
|
|
}
|