-
Notifications
You must be signed in to change notification settings - Fork 1
/
protectTheCastle.sol
148 lines (127 loc) · 5.17 KB
/
protectTheCastle.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
contract ProtectTheCastle {
// King's Jester
address public jester;
// Record the last Reparation time
uint public lastReparation;
// Piggy Bank Amount
uint public piggyBank;
// Collected Fee Amount
uint public collectedFee;
// Track the citizens who helped to repair the castle
address[] public citizensAddresses;
uint[] public citizensAmounts;
uint32 public totalCitizens;
uint32 public lastCitizenPaid;
// Brided Citizen who made the system works
address public bribedCitizen;
// Record how many times the castle had fell
uint32 public round;
// Amount already paid back in this round
uint public amountAlreadyPaidBack;
// Amount invested in this round
uint public amountInvested;
uint constant SIX_HOURS = 60 * 60 * 6;
function ProtectTheCastle() {
// Define the first castle
bribedCitizen = msg.sender;
jester = msg.sender;
lastReparation = block.timestamp;
amountAlreadyPaidBack = 0;
amountInvested = 0;
totalCitizens = 0;
}
function repairTheCastle() returns(bool) {
uint amount = msg.value;
// Check if the minimum amount if reached
if (amount < 10 finney) {
msg.sender.send(msg.value);
return false;
}
// If the amount received is more than 100 ETH return the difference
if (amount > 100 ether) {
msg.sender.send(msg.value - 100 ether);
amount = 100 ether;
}
// Check if the Castle has fell
if (lastReparation + SIX_HOURS < block.timestamp) {
// Send the Piggy Bank to the last 3 citizens
// If there is no one who contributed this last 6 hours, no action needed
if (totalCitizens == 1) {
// If there is only one Citizen who contributed, he gets the full Pigg Bank
citizensAddresses[citizensAddresses.length - 1].send(piggyBank);
} else if (totalCitizens == 2) {
// If only 2 citizens contributed
citizensAddresses[citizensAddresses.length - 1].send(piggyBank * 65 / 100);
citizensAddresses[citizensAddresses.length - 2].send(piggyBank * 35 / 100);
} else if (totalCitizens >= 3) {
// If there is 3 or more citizens who contributed
citizensAddresses[citizensAddresses.length - 1].send(piggyBank * 55 / 100);
citizensAddresses[citizensAddresses.length - 2].send(piggyBank * 30 / 100);
citizensAddresses[citizensAddresses.length - 3].send(piggyBank * 15 / 100);
}
// Define the new Piggy Bank
piggyBank = 0;
// Define the new Castle
jester = msg.sender;
lastReparation = block.timestamp;
citizensAddresses.push(msg.sender);
citizensAmounts.push(amount * 2);
totalCitizens += 1;
amountInvested += amount;
// All goes to the Piggy Bank
piggyBank += amount;
// The Jetster take 3%
jester.send(amount * 3 / 100);
// The brided Citizen takes 3%
collectedFee += amount * 3 / 100;
round += 1;
} else {
// The Castle is still up
lastReparation = block.timestamp;
citizensAddresses.push(msg.sender);
citizensAmounts.push(amount * 2);
totalCitizens += 1;
amountInvested += amount;
// 5% goes to the Piggy Bank
piggyBank += (amount * 5 / 100);
// The Jetster takes 3%
jester.send(amount * 3 / 100);
// The brided Citizen takes 3%
collectedFee += amount * 3 / 100;
while (citizensAmounts[lastCitizenPaid] < (address(this).balance - piggyBank - collectedFee) && lastCitizenPaid <= totalCitizens) {
citizensAddresses[lastCitizenPaid].send(citizensAmounts[lastCitizenPaid]);
amountAlreadyPaidBack += citizensAmounts[lastCitizenPaid];
lastCitizenPaid += 1;
}
}
}
// fallback function
function() {
repairTheCastle();
}
// When the castle would be no more...
function surrender() {
if (msg.sender == bribedCitizen) {
bribedCitizen.send(address(this).balance);
selfdestruct(bribedCitizen);
}
}
// When the brided Citizen decides to give his seat to someone else
function newBribedCitizen(address newBribedCitizen) {
if (msg.sender == bribedCitizen) {
bribedCitizen = newBribedCitizen;
}
}
// When the brided Citizen decides to collect his fees
function collectFee() {
if (msg.sender == bribedCitizen) {
bribedCitizen.send(collectedFee);
}
}
// When the jester can't handle it anymore, he can give his position to someone else
function newJester(address newJester) {
if (msg.sender == jester) {
jester = newJester;
}
}
}