rolePlugin.go
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
}