```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

feat(game): 实现扭蛋系统批量物品添加功能并优化地图逻辑

- 新增ItemAddBatch方法用于批量添加物品,支持普通道具和特殊道具的分别处理
- 优化扭蛋游戏玩法中的物品添加逻辑,使用新的批量接口提升性能
- 在扭蛋机器人命令中实现完整的物品检查和批量添加流程

refactor(map): 重构地图控制器代码结构并添加注释

- 为EnterMap、LeaveMap、GetMapPlayerList等方法添加中文注释
- 统一地图相关的命名规范,如enter
This commit is contained in:
昔念
2026-04-01 20:10:29 +08:00
parent 1b6586aedc
commit 5995f0670c
14 changed files with 597 additions and 214 deletions

View File

@@ -205,6 +205,114 @@ func (p *Player) SendPack(b []byte) error {
}
// 添加物品 返回成功添加的物品
// ItemAddBatch 批量添加物品,普通道具先按 ItemAdd 的规则校验上限,再批量落库。
func (p *Player) ItemAddBatch(items []data.ItemInfo) []data.ItemInfo {
if len(items) == 0 {
return nil
}
sendErr := func(code errorcode.ErrorCode) {
t1 := common.NewTomeeHeader(2601, p.Info.UserID)
t1.Result = uint32(code)
p.SendPack(t1.Pack(nil))
}
var (
regularItems = make([]data.ItemInfo, 0, len(items))
regularIndexes = make([]int, 0, len(items))
itemIDs = make([]uint32, 0, len(items))
seenIDs = make(map[uint32]struct{}, len(items))
specialSuccess = make(map[int]bool, len(items))
)
for idx, item := range items {
if item.ItemCnt <= 0 {
sendErr(errorcode.ErrorCodes.ErrSystemError200007)
continue
}
switch item.ItemId {
case 1, 3, 5, 9:
if p.ItemAdd(item.ItemId, item.ItemCnt) {
specialSuccess[idx] = true
}
default:
regularItems = append(regularItems, item)
regularIndexes = append(regularIndexes, idx)
itemID := uint32(item.ItemId)
if _, ok := seenIDs[itemID]; ok {
continue
}
seenIDs[itemID] = struct{}{}
itemIDs = append(itemIDs, itemID)
}
}
if len(regularItems) == 0 {
result := make([]data.ItemInfo, 0, len(items))
for idx, item := range items {
if specialSuccess[idx] {
result = append(result, item)
}
}
return result
}
currentItems := p.Service.Item.CheakItemM(itemIDs...)
currentMap := make(map[uint32]int64, len(currentItems))
for _, item := range currentItems {
currentMap[item.ItemId] = item.ItemCnt
}
maxMap := dictrvice.NewDictInfoService().GetMaxMap(itemIDs...)
batchItems := make([]data.ItemInfo, 0, len(regularItems))
pendingMap := make(map[uint32]int64, len(itemIDs))
for _, item := range regularItems {
itemID := uint32(item.ItemId)
itemmax := maxMap[itemID]
if itemmax == 0 {
cool.Logger.Error(context.TODO(), "物品不存在", p.Info.UserID, item.ItemId)
sendErr(errorcode.ErrorCodes.ErrSystemError200007)
continue
}
if currentMap[itemID]+pendingMap[itemID]+item.ItemCnt > int64(itemmax) {
println(p.Info.UserID, "物品超过拥有最大限制", item.ItemId)
sendErr(errorcode.ErrorCodes.ErrTooManyOfItem)
continue
}
pendingMap[itemID] += item.ItemCnt
batchItems = append(batchItems, item)
}
addedRegularItems, err := p.Service.Item.AddItemsChecked(batchItems, currentMap)
if err != nil {
sendErr(errorcode.ErrorCodes.ErrSystemError200007)
return nil
}
regularSuccess := make(map[int]bool, len(addedRegularItems))
regularPos := 0
for idx, item := range regularItems {
if regularPos < len(addedRegularItems) &&
addedRegularItems[regularPos].ItemId == item.ItemId &&
addedRegularItems[regularPos].ItemCnt == item.ItemCnt {
regularSuccess[regularIndexes[idx]] = true
regularPos++
}
}
result := make([]data.ItemInfo, 0, len(items))
for idx, item := range items {
if specialSuccess[idx] || regularSuccess[idx] {
result = append(result, item)
}
}
return result
}
func (p *Player) ItemAdd(ItemId, ItemCnt int64) (result bool) {
if ItemCnt <= 0 {
t1 := common.NewTomeeHeader(2601, p.Info.UserID)