feat(game): 实现扭蛋系统批量物品添加功能并优化地图逻辑 - 新增ItemAddBatch方法用于批量添加物品,支持普通道具和特殊道具的分别处理 - 优化扭蛋游戏玩法中的物品添加逻辑,使用新的批量接口提升性能 - 在扭蛋机器人命令中实现完整的物品检查和批量添加流程 refactor(map): 重构地图控制器代码结构并添加注释 - 为EnterMap、LeaveMap、GetMapPlayerList等方法添加中文注释 - 统一地图相关的命名规范,如enter
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package robot
|
||||
|
||||
import (
|
||||
"blazing/common/data"
|
||||
"blazing/common/data/xmlres"
|
||||
base "blazing/modules/base/service"
|
||||
config "blazing/modules/config/service"
|
||||
dictservice "blazing/modules/dict/service"
|
||||
"blazing/modules/player/model"
|
||||
"blazing/modules/player/service"
|
||||
"strings"
|
||||
@@ -17,51 +19,129 @@ import (
|
||||
func init() {
|
||||
zero.OnCommand("扭蛋").
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
|
||||
msgs := strings.Fields(ctx.Event.Message.String())
|
||||
|
||||
if len(msgs) > 1 {
|
||||
|
||||
count := gconv.Int(msgs[1])
|
||||
if count > 10 {
|
||||
count = 10
|
||||
}
|
||||
user := base.NewBaseSysUserService().GetQQ(ctx.Event.Sender.ID)
|
||||
if user == nil {
|
||||
ctx.Send("未绑定,请个人中心复制token发给机器人")
|
||||
return
|
||||
}
|
||||
itemservice := service.NewItemService(uint32(user.ID))
|
||||
havs := itemservice.CheakItem(400501)
|
||||
if havs < int64(count) {
|
||||
ctx.Send("扭蛋币不足,当前扭蛋币数量:" + gconv.String(havs))
|
||||
return
|
||||
}
|
||||
var buf strings.Builder
|
||||
buf.WriteString("当前扭蛋币数量:" + gconv.String(havs) + "\n")
|
||||
if grand.Meet(int(count), 100) {
|
||||
r := config.NewPetRewardService().GetEgg()
|
||||
newPet := model.GenPetInfo(int(r.MonID), int(r.DV), int(r.Nature), int(r.Effect), int(r.Lv), nil, 0)
|
||||
if grand.Meet(1, 500) {
|
||||
newPet.RandomByWeightShiny()
|
||||
}
|
||||
service.NewPetService(uint32(user.ID)).PetAdd(newPet, 0)
|
||||
buf.WriteString("恭喜你获得" + xmlres.PetMAP[int(newPet.ID)].DefName + "\n")
|
||||
|
||||
}
|
||||
|
||||
items := config.NewItemService().GetEgg(int(count))
|
||||
|
||||
for _, item := range items {
|
||||
|
||||
itemservice.UPDATE(uint32(item.ItemId), int(item.ItemCnt))
|
||||
buf.WriteString("恭喜你获得" + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
}
|
||||
|
||||
itemservice.UPDATE(400501, int(-count))
|
||||
|
||||
ctx.SendChain(message.At(ctx.Event.Sender.ID), message.Reply(ctx.Event.MessageID), message.Text(buf.String()))
|
||||
if len(msgs) <= 1 {
|
||||
return
|
||||
}
|
||||
|
||||
count := gconv.Int(msgs[1])
|
||||
if count > 10 {
|
||||
count = 10
|
||||
}
|
||||
if count <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
user := base.NewBaseSysUserService().GetQQ(ctx.Event.Sender.ID)
|
||||
if user == nil {
|
||||
ctx.Send("未绑定,请先在个人中心绑定 token")
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint32(user.ID)
|
||||
itemService := service.NewItemService(userID)
|
||||
infoService := service.NewInfoService(userID)
|
||||
playerInfo := infoService.GetLogin()
|
||||
if playerInfo == nil {
|
||||
ctx.Send("未创建角色,请先登录游戏")
|
||||
return
|
||||
}
|
||||
|
||||
havs := itemService.CheakItem(400501)
|
||||
if havs < int64(count) {
|
||||
ctx.Send("扭蛋币不足,当前扭蛋币数量:" + gconv.String(havs))
|
||||
return
|
||||
}
|
||||
|
||||
var buf strings.Builder
|
||||
buf.WriteString("当前扭蛋币数量:" + gconv.String(havs) + "\n")
|
||||
|
||||
if grand.Meet(count, 100) {
|
||||
r := config.NewPetRewardService().GetEgg()
|
||||
newPet := model.GenPetInfo(int(r.MonID), int(r.DV), int(r.Nature), int(r.Effect), int(r.Lv), nil, 0)
|
||||
if grand.Meet(1, 500) {
|
||||
newPet.RandomByWeightShiny()
|
||||
}
|
||||
service.NewPetService(userID).PetAdd(newPet, 0)
|
||||
buf.WriteString("恭喜你获得 " + xmlres.PetMAP[int(newPet.ID)].DefName + "\n")
|
||||
}
|
||||
|
||||
items := config.NewItemService().GetEgg(count)
|
||||
regularItems := make([]data.ItemInfo, 0, len(items))
|
||||
itemIDs := make([]uint32, 0, len(items))
|
||||
seenIDs := make(map[uint32]struct{}, len(items))
|
||||
infoDirty := false
|
||||
|
||||
for _, item := range items {
|
||||
switch item.ItemId {
|
||||
case 1:
|
||||
playerInfo.Coins += item.ItemCnt
|
||||
infoDirty = true
|
||||
buf.WriteString("恭喜你获得 " + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
case 3:
|
||||
playerInfo.ExpPool += item.ItemCnt
|
||||
infoDirty = true
|
||||
buf.WriteString("恭喜你获得 " + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
case 5:
|
||||
base.NewBaseSysUserService().UpdateGold(userID, item.ItemCnt*100)
|
||||
buf.WriteString("恭喜你获得 " + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
case 9:
|
||||
playerInfo.EVPool += item.ItemCnt
|
||||
infoDirty = true
|
||||
buf.WriteString("恭喜你获得 " + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
default:
|
||||
regularItems = append(regularItems, item)
|
||||
|
||||
itemID := uint32(item.ItemId)
|
||||
if _, ok := seenIDs[itemID]; ok {
|
||||
continue
|
||||
}
|
||||
seenIDs[itemID] = struct{}{}
|
||||
itemIDs = append(itemIDs, itemID)
|
||||
}
|
||||
}
|
||||
|
||||
if infoDirty {
|
||||
infoService.Save(*playerInfo)
|
||||
}
|
||||
|
||||
currentItems := itemService.CheakItemM(itemIDs...)
|
||||
currentMap := make(map[uint32]int64, len(currentItems))
|
||||
for _, item := range currentItems {
|
||||
currentMap[item.ItemId] = item.ItemCnt
|
||||
}
|
||||
maxMap := dictservice.NewDictInfoService().GetMaxMap(itemIDs...)
|
||||
|
||||
addableItems := 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 {
|
||||
buf.WriteString("未发放奖励:物品不存在 " + gconv.String(item.ItemId) + "\n")
|
||||
continue
|
||||
}
|
||||
|
||||
if currentMap[itemID]+pendingMap[itemID]+item.ItemCnt > int64(itemMax) {
|
||||
buf.WriteString("未发放奖励:" + xmlres.ItemsMAP[int(item.ItemId)].Name + " 超出最大持有数量\n")
|
||||
continue
|
||||
}
|
||||
|
||||
pendingMap[itemID] += item.ItemCnt
|
||||
addableItems = append(addableItems, item)
|
||||
}
|
||||
|
||||
addedItems, err := itemService.AddItemsChecked(addableItems, currentMap)
|
||||
if err != nil {
|
||||
ctx.Send("扭蛋失败,请稍后再试")
|
||||
return
|
||||
}
|
||||
for _, item := range addedItems {
|
||||
buf.WriteString("恭喜你获得 " + xmlres.ItemsMAP[int(item.ItemId)].Name + ":" + gconv.String(item.ItemCnt) + "\n")
|
||||
}
|
||||
|
||||
itemService.UPDATE(400501, -count)
|
||||
|
||||
ctx.SendChain(message.At(ctx.Event.Sender.ID), message.Reply(ctx.Event.MessageID), message.Text(buf.String()))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ func (s *InfoService) GetLogin() *model.PlayerInfo {
|
||||
// //defer t.Service.Talk_Reset()
|
||||
_, err := s.dbm_fix(s.Model).Data("last_reset_time", gtime.Now()).Update()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
cool.Logger.Error(context.TODO(), "update last_reset_time failed", s.userid, err)
|
||||
}
|
||||
}
|
||||
if !utils.IsWEEK(tt.WeekLastResetTime) {
|
||||
@@ -136,7 +136,7 @@ func (s *InfoService) GetLogin() *model.PlayerInfo {
|
||||
}
|
||||
_, err := s.dbm_fix(s.Model).Data("week_last_reset_time", gtime.Now()).Update()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
cool.Logger.Error(context.TODO(), "update week_last_reset_time failed", s.userid, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,15 +246,14 @@ func (s *InfoService) Save(data model.PlayerInfo) {
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
_, err := s.dbm_fix(s.Model).Data("data", data).Update()
|
||||
if err != nil {
|
||||
if i == 2 {
|
||||
//todo 待实现兜底保存,现在有可能出错
|
||||
s.saveToLocalFile(&data, err)
|
||||
panic(err)
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
break
|
||||
if i == 2 {
|
||||
cool.Logger.Error(context.TODO(), "player save failed after retries, fallback to local file", data.UserID, err)
|
||||
s.saveToLocalFile(&data, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/player/model"
|
||||
"context"
|
||||
|
||||
"github.com/pointernil/bitset32"
|
||||
)
|
||||
@@ -58,7 +59,7 @@ func (s *TaskService) Exec(id uint32, t func(*model.Task) bool) {
|
||||
gg.TaskID = id
|
||||
_, err := m1.Save(gg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
cool.Logger.Error(context.TODO(), "task save failed", s.userid, id, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user