Smart Contract
GomokuResult
A.a10dde51240c1ec7.GomokuResult
1import GomokuType from 0xa10dde51240c1ec7
2
3pub contract GomokuResult {
4
5 // Paths
6 pub let CollectionStoragePath: StoragePath
7 pub let CollectionPublicPath: PublicPath
8
9 // Events
10 pub event TokenCreated(winner: Address?, losser: Address?, gain: Fix64)
11 pub event CollectionCreated()
12 pub event Withdraw(id: UInt32, from: Address?)
13 pub event Deposit(id: UInt32, to: Address?)
14
15 init() {
16 self.CollectionStoragePath = /storage/gomokuResultCollection
17 self.CollectionPublicPath = /public/gomokuResultCollection
18 }
19
20 pub resource ResultToken {
21 pub let id: UInt32
22 pub let winner: Address?
23 pub let losser: Address?
24 pub let isDraw: Bool
25 pub let roundWinners: [GomokuType.Result]
26 pub let gain: Fix64
27 access(account) let steps: [[AnyStruct{GomokuType.StoneDataing}]]
28
29 priv var destroyable: Bool
30
31 init(
32 id: UInt32,
33 winner: Address?,
34 losser: Address?,
35 gain: Fix64,
36 roundWinners: [GomokuType.Result],
37 steps: [[AnyStruct{GomokuType.StoneDataing}]]
38 ) {
39 self.id = id
40 self.winner = winner
41 self.losser = losser
42 if winner == nil && losser == nil {
43 self.isDraw = true
44 } else {
45 self.isDraw = false
46 }
47 self.gain = gain
48 self.roundWinners = roundWinners
49 self.destroyable = false
50 self.steps = steps
51 emit TokenCreated(
52 winner: winner,
53 losser: losser,
54 gain: gain
55 )
56 }
57
58 access(account) fun setDestroyable(_ value: Bool) {
59 self.destroyable = value
60 }
61
62 pub fun getSteps(round: UInt32): [AnyStruct{GomokuType.StoneDataing}] {
63 pre {
64 round < UInt32(self.steps.length): "Invalid round index."
65 }
66
67 return self.steps[round]
68 }
69
70 destroy() {
71 if self.destroyable == false {
72 panic("You can't destroy this token before setting destroyable to true.")
73 }
74 }
75
76 }
77
78 access(account) fun createResult(
79 id: UInt32,
80 winner: Address?,
81 losser: Address?,
82 gain: Fix64,
83 roundWinners: [GomokuType.Result],
84 steps: [[AnyStruct{GomokuType.StoneDataing}]]
85 ): @GomokuResult.ResultToken {
86 return <- create ResultToken(
87 id: id,
88 winner: winner,
89 losser: losser,
90 gain: gain,
91 roundWinners: roundWinners,
92 steps: steps
93 )
94 }
95
96 pub resource ResultCollection {
97
98 pub let StoragePath: StoragePath
99 pub let PublicPath: PublicPath
100
101 priv var ownedResultTokenMap: @{UInt32: GomokuResult.ResultToken}
102 priv var destroyable: Bool
103
104 init () {
105 self.ownedResultTokenMap <- {}
106 self.destroyable = false
107 self.StoragePath = /storage/gomokuResultCollection
108 self.PublicPath = /public/gomokuResultCollection
109 }
110
111 access(account) fun withdraw(by id: UInt32): @GomokuResult.ResultToken? {
112 if let token <- self.ownedResultTokenMap.remove(key: id) {
113 emit Withdraw(id: token.id, from: self.owner?.address)
114 if self.ownedResultTokenMap.keys.length == 0 {
115 self.destroyable = true
116 }
117 return <- token
118 } else {
119 return nil
120 }
121 }
122
123 access(account) fun deposit(token: @GomokuResult.ResultToken) {
124 let token <- token
125 let id: UInt32 = token.id
126 let oldToken <- self.ownedResultTokenMap[id] <- token
127 emit Deposit(id: id, to: self.owner?.address)
128 self.destroyable = false
129 destroy oldToken
130 }
131
132 pub fun getIds(): [UInt32] {
133 return self.ownedResultTokenMap.keys
134 }
135
136 pub fun borrow(id: UInt32): &GomokuResult.ResultToken? {
137 return &self.ownedResultTokenMap[id] as &GomokuResult.ResultToken?
138 }
139
140 pub fun getBalance(): Int {
141 return self.ownedResultTokenMap.keys.length
142 }
143
144 destroy() {
145 destroy self.ownedResultTokenMap
146 if self.destroyable == false {
147 panic("Ha Ha! Got you! You can't destory this collection if there are Gomoku ResultToken!")
148 }
149 }
150 }
151
152 pub fun createEmptyVault(): @GomokuResult.ResultCollection {
153 emit CollectionCreated()
154 return <- create ResultCollection()
155 }
156
157}
158