Files
bl/logic/controller/login.go
昔念 ec082db71d ```
feat(socket): 优化服务器事件处理逻辑并修复数据解码问题

- 移除重复的 `gnet.WithTicker(true)` 配置项
- 调整 `OnTick` 的执行间隔从 10 秒延长至 30 秒
- 更新 `NewClientData` 方法以传入连接对象,用于后续消息处理
- 将 `c.Read` 替换为 `c.Discard` 以正确丢弃已读数据
- 改进数据包处理逻辑,增强对不完整包的处理能力
- 修正 `TomeeHeader.Version` 类型由 string 转为 byte,并更新相关读写操作
- 在消息处理中增加错误日志打印
2025-10-28 02:28:15 +08:00

112 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"blazing/common/data/share"
"blazing/common/data/xmlres"
"blazing/cool"
"fmt"
"blazing/common/socket/errorcode"
"blazing/logic/service/common"
"blazing/logic/service/user"
"blazing/logic/service/maps"
"blazing/logic/service/player"
"blazing/logic/service/space"
blservice "blazing/modules/blazing/service"
"context"
"time"
"github.com/gogf/gf/v2/os/gtime"
"github.com/jinzhu/copier"
"github.com/panjf2000/gnet/v2"
)
func IsToday(t time.Time) bool {
// 获取当前时间
now := time.Now()
// 比较年、月、日是否相同
return t.Year() == now.Year() &&
t.Month() == now.Month() &&
t.Day() == now.Day()
}
// 处理命令: 1001
func (h *Controller) Login(data *user.MAIN_LOGIN_IN, c gnet.Conn) (result *user.LoginMSInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
if tt := data.CheakSession(); tt { //说明sid正确
cool.Loger.Info(context.TODO(), "准备踢人")
err := h.RPCClient.Kick(data.Head.UserID) //先踢人
if err != nil {
fmt.Println("踢人失败", err)
}
//player.KickPlayer(data.Head.UserID)
cool.Loger.Info(context.TODO(), "踢人请求完成,继续登录流程")
// <-time.After(time.Millisecond * 3000)
share.ShareManager.SetUserOnline(data.Head.UserID, h.Port) //设置用户登录服务器
t := player.GetPlayer(c, data.Head.UserID)
t.Service = blservice.NewUserService(data.Head.UserID)
t.Info = t.Service.Person(data.Head.UserID)
t.Info.UserID = data.Head.UserID
t.Onlinetime = uint32(time.Now().Unix()) //保存时间戳
t.Changemap = true
cool.Loger.Info(context.Background(), "用户上次重置日期", t.Info.LastResetTime.String())
if !IsToday(t.Info.LastResetTime) { //判断是否是今天
t.Info.LastResetTime = gtime.Now().Time
//每天login时候检查重置时间然后把电池任务挖矿重置
//挖矿需要单独存,因为防止多开挖矿
t.Info.TimeToday = 0 //重置电池
for i := 400; i < 500; i++ { //每日任务区段
tttL, ok := xmlres.TaskMap[i]
if ok {
if tttL.Type == 1 { //日常任务
t.Info.TaskList[i-1] = 0 //重置每日任务
}
}
}
for i := 0; i < 50; i++ { //每日任务区段
t.Info.DailyResArr[i] = 0 //重置每日任务
}
go t.Service.Talk(func(m map[uint32]uint32) bool { //挖矿
m = map[uint32]uint32{}
return true
})
}
t.CompleteLogin() //通知客户端登录成功
result = user.NewOutInfo() //设置登录消息
result.PlayerInfo = *t.Info
defer func() {
tt := maps.NewOutInfo()
copier.CopyWithOption(tt, t.Info, copier.Option{DeepCopy: true})
//copier.Copy(t.Info, tt)
t1 := player.NewTomeeHeader(2001, t.Info.UserID)
space.GetSpace(t.Info.MapID).User.IterCb(func(playerID uint32, player common.PlayerI) {
player.SendPack(t1.Pack(tt))
})
space.GetSpace(t.Info.MapID).User.Set(t.Info.UserID, t)
}()
return result, 0
} else {
err = errorcode.ErrorCodes.ErrLoginServerError
}
return
}