-
Notifications
You must be signed in to change notification settings - Fork 21
/
PlayerScore.sol
49 lines (42 loc) · 1.36 KB
/
PlayerScore.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
contract PlayerScore {
uint maxTopScores = 5;
struct TopScore{
address addr;
int score;
}
TopScore[] public topScores;
mapping (address=>int) public userTopScores;
function setTopScore(int score) {
var currentTopScore = userTopScores[msg.sender];
if(currentTopScore < score){
userTopScores[msg.sender] = score;
}
if(topScores.length < maxTopScores){
var topScore = TopScore(msg.sender, score);
topScores.push(topScore);
}else{
int lowestScore = 0;
uint lowestScoreIndex = 0;
for (uint i = 0; i < topScores.length; i++)
{
TopScore currentScore = topScores[i];
if(i == 0){
lowestScore = currentScore.score;
lowestScoreIndex = i;
}else{
if(lowestScore > currentScore.score){
lowestScore = currentScore.score;
lowestScoreIndex = i;
}
}
}
if(score > lowestScore){
var newtopScore = TopScore(msg.sender, score);
topScores[lowestScoreIndex] = newtopScore;
}
}
}
function getCountTopScores() returns(uint) {
return topScores.length;
}
}