Files
bl/logic/controller/item.go
昔念 6979b7018d ```
feat(space): 替换并发安全map实现以提升性能

将原来基于`utils.ConcurrentMap`的玩家存储结构替换为
`github.com/mhmtszr/concurrent-swiss-map`提供的`CsMap`,
以获得更高效的并发读写能力。

同时修改了相关API调用方式:
- `Set` 改为 `Store`
- `Remove` 改为 `Delete`
- `IterCb` 改为 `Range`,并支持提前终止迭代
- `Items()` 不再使用

此外,调整了部分业务逻辑中对玩家列表遍历的方式,
确保在发送网络包后及时跳出循环,避免不必要的操作。

新增战斗类型处理函数`PET_King`用于处理宠物王相关的
战斗请求,并修复了`PET_MELEE`方法中的逻辑问题。

更新了go.mod和go.sum引入新的依赖库。
```
2025-11-15 15:22:58 +08:00

173 lines
4.6 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/xmlres"
"blazing/common/socket/errorcode"
"math/rand"
"time"
"blazing/logic/service/common"
"blazing/logic/service/item"
"blazing/logic/service/player"
"blazing/logic/service/space"
"blazing/modules/blazing/model"
)
func (h Controller) UserItemList(data *item.ItemListInboundInfo, c *player.Player) (result *item.ItemListOutboundInfo, err errorcode.ErrorCode) {
result = &item.ItemListOutboundInfo{}
result.ItemList = make([]model.SingleItemInfo, 0)
item := c.Service.Item(data.Param1, data.Param2)
for _, v := range item {
var vv model.SingleItemInfo
vv.ItemId = v.ItemId
vv.ItemCnt = v.ItemCnt
vv.LeftTime = 360000
result.ItemList = append(result.ItemList, vv)
}
return result, 0
}
func (h Controller) PlayerGoldCount(data *item.GoldOnlineRemainInboundInfo, c *player.Player) (result *item.GoldOnlineRemainOutboundInfo, err errorcode.ErrorCode) {
return &item.GoldOnlineRemainOutboundInfo{
GoldNumber: uint32(c.Info.GoldBean) * 100,
Coin: c.Info.Coins,
}, 0
}
func (h Controller) PlayerExp(data *item.ExpTotalRemainInboundInfo, c *player.Player) (result *item.ExpTotalRemainOutboundInfo, err errorcode.ErrorCode) {
return &item.ExpTotalRemainOutboundInfo{
TotalExp: uint32(c.Info.ExpPool),
}, 0
}
func (h Controller) BuyItem(data *item.BuyInboundInfo, c *player.Player) (result *item.BuyOutboundInfo, err errorcode.ErrorCode) {
tt, ok := xmlres.ItemsMAP[int(data.ItemId)]
if ok && tt.Price != 0 && c.UseCoins(data.Count*uint32(tt.Price)) {
r := c.ItemAdd(model.SingleItemInfo{ItemId: data.ItemId, ItemCnt: data.Count})
if len(r) != 0 {
return &item.BuyOutboundInfo{
ItemId: data.ItemId,
Level: 1,
Count: data.Count,
Coins: c.Info.Coins,
}, 0
}
}
return &item.BuyOutboundInfo{
Coins: c.Info.Coins,
}, 0
}
func (h Controller) ChangePlayerCloth(data *item.ChangePlayerClothInboundInfo, c *player.Player) (result *item.ChangePlayerClothOutboundInfo, err errorcode.ErrorCode) {
result = &item.ChangePlayerClothOutboundInfo{
UserID: c.Info.UserID,
ClothList: make([]model.PeopleItemInfo, 0),
}
for _, v := range data.ClothList {
result.ClothList = append(result.ClothList, model.PeopleItemInfo{ID: v, Level: 1})
}
c.Info.Clothes = result.ClothList
space.GetSpace(c.Info.MapID).User.Range(func(playerID uint32, player common.PlayerI) bool {
// fmt.Println("ChangePlayerCloth", playerID, data.Head.Pack(result))
data.Head.Result = 0
player.SendPack(data.Head.Pack(result))
return false
})
return nil, -1
}
func (h Controller) Talk(data *item.TalkCountInboundInfo, c *player.Player) (result *item.TalkCountOutboundInfo, err errorcode.ErrorCode) {
result = &item.TalkCountOutboundInfo{}
c.Service.Talk(func(t map[uint32]uint32) bool {
tt, ok := t[data.ID]
if ok {
result.GiftCount = tt
}
return false
})
return result, 0
}
var talkcacche = make(map[string]uint32)
func (h Controller) TalkCate(data *item.TalkCateInboundInfo, c *player.Player) (result *item.DayTalkInfo, err errorcode.ErrorCode) {
result = &item.DayTalkInfo{}
result.OutList = make([]item.CateInfo, 0)
for _, te := range xmlres.TalkConfig.Energies {
if te.MapID == uint64(c.Info.MapID) && te.Type == uint64(data.ID) { //
_, ok := talkcacche[te.Name]
if !ok {
for _, v := range xmlres.ItemsMAP {
if v.Name == te.Name {
talkcacche[te.Name] = uint32(v.ID)
}
}
}
rand.Seed(time.Now().UnixNano()) // UnixNano 精度更高,避免短时间内种子重复
// 2. 生成 1-10 的随机数rand.Intn(10) → 0-9+1 后范围变为 1-10
randomNum := rand.Intn(10) + 1
c.Service.Talk(func(t map[uint32]uint32) bool {
if t == nil {
t = make(map[uint32]uint32)
}
_, ok := t[data.ID]
if !ok {
t[data.ID] = 0
}
t[data.ID] += 1
if t[data.ID] < uint32(te.CollectCnt) {
result.OutList = append(result.OutList, item.CateInfo{ID: uint32(talkcacche[te.Name]), Count: uint32(randomNum)})
c.ItemAdd(model.SingleItemInfo{ItemId: uint32(talkcacche[te.Name]), ItemCnt: uint32(randomNum)})
}
return true
})
break
}
}
return result, 0
}
func (h Controller) BuyMItem(data *item.BuyMultiInboundInfo, c *player.Player) (result *item.BuyMultiOutboundInfo, err errorcode.ErrorCode) {
var rrr []model.SingleItemInfo
for _, v := range data.ItemIds {
_, ok := xmlres.ItemsMAP[int(v)]
if ok {
rrr = append(rrr, model.SingleItemInfo{ItemId: uint32(v), ItemCnt: 1})
}
}
r := c.ItemAdd(rrr...)
if len(r) != 0 {
return &item.BuyMultiOutboundInfo{
Coins: c.Info.Coins,
}, 0
}
return &item.BuyMultiOutboundInfo{
Coins: c.Info.Coins,
}, 0
}