package maps import ( "f1-game/internal/pb" cherryLogger "github.com/cherry-game/cherry/logger" "github.com/spf13/cast" ) const ( Int32 KeyType = 1 Int64 KeyType = 2 String KeyType = 3 ) type ( // KeyType 属性类型 KeyType int32 // KeyMap key map KeyMap struct { maps pb.KeyMap } ) func NewKeyMap() *KeyMap { return &KeyMap{ maps: *NewPBKeyMap(), } } func NewPBKeyMap() *pb.KeyMap { return &pb.KeyMap{ Int32Maps: make(map[int32]int32), Int64Maps: make(map[int32]int64), StringMaps: make(map[int32]string), } } func (p *KeyMap) Set(key int32, typ KeyType, value any) { switch typ { case Int32: { result, err := cast.ToInt32E(value) if err != nil { p.error(key, value, "Set->Int32", err) return } p.maps.Int32Maps[key] = result break } case Int64: { result, err := cast.ToInt64E(value) if err != nil { p.error(key, value, "Set->Int64", err) return } p.maps.Int64Maps[key] = result break } case String: { result, err := cast.ToStringE(value) if err != nil { p.error(key, value, "Set->String", err) return } p.maps.StringMaps[key] = result break } } } func (p *KeyMap) GetKeys(keys ...int32) *KeyMap { newKeyMap := NewKeyMap() for _, key := range keys { v32, found := p.maps.Int32Maps[key] if found { newKeyMap.maps.Int32Maps[key] = v32 continue } v64, found := p.maps.Int64Maps[key] if found { newKeyMap.maps.Int64Maps[key] = v64 continue } vString, found := p.maps.StringMaps[key] if found { newKeyMap.maps.StringMaps[key] = vString continue } } return newKeyMap } func (p *KeyMap) GetInt32(key int32) (int32, bool) { val, found := p.maps.Int32Maps[key] return val, found } func (p *KeyMap) GetInt64(key int32) (int64, bool) { val, found := p.maps.Int64Maps[key] return val, found } func (p *KeyMap) GetString(key int32) (string, bool) { val, found := p.maps.StringMaps[key] return val, found } func (p *KeyMap) Int32Maps() map[int32]int32 { return p.maps.GetInt32Maps() } func (p *KeyMap) Int64Maps() map[int32]int64 { return p.maps.GetInt64Maps() } func (p *KeyMap) StringMaps() map[int32]string { return p.maps.GetStringMaps() } func (*KeyMap) error(key int32, value any, info string, err error) { cherryLogger.Warnf("[key = %v,value = %v] %s fail = %v", key, value, info, err, ) } func (p *KeyMap) ToProtobuf() *pb.KeyMap { return &p.maps }