package models import ( "fmt" "github.com/golang/protobuf/proto" "pro2d/common" "pro2d/common/logger" "pro2d/pb" ) //背包系统 func (m *RoleModel) GetItemCount(key string) uint32 { c, ok := m.Items[key] if !ok { c = 0 } return c.(uint32) } func (m *RoleModel) CostItem(key string, count int32) bool { if uint32(count) > m.GetItemCount(key) { return false } return m.AddItem(key, -count) } func (m *RoleModel) CostItems(params common.IMapString) bool { for k, v := range params { if v.(uint32) > m.GetItemCount(k) { return false } m.AddItem(k, -v.(int32)) } return true } func (m *RoleModel) AddItem(key string, count int32) bool { c := m.GetItemCount(key) num := int32(c) + count if num > 0 { m.Items[key] = uint32(num) } else { delete(m.Items, key) } m.SetProperty("items", common.MapToString(m.Items)) rsp, err := proto.Marshal(&pb.RoleUpdateItemsRsp{Items: fmt.Sprintf("%s=%d", key, num)}) if err != nil { logger.Error(err.Error()) return true } m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) return true } func (m *RoleModel) AddItems(params common.IMapString) bool { tmp := make(common.IMapString) for k, v := range params { c := m.GetItemCount(k) num := c + v.(uint32) if num > 0 { m.Items[k] = num tmp[k] = num } else { delete(m.Items, k) } } m.SetProperty("items", common.MapToString(m.Items)) rsp, err := proto.Marshal(&pb.RoleUpdateItemsRsp{Items: common.MapToString(tmp)}) if err != nil { logger.Error(err.Error()) return true } if m.GetConn() != nil { m.GetConn().Send(0, uint32(pb.ProtoCode_RoleUpdateItemsRsp), rsp) } return true }