/
Rankings.java
225 lines (177 loc) · 5.23 KB
/
Rankings.java
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
/*
* Pixel Dungeon
* Copyright (C) 2012-2014 Oleg Dolya
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.hmdzl.spspd;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import com.hmdzl.spspd.actors.hero.HeroClass;
import com.hmdzl.spspd.messages.Messages;
import com.watabou.noosa.Game;
import com.watabou.utils.Bundlable;
import com.watabou.utils.Bundle;
import com.watabou.utils.SystemTime;
public enum Rankings {
INSTANCE;
public static final int TABLE_SIZE = 11;
public static final String RANKINGS_FILE = "rankings.dat";
public static final String DETAILS_FILE = "game_%d.dat";
public ArrayList<Record> records;
public int lastRecord;
public int totalNumber;
public int wonNumber;
public void submit(boolean win) {
load();
Record rec = new Record();
rec.info = Dungeon.resultDescription;
rec.win = win;
rec.heroClass = Dungeon.hero.heroClass;
rec.skin = Dungeon.skins;
rec.herolevel = Dungeon.hero.lvl;
rec.depth = Dungeon.depth;
rec.score = score(win);
String gameFile = Messages.format( DETAILS_FILE, SystemTime.now );
try {
Dungeon.saveGame(gameFile);
rec.gameFile = gameFile;
} catch (IOException e) {
rec.gameFile = "";
}
records.add(rec);
Collections.sort(records, scoreComparator);
lastRecord = records.indexOf(rec);
int size = records.size();
while (size > TABLE_SIZE) {
Record removedGame;
if (lastRecord == size - 1) {
removedGame = records.remove(size - 2);
lastRecord--;
} else {
removedGame = records.remove(size - 1);
}
if (removedGame.gameFile.length() > 0) {
Game.instance.deleteFile(removedGame.gameFile);
}
size = records.size();
}
totalNumber++;
if (win) {
wonNumber++;
}
Badges.validateGamesPlayed();
save();
}
private int score(boolean win) {
return (Statistics.goldCollected + Dungeon.hero.lvl
* (win ? 26 : Dungeon.depth) * 100)
* (win ? 2 : 1);
}
private static final String RECORDS = "records";
private static final String LATEST = "latest";
private static final String TOTAL = "total";
private static final String WON = "won";
public void save() {
Bundle bundle = new Bundle();
bundle.put(RECORDS, records);
bundle.put(LATEST, lastRecord);
bundle.put(TOTAL, totalNumber);
bundle.put(WON, wonNumber);
try {
OutputStream output = Game.instance.openFileOutput(RANKINGS_FILE,
Game.MODE_PRIVATE);
Bundle.write(bundle, output);
output.close();
} catch (IOException e) {
}
}
public void load() {
if (records != null) {
return;
}
records = new ArrayList<Rankings.Record>();
try {
InputStream input = Game.instance.openFileInput(RANKINGS_FILE);
Bundle bundle = Bundle.read(input);
input.close();
for (Bundlable record : bundle.getCollection(RECORDS)) {
records.add((Record) record);
}
lastRecord = bundle.getInt(LATEST);
totalNumber = bundle.getInt(TOTAL);
if (totalNumber == 0) {
totalNumber = records.size();
}
wonNumber = bundle.getInt(WON);
if (wonNumber == 0) {
for (Record rec : records) {
if (rec.win) {
wonNumber++;
}
}
}
} catch (IOException e) {
}
}
public static class Record implements Bundlable {
private static final String REASON = "reason";
private static final String WIN = "win";
private static final String SCORE = "score";
private static final String SKIN = "skin";
private static final String LEVEL = "level";
private static final String DEPTH = "depth";
private static final String GAME = "gameFile";
public String info;
public boolean win;
public HeroClass heroClass;
public int skin;
public int herolevel;
public int depth;
public int score;
public String gameFile;
@Override
public void restoreFromBundle(Bundle bundle) {
info = bundle.getString(REASON);
win = bundle.getBoolean(WIN);
score = bundle.getInt(SCORE);
heroClass = HeroClass.restoreInBundle(bundle);
skin = bundle.getInt(SKIN);
gameFile = bundle.getString(GAME);
depth = bundle.getInt(DEPTH);
herolevel = bundle.getInt(LEVEL);
}
@Override
public void storeInBundle(Bundle bundle) {
bundle.put(REASON, info);
bundle.put(WIN, win);
bundle.put(SCORE, score);
heroClass.storeInBundle(bundle);
bundle.put(SKIN, skin);
bundle.put(LEVEL, herolevel);
bundle.put(DEPTH, depth);
bundle.put(GAME, gameFile);
}
}
private static final Comparator<Record> scoreComparator = new Comparator<Rankings.Record>() {
@Override
public int compare(Record lhs, Record rhs) {
return (int) Math.signum(rhs.score - lhs.score);
}
};
}