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

feat(fight): 使用专用函数构建战斗结束数据包

为战斗结束消息创建专用的构建函数,
统一处理战斗结束信息的数据包构建逻辑,
提高代码的一致性和可维护性。

fix(config): 优化数据库查询语句以提高性能

将数组包含操作(@>)替换为 ANY 操作符,
在 Egg、MapPit、PetFusion 等服务中使用更高效
的查询方式
This commit is contained in:
昔念
2026-04-12 13:27:39 +08:00
parent 174830731c
commit f9543a5156
9 changed files with 65 additions and 14 deletions

View File

@@ -0,0 +1,31 @@
package fight
import "blazing/modules/player/model"
// buildFightOverPayload builds the legacy 2506 payload expected by the flash client.
// Regular fight-over packets use a different reason mapping than group fight 7560:
// 0=normal end 1=player lost/offline 2=overtime 3=draw 4=system error 5=npc escape.
func buildFightOverPayload(over model.FightOverInfo) *model.FightOverInfo {
payload := over
payload.Reason = mapFightOverReasonFor2506(over.Reason)
return &payload
}
func mapFightOverReasonFor2506(reason model.EnumBattleOverReason) model.EnumBattleOverReason {
switch reason {
case model.BattleOverReason.PlayerOffline:
return 1
case model.BattleOverReason.PlayerOVerTime:
return 2
case model.BattleOverReason.NOTwind:
return 3
case model.BattleOverReason.PlayerEscape:
// Player-initiated escape is handled by 2410 on the flash side; 2506 should
// still land in a non-error bucket instead of "system error".
return 1
case model.BattleOverReason.Cacthok, model.BattleOverReason.DefaultEnd:
return 0
default:
return 4
}
}

View File

@@ -177,7 +177,7 @@ func (f *FightC) battleLoop() {
if f.LegacyGroupProtocol {
f.sendLegacyGroupOver(p, &f.FightOverInfo)
} else {
f.sendFightPacket(p, fightPacketOver, &f.FightOverInfo)
f.sendFightPacket(p, fightPacketOver, buildFightOverPayload(f.FightOverInfo))
}
p.QuitFight()

View File

@@ -342,7 +342,7 @@ func buildFight(opts *fightBuildOptions) (*FightC, errorcode.ErrorCode) {
if f.LegacyGroupProtocol {
f.sendLegacyGroupOver(p, &f.FightOverInfo)
} else {
f.sendFightPacket(p, fightPacketOver, &f.FightOverInfo)
f.sendFightPacket(p, fightPacketOver, buildFightOverPayload(f.FightOverInfo))
}
p.QuitFight()
})

View File

@@ -26,7 +26,7 @@ func (s *EggService) GetData(p1 uint32) []int32 {
m := dbm_enable(s.Model)
var pet []model.Egg //一个特性应该是唯一的,但是我们要获取默认随机特性
m.Wheref(`male_pet_ids @> ARRAY[?]::integer[]`, p1).Scan(&pet)
m.Wheref(`? = ANY(male_pet_ids)`, p1).Scan(&pet)
var petIDs []int32
for _, p := range pet {
petIDs = append(petIDs, p.FemalePetIDs...)
@@ -40,8 +40,8 @@ func (s *EggService) GetResult(m, f, level uint32) (uint32, bool) {
md := dbm_enable(s.Model)
var pet *model.Egg //一个特性应该是唯一的,但是我们要获取默认随机特性
md.Wheref(`male_pet_ids @> ARRAY[?]::integer[]`, m).
Wheref(`female_pet_ids @> ARRAY[?]::integer[]`, f).Scan(&pet)
md.Wheref(`? = ANY(male_pet_ids)`, m).
Wheref(`? = ANY(female_pet_ids)`, f).Scan(&pet)
if pet != nil {
pet.Probs[len(pet.Probs)-1] += int32(level)
t, _ := utils.RandomByWeight(pet.OutputMons, pet.Probs)

View File

@@ -41,7 +41,7 @@ func (s *MapPitService) GetDataALL(mapid uint32) []model.MapPit {
func (s *MapPitService) GetPet(petid uint32) []model.MapPit {
var pet []model.MapPit //一个特性应该是唯一的,但是我们要获取默认随机特性
dbm_enable(s.Model).Wheref(`refresh_id @> ARRAY[?]::integer[]`, petid).Scan(&pet)
dbm_enable(s.Model).Wheref(`? = ANY(refresh_id)`, petid).Scan(&pet)
return pet

View File

@@ -51,7 +51,7 @@ func (s *PetFusionService) Data(p1, p2, rand uint32) uint32 {
func (s *PetFusionService) getData(p1, p2 uint32) uint32 {
var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性
dbm_enable(s.Model).Where("main_pet_id", p1).Wheref(`sub_pet_ids @> ARRAY[?]::integer[]`, p2).Scan(&pet)
dbm_enable(s.Model).Where("main_pet_id", p1).Wheref(`? = ANY(sub_pet_ids)`, p2).Scan(&pet)
if len(pet) != 0 {
var pets, props []int

View File

@@ -47,7 +47,7 @@ func (s *ShinyService) listByPetID(id uint32) []model.ColorfulSkin {
var ret []model.ColorfulSkin
dbm_enable(s.Model).
Wheref(`bind_elf_ids @> ?::jsonb`, id).
Wheref(`CAST(? AS text) = ANY(ARRAY(SELECT jsonb_array_elements_text(bind_elf_ids)))`, id).
Wheref(`jsonb_typeof(bind_elf_ids) = ?`, "array").
Scan(&ret)

View File

@@ -3,9 +3,10 @@ package service
import (
"blazing/cool"
"blazing/modules/player/model"
"fmt"
"strings"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type DoneService struct {
@@ -68,10 +69,10 @@ func (s *DoneService) update(ptye model.EnumMilestone, args []uint32, results []
return
}
ar := gconv.String(args)
if t, _ := s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(`args @> ?::jsonb`, ar).
whereArgs, whereParams := buildJSONBArrayAnyCondition("args", args)
if t, _ := s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(whereArgs, whereParams...).
Wheref(`jsonb_typeof(args) = ?`, "array").Exist(); t {
s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(`args @> ?::jsonb`, ar).
s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(whereArgs, whereParams...).
Wheref(`jsonb_typeof(args) = ?`, "array").Data(
g.Map{
"results": results,
@@ -94,12 +95,31 @@ func (s *DoneService) update(ptye model.EnumMilestone, args []uint32, results []
func (s *DoneService) get(ptye model.EnumMilestone, args []uint32) *model.Milestone {
var Barges *model.Milestone
s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(`args @> ?::jsonb`, args).
whereArgs, whereParams := buildJSONBArrayAnyCondition("args", args)
s.dbm_fix(s.Model).Where("done_type", ptye).Wheref(whereArgs, whereParams...).
Wheref(`jsonb_typeof(args) = ?`, "array").Scan(&Barges)
return Barges
}
func buildJSONBArrayAnyCondition(column string, values []uint32) (string, []any) {
if len(values) == 0 {
return "FALSE", nil
}
clauses := make([]string, 0, len(values)+1)
params := make([]any, 0, len(values)+1)
for _, value := range values {
clauses = append(clauses, fmt.Sprintf(`CAST(? AS text) = ANY(ARRAY(SELECT jsonb_array_elements_text(%s)))`, column))
params = append(params, value)
}
clauses = append(clauses, fmt.Sprintf(`jsonb_array_length(%s) = ?`, column))
params = append(params, len(values))
return strings.Join(clauses, " AND "), params
}
func (s *DoneService) PetBarge(start, end uint32) []model.Milestone {
var Barges []model.Milestone

View File

@@ -28,7 +28,7 @@ func (s *TitleService) Get() []uint32 {
func (s *TitleService) Can(id uint32) bool {
m1 := s.dbm(s.Model)
ok, _ := m1.Wheref(`available_title @> ?::jsonb`, id).Exist()
ok, _ := m1.Wheref(`CAST(? AS text) = ANY(ARRAY(SELECT jsonb_array_elements_text(available_title)))`, id).Exist()
return ok
}