-
Notifications
You must be signed in to change notification settings - Fork 27
/
EtheremonData.sol
475 lines (411 loc) · 17.8 KB
/
EtheremonData.sol
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
pragma solidity ^0.4.16;
// copyright [email protected]
// already on live and can not be changed
// address: https://etherscan.io/address/0xabc1c404424bdf24c19a5cc5ef8f47781d18eb3e
// owner will be removed to prevent overusef some powerful function
contract SafeMath {
/* function assert(bool assertion) internal { */
/* if (!assertion) { */
/* throw; */
/* } */
/* } // assert no longer needed once solidity is on 0.4.10 */
function safeAdd(uint256 x, uint256 y) pure internal returns(uint256) {
uint256 z = x + y;
assert((z >= x) && (z >= y));
return z;
}
function safeSubtract(uint256 x, uint256 y) pure internal returns(uint256) {
assert(x >= y);
uint256 z = x - y;
return z;
}
function safeMult(uint256 x, uint256 y) pure internal returns(uint256) {
uint256 z = x * y;
assert((x == 0)||(z/x == y));
return z;
}
}
contract BasicAccessControl {
address public owner;
address[] public moderators;
function BasicAccessControl() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
modifier onlyModerators() {
if (msg.sender != owner) {
bool found = false;
for (uint index = 0; index < moderators.length; index++) {
if (moderators[index] == msg.sender) {
found = true;
break;
}
}
require(found);
}
_;
}
function ChangeOwner(address _newOwner) onlyOwner public {
if (_newOwner != address(0)) {
owner = _newOwner;
}
}
function Kill() onlyOwner public {
selfdestruct(owner);
}
function AddModerator(address _newModerator) onlyOwner public {
if (_newModerator != address(0)) {
for (uint index = 0; index < moderators.length; index++) {
if (moderators[index] == _newModerator) {
return;
}
}
moderators.push(_newModerator);
}
}
function RemoveModerator(address _oldModerator) onlyOwner public {
uint foundIndex = 0;
for (; foundIndex < moderators.length; foundIndex++) {
if (moderators[foundIndex] == _oldModerator) {
break;
}
}
if (foundIndex < moderators.length) {
moderators[foundIndex] = moderators[moderators.length-1];
delete moderators[moderators.length-1];
moderators.length--;
}
}
}
contract EtheremonEnum {
enum ResultCode {
SUCCESS,
ERROR_CLASS_NOT_FOUND,
ERROR_LOW_BALANCE,
ERROR_SEND_FAIL,
ERROR_NOT_TRAINER,
ERROR_NOT_ENOUGH_MONEY,
ERROR_INVALID_AMOUNT
}
enum ArrayType {
CLASS_TYPE,
STAT_STEP,
STAT_START,
STAT_BASE,
OBJ_SKILL
}
}
contract EtheremonDataBase is EtheremonEnum, BasicAccessControl, SafeMath {
uint64 public totalMonster;
uint32 public totalClass;
// write
function addElementToArrayType(ArrayType _type, uint64 _id, uint8 _value) onlyModerators public returns(uint);
function updateIndexOfArrayType(ArrayType _type, uint64 _id, uint _index, uint8 _value) onlyModerators public returns(uint);
function setMonsterClass(uint32 _classId, uint256 _price, uint256 _returnPrice, bool _catchable) onlyModerators public returns(uint32);
function addMonsterObj(uint32 _classId, address _trainer, string _name) onlyModerators public returns(uint64);
function setMonsterObj(uint64 _objId, string _name, uint32 _exp, uint32 _createIndex, uint32 _lastClaimIndex) onlyModerators public;
function increaseMonsterExp(uint64 _objId, uint32 amount) onlyModerators public;
function decreaseMonsterExp(uint64 _objId, uint32 amount) onlyModerators public;
function removeMonsterIdMapping(address _trainer, uint64 _monsterId) onlyModerators public;
function addMonsterIdMapping(address _trainer, uint64 _monsterId) onlyModerators public;
function clearMonsterReturnBalance(uint64 _monsterId) onlyModerators public returns(uint256 amount);
function collectAllReturnBalance(address _trainer) onlyModerators public returns(uint256 amount);
function transferMonster(address _from, address _to, uint64 _monsterId) onlyModerators public returns(ResultCode);
function addExtraBalance(address _trainer, uint256 _amount) onlyModerators public returns(uint256);
function deductExtraBalance(address _trainer, uint256 _amount) onlyModerators public returns(uint256);
function setExtraBalance(address _trainer, uint256 _amount) onlyModerators public;
// read
function getSizeArrayType(ArrayType _type, uint64 _id) constant public returns(uint);
function getElementInArrayType(ArrayType _type, uint64 _id, uint _index) constant public returns(uint8);
function getMonsterClass(uint32 _classId) constant public returns(uint32 classId, uint256 price, uint256 returnPrice, uint32 total, bool catchable);
function getMonsterObj(uint64 _objId) constant public returns(uint64 objId, uint32 classId, address trainer, uint32 exp, uint32 createIndex, uint32 lastClaimIndex, uint createTime);
function getMonsterName(uint64 _objId) constant public returns(string name);
function getExtraBalance(address _trainer) constant public returns(uint256);
function getMonsterDexSize(address _trainer) constant public returns(uint);
function getMonsterObjId(address _trainer, uint index) constant public returns(uint64);
function getExpectedBalance(address _trainer) constant public returns(uint256);
function getMonsterReturn(uint64 _objId) constant public returns(uint256 current, uint256 total);
}
contract EtheremonData is EtheremonDataBase {
struct MonsterClass {
uint32 classId;
uint8[] types;
uint8[] statSteps;
uint8[] statStarts;
uint256 price;
uint256 returnPrice;
uint32 total;
bool catchable;
}
struct MonsterObj {
uint64 monsterId;
uint32 classId;
address trainer;
string name;
uint32 exp;
uint8[] statBases;
uint8[] skills;
uint32 createIndex;
uint32 lastClaimIndex;
uint createTime;
}
mapping(uint32 => MonsterClass) public monsterClass;
mapping(uint64 => MonsterObj) public monsterWorld;
mapping(address => uint64[]) public trainerDex;
mapping(address => uint256) public trainerExtraBalance;
// write access
function withdrawEther(address _sendTo, uint _amount) onlyOwner public returns(ResultCode) {
if (_amount > this.balance) {
return ResultCode.ERROR_INVALID_AMOUNT;
}
_sendTo.transfer(_amount);
return ResultCode.SUCCESS;
}
function addElementToArrayType(ArrayType _type, uint64 _id, uint8 _value) onlyModerators public returns(uint) {
uint8[] storage array = monsterWorld[_id].statBases;
if (_type == ArrayType.CLASS_TYPE) {
array = monsterClass[uint32(_id)].types;
} else if (_type == ArrayType.STAT_STEP) {
array = monsterClass[uint32(_id)].statSteps;
} else if (_type == ArrayType.STAT_START) {
array = monsterClass[uint32(_id)].statStarts;
} else if (_type == ArrayType.OBJ_SKILL) {
array = monsterWorld[_id].skills;
}
array.push(_value);
return array.length;
}
function updateIndexOfArrayType(ArrayType _type, uint64 _id, uint _index, uint8 _value) onlyModerators public returns(uint) {
uint8[] storage array = monsterWorld[_id].statBases;
if (_type == ArrayType.CLASS_TYPE) {
array = monsterClass[uint32(_id)].types;
} else if (_type == ArrayType.STAT_STEP) {
array = monsterClass[uint32(_id)].statSteps;
} else if (_type == ArrayType.STAT_START) {
array = monsterClass[uint32(_id)].statStarts;
} else if (_type == ArrayType.OBJ_SKILL) {
array = monsterWorld[_id].skills;
}
if (_index < array.length) {
if (_value == 255) {
// consider as delete
for(uint i = _index; i < array.length - 1; i++) {
array[i] = array[i+1];
}
delete array[array.length-1];
array.length--;
} else {
array[_index] = _value;
}
}
}
function setMonsterClass(uint32 _classId, uint256 _price, uint256 _returnPrice, bool _catchable) onlyModerators public returns(uint32) {
MonsterClass storage class = monsterClass[_classId];
if (class.classId == 0) {
totalClass += 1;
}
class.classId = _classId;
class.price = _price;
class.returnPrice = _returnPrice;
class.catchable = _catchable;
return totalClass;
}
function addMonsterObj(uint32 _classId, address _trainer, string _name) onlyModerators public returns(uint64) {
MonsterClass storage class = monsterClass[_classId];
if (class.classId == 0)
return 0;
// construct new monster
totalMonster += 1;
class.total += 1;
MonsterObj storage obj = monsterWorld[totalMonster];
obj.monsterId = totalMonster;
obj.classId = _classId;
obj.trainer = _trainer;
obj.name = _name;
obj.exp = 1;
obj.createIndex = class.total;
obj.lastClaimIndex = class.total;
obj.createTime = now;
// add to monsterdex
addMonsterIdMapping(_trainer, obj.monsterId);
return obj.monsterId;
}
function setMonsterObj(uint64 _objId, string _name, uint32 _exp, uint32 _createIndex, uint32 _lastClaimIndex) onlyModerators public {
MonsterObj storage obj = monsterWorld[_objId];
if (obj.monsterId == _objId) {
obj.name = _name;
obj.exp = _exp;
obj.createIndex = _createIndex;
obj.lastClaimIndex = _lastClaimIndex;
}
}
function increaseMonsterExp(uint64 _objId, uint32 amount) onlyModerators public {
MonsterObj storage obj = monsterWorld[_objId];
if (obj.monsterId == _objId) {
obj.exp = uint32(safeAdd(obj.exp, amount));
}
}
function decreaseMonsterExp(uint64 _objId, uint32 amount) onlyModerators public {
MonsterObj storage obj = monsterWorld[_objId];
if (obj.monsterId == _objId) {
obj.exp = uint32(safeSubtract(obj.exp, amount));
}
}
function removeMonsterIdMapping(address _trainer, uint64 _monsterId) onlyModerators public {
uint foundIndex = 0;
uint64[] storage objIdList = trainerDex[_trainer];
for (; foundIndex < objIdList.length; foundIndex++) {
if (objIdList[foundIndex] == _monsterId) {
break;
}
}
if (foundIndex < objIdList.length) {
objIdList[foundIndex] = objIdList[objIdList.length-1];
delete objIdList[objIdList.length-1];
objIdList.length--;
MonsterObj storage monster = monsterWorld[_monsterId];
monster.trainer = 0;
}
}
function addMonsterIdMapping(address _trainer, uint64 _monsterId) onlyModerators public {
if (_trainer != address(0) && _monsterId > 0) {
uint64[] storage objIdList = trainerDex[_trainer];
for (uint i = 0; i < objIdList.length; i++) {
if (objIdList[i] == _monsterId) {
return;
}
}
objIdList.push(_monsterId);
MonsterObj storage monster = monsterWorld[_monsterId];
monster.trainer = _trainer;
}
}
function clearMonsterReturnBalance(uint64 _monsterId) onlyModerators public returns(uint256) {
MonsterObj storage monster = monsterWorld[_monsterId];
MonsterClass storage class = monsterClass[monster.classId];
if (monster.monsterId == 0 || class.classId == 0)
return 0;
uint256 amount = 0;
uint32 gap = uint32(safeSubtract(class.total, monster.lastClaimIndex));
if (gap > 0) {
monster.lastClaimIndex = class.total;
amount = safeMult(gap, class.returnPrice);
trainerExtraBalance[monster.trainer] = safeAdd(trainerExtraBalance[monster.trainer], amount);
}
return amount;
}
function collectAllReturnBalance(address _trainer) onlyModerators public returns(uint256 amount) {
uint64[] storage objIdList = trainerDex[_trainer];
for (uint i = 0; i < objIdList.length; i++) {
clearMonsterReturnBalance(objIdList[i]);
}
return trainerExtraBalance[_trainer];
}
function transferMonster(address _from, address _to, uint64 _monsterId) onlyModerators public returns(ResultCode) {
MonsterObj storage monster = monsterWorld[_monsterId];
if (monster.trainer != _from) {
return ResultCode.ERROR_NOT_TRAINER;
}
clearMonsterReturnBalance(_monsterId);
removeMonsterIdMapping(_from, _monsterId);
addMonsterIdMapping(_to, _monsterId);
return ResultCode.SUCCESS;
}
function addExtraBalance(address _trainer, uint256 _amount) onlyModerators public returns(uint256) {
trainerExtraBalance[_trainer] = safeAdd(trainerExtraBalance[_trainer], _amount);
return trainerExtraBalance[_trainer];
}
function deductExtraBalance(address _trainer, uint256 _amount) onlyModerators public returns(uint256) {
trainerExtraBalance[_trainer] = safeSubtract(trainerExtraBalance[_trainer], _amount);
return trainerExtraBalance[_trainer];
}
function setExtraBalance(address _trainer, uint256 _amount) onlyModerators public {
trainerExtraBalance[_trainer] = _amount;
}
// public
function () payable public {
addExtraBalance(msg.sender, msg.value);
}
// read access
function getSizeArrayType(ArrayType _type, uint64 _id) constant public returns(uint) {
uint8[] storage array = monsterWorld[_id].statBases;
if (_type == ArrayType.CLASS_TYPE) {
array = monsterClass[uint32(_id)].types;
} else if (_type == ArrayType.STAT_STEP) {
array = monsterClass[uint32(_id)].statSteps;
} else if (_type == ArrayType.STAT_START) {
array = monsterClass[uint32(_id)].statStarts;
} else if (_type == ArrayType.OBJ_SKILL) {
array = monsterWorld[_id].skills;
}
return array.length;
}
function getElementInArrayType(ArrayType _type, uint64 _id, uint _index) constant public returns(uint8) {
uint8[] storage array = monsterWorld[_id].statBases;
if (_type == ArrayType.CLASS_TYPE) {
array = monsterClass[uint32(_id)].types;
} else if (_type == ArrayType.STAT_STEP) {
array = monsterClass[uint32(_id)].statSteps;
} else if (_type == ArrayType.STAT_START) {
array = monsterClass[uint32(_id)].statStarts;
} else if (_type == ArrayType.OBJ_SKILL) {
array = monsterWorld[_id].skills;
}
if (_index >= array.length)
return 0;
return array[_index];
}
function getMonsterClass(uint32 _classId) constant public returns(uint32 classId, uint256 price, uint256 returnPrice, uint32 total, bool catchable) {
MonsterClass storage class = monsterClass[_classId];
classId = class.classId;
price = class.price;
returnPrice = class.returnPrice;
total = class.total;
catchable = class.catchable;
}
function getMonsterObj(uint64 _objId) constant public returns(uint64 objId, uint32 classId, address trainer, uint32 exp, uint32 createIndex, uint32 lastClaimIndex, uint createTime) {
MonsterObj storage monster = monsterWorld[_objId];
objId = monster.monsterId;
classId = monster.classId;
trainer = monster.trainer;
exp = monster.exp;
createIndex = monster.createIndex;
lastClaimIndex = monster.lastClaimIndex;
createTime = monster.createTime;
}
function getMonsterName(uint64 _objId) constant public returns(string name) {
return monsterWorld[_objId].name;
}
function getExtraBalance(address _trainer) constant public returns(uint256) {
return trainerExtraBalance[_trainer];
}
function getMonsterDexSize(address _trainer) constant public returns(uint) {
return trainerDex[_trainer].length;
}
function getMonsterObjId(address _trainer, uint index) constant public returns(uint64) {
if (index >= trainerDex[_trainer].length)
return 0;
return trainerDex[_trainer][index];
}
function getExpectedBalance(address _trainer) constant public returns(uint256) {
uint64[] storage objIdList = trainerDex[_trainer];
uint256 monsterBalance = 0;
for (uint i = 0; i < objIdList.length; i++) {
MonsterObj memory monster = monsterWorld[objIdList[i]];
MonsterClass storage class = monsterClass[monster.classId];
uint32 gap = uint32(safeSubtract(class.total, monster.lastClaimIndex));
monsterBalance += safeMult(gap, class.returnPrice);
}
return monsterBalance;
}
function getMonsterReturn(uint64 _objId) constant public returns(uint256 current, uint256 total) {
MonsterObj memory monster = monsterWorld[_objId];
MonsterClass storage class = monsterClass[monster.classId];
uint32 totalGap = uint32(safeSubtract(class.total, monster.createIndex));
uint32 currentGap = uint32(safeSubtract(class.total, monster.lastClaimIndex));
return (safeMult(currentGap, class.returnPrice), safeMult(totalGap, class.returnPrice));
}
}