feat(game): 实现扭蛋系统批量物品添加功能并优化地图逻辑 - 新增ItemAddBatch方法用于批量添加物品,支持普通道具和特殊道具的分别处理 - 优化扭蛋游戏玩法中的物品添加逻辑,使用新的批量接口提升性能 - 在扭蛋机器人命令中实现完整的物品检查和批量添加流程 refactor(map): 重构地图控制器代码结构并添加注释 - 为EnterMap、LeaveMap、GetMapPlayerList等方法添加中文注释 - 统一地图相关的命名规范,如enter
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/common/data"
|
||||
"blazing/cool"
|
||||
"blazing/modules/player/model"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
dictservice "blazing/modules/dict/service"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
@@ -75,8 +78,151 @@ func (s *ItemService) UPDATE(id uint32, count int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddUniqueItems 为一组互不重复的物品各增加 1 个。
|
||||
// 返回值只包含本次实际成功增加的物品 id。
|
||||
// AddItems 批量添加道具,返回本次实际成功添加的奖励明细(保留原始顺序)。
|
||||
// AddItemsChecked 写入已完成上限校验的批量道具。
|
||||
func (s *ItemService) AddItemsChecked(items []data.ItemInfo, currentMap map[uint32]int64) ([]data.ItemInfo, error) {
|
||||
if len(items) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
updateCounts := make(map[uint32]int64, len(items))
|
||||
insertData := g.List{}
|
||||
successItems := make([]data.ItemInfo, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
if item.ItemId <= 0 || item.ItemCnt <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
itemID := uint32(item.ItemId)
|
||||
successItems = append(successItems, item)
|
||||
if _, ok := currentMap[itemID]; ok {
|
||||
updateCounts[itemID] += item.ItemCnt
|
||||
continue
|
||||
}
|
||||
|
||||
currentMap[itemID] = item.ItemCnt
|
||||
insertData = append(insertData, g.Map{
|
||||
"player_id": s.userid,
|
||||
"item_id": itemID,
|
||||
"item_cnt": item.ItemCnt,
|
||||
"is_vip": cool.Config.ServerInfo.IsVip,
|
||||
})
|
||||
}
|
||||
|
||||
err := g.DB().Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
|
||||
if len(updateCounts) > 0 {
|
||||
if err := s.batchIncrementItems(tx, updateCounts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(insertData) > 0 {
|
||||
if _, err := tx.Model(s.Model).Data(insertData).Insert(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return successItems, nil
|
||||
}
|
||||
|
||||
func (s *ItemService) AddItems(items []data.ItemInfo) ([]data.ItemInfo, error) {
|
||||
if len(items) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var (
|
||||
itemIDs = make([]uint32, 0, len(items))
|
||||
seenIDs = make(map[uint32]struct{}, len(items))
|
||||
)
|
||||
|
||||
for _, item := range items {
|
||||
if item.ItemId <= 0 || item.ItemCnt <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
itemID := uint32(item.ItemId)
|
||||
if _, ok := seenIDs[itemID]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
seenIDs[itemID] = struct{}{}
|
||||
itemIDs = append(itemIDs, itemID)
|
||||
}
|
||||
|
||||
if len(itemIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
currentItems := s.CheakItemM(itemIDs...)
|
||||
currentMap := make(map[uint32]int64, len(currentItems))
|
||||
for _, item := range currentItems {
|
||||
currentMap[item.ItemId] = item.ItemCnt
|
||||
}
|
||||
maxMap := dictservice.NewDictInfoService().GetMaxMap(itemIDs...)
|
||||
|
||||
pendingMap := make(map[uint32]int64, len(itemIDs))
|
||||
checkedItems := make([]data.ItemInfo, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
if item.ItemId <= 0 || item.ItemCnt <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
itemID := uint32(item.ItemId)
|
||||
itemMax := maxMap[itemID]
|
||||
if itemMax == 0 {
|
||||
continue
|
||||
}
|
||||
if currentMap[itemID]+pendingMap[itemID]+item.ItemCnt > int64(itemMax) {
|
||||
continue
|
||||
}
|
||||
|
||||
pendingMap[itemID] += item.ItemCnt
|
||||
checkedItems = append(checkedItems, item)
|
||||
}
|
||||
|
||||
return s.AddItemsChecked(checkedItems, currentMap)
|
||||
}
|
||||
|
||||
func (s *ItemService) batchIncrementItems(tx gdb.TX, itemCounts map[uint32]int64) error {
|
||||
var (
|
||||
builder strings.Builder
|
||||
args = make([]any, 0, len(itemCounts)*3+2)
|
||||
itemIDs = make([]any, 0, len(itemCounts))
|
||||
)
|
||||
|
||||
builder.WriteString("UPDATE ")
|
||||
builder.WriteString(s.Model.TableName())
|
||||
builder.WriteString(" SET item_cnt = CASE item_id ")
|
||||
|
||||
for itemID, itemCnt := range itemCounts {
|
||||
builder.WriteString("WHEN ? THEN item_cnt + ? ")
|
||||
args = append(args, itemID, itemCnt)
|
||||
itemIDs = append(itemIDs, itemID)
|
||||
}
|
||||
|
||||
builder.WriteString("ELSE item_cnt END WHERE player_id = ? AND is_vip = ? AND item_id IN (")
|
||||
for i := range itemIDs {
|
||||
if i > 0 {
|
||||
builder.WriteString(",")
|
||||
}
|
||||
builder.WriteString("?")
|
||||
}
|
||||
builder.WriteString(")")
|
||||
|
||||
args = append(args, s.userid, cool.Config.ServerInfo.IsVip)
|
||||
args = append(args, itemIDs...)
|
||||
|
||||
_, err := tx.Exec(builder.String(), args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *ItemService) AddUniqueItems(ids []uint32) ([]uint32, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user