forked from hazard999/SharpDungeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rankings.cs
197 lines (146 loc) · 4.85 KB
/
Rankings.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Java.IO;
using pdsharp.noosa;
using pdsharp.utils;
using sharpdungeon.actors.hero;
using sharpdungeon.utils;
namespace sharpdungeon
{
public class Rankings
{
public static Rankings Instance;
public static int TABLE_SIZE = 6;
public static String RANKINGS_FILE = "rankings.dat";
public static String DETAILS_FILE = "game_{0}.dat";
public List<Record> records;
public int lastRecord;
public int totalNumber;
private static String RECORDS = "records";
private static String LATEST = "latest";
private static String TOTAL = "total";
//private static IComparator scoreComparator = Comperator<Record>.Default;
public void Submit(bool win)
{
Load();
var rec = new Record();
rec.info = Dungeon.ResultDescription;
rec.win = win;
rec.heroClass = Dungeon.Hero.heroClass;
rec.armorTier = Dungeon.Hero.Tier();
rec.score = Score(win);
var gameFile = Utils.Format(DETAILS_FILE, DateTime.Now);
try
{
Dungeon.SaveGame(gameFile);
rec.gameFile = gameFile;
}
catch (IOException)
{
rec.gameFile = "";
}
records.Add(rec);
records = records.OrderByDescending(o => o.score).ToList();
lastRecord = records.IndexOf(rec);
var size = records.Count;
if (size > TABLE_SIZE)
{
Record removedGame;
if (lastRecord == size - 1)
{
removedGame = records[size - 2];
records.Remove(removedGame);
lastRecord--;
}
else
{
removedGame = records[size - 1];
records.Remove(removedGame);
}
if (removedGame.gameFile.Length > 0)
Game.Instance.DeleteFile(removedGame.gameFile);
}
totalNumber++;
Badge.ValidateGamesPlayed();
Save();
}
private static int Score(bool win)
{
return (Statistics.GoldCollected + Dungeon.Hero.Lvl * Dungeon.Depth * 100) * (win ? 2 : 1);
}
public void Save()
{
var bundle = new Bundle();
bundle.Put(RECORDS, records);
bundle.Put(LATEST, lastRecord);
bundle.Put(TOTAL, totalNumber);
try
{
var output = Game.Instance.OpenFileOutput(RANKINGS_FILE, Android.Content.FileCreationMode.Private);
Bundle.Write(bundle, output);
output.Close();
}
catch (Exception)
{
}
}
public void Load()
{
if (records != null)
return;
records = new List<Record>();
try
{
var input = Game.Instance.OpenFileInput(RANKINGS_FILE);
var bundle = Bundle.Read(input);
input.Close();
foreach (var record in bundle.GetCollection(RECORDS))
records.Add((Record)record);
lastRecord = bundle.GetInt(LATEST);
totalNumber = bundle.GetInt(TOTAL);
if (totalNumber == 0)
totalNumber = records.Count;
}
catch (Exception)
{
}
}
public int Compare(Record lhs, Record rhs)
{
return Math.Sign(rhs.score - lhs.score);
}
}
public class Record : Bundlable
{
private static String REASON = "reason";
public static String WIN = "win";
private static String SCORE = "score";
private static String TIER = "tier";
private static String GAME = "gameFile";
public String info;
public bool win;
public HeroClass heroClass;
public int armorTier;
public int score;
public String gameFile;
public void RestoreFromBundle(pdsharp.utils.Bundle bundle)
{
info = bundle.GetString(REASON);
win = bundle.GetBoolean(WIN);
score = bundle.GetInt(SCORE);
heroClass = HeroClass.ReStoreInBundle(bundle);
armorTier = bundle.GetInt(TIER);
gameFile = bundle.GetString(GAME);
}
public void StoreInBundle(Bundle bundle)
{
bundle.Put(REASON, info);
bundle.Put(WIN, win);
bundle.Put(SCORE, score);
heroClass.StoreInBundle(bundle);
bundle.Put(TIER, armorTier);
bundle.Put(GAME, gameFile);
}
}
}