/
blackwhite.go
295 lines (263 loc) · 8.03 KB
/
blackwhite.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"fmt"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
gt "github.com/33cn/plugin/plugin/dapp/blackwhite/types"
)
var (
clog = log.New("module", "execs.blackwhite")
blackwhiteAddr string
driverName = gt.BlackwhiteX
)
// Init 重命名执行器名称
func Init(name string, cfg *types.Chain33Config, sub []byte) {
driverName = name
gt.BlackwhiteX = driverName
gt.ExecerBlackwhite = []byte(driverName)
blackwhiteAddr = address.ExecAddress(cfg.ExecName(gt.BlackwhiteX))
drivers.Register(cfg, name, newBlackwhite, cfg.GetDappFork(driverName, "Enable"))
InitExecType()
}
//InitExecType ...
func InitExecType() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&Blackwhite{}))
}
// Blackwhite 几类执行器结构体
type Blackwhite struct {
drivers.DriverBase
}
func newBlackwhite() drivers.Driver {
c := &Blackwhite{}
c.SetChild(c)
c.SetExecutorType(types.LoadExecutorType(driverName))
return c
}
// GetName 获取执行器别名
func GetName() string {
return newBlackwhite().GetName()
}
// GetDriverName 获取执行器名字
func (c *Blackwhite) GetDriverName() string {
return driverName
}
func (c *Blackwhite) saveLoopResult(res *gt.ReplyLoopResults) (kvs []*types.KeyValue) {
kv := &types.KeyValue{}
kv.Key = calcRoundKey4LoopResult(res.GetGameID())
kv.Value = types.Encode(res)
kvs = append(kvs, kv)
return kvs
}
func (c *Blackwhite) delLoopResult(res *gt.ReplyLoopResults) (kvs []*types.KeyValue) {
kv := &types.KeyValue{}
kv.Key = calcRoundKey4LoopResult(res.GetGameID())
kv.Value = nil
kvs = append(kvs, kv)
return kvs
}
func (c *Blackwhite) saveHeightIndex(res *gt.ReceiptBlackwhiteStatus) (kvs []*types.KeyValue) {
heightstr := genHeightIndexStr(res.GetIndex())
kv := &types.KeyValue{}
kv.Key = calcRoundKey4AddrHeight(res.GetAddr(), heightstr)
kv.Value = []byte(res.GetGameID())
kvs = append(kvs, kv)
kv1 := &types.KeyValue{}
kv1.Key = calcRoundKey4StatusAddrHeight(res.GetStatus(), res.GetAddr(), heightstr)
kv1.Value = []byte(res.GetGameID())
kvs = append(kvs, kv1)
if res.GetStatus() >= 1 {
kv := &types.KeyValue{}
kv.Key = calcRoundKey4StatusAddrHeight(res.GetPrevStatus(), res.GetAddr(), heightstr)
kv.Value = nil
kvs = append(kvs, kv)
}
return kvs
}
func (c *Blackwhite) saveRollHeightIndex(res *gt.ReceiptBlackwhiteStatus) (kvs []*types.KeyValue) {
heightstr := genHeightIndexStr(res.GetIndex())
kv := &types.KeyValue{}
kv.Key = calcRoundKey4AddrHeight(res.GetAddr(), heightstr)
kv.Value = []byte(res.GetGameID())
kvs = append(kvs, kv)
kv1 := &types.KeyValue{}
kv1.Key = calcRoundKey4StatusAddrHeight(res.GetPrevStatus(), res.GetAddr(), heightstr)
kv1.Value = []byte(res.GetGameID())
kvs = append(kvs, kv1)
return kvs
}
func (c *Blackwhite) delHeightIndex(res *gt.ReceiptBlackwhiteStatus) (kvs []*types.KeyValue) {
heightstr := genHeightIndexStr(res.GetIndex())
kv := &types.KeyValue{}
kv.Key = calcRoundKey4AddrHeight(res.GetAddr(), heightstr)
kv.Value = nil
kvs = append(kvs, kv)
kv1 := &types.KeyValue{}
kv1.Key = calcRoundKey4StatusAddrHeight(res.GetStatus(), res.GetAddr(), heightstr)
kv1.Value = nil
kvs = append(kvs, kv1)
return kvs
}
// GetBlackwhiteRoundInfo 获取当前游戏信息
func (c *Blackwhite) GetBlackwhiteRoundInfo(req *gt.ReqBlackwhiteRoundInfo) (types.Message, error) {
gameID := req.GameID
key := calcMavlRoundKey(gameID)
values, err := c.GetStateDB().Get(key)
if err != nil {
return nil, err
}
var round gt.BlackwhiteRound
err = types.Decode(values, &round)
if err != nil {
return nil, err
}
//密钥不显示
for _, addRes := range round.AddrResult {
addRes.ShowSecret = ""
}
roundRes := >.BlackwhiteRoundResult{
GameID: round.GameID,
Status: round.Status,
PlayAmount: round.PlayAmount,
PlayerCount: round.PlayerCount,
CurPlayerCount: round.CurPlayerCount,
Loop: round.Loop,
CurShowCount: round.CurShowCount,
CreateTime: round.CreateTime,
ShowTime: round.ShowTime,
Timeout: round.Timeout,
CreateAddr: round.CreateAddr,
GameName: round.GameName,
AddrResult: round.AddrResult,
Winner: round.Winner,
Index: round.Index,
}
var rep gt.ReplyBlackwhiteRoundInfo
rep.Round = roundRes
return &rep, nil
}
// GetBwRoundListInfo 根据要求获取游戏信息,包括游戏所处状态,或者参与者地址
func (c *Blackwhite) GetBwRoundListInfo(req *gt.ReqBlackwhiteRoundList) (types.Message, error) {
var key []byte
var values [][]byte
var err error
var prefix []byte
if 0 == req.Status {
prefix = calcRoundKey4AddrHeight(req.Address, "")
} else {
prefix = calcRoundKey4StatusAddrHeight(req.Status, req.Address, "")
}
localDb := c.GetLocalDB()
if req.GetIndex() == -1 {
values, err = localDb.List(prefix, nil, req.Count, req.GetDirection())
if err != nil {
return nil, err
}
if len(values) == 0 {
return nil, types.ErrNotFound
}
} else { //翻页查找指定的txhash列表
heightstr := genHeightIndexStr(req.GetIndex())
if 0 == req.Status {
key = calcRoundKey4AddrHeight(req.Address, heightstr)
} else {
key = calcRoundKey4StatusAddrHeight(req.Status, req.Address, heightstr)
}
values, err = localDb.List(prefix, key, req.Count, req.Direction)
if err != nil {
return nil, err
}
if len(values) == 0 {
return nil, types.ErrNotFound
}
}
if len(values) == 0 {
return nil, types.ErrNotFound
}
storeDb := c.GetStateDB()
var rep gt.ReplyBlackwhiteRoundList
for _, value := range values {
v, err := storeDb.Get(calcMavlRoundKey(string(value)))
if nil != err {
return nil, err
}
var round gt.BlackwhiteRound
err = types.Decode(v, &round)
if err != nil {
return nil, err
}
//密钥不显示
for _, addRes := range round.AddrResult {
addRes.ShowSecret = ""
}
roundRes := >.BlackwhiteRoundResult{
GameID: round.GameID,
Status: round.Status,
PlayAmount: round.PlayAmount,
PlayerCount: round.PlayerCount,
CurPlayerCount: round.CurPlayerCount,
Loop: round.Loop,
CurShowCount: round.CurShowCount,
CreateTime: round.CreateTime,
ShowTime: round.ShowTime,
Timeout: round.Timeout,
CreateAddr: round.CreateAddr,
GameName: round.GameName,
AddrResult: round.AddrResult,
Winner: round.Winner,
Index: round.Index,
}
rep.Round = append(rep.Round, roundRes)
}
return &rep, nil
}
// GetBwRoundLoopResult 获取游戏中每轮的胜负结果
func (c *Blackwhite) GetBwRoundLoopResult(req *gt.ReqLoopResult) (types.Message, error) {
localDb := c.GetLocalDB()
values, err := localDb.Get(calcRoundKey4LoopResult(req.GameID))
if err != nil {
return nil, err
}
if len(values) == 0 {
return nil, types.ErrNotFound
}
var result gt.ReplyLoopResults
err = types.Decode(values, &result)
if err != nil {
return nil, err
}
if req.LoopSeq > 0 { //取出具体一轮
if len(result.Results) < int(req.LoopSeq) {
return nil, gt.ErrNoLoopSeq
}
res := >.ReplyLoopResults{
GameID: result.GameID,
}
index := int(req.LoopSeq)
perRes := >.PerLoopResult{}
perRes.Winers = append(perRes.Winers, res.Results[index-1].Winers...)
perRes.Losers = append(perRes.Losers, res.Results[index-1].Losers...)
res.Results = append(res.Results, perRes)
return res, nil
}
return &result, nil //将所有轮数取出
}
func genHeightIndexStr(index int64) string {
return fmt.Sprintf("%018d", index)
}
func heightIndexToIndex(height int64, index int32) int64 {
return height*types.MaxTxsPerBlock + int64(index)
}
// GetPayloadValue 获取执行器action结构体
func (c *Blackwhite) GetPayloadValue() types.Message {
return >.BlackwhiteAction{}
}
// CheckReceiptExecOk return true to check if receipt ty is ok
func (c *Blackwhite) CheckReceiptExecOk() bool {
return true
}