-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path02_TowerDefenceStorage.cairo
executable file
·249 lines (208 loc) · 6.78 KB
/
02_TowerDefenceStorage.cairo
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
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.syscalls import get_caller_address
from contracts.desiege.utils.interfaces import IModuleController
// ############ TowerDefence Storage ################
// Contract maintains the storage variables
@storage_var
func controller_address() -> (address: felt) {
}
// The game index increases each game
// Widely used to scope other variables by game index
@storage_var
func latest_game_index() -> (game_idx: felt) {
}
// The marker to indicate when a game started
@storage_var
func game_start(game_idx: felt) -> (started_at: felt) {
}
// Stores the wall health for a given game index
// Dicreases when attacks get through the shield.
@storage_var
func main_health(game_idx: felt) -> (health: felt) {
}
// Stores the shield value for a given game index
// Increases through replenishment, dicreases through attacks.
@storage_var
func shield_value(game_idx: felt, token_id: felt) -> (value: felt) {
}
// Tracks total tokens accumulated for a game idx
// Increases everytime any user contributes elements
@storage_var
func token_reward_pool(game_idx: felt, token_id: felt) -> (value: felt) {
}
// Tracks total reward allocations
// Increases everytime any user contributes to a side (0:shielders,1:attackers)
@storage_var
func total_reward_alloc(game_idx: felt, side: felt) -> (value: felt) {
}
// Tracks user reward allocations
// Increases based on the elements contributed to a battle from a single user
@storage_var
func user_reward_alloc(game_idx: felt, user: felt, side: felt) -> (value: felt) {
}
// Attributes of a tower for a given tower index
// Multiple attributes are packed into a single felt
@storage_var
func tower_attributes(game_idx: felt, tower_idx: felt) -> (attrs: felt) {
}
// Stores the number of towers for a given game index
// You can loop recursively through all of the towers using the count
@storage_var
func tower_count(game_idx: felt) -> (count: felt) {
}
@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
address_of_controller: felt
) {
controller_address.write(address_of_controller);
return ();
}
@view
func get_latest_game_index{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
game_idx: felt
) {
let (game_idx) = latest_game_index.read();
return (game_idx,);
}
@external
func set_latest_game_index{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt
) {
only_approved();
latest_game_index.write(game_idx);
return ();
}
@view
func get_game_start{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt
) -> (started_at: felt) {
let (param) = game_start.read(game_idx);
return (param,);
}
@external
func set_game_start{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, started_at: felt
) {
only_approved();
game_start.write(game_idx, started_at);
return ();
}
@view
func get_main_health{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt
) -> (health: felt) {
let (health) = main_health.read(game_idx);
return (health,);
}
@external
func set_main_health{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, health: felt
) {
only_approved();
main_health.write(game_idx, health);
return ();
}
@view
func get_shield_value{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, token_id: felt
) -> (value: felt) {
let (value) = shield_value.read(game_idx, token_id);
return (value,);
}
@external
func set_shield_value{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, token_id: felt, value: felt
) {
only_approved();
shield_value.write(game_idx, token_id, value);
return ();
}
@view
func get_token_reward_pool{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, token_id: felt
) -> (value: felt) {
let (value) = token_reward_pool.read(game_idx, token_id);
return (value,);
}
@external
func set_token_reward_pool{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, token_id: felt, value: felt
) {
only_approved();
token_reward_pool.write(game_idx, token_id, value);
return ();
}
@view
func get_total_reward_alloc{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, side: felt
) -> (value: felt) {
let (value) = total_reward_alloc.read(game_idx, side);
return (value,);
}
@external
func set_total_reward_alloc{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, side: felt, value: felt
) {
only_approved();
total_reward_alloc.write(game_idx, side, value);
return ();
}
@view
func get_user_reward_alloc{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, user: felt, side: felt
) -> (value: felt) {
let (value) = user_reward_alloc.read(game_idx, user, side);
return (value,);
}
@external
func set_user_reward_alloc{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, user: felt, side: felt, value: felt
) {
only_approved();
user_reward_alloc.write(game_idx, user, side, value);
return ();
}
@view
func get_tower_attributes{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, tower_idx: felt
) -> (attrs_packed: felt) {
let (attrs_packed) = tower_attributes.read(game_idx, tower_idx);
return (attrs_packed,);
}
@external
func set_tower_attributes{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, tower_idx: felt, attrs_packed: felt
) {
only_approved();
tower_attributes.write(game_idx, tower_idx, attrs_packed);
return ();
}
@view
func get_tower_count{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt
) -> (count: felt) {
let (count) = tower_count.read(game_idx);
return (count,);
}
@external
func set_tower_count{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
game_idx: felt, count: felt
) {
only_approved();
tower_count.write(game_idx, count);
return ();
}
// Checks write-permission of the calling contract.
func only_approved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
// Get the address of the module trying to write to this contract.
let (caller) = get_caller_address();
let (controller) = controller_address.read();
// Pass this address on to the ModuleController.
// "Does this address have write-authority here?"
// Will revert the transaction if not.
IModuleController.has_write_access(
contract_address=controller, address_attempting_to_write=caller
);
return ();
}