You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.0 KiB
105 lines
3.0 KiB
|
1 year ago
|
package hall
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"server/call"
|
||
|
|
"server/db"
|
||
|
|
"server/natsClient"
|
||
|
|
"server/pb"
|
||
|
|
"server/util"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"server/common"
|
||
|
|
|
||
|
|
"github.com/liangdas/mqant/gate"
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
timewheel "github.com/liangdas/mqant/module/modules/timer"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (h *Hall) login(session gate.Session, req *pb.LoginRequest) (string, error) {
|
||
|
|
log.Info("login sessionid:%v,serverid:%v,userid:%v,req:%+v", session.GetSessionID(), session.GetServerID(), session.GetUserID(), req)
|
||
|
|
resp := &pb.LoginResponse{
|
||
|
|
Result: 0,
|
||
|
|
UserId: 0,
|
||
|
|
}
|
||
|
|
rUID, err := db.Redis().GetInt(common.GetRedisKeyToken(req.Token))
|
||
|
|
if err != nil || rUID != int(req.UserId) {
|
||
|
|
log.Error("err:%v,ruid:%v", err, rUID)
|
||
|
|
resp.Result = 1
|
||
|
|
call.SendSS(session, int(pb.ServerGateResp_GateLoginResp), resp)
|
||
|
|
return "", nil
|
||
|
|
}
|
||
|
|
p, ok := playerSessionMap.Load(session.GetSessionID())
|
||
|
|
if ok {
|
||
|
|
id := p.(*player).db.Id
|
||
|
|
if id != int(req.UserId) {
|
||
|
|
util.Go(func() {
|
||
|
|
delPlayerByID(id)
|
||
|
|
call.UpdateUserXInfo(&common.PlayerDBInfo{Id: id}, map[string]interface{}{"online": 2})
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
h.onLogin(session, resp, rUID)
|
||
|
|
return "", nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Hall) logout(session gate.Session, req *pb.LoginRequest) (string, error) {
|
||
|
|
fmt.Println("Call Logout")
|
||
|
|
return "", nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Hall) onLogin(session gate.Session, resp *pb.LoginResponse, uid int) {
|
||
|
|
resp.UserId = uint32(uid)
|
||
|
|
defer func() {
|
||
|
|
call.SendSS(session, int(pb.ServerGateResp_GateLoginResp), resp)
|
||
|
|
}()
|
||
|
|
errbind := session.Bind(strconv.Itoa(uid))
|
||
|
|
if errbind != "" {
|
||
|
|
log.Error("login bind userid error:%s,uid:%v", errbind, uid)
|
||
|
|
resp.Result = 1
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
p := new(player)
|
||
|
|
// 发布新玩家登录的消息
|
||
|
|
if v, ok := playerIdMap.Load(uid); ok {
|
||
|
|
p = v.(*player)
|
||
|
|
psid := p.session.GetSessionID()
|
||
|
|
sid := session.GetSessionID()
|
||
|
|
if psid == sid {
|
||
|
|
log.Debug("onLogin player same session, uid:%d, session:%s", p.db.Id, psid)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
delPlayerBySession(psid)
|
||
|
|
call.SendSS(p.session, int(pb.ServerGateResp_GateRepeatResp), nil)
|
||
|
|
log.Debug("onLogin player duplicate login, uid:%d, old session:%s, new session:%s", p.db.Id, psid, sid)
|
||
|
|
p.session = session
|
||
|
|
} else {
|
||
|
|
p = newPlayer(uid)
|
||
|
|
p.session = session
|
||
|
|
addPlayerById(uid, p)
|
||
|
|
err := call.Publish(natsClient.TopicNewPlayer, &pb.NewPlayerLogin{Uid: int32(uid), GateServerID: session.GetServerID(), HallServerID: h.GetServerID()})
|
||
|
|
if err != nil {
|
||
|
|
log.Error("player login nats publish topic player duplogin error:%v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
p.lastLogin = time.Now().Unix()
|
||
|
|
|
||
|
|
sessionID := session.GetSessionID()
|
||
|
|
addPlayerBySession(sessionID, p)
|
||
|
|
serverID := session.GetServerID()
|
||
|
|
db.Redis().UpdateUserFields(uid, map[string]interface{}{"sessionID": sessionID, "gateID": serverID})
|
||
|
|
// ip := session.GetIP()
|
||
|
|
// cid := p.db.ChannelID
|
||
|
|
// birth := p.db.Birth
|
||
|
|
util.Go(func() {
|
||
|
|
timewheel.GetTimeWheel().AddTimer(3*time.Second, nil, func(arge interface{}) {
|
||
|
|
p.PushBroadcast()
|
||
|
|
})
|
||
|
|
p.PushRedPoint()
|
||
|
|
call.UpdateUserXInfo(&common.PlayerDBInfo{Id: uid}, map[string]interface{}{"online": 1})
|
||
|
|
// call.InsertLoginRecord(uid, cid, ip, 0, birth)
|
||
|
|
})
|
||
|
|
}
|