112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
|
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
|
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
|
|
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
|
"google.golang.org/protobuf/proto"
|
|
"time"
|
|
)
|
|
|
|
func (c *Client) handle(event Event) {
|
|
switch e := event.(type) {
|
|
case *ClientEvent:
|
|
msg := &ss_pb.Message{}
|
|
if err := proto.Unmarshal(e.Msg, msg); err != nil {
|
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
|
c.cancel()
|
|
return
|
|
}
|
|
switch msg.ID {
|
|
case ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE:
|
|
m := &ss_pb.C2S_EnterInstance{}
|
|
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
|
c.cancel()
|
|
return
|
|
}
|
|
c.onEnter(m)
|
|
case ss_pb.MessageID_MESSAGE_ID_ACTION:
|
|
m := &ss_pb.C2S_Action{}
|
|
if err := proto.Unmarshal(msg.Payload, m); err != nil {
|
|
c.logger.Errorf("handle event proto.Unmarshal err: %v", err)
|
|
c.cancel()
|
|
return
|
|
}
|
|
c.onAction(m)
|
|
}
|
|
case *PongEvent:
|
|
c.heartBeat = time.Now()
|
|
case *SystemLoginSuccessEvent:
|
|
if c.Status == 0 {
|
|
c.Status = 1
|
|
UserMgr.Add(c.USN, c)
|
|
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
|
|
c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{
|
|
InstanceID: 1,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
|
client, err := grpc_client.SceneNewClientLB()
|
|
if err != nil {
|
|
c.logger.Errorf("SceneNewClient err: %v", err)
|
|
return
|
|
}
|
|
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
|
|
USN: c.USN,
|
|
GatewaySID: global.GatewaySID,
|
|
InstanceID: msg.InstanceID,
|
|
})
|
|
if err != nil {
|
|
c.logger.Errorf("enter err: %v", err)
|
|
return
|
|
}
|
|
c.SceneSID = resp.SceneSID
|
|
c.UniqueNo = resp.UniqueNo
|
|
c.InstanceID = msg.InstanceID
|
|
c.WriteBytes(ss_pb.MessageID(resp.MessageID), resp.Payload)
|
|
}
|
|
|
|
func (c *Client) onLeave() {
|
|
if c.SceneSID == 0 {
|
|
return
|
|
}
|
|
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
|
if err != nil {
|
|
c.logger.Errorf("SceneNewClient err: %v", err)
|
|
return
|
|
}
|
|
_, err = client.Leave(context.Background(), &grpc_pb.LeaveReq{
|
|
USN: c.USN,
|
|
UniqueNo: c.UniqueNo,
|
|
})
|
|
if err != nil {
|
|
c.logger.Errorf("leave err: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
|
if c.SceneSID == 0 {
|
|
return
|
|
}
|
|
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
|
UniqueNo: c.UniqueNo,
|
|
USN: c.USN,
|
|
Action: int32(msg.Action),
|
|
DirX: msg.DirX,
|
|
DirY: msg.DirY,
|
|
SkillID: msg.SkillID,
|
|
}); err != nil {
|
|
c.logger.Errorf("send action err: %v", err)
|
|
return
|
|
}
|
|
}
|