refactor: 优化代码结构和逻辑
This commit is contained in:
@@ -10,133 +10,159 @@ import (
|
||||
|
||||
"github.com/alpacahq/alpacadecimal"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const (
|
||||
petFusionCost = 1000
|
||||
petFusionKeepAuxItemID = 300043
|
||||
petFusionFailureItemID = 300044
|
||||
petFusionSoulID = 1000017
|
||||
)
|
||||
|
||||
func (h Controller) PetFusion(data *pet.C2S_PetFusion, c *player.Player) (result *pet.PetFusionInfo, err errorcode.ErrorCode) {
|
||||
if !c.GetCoins(1000) {
|
||||
return result, errorcode.ErrorCodes.ErrSunDouInsufficient10016
|
||||
result = &pet.PetFusionInfo{
|
||||
SoulID: petFusionSoulID,
|
||||
}
|
||||
c.Info.Coins -= 1000
|
||||
// g.Dump(c.Info.PetList)
|
||||
|
||||
//防止同一只
|
||||
if data.Mcatchtime == data.Auxcatchtime {
|
||||
return result, errorcode.ErrorCodes.ErrPokemonNotFusionReady
|
||||
}
|
||||
|
||||
_, Mcatchpetinfo, ok := c.FindPet(data.Mcatchtime)
|
||||
_, masterPet, ok := c.FindPet(data.Mcatchtime)
|
||||
if !ok {
|
||||
return result, errorcode.ErrorCodes.ErrPokemonNotFusionReady2
|
||||
}
|
||||
|
||||
if xmlres.PetMAP[int(Mcatchpetinfo.ID)].FuseMaster == 0 {
|
||||
masterCfg, ok := xmlres.PetMAP[int(masterPet.ID)]
|
||||
if !ok || masterCfg.FuseMaster == 0 {
|
||||
return result, errorcode.ErrorCodes.ErrPokemonNotFusionReady3
|
||||
}
|
||||
_, Auxpetinfo, ok := c.FindPet(data.Auxcatchtime)
|
||||
|
||||
_, auxPet, ok := c.FindPet(data.Auxcatchtime)
|
||||
if !ok {
|
||||
return result, errorcode.ErrorCodes.ErrPokemonNotFusionReady2
|
||||
}
|
||||
|
||||
if xmlres.PetMAP[int(Auxpetinfo.ID)].FuseSub == 0 {
|
||||
auxCfg, ok := xmlres.PetMAP[int(auxPet.ID)]
|
||||
if !ok || auxCfg.FuseSub == 0 {
|
||||
return result, errorcode.ErrorCodes.ErrPokemonNotFusionReady3
|
||||
}
|
||||
//println(len(c.Info.PetList), data.Mcatchtime, data.Auxcatchtime, Mcatchpetinfo.CatchTime, Auxpetinfo.CatchTime)
|
||||
///性格生成
|
||||
var natureId int32 = -1
|
||||
if Auxpetinfo.Nature == Mcatchpetinfo.Nature {
|
||||
natureId = int32(Auxpetinfo.Nature)
|
||||
|
||||
materialCounts := collectItemCounts(data.Item1[:])
|
||||
if !hasEnoughItems(c, materialCounts) {
|
||||
return result, errorcode.ErrorCodes.ErrInsufficientItems
|
||||
}
|
||||
|
||||
for _, v := range c.Service.Item.CheakItemM(data.Item1[:]...) {
|
||||
|
||||
if v.ItemCnt <= 0 {
|
||||
return result, errorcode.ErrorCodes.ErrInsufficientItems
|
||||
}
|
||||
}
|
||||
|
||||
resid := int(service.NewPetFusionService().Data(Mcatchpetinfo.ID, Auxpetinfo.ID, Mcatchpetinfo.Level+Auxpetinfo.Level))
|
||||
effect := int(service.NewPetFusionMaterialService().Data(data.Item1))
|
||||
|
||||
materialService := service.NewPetFusionMaterialService()
|
||||
effect := int(materialService.Data(data.Item1))
|
||||
if effect == 0 {
|
||||
return result, errorcode.ErrorCodes.ErrSpiritOrbNotExists
|
||||
}
|
||||
//c.Service.Item.UPDATEM(data.Item1[:], -1)
|
||||
|
||||
// utils.CountSliceElements(data.Item1[:])
|
||||
|
||||
for _, v := range data.Item1 {
|
||||
err := c.Service.Item.UPDATE(v, -1)
|
||||
if err != nil {
|
||||
return result, errorcode.ErrorCodes.ErrInsufficientItems
|
||||
}
|
||||
fusionService := service.NewPetFusionService()
|
||||
resultPetID := int(fusionService.Data(masterPet.ID, auxPet.ID, masterPet.Level+auxPet.Level))
|
||||
|
||||
if !c.GetCoins(petFusionCost) {
|
||||
return result, errorcode.ErrorCodes.ErrSunDouInsufficient10016
|
||||
}
|
||||
result = &pet.PetFusionInfo{
|
||||
|
||||
SoulID: 1000017,
|
||||
consumeItems(c, materialCounts)
|
||||
c.Info.Coins -= petFusionCost
|
||||
|
||||
CostItemFlag: 0,
|
||||
}
|
||||
if resid == 0 {
|
||||
|
||||
_, ok := lo.Find(data.GoldItem1[:], func(item uint32) bool {
|
||||
return item == 300044
|
||||
})
|
||||
if c.Service.Item.CheakItem(300044) > 0 && ok {
|
||||
c.Service.Item.UPDATE(300044, -1)
|
||||
if resultPetID == 0 {
|
||||
if useOptionalItem(c, data.GoldItem1[:], petFusionFailureItemID) {
|
||||
result.CostItemFlag = 1
|
||||
|
||||
} else if auxPet.Level > 5 {
|
||||
auxPet.Downgrade(auxPet.Level - 5)
|
||||
} else {
|
||||
if Auxpetinfo.Level > 5 {
|
||||
// Auxpetinfo.Level = Auxpetinfo.Level - 5
|
||||
Auxpetinfo.Downgrade(Auxpetinfo.Level - 5)
|
||||
|
||||
} else {
|
||||
Auxpetinfo.Downgrade(1)
|
||||
|
||||
}
|
||||
|
||||
auxPet.Downgrade(1)
|
||||
}
|
||||
|
||||
return &pet.PetFusionInfo{}, 0
|
||||
}
|
||||
|
||||
dv1 := alpacadecimal.NewFromInt(2).Div(alpacadecimal.NewFromInt(3)).Mul(alpacadecimal.NewFromInt(int64(Mcatchpetinfo.Dv)))
|
||||
dv2 := alpacadecimal.NewFromInt(1).Div(alpacadecimal.NewFromInt(3)).Mul(alpacadecimal.NewFromInt(int64(Auxpetinfo.Dv)))
|
||||
natureID := int32(-1)
|
||||
if auxPet.Nature == masterPet.Nature {
|
||||
natureID = int32(auxPet.Nature)
|
||||
}
|
||||
|
||||
dv1 := alpacadecimal.NewFromInt(2).Div(alpacadecimal.NewFromInt(3)).Mul(alpacadecimal.NewFromInt(int64(masterPet.Dv)))
|
||||
dv2 := alpacadecimal.NewFromInt(1).Div(alpacadecimal.NewFromInt(3)).Mul(alpacadecimal.NewFromInt(int64(auxPet.Dv)))
|
||||
dv := dv1.Add(dv2).Add(alpacadecimal.NewFromInt(1)).IntPart()
|
||||
r := model.GenPetInfo(resid, int(dv), int(natureId), effect, 1, nil, -1)
|
||||
r.OldCatchTime = Mcatchpetinfo.CatchTime
|
||||
newPet := model.GenPetInfo(resultPetID, int(dv), int(natureID), effect, 1, nil, -1)
|
||||
newPet.OldCatchTime = masterPet.CatchTime
|
||||
|
||||
shinycont := 1
|
||||
if Mcatchpetinfo.IsShiny() || Auxpetinfo.IsShiny() {
|
||||
shinycont = 50
|
||||
shinyChance := 1
|
||||
if masterPet.IsShiny() || auxPet.IsShiny() {
|
||||
shinyChance = 50
|
||||
}
|
||||
if Mcatchpetinfo.IsShiny() && Auxpetinfo.IsShiny() {
|
||||
shinycont = 100
|
||||
if masterPet.IsShiny() && auxPet.IsShiny() {
|
||||
shinyChance = 100
|
||||
}
|
||||
if grand.Meet(shinyChance, 100) {
|
||||
newPet.RandomByWeightShiny()
|
||||
}
|
||||
if grand.Meet(shinycont, 100) {
|
||||
r.RandomByWeightShiny()
|
||||
|
||||
}
|
||||
c.Service.Pet.PetAdd(r, 0)
|
||||
println(c.Info.UserID, "进行融合", len(c.Info.PetList), Mcatchpetinfo.ID, Auxpetinfo.ID, r.ID)
|
||||
c.Service.Pet.PetAdd(newPet, 0)
|
||||
println(c.Info.UserID, "进行融合", len(c.Info.PetList), masterPet.ID, auxPet.ID, newPet.ID)
|
||||
|
||||
c.PetDel(data.Mcatchtime)
|
||||
_, ok2 := lo.Find(data.GoldItem1[:], func(item uint32) bool {
|
||||
return item == 300043
|
||||
})
|
||||
|
||||
if c.Service.Item.CheakItem(300043) > 0 && ok2 {
|
||||
c.Service.Item.UPDATE(300043, -1)
|
||||
if useOptionalItem(c, data.GoldItem1[:], petFusionKeepAuxItemID) {
|
||||
result.CostItemFlag = 1
|
||||
} else {
|
||||
|
||||
c.PetDel(data.Auxcatchtime)
|
||||
|
||||
}
|
||||
result.ObtainTime = r.CatchTime
|
||||
result.StarterCpTm = r.ID
|
||||
//todo材料扣除
|
||||
|
||||
result.ObtainTime = newPet.CatchTime
|
||||
result.StarterCpTm = newPet.ID
|
||||
return result, 0
|
||||
}
|
||||
|
||||
func collectItemCounts(itemIDs []uint32) map[uint32]int {
|
||||
counts := make(map[uint32]int, len(itemIDs))
|
||||
for _, itemID := range itemIDs {
|
||||
counts[itemID]++
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
func hasEnoughItems(c *player.Player, itemCounts map[uint32]int) bool {
|
||||
if len(itemCounts) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
itemIDs := make([]uint32, 0, len(itemCounts))
|
||||
for itemID := range itemCounts {
|
||||
itemIDs = append(itemIDs, itemID)
|
||||
}
|
||||
|
||||
hadItems := c.Service.Item.CheakItemM(itemIDs...)
|
||||
ownedCounts := make(map[uint32]int64, len(hadItems))
|
||||
for _, item := range hadItems {
|
||||
ownedCounts[item.ItemId] = item.ItemCnt
|
||||
}
|
||||
|
||||
for itemID, need := range itemCounts {
|
||||
if ownedCounts[itemID] < int64(need) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func consumeItems(c *player.Player, itemCounts map[uint32]int) {
|
||||
for itemID, count := range itemCounts {
|
||||
_ = c.Service.Item.UPDATE(itemID, -count)
|
||||
}
|
||||
}
|
||||
|
||||
func useOptionalItem(c *player.Player, itemIDs []uint32, target uint32) bool {
|
||||
if c.Service.Item.CheakItem(target) <= 0 {
|
||||
return false
|
||||
}
|
||||
for _, itemID := range itemIDs {
|
||||
if itemID == target {
|
||||
_ = c.Service.Item.UPDATE(target, -1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user