96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"mongo.games.com/goserver/core/timer"
|
|
"time"
|
|
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/netlib"
|
|
|
|
"mongo.games.com/game/model"
|
|
"mongo.games.com/game/proto"
|
|
loginproto "mongo.games.com/game/protocol/login"
|
|
playerproto "mongo.games.com/game/protocol/player"
|
|
)
|
|
|
|
func init() {
|
|
// 心跳
|
|
netlib.Register(int(loginproto.GatePacketID_PACKET_SC_PONG), loginproto.SCPong{}, SCPong)
|
|
// 登录
|
|
netlib.Register(int(loginproto.LoginPacketID_PACKET_SC_LOGIN), loginproto.SCLogin{}, SCLogin)
|
|
// 玩家信息
|
|
netlib.Register(int(playerproto.PlayerPacketID_PACKET_SC_PLAYERDATA), playerproto.SCPlayerData{}, SCPlayerData)
|
|
}
|
|
|
|
func SCPong(s *netlib.Session, packetid int, data interface{}) error {
|
|
accountID := s.GetAttribute(SessionAttributeClientAccountId)
|
|
logger.Logger.Tracef("SCPong username:%v %v", accountID, data)
|
|
return nil
|
|
}
|
|
|
|
func SCLogin(s *netlib.Session, packetid int, data interface{}) error {
|
|
logger.Logger.Trace("SCLogin ", data)
|
|
|
|
msg, ok := data.(*loginproto.SCLogin)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if msg.GetOpRetCode() != loginproto.OpResultCode_OPRC_Sucess {
|
|
accountID := s.GetAttribute(SessionAttributeClientAccountId)
|
|
logger.Logger.Error("登录失败 ", accountID)
|
|
s.Close()
|
|
return nil
|
|
}
|
|
|
|
csPlayerData := &playerproto.CSPlayerData{
|
|
AccId: msg.GetAccId(),
|
|
}
|
|
pp := &model.PlayerParams{
|
|
Platform: 1,
|
|
Ip: fmt.Sprintf("%v.%v.%v.%v", 1+rand.Int31n(255), 1+rand.Int31n(255), 1+rand.Int31n(255), 1+rand.Int31n(255)),
|
|
City: "北京",
|
|
Logininmodel: "app",
|
|
}
|
|
d, err := json.Marshal(pp)
|
|
if err == nil {
|
|
csPlayerData.Params = proto.String(string(d))
|
|
}
|
|
|
|
s.Send(int(playerproto.PlayerPacketID_PACKET_CS_PLAYERDATA), csPlayerData)
|
|
logger.Logger.Info("登录成功 ", msg.GetAccId())
|
|
return nil
|
|
}
|
|
|
|
func SCPlayerData(s *netlib.Session, packetid int, data interface{}) error {
|
|
logger.Logger.Trace("SCPlayerData ", data)
|
|
msg, ok := data.(*playerproto.SCPlayerData)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if msg.GetOpRetCode() != playerproto.OpResultCode_OPRC_Sucess {
|
|
accountID := s.GetAttribute(SessionAttributeClientAccountId)
|
|
logger.Logger.Errorf("获取玩家信息失败 %v", accountID)
|
|
s.Close()
|
|
return nil
|
|
}
|
|
|
|
s.SetAttribute(SessionAttributeUser, msg)
|
|
|
|
StartSessionPingTimer(s, timer.TimerActionWrapper(func(h timer.TimerHandle, ud interface{}) bool {
|
|
if !s.IsConned() {
|
|
StopSessionPingTimer(s)
|
|
return false
|
|
}
|
|
pack := &loginproto.CSPing{}
|
|
s.Send(int(loginproto.GatePacketID_PACKET_CS_PING), pack)
|
|
return true
|
|
}), nil, time.Second*time.Duration(60+rand.Int31n(100)), -1)
|
|
|
|
return nil
|
|
}
|