2025-08-22 22:40:32 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-09 02:48:41 +08:00
|
|
|
|
"blazing/common/data/share"
|
2025-08-22 22:40:32 +08:00
|
|
|
|
"blazing/cool"
|
2026-01-01 15:37:43 +08:00
|
|
|
|
"blazing/modules/config/service"
|
2026-01-19 18:51:56 +08:00
|
|
|
|
"blazing/modules/player/model"
|
2025-08-22 22:40:32 +08:00
|
|
|
|
"context"
|
2026-01-09 19:58:12 +08:00
|
|
|
|
"encoding/hex"
|
2026-01-19 18:51:56 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
|
2026-01-09 19:58:12 +08:00
|
|
|
|
"strings"
|
2025-12-09 02:48:41 +08:00
|
|
|
|
|
2025-08-28 21:57:30 +00:00
|
|
|
|
"time"
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2025-12-07 19:23:44 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2026-01-09 19:58:12 +08:00
|
|
|
|
"github.com/google/uuid"
|
2026-01-19 18:51:56 +08:00
|
|
|
|
csmap "github.com/mhmtszr/concurrent-swiss-map"
|
2025-08-22 22:40:32 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 是否注册,如果注册过,那么就会产生用户player信息
|
2025-11-16 20:30:17 +00:00
|
|
|
|
func (s *InfoService) IsReg() bool {
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
2026-01-08 03:30:18 +08:00
|
|
|
|
m := s.PModel(s.Model)
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
2026-01-20 06:15:55 +08:00
|
|
|
|
record, _ := m.Exist()
|
|
|
|
|
|
|
|
|
|
|
|
return record
|
2025-08-22 22:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 实现注册,id+昵称+颜色
|
2026-01-25 07:42:32 +08:00
|
|
|
|
func (s *InfoService) Reg(nick string, color uint32) *model.PlayerInfo {
|
2025-09-23 15:34:55 +00:00
|
|
|
|
if s.IsReg() {
|
2026-01-25 07:42:32 +08:00
|
|
|
|
return nil
|
2025-09-23 15:34:55 +00:00
|
|
|
|
}
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
|
|
|
|
|
t := model.NewPlayer()
|
2025-08-28 14:38:13 +00:00
|
|
|
|
t.PlayerID = uint64(s.userid)
|
2025-08-22 22:40:32 +08:00
|
|
|
|
//设置用户信息
|
2025-09-23 15:18:43 +00:00
|
|
|
|
t.Data = model.NewPlayerInfo()
|
|
|
|
|
|
t.Data.Nick = nick
|
2025-12-12 19:10:09 +00:00
|
|
|
|
t.Data.UserID = s.userid
|
2025-09-23 15:18:43 +00:00
|
|
|
|
t.Data.Color = color
|
|
|
|
|
|
t.Data.RegisterTime = uint32(time.Now().Unix()) //写入注册时间
|
2025-08-23 17:44:12 +08:00
|
|
|
|
|
2025-11-16 20:30:17 +00:00
|
|
|
|
_, err := cool.DBM(s.Model).Data(t).FieldsEx("id").Insert()
|
2025-08-22 22:40:32 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
glog.Error(context.Background(), err)
|
2026-01-25 07:42:32 +08:00
|
|
|
|
|
2025-08-22 22:40:32 +08:00
|
|
|
|
}
|
2026-01-25 07:42:32 +08:00
|
|
|
|
return &t.Data
|
2025-08-22 22:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 22:08:36 +00:00
|
|
|
|
func (s *InfoService) Person(userid uint32) (out *model.PlayerEX) {
|
2025-09-20 00:17:29 +08:00
|
|
|
|
|
2026-01-20 22:08:36 +00:00
|
|
|
|
cool.DBM(s.Model).Where("player_id", userid).Scan(&out)
|
2025-12-07 19:23:44 +08:00
|
|
|
|
|
2026-01-20 22:08:36 +00:00
|
|
|
|
return
|
2025-12-07 19:23:44 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2026-01-19 18:51:56 +08:00
|
|
|
|
func (s *InfoService) GetCache() *model.PlayerInfo {
|
|
|
|
|
|
|
2026-01-25 07:42:32 +08:00
|
|
|
|
ret, _ := cool.CacheManager.Get(context.TODO(), fmt.Sprintf("player:%d", s.userid))
|
2026-01-19 18:51:56 +08:00
|
|
|
|
if ret == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var rets *model.PlayerInfo
|
|
|
|
|
|
ret.Struct(&rets)
|
|
|
|
|
|
return rets
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
func (s *InfoService) SetLogin() *model.PlayerInfo {
|
|
|
|
|
|
|
2025-12-07 19:23:44 +08:00
|
|
|
|
m := cool.DBM(s.Model).Where("player_id", s.userid)
|
2026-01-19 18:51:56 +08:00
|
|
|
|
var tt *model.PlayerEX
|
|
|
|
|
|
m.Scan(&tt)
|
|
|
|
|
|
if tt == nil {
|
2025-12-07 19:23:44 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-12-12 19:10:09 +00:00
|
|
|
|
tt.Data.AllPetNumber = uint32(NewPetService(s.userid).PetCount(0))
|
|
|
|
|
|
|
2026-01-25 23:17:46 +08:00
|
|
|
|
if tt.Data.MapID > 300 || tt.Data.MapID == 0 { //如果位于基地,就重置到传送仓
|
|
|
|
|
|
tt.Data.MapID = 1
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
if tt.Data.IsNewPlayer() { //重置新手地图,放到机械仓
|
|
|
|
|
|
tt.Data.SetTask(4, model.Completed) //设置新手任务默认完成
|
|
|
|
|
|
tt.Data.MapID = 8
|
|
|
|
|
|
if len(tt.Data.PetList) == 0 {
|
|
|
|
|
|
//这个是添加后防止卡死
|
|
|
|
|
|
rr := NewPetService(s.userid).PetInfo(0)
|
|
|
|
|
|
if len(rr) > 0 {
|
|
|
|
|
|
tt.Data.PetList = append(tt.Data.PetList, rr[0].Data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if tt.Data.MaxPuniLv < 9 {
|
|
|
|
|
|
|
|
|
|
|
|
for i := 291; i < 299; i++ {
|
|
|
|
|
|
|
|
|
|
|
|
if tt.Data.GetTask(i) == model.Completed {
|
|
|
|
|
|
tt.Data.MaxPuniLv = uint32(i) - 290
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-07 19:23:44 +08:00
|
|
|
|
if !IsToday(tt.LastResetTime) { //判断是否是今天
|
|
|
|
|
|
|
|
|
|
|
|
//每天login时候检查重置时间,然后把电池,任务,挖矿重置
|
|
|
|
|
|
//挖矿需要单独存,因为防止多开挖矿
|
|
|
|
|
|
tt.LastResetTime = gtime.Now()
|
2025-12-09 02:48:41 +08:00
|
|
|
|
//每天login时候检查重置时间,然后把电池,任务,挖矿重置
|
|
|
|
|
|
//挖矿需要单独存,因为防止多开挖矿
|
2026-01-01 15:37:43 +08:00
|
|
|
|
tt.Data.TimeToday = 0 //重置电池
|
2025-12-07 19:23:44 +08:00
|
|
|
|
|
2026-01-01 15:37:43 +08:00
|
|
|
|
for _, v := range service.NewTaskService().GetDaily() {
|
2025-12-08 19:16:37 +08:00
|
|
|
|
|
2026-01-01 15:37:43 +08:00
|
|
|
|
tt.Data.SetTask(int(v.TaskId), model.Unaccepted)
|
2025-12-07 19:23:44 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2026-01-01 15:37:43 +08:00
|
|
|
|
|
2025-12-07 19:23:44 +08:00
|
|
|
|
for i := 0; i < 50; i++ { //每日任务区段
|
|
|
|
|
|
tt.Data.DailyResArr[i] = 0 //重置每日任务
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//defer t.Service.Talk_Reset()
|
|
|
|
|
|
_, err := m.Save(tt)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-25 14:08:35 +00:00
|
|
|
|
ret := tt.Data
|
2026-01-19 18:51:56 +08:00
|
|
|
|
|
2025-10-18 23:58:19 +08:00
|
|
|
|
return &ret
|
2025-09-20 00:17:29 +08:00
|
|
|
|
|
2025-12-09 02:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 18:51:56 +08:00
|
|
|
|
var User = csmap.New[string, uint32](
|
|
|
|
|
|
// set the number of map shards. the default value is 32.
|
|
|
|
|
|
csmap.WithShardCount[string, uint32](32),
|
|
|
|
|
|
|
|
|
|
|
|
// set the total capacity, every shard map has total capacity/shard count capacity. the default value is 0.
|
|
|
|
|
|
// csmap.WithSize[string, int](1000),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-09 19:58:12 +08:00
|
|
|
|
// 生成session
|
|
|
|
|
|
// GetSessionId 生成并返回会话ID、UUID字符串及可能的错误
|
|
|
|
|
|
// 会话ID由accountID(4字节) + UUID(16字节) + 随机数(4字节)组成,最终编码为十六进制字符串
|
|
|
|
|
|
func (s *InfoService) Gensession() string {
|
|
|
|
|
|
uuidV7, _ := uuid.NewV7()
|
|
|
|
|
|
|
|
|
|
|
|
// 移除UUID中的连字符,便于后续处理
|
|
|
|
|
|
uuidStr := strings.ReplaceAll(uuidV7.String(), "-", "")
|
|
|
|
|
|
|
2026-01-25 07:42:32 +08:00
|
|
|
|
// // 解码UUID字符串为字节数组(32位十六进制字符串对应16字节)
|
2026-01-09 19:58:12 +08:00
|
|
|
|
uuidBytes, _ := hex.DecodeString(uuidStr)
|
|
|
|
|
|
|
2026-01-25 07:42:32 +08:00
|
|
|
|
// // 将accountID转换为4字节大端序字节数组
|
|
|
|
|
|
// accountBytes := make([]byte, 4)
|
|
|
|
|
|
// binary.BigEndian.PutUint32(accountBytes, uint32(s.userid))
|
|
|
|
|
|
|
|
|
|
|
|
// // 预分配缓冲区(总长度:4+16+4=24字节),减少内存分配
|
|
|
|
|
|
// sessionBytes := make([]byte, 0, 24)
|
|
|
|
|
|
// sessionBytes = append(sessionBytes, accountBytes...)
|
|
|
|
|
|
// sessionBytes = append(sessionBytes, uuidBytes...)
|
|
|
|
|
|
// //sessionBytes = append(sessionBytes, grand.B(4)...)
|
|
|
|
|
|
|
|
|
|
|
|
// // 编码为十六进制字符串作为最终会话ID
|
|
|
|
|
|
sessionID := hex.EncodeToString(uuidBytes)
|
|
|
|
|
|
cool.CacheManager.Set(context.Background(), fmt.Sprintf("session:%d", uint32(s.userid)), sessionID, 0)
|
|
|
|
|
|
// ///User.Store(string(uuidStr), uint32(s.userid))
|
|
|
|
|
|
// //share.ShareManager.SaveSession(string(uuidStr), uint32(s.userid))
|
2026-01-09 19:58:12 +08:00
|
|
|
|
return sessionID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 18:51:56 +08:00
|
|
|
|
func (s *InfoService) Kick(id uint32) error {
|
2025-12-09 02:48:41 +08:00
|
|
|
|
|
|
|
|
|
|
useid1, err := share.ShareManager.GetUserOnline(id)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2026-01-19 18:51:56 +08:00
|
|
|
|
return err
|
2025-12-09 02:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cl, ok := cool.GetClient(useid1)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
err := cl.KickPerson(id) //实现指定服务器踢人
|
|
|
|
|
|
if err != nil {
|
2026-01-19 18:51:56 +08:00
|
|
|
|
return err
|
2025-12-09 02:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-19 18:51:56 +08:00
|
|
|
|
return nil
|
2025-08-22 22:40:32 +08:00
|
|
|
|
}
|
2025-11-17 11:00:12 +08:00
|
|
|
|
func (s *InfoService) Save(data model.PlayerInfo) {
|
2026-01-30 01:34:07 +08:00
|
|
|
|
if cool.Config.ServerInfo.IsVip != 0 {
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
2026-01-30 01:34:07 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-08 03:30:18 +08:00
|
|
|
|
m := s.PModel(s.Model)
|
2025-09-23 15:18:43 +00:00
|
|
|
|
var tt model.PlayerEX
|
2025-08-22 22:40:32 +08:00
|
|
|
|
m.Scan(&tt)
|
2025-11-17 11:00:12 +08:00
|
|
|
|
tt.Data = data
|
|
|
|
|
|
_, err := m.Save(tt)
|
2025-08-30 21:59:52 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
2025-08-22 22:40:32 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-16 20:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
type InfoService struct {
|
|
|
|
|
|
BaseService
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewInfoService(id uint32) *InfoService {
|
|
|
|
|
|
return &InfoService{
|
|
|
|
|
|
|
|
|
|
|
|
BaseService: BaseService{userid: id,
|
|
|
|
|
|
|
|
|
|
|
|
Service: &cool.Service{Model: model.NewPlayer(), UniqueKey: map[string]string{
|
|
|
|
|
|
"player_id": "角色名称不能重复",
|
2025-12-13 21:47:07 +08:00
|
|
|
|
}, PageQueryOp: &cool.QueryOp{
|
|
|
|
|
|
FieldEQ: []string{"player_id"},
|
2025-11-16 20:30:17 +00:00
|
|
|
|
}},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|