-
Notifications
You must be signed in to change notification settings - Fork 27
/
EtheremonTransform.sol
728 lines (616 loc) · 28 KB
/
EtheremonTransform.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
pragma solidity ^0.4.16;
// copyright [email protected]
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;
uint16 public totalModerators = 0;
mapping (address => bool) public moderators;
bool public isMaintaining = true;
function BasicAccessControl() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
modifier onlyModerators() {
require(msg.sender == owner || moderators[msg.sender] == true);
_;
}
modifier isActive {
require(!isMaintaining);
_;
}
function ChangeOwner(address _newOwner) onlyOwner public {
if (_newOwner != address(0)) {
owner = _newOwner;
}
}
function AddModerator(address _newModerator) onlyOwner public {
if (moderators[_newModerator] == false) {
moderators[_newModerator] = true;
totalModerators += 1;
}
}
function RemoveModerator(address _oldModerator) onlyOwner public {
if (moderators[_oldModerator] == true) {
moderators[_oldModerator] = false;
totalModerators -= 1;
}
}
function UpdateMaintaining(bool _isMaintaining) onlyOwner public {
isMaintaining = _isMaintaining;
}
}
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,
ERROR_OBJ_NOT_FOUND,
ERROR_OBJ_INVALID_OWNERSHIP
}
enum ArrayType {
CLASS_TYPE,
STAT_STEP,
STAT_START,
STAT_BASE,
OBJ_SKILL
}
enum PropertyType {
ANCESTOR,
XFACTOR
}
}
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 removeElementOfArrayType(ArrayType _type, uint64 _id, 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 EtheremonTransformData {
uint64 public totalEgg = 0;
function getHatchingEggId(address _trainer) constant external returns(uint64);
function getHatchingEggData(address _trainer) constant external returns(uint64, uint64, uint32, address, uint, uint64);
function getTranformedId(uint64 _objId) constant external returns(uint64);
function countEgg(uint64 _objId) constant external returns(uint);
function setHatchTime(uint64 _eggId, uint _hatchTime) external;
function setHatchedEgg(uint64 _eggId, uint64 _newObjId) external;
function addEgg(uint64 _objId, uint32 _classId, address _trainer, uint _hatchTime) external returns(uint64);
function setTranformed(uint64 _objId, uint64 _newObjId) external;
}
contract EtheremonWorld is EtheremonEnum {
function getGen0COnfig(uint32 _classId) constant public returns(uint32, uint256, uint32);
function getTrainerEarn(address _trainer) constant public returns(uint256);
function getReturnFromMonster(uint64 _objId) constant public returns(uint256 current, uint256 total);
function getClassPropertyValue(uint32 _classId, PropertyType _type, uint index) constant external returns(uint32);
function getClassPropertySize(uint32 _classId, PropertyType _type) constant external returns(uint);
}
interface EtheremonBattle {
function isOnBattle(uint64 _objId) constant external returns(bool);
function getMonsterLevel(uint64 _objId) constant public returns(uint8);
}
interface EtheremonTradeInterface {
function isOnTrading(uint64 _objId) constant external returns(bool);
}
contract EtheremonTransform is EtheremonEnum, BasicAccessControl, SafeMath {
uint8 constant public STAT_COUNT = 6;
uint8 constant public STAT_MAX = 32;
uint8 constant public GEN0_NO = 24;
struct MonsterClassAcc {
uint32 classId;
uint256 price;
uint256 returnPrice;
uint32 total;
bool catchable;
}
struct MonsterObjAcc {
uint64 monsterId;
uint32 classId;
address trainer;
string name;
uint32 exp;
uint32 createIndex;
uint32 lastClaimIndex;
uint createTime;
}
struct MonsterEgg {
uint64 eggId;
uint64 objId;
uint32 classId;
address trainer;
uint hatchTime;
uint64 newObjId;
}
struct BasicObjInfo {
uint32 classId;
address owner;
uint8 level;
}
// Gen0 has return price & no longer can be caught when this contract is deployed
struct Gen0Config {
uint32 classId;
uint256 originalPrice;
uint256 returnPrice;
uint32 total; // total caught (not count those from eggs)
}
// hatching range
uint16 public hatchStartTime = 2; // hour
uint16 public hatchMaxTime = 46; // hour
uint public removeHatchingTimeFee = 0.05 ether; // ETH
uint public buyEggFee = 0.06 ether; // ETH
uint32[] public randomClassIds;
mapping(uint32 => uint8) public layingEggLevels;
mapping(uint32 => uint8) public layingEggDeductions;
mapping(uint32 => uint8) public transformLevels;
mapping(uint32 => uint32) public transformClasses;
mapping(uint8 => uint32) public levelExps;
address private lastHatchingAddress;
mapping(uint32 => Gen0Config) public gen0Config;
// linked smart contract
address public dataContract;
address public worldContract;
address public transformDataContract;
address public battleContract;
address public tradeContract;
// events
event EventLayEgg(address indexed trainer, uint64 objId, uint64 eggId);
event EventHatchEgg(address indexed trainer, uint64 eggId, uint64 objId);
event EventTransform(address indexed trainer, uint64 oldObjId, uint64 newObjId);
event EventRelease(address indexed trainer, uint64 objId);
// modifier
modifier requireDataContract {
require(dataContract != address(0));
_;
}
modifier requireTransformDataContract {
require(transformDataContract != address(0));
_;
}
modifier requireBattleContract {
require(battleContract != address(0));
_;
}
modifier requireTradeContract {
require(tradeContract != address(0));
_;
}
// constructor
function EtheremonTransform(address _dataContract, address _worldContract, address _transformDataContract, address _battleContract, address _tradeContract) public {
dataContract = _dataContract;
worldContract = _worldContract;
transformDataContract = _transformDataContract;
battleContract = _battleContract;
tradeContract = _tradeContract;
}
// helper
function getRandom(uint16 maxRan, uint8 index, address priAddress) constant public returns(uint8) {
uint256 genNum = uint256(block.blockhash(block.number-1)) + uint256(priAddress);
for (uint8 i = 0; i < index && i < 6; i ++) {
genNum /= 256;
}
return uint8(genNum % maxRan);
}
function addNewObj(address _trainer, uint32 _classId) private returns(uint64) {
EtheremonDataBase data = EtheremonDataBase(dataContract);
uint64 objId = data.addMonsterObj(_classId, _trainer, "..name me...");
for (uint i=0; i < STAT_COUNT; i+= 1) {
uint8 value = getRandom(STAT_MAX, uint8(i), lastHatchingAddress) + data.getElementInArrayType(ArrayType.STAT_START, uint64(_classId), i);
data.addElementToArrayType(ArrayType.STAT_BASE, objId, value);
}
return objId;
}
// admin & moderators
function setContract(address _dataContract, address _worldContract, address _transformDataContract, address _battleContract, address _tradeContract) onlyModerators external {
dataContract = _dataContract;
worldContract = _worldContract;
transformDataContract = _transformDataContract;
battleContract = _battleContract;
tradeContract = _tradeContract;
}
function setOriginalPriceGen0() onlyModerators external {
gen0Config[1] = Gen0Config(1, 0.3 ether, 0.003 ether, 374);
gen0Config[2] = Gen0Config(2, 0.3 ether, 0.003 ether, 408);
gen0Config[3] = Gen0Config(3, 0.3 ether, 0.003 ether, 373);
gen0Config[4] = Gen0Config(4, 0.2 ether, 0.002 ether, 437);
gen0Config[5] = Gen0Config(5, 0.1 ether, 0.001 ether, 497);
gen0Config[6] = Gen0Config(6, 0.3 ether, 0.003 ether, 380);
gen0Config[7] = Gen0Config(7, 0.2 ether, 0.002 ether, 345);
gen0Config[8] = Gen0Config(8, 0.1 ether, 0.001 ether, 518);
gen0Config[9] = Gen0Config(9, 0.1 ether, 0.001 ether, 447);
gen0Config[10] = Gen0Config(10, 0.2 ether, 0.002 ether, 380);
gen0Config[11] = Gen0Config(11, 0.2 ether, 0.002 ether, 354);
gen0Config[12] = Gen0Config(12, 0.2 ether, 0.002 ether, 346);
gen0Config[13] = Gen0Config(13, 0.2 ether, 0.002 ether, 351);
gen0Config[14] = Gen0Config(14, 0.2 ether, 0.002 ether, 338);
gen0Config[15] = Gen0Config(15, 0.2 ether, 0.002 ether, 341);
gen0Config[16] = Gen0Config(16, 0.35 ether, 0.0035 ether, 384);
gen0Config[17] = Gen0Config(17, 1 ether, 0.01 ether, 305);
gen0Config[18] = Gen0Config(18, 0.1 ether, 0.001 ether, 427);
gen0Config[19] = Gen0Config(19, 1 ether, 0.01 ether, 304);
gen0Config[20] = Gen0Config(20, 0.4 ether, 0.05 ether, 82);
gen0Config[21] = Gen0Config(21, 1, 1, 123);
gen0Config[22] = Gen0Config(22, 0.2 ether, 0.001 ether, 468);
gen0Config[23] = Gen0Config(23, 0.5 ether, 0.0025 ether, 302);
gen0Config[24] = Gen0Config(24, 1 ether, 0.005 ether, 195);
}
function updateHatchingRange(uint16 _start, uint16 _max) onlyModerators external {
hatchStartTime = _start;
hatchMaxTime = _max;
}
function withdrawEther(address _sendTo, uint _amount) onlyModerators external {
// no user money is kept in this contract, only trasaction fee
if (_amount > this.balance) {
revert();
}
_sendTo.transfer(_amount);
}
function setConfigClass(uint32 _classId, uint8 _layingLevel, uint8 _layingCost, uint8 _transformLevel, uint32 _tranformClass) onlyModerators external {
layingEggLevels[_classId] = _layingLevel;
layingEggDeductions[_classId] = _layingCost;
transformLevels[_classId] = _transformLevel;
transformClasses[_classId] = _tranformClass;
}
function setConfig(uint _removeHatchingTimeFee, uint _buyEggFee) onlyModerators external {
removeHatchingTimeFee = _removeHatchingTimeFee;
buyEggFee = _buyEggFee;
}
function genLevelExp() onlyModerators external {
uint8 level = 1;
uint32 requirement = 100;
uint32 sum = requirement;
while(level <= 100) {
levelExps[level] = sum;
level += 1;
requirement = (requirement * 11) / 10 + 5;
sum += requirement;
}
}
function addRandomClass(uint32 _newClassId) onlyModerators public {
if (_newClassId > 0) {
for (uint index = 0; index < randomClassIds.length; index++) {
if (randomClassIds[index] == _newClassId) {
return;
}
}
randomClassIds.push(_newClassId);
}
}
function removeRandomClass(uint32 _oldClassId) onlyModerators public {
uint foundIndex = 0;
for (; foundIndex < randomClassIds.length; foundIndex++) {
if (randomClassIds[foundIndex] == _oldClassId) {
break;
}
}
if (foundIndex < randomClassIds.length) {
randomClassIds[foundIndex] = randomClassIds[randomClassIds.length-1];
delete randomClassIds[randomClassIds.length-1];
randomClassIds.length--;
}
}
function removeHatchingTimeWithToken(address _trainer) isActive onlyModerators requireDataContract requireTransformDataContract external {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
MonsterEgg memory egg;
(egg.eggId, egg.objId, egg.classId, egg.trainer, egg.hatchTime, egg.newObjId) = transformData.getHatchingEggData(_trainer);
// not hatching any egg
if (egg.eggId == 0 || egg.trainer != _trainer || egg.newObjId > 0)
revert();
transformData.setHatchTime(egg.eggId, 0);
}
function buyEggWithToken(address _trainer) isActive onlyModerators requireDataContract requireTransformDataContract external {
if (randomClassIds.length == 0) {
revert();
}
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
// make sure no hatching egg at the same time
if (transformData.getHatchingEggId(_trainer) > 0) {
revert();
}
// add random egg
uint8 classIndex = getRandom(uint16(randomClassIds.length), 1, lastHatchingAddress);
uint64 eggId = transformData.addEgg(0, randomClassIds[classIndex], _trainer, block.timestamp + (hatchStartTime + getRandom(hatchMaxTime, 0, lastHatchingAddress)) * 3600);
// deduct exp
EventLayEgg(msg.sender, 0, eggId);
}
// public
function ceil(uint a, uint m) pure public returns (uint) {
return ((a + m - 1) / m) * m;
}
function getLevel(uint32 exp) view public returns (uint8) {
uint8 minIndex = 1;
uint8 maxIndex = 100;
uint8 currentIndex;
while (minIndex < maxIndex) {
currentIndex = (minIndex + maxIndex) / 2;
if (exp < levelExps[currentIndex])
maxIndex = currentIndex;
else
minIndex = currentIndex + 1;
}
return minIndex;
}
function getGen0ObjInfo(uint64 _objId) constant public returns(uint32, uint32, uint256) {
EtheremonDataBase data = EtheremonDataBase(dataContract);
MonsterObjAcc memory obj;
(obj.monsterId, obj.classId, obj.trainer, obj.exp, obj.createIndex, obj.lastClaimIndex, obj.createTime) = data.getMonsterObj(_objId);
Gen0Config memory gen0 = gen0Config[obj.classId];
if (gen0.classId != obj.classId) {
return (gen0.classId, obj.createIndex, 0);
}
uint32 totalGap = 0;
if (obj.createIndex < gen0.total)
totalGap = gen0.total - obj.createIndex;
return (obj.classId, obj.createIndex, safeMult(totalGap, gen0.returnPrice));
}
function getObjClassId(uint64 _objId) requireDataContract constant public returns(uint32, address, uint8) {
EtheremonDataBase data = EtheremonDataBase(dataContract);
MonsterObjAcc memory obj;
uint32 _ = 0;
(obj.monsterId, obj.classId, obj.trainer, obj.exp, _, _, obj.createTime) = data.getMonsterObj(_objId);
return (obj.classId, obj.trainer, getLevel(obj.exp));
}
function getClassCheckOwner(uint64 _objId, address _trainer) requireDataContract constant public returns(uint32) {
EtheremonDataBase data = EtheremonDataBase(dataContract);
MonsterObjAcc memory obj;
uint32 _ = 0;
(obj.monsterId, obj.classId, obj.trainer, obj.exp, _, _, obj.createTime) = data.getMonsterObj(_objId);
if (_trainer != obj.trainer)
return 0;
return obj.classId;
}
function calculateMaxEggG0(uint64 _objId) constant public returns(uint) {
uint32 classId;
uint32 createIndex;
uint256 totalEarn;
(classId, createIndex, totalEarn) = getGen0ObjInfo(_objId);
if (classId > GEN0_NO || classId == 20 || classId == 21)
return 0;
Gen0Config memory config = gen0Config[classId];
// the one from egg can not lay
if (createIndex > config.total)
return 0;
// calculate agv price
uint256 avgPrice = config.originalPrice;
uint rate = config.originalPrice/config.returnPrice;
if (config.total > rate) {
uint k = config.total - rate;
avgPrice = (config.total * config.originalPrice + config.returnPrice * k * (k+1) / 2) / config.total;
}
uint256 catchPrice = config.originalPrice;
if (createIndex > rate) {
catchPrice += config.returnPrice * safeSubtract(createIndex, rate);
}
if (totalEarn >= catchPrice) {
return 0;
}
return ceil((catchPrice - totalEarn)*15*1000/avgPrice, 10000)/10000;
}
function canLayEgg(uint64 _objId, uint32 _classId, uint32 _level) constant public returns(bool) {
if (_classId <= GEN0_NO) {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
// legends
if (transformData.countEgg(_objId) >= calculateMaxEggG0(_objId))
return false;
return true;
} else {
if (layingEggLevels[_classId] == 0 || _level < layingEggLevels[_classId])
return false;
return true;
}
}
function layEgg(uint64 _objId) isActive requireDataContract requireTransformDataContract external {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
// make sure no hatching egg at the same time
if (transformData.getHatchingEggId(msg.sender) > 0) {
revert();
}
// can not lay egg when trading
EtheremonTradeInterface trade = EtheremonTradeInterface(tradeContract);
if (trade.isOnTrading(_objId))
revert();
// check obj
EtheremonDataBase data = EtheremonDataBase(dataContract);
MonsterObjAcc memory obj;
uint32 _ = 0;
(obj.monsterId, obj.classId, obj.trainer, obj.exp, _, _, obj.createTime) = data.getMonsterObj(_objId);
if (obj.monsterId != _objId || obj.trainer != msg.sender) {
revert();
}
// check lay egg condition
uint8 currentLevel = getLevel(obj.exp);
uint8 afterLevel = 0;
if (!canLayEgg(_objId, obj.classId, currentLevel))
revert();
if (layingEggDeductions[obj.classId] >= currentLevel)
revert();
afterLevel = currentLevel - layingEggDeductions[obj.classId];
// add egg
uint64 eggId = transformData.addEgg(obj.monsterId, obj.classId, msg.sender, block.timestamp + (hatchStartTime + getRandom(hatchMaxTime, 0, lastHatchingAddress)) * 3600);
// deduct exp
if (afterLevel < currentLevel)
data.decreaseMonsterExp(_objId, obj.exp - levelExps[afterLevel-1]);
EventLayEgg(msg.sender, _objId, eggId);
}
function hatchEgg() isActive requireDataContract requireTransformDataContract external {
// use as a seed for random
lastHatchingAddress = msg.sender;
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
MonsterEgg memory egg;
(egg.eggId, egg.objId, egg.classId, egg.trainer, egg.hatchTime, egg.newObjId) = transformData.getHatchingEggData(msg.sender);
// not hatching any egg
if (egg.eggId == 0 || egg.trainer != msg.sender)
revert();
// need more time
if (egg.newObjId > 0 || egg.hatchTime > block.timestamp) {
revert();
}
uint64 objId = addNewObj(msg.sender, egg.classId);
transformData.setHatchedEgg(egg.eggId, objId);
EventHatchEgg(msg.sender, egg.eggId, objId);
}
function removeHatchingTime() isActive requireDataContract requireTransformDataContract external payable {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
MonsterEgg memory egg;
(egg.eggId, egg.objId, egg.classId, egg.trainer, egg.hatchTime, egg.newObjId) = transformData.getHatchingEggData(msg.sender);
// not hatching any egg
if (egg.eggId == 0 || egg.trainer != msg.sender || egg.newObjId > 0)
revert();
if (msg.value != removeHatchingTimeFee) {
revert();
}
transformData.setHatchTime(egg.eggId, 0);
}
function checkAncestors(uint32 _classId, address _trainer, uint64 _a1, uint64 _a2, uint64 _a3) constant public returns(bool) {
EtheremonWorld world = EtheremonWorld(worldContract);
uint index = 0;
uint32 temp = 0;
// check ancestor
uint32[3] memory ancestors;
uint32[3] memory requestAncestors;
index = world.getClassPropertySize(_classId, PropertyType.ANCESTOR);
while (index > 0) {
index -= 1;
ancestors[index] = world.getClassPropertyValue(_classId, PropertyType.ANCESTOR, index);
}
if (_a1 > 0) {
temp = getClassCheckOwner(_a1, _trainer);
if (temp == 0)
return false;
requestAncestors[0] = temp;
}
if (_a2 > 0) {
temp = getClassCheckOwner(_a2, _trainer);
if (temp == 0)
return false;
requestAncestors[1] = temp;
}
if (_a3 > 0) {
temp = getClassCheckOwner(_a3, _trainer);
if (temp == 0)
return false;
requestAncestors[2] = temp;
}
if (requestAncestors[0] > 0 && (requestAncestors[0] == requestAncestors[1] || requestAncestors[0] == requestAncestors[2]))
return false;
if (requestAncestors[1] > 0 && (requestAncestors[1] == requestAncestors[2]))
return false;
for (index = 0; index < ancestors.length; index++) {
temp = ancestors[index];
if (temp > 0 && temp != requestAncestors[0] && temp != requestAncestors[1] && temp != requestAncestors[2])
return false;
}
return true;
}
function transform(uint64 _objId, uint64 _a1, uint64 _a2, uint64 _a3) isActive requireDataContract requireTransformDataContract external payable {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
if (transformData.getTranformedId(_objId) > 0)
revert();
EtheremonBattle battle = EtheremonBattle(battleContract);
EtheremonTradeInterface trade = EtheremonTradeInterface(tradeContract);
if (battle.isOnBattle(_objId) || trade.isOnTrading(_objId))
revert();
EtheremonDataBase data = EtheremonDataBase(dataContract);
BasicObjInfo memory objInfo;
(objInfo.classId, objInfo.owner, objInfo.level) = getObjClassId(_objId);
uint32 transformClass = transformClasses[objInfo.classId];
if (objInfo.classId == 0 || objInfo.owner != msg.sender)
revert();
if (transformLevels[objInfo.classId] == 0 || objInfo.level < transformLevels[objInfo.classId])
revert();
if (transformClass == 0)
revert();
// gen0 - can not transform if it has bonus egg
if (objInfo.classId <= GEN0_NO) {
// legends
if (getBonusEgg(_objId) > 0)
revert();
} else {
if (!checkAncestors(objInfo.classId, msg.sender, _a1, _a2, _a3))
revert();
}
uint64 newObjId = addNewObj(msg.sender, transformClass);
// remove old one
data.removeMonsterIdMapping(msg.sender, _objId);
transformData.setTranformed(_objId, newObjId);
EventTransform(msg.sender, _objId, newObjId);
}
function buyEgg() isActive requireDataContract requireTransformDataContract external payable {
if (msg.value != buyEggFee) {
revert();
}
if (randomClassIds.length == 0) {
revert();
}
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
// make sure no hatching egg at the same time
if (transformData.getHatchingEggId(msg.sender) > 0) {
revert();
}
// add random egg
uint8 classIndex = getRandom(uint16(randomClassIds.length), 1, lastHatchingAddress);
uint64 eggId = transformData.addEgg(0, randomClassIds[classIndex], msg.sender, block.timestamp + (hatchStartTime + getRandom(hatchMaxTime, 0, lastHatchingAddress)) * 3600);
// deduct exp
EventLayEgg(msg.sender, 0, eggId);
}
// read
function getBonusEgg(uint64 _objId) constant public returns(uint) {
EtheremonTransformData transformData = EtheremonTransformData(transformDataContract);
uint totalBonusEgg = calculateMaxEggG0(_objId);
if (totalBonusEgg > 0) {
return (totalBonusEgg - transformData.countEgg(_objId));
}
return 0;
}
}