Files
GaCy-GO/internal/sdkserver/battle_live_breakdown.go
T

62 lines
1.7 KiB
Go

package sdkserver
import (
"fmt"
pb "gac-hkrpg-go/protocol/proto"
"math"
"sort"
)
type liveAvatarBreakdown struct {
Damage map[string]float64 `json:"attack_type_damage"`
BreakDamage map[string]float64 `json:"attack_type_break_damage"`
MaxDamage map[string]float64 `json:"attack_type_max_damage"`
}
func liveDamageProperties(values map[string]float64) ([]*pb.AttackDamageProperty, error) {
if len(values) > 64 {
return nil, fmt.Errorf("too many attack types")
}
keys := make([]string, 0, len(values))
for key, value := range values {
if key == "" || len(key) > 128 || math.IsNaN(value) || math.IsInf(value, 0) || value < 0 {
return nil, fmt.Errorf("invalid native attack-type statistic")
}
keys = append(keys, key)
}
sort.Strings(keys)
list := make([]*pb.AttackDamageProperty, 0, len(keys))
for _, key := range keys {
list = append(list, &pb.AttackDamageProperty{AttackType: key, Damage: values[key]})
}
return list, nil
}
func applyLiveAvatarBreakdowns(avatars []*pb.AvatarBattleInfo, values []liveAvatarBreakdown) error {
if len(values) == 0 {
return nil
}
if len(values) != len(avatars) {
return fmt.Errorf("native breakdown count does not match avatars")
}
for i, value := range values {
if avatars[i] == nil {
return fmt.Errorf("missing native avatar")
}
for _, pair := range []struct {
values map[string]float64
target *[]*pb.AttackDamageProperty
}{{value.Damage, &avatars[i].AttackTypeDamage}, {value.BreakDamage, &avatars[i].AttackTypeBreakDamage}, {value.MaxDamage, &avatars[i].AttackTypeMaxDamage}} {
if pair.values == nil {
continue
}
properties, err := liveDamageProperties(pair.values)
if err != nil {
return err
}
*pair.target = properties
}
}
return nil
}