-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameResults.cs
275 lines (254 loc) · 6.13 KB
/
GameResults.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
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
// GameResults.cs
/*
* Thes classes handle keeping track of the win/loss record and making sure they are
* not tampered with.
*
*/
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Rails
{
public class PlayerResult
{
public string Name; // null for bots
public OfficialGame Won;
public int Funds;
public PlayerResult(string name, OfficialGame won, int funds)
{
Name = name;
Won = won;
Funds = funds;
}
public PlayerResult(BinaryReader r)
{
int version = r.ReadInt32();
bool b = r.ReadBoolean();
Name = b ? r.ReadString() : null;
if (version >= 1)
{
Won = (OfficialGame) r.ReadInt32();
}
else
{
b = r.ReadBoolean();
Won = b ? OfficialGame.Yes : OfficialGame.No;
}
Funds = r.ReadInt32();
}
public void Save(BinaryWriter w)
{
w.Write((int) 1); // version
w.Write(Name != null);
if (Name != null) w.Write(Name);
w.Write((int) Won);
w.Write(Funds);
}
public override String ToString()
{
bool won = (Won == OfficialGame.Yes);
return (Name == null ? String.Empty : Name) + ":" + won.ToString() + Funds.ToString();
}
}
public enum OfficialGame
{
Yes, No, Unknown
}
public class GameResult
{
public object Map;
public int NumPlayers;
public PlayerResult[] Players;
public Options GameOptions;
public int Turns;
public byte[] Checksum;
public long FileOffset;
public DateTime Date;
public OfficialGame Official;
public GameResult(Game game)
{
if (game.map is RandomMap)
Map = unchecked((uint) ((RandomMap) game.map).Number);
else if (game.map is RandomMap2)
Map = ((RandomMap2) game.map).Name;
else
Map = ((AuthoredMap) game.map).Name;
NumPlayers = game.state.NumPlayers;
Players = new PlayerResult[NumPlayers];
for (int i=0; i<NumPlayers; i++)
{
string name = null;
if (game.state.PlayerInfo[i].Human)
name = game.state.PlayerInfo[i].Name;
OfficialGame won = OfficialGame.Unknown;
if (game.IsOfficial)
won = game.state.Winners.Contains(i) ? OfficialGame.Yes : OfficialGame.No;
Players[i] = new PlayerResult(name, won, game.state.PlayerInfo[i].Funds);
}
GameOptions = game.options;
Turns = game.state.Turn - 1;
FileOffset = -1;
Date = DateTime.Now;
Official = game.IsOfficial ? OfficialGame.Yes : OfficialGame.No;
}
public GameResult(BinaryReader r)
{
FileOffset = r.BaseStream.Position;
int version = r.ReadInt32();
int mapType;
if (version >= 2)
mapType = r.ReadInt32();
else
mapType = 0;
if (mapType == 0)
Map = r.ReadUInt32();
else
Map = r.ReadString();
NumPlayers = r.ReadInt32();
Players = new PlayerResult[NumPlayers];
for (int i=0; i<NumPlayers; i++)
Players[i] = new PlayerResult(r);
GameOptions = new Options(r);
if (version >= 1)
{
Turns = r.ReadInt32();
Date = new DateTime(r.ReadInt64());
}
else
{
Turns = -1;
Date = DateTime.MinValue;
}
if (version >= 3)
{
Official = (OfficialGame) r.ReadInt32();
}
else
{
Official = OfficialGame.Unknown;
}
Checksum = new byte[20];
for (int i=0; i<20; i++)
Checksum[i] = r.ReadByte();
byte[] hash = ComputeHash();
for (int i=0; i<20; i++)
if (Checksum[i] != hash[i])
{
MessageBox.Show(Resource.GetString("Game.WinLossError"), Resource.GetString("Rails"));
Application.Exit();
}
}
public void Save(BinaryWriter w)
{
FileOffset = w.BaseStream.Position;
w.Write((int) 3); // version
if (Map is uint)
{
w.Write((int) 0);
w.Write((uint) Map);
}
else
{
w.Write((int) 1);
w.Write((string) Map);
}
w.Write(NumPlayers);
foreach (PlayerResult r in Players)
r.Save(w);
GameOptions.Save(w);
w.Write(Turns);
w.Write(Date.Ticks);
w.Write((int) Official);
Checksum = ComputeHash();
for (int i=0; i<20; i++)
w.Write(Checksum[i]);
}
string GetDescription()
{
System.Globalization.NumberFormatInfo numberFormat = System.Globalization.CultureInfo.InvariantCulture.NumberFormat;
StringBuilder b = new StringBuilder(NumPlayers.ToString(numberFormat) + ":" + GameOptions.ToString()
+ ":" + FileOffset.ToString(numberFormat) + ":" + Map.ToString() + (Turns == -1 ? "" : ":" + Turns.ToString(numberFormat)) + (Date == DateTime.MinValue ? "" : ":" + Date.Ticks));
foreach (PlayerResult r in Players)
b.Append(":" + r.ToString());
if (Official != OfficialGame.Unknown)
b.Append(":" + Official);
return b.ToString();
}
internal byte[] ComputeHash()
{
string s = GetDescription();
byte[] data = Encoding.UTF8.GetBytes(s + ":bc74baaa18c16480a545263ef434ee61");
SHA1 sha = SHA1.Create();
byte[] hash = sha.ComputeHash(data);
return hash;
}
}
public class GameResultsCollection : IEnumerable
{
public ArrayList Results;
public GameResultsCollection()
{
Stream s = null;
BinaryReader r = null;
try
{
s = new FileStream("winloss.rec", FileMode.Open);
r = new BinaryReader(s);
Results = new ArrayList();
while (s.Position < s.Length)
Results.Add(new GameResult(r));
}
catch(System.IO.IOException)
{
return;
}
finally
{
if (r != null)
r.Close();
if (s != null)
s.Close();
}
}
public static void Record(GameResult result)
{
Stream s = null;
BinaryWriter w = null;
try
{
s = new FileStream("winloss.rec", FileMode.OpenOrCreate);
s.Seek(0, SeekOrigin.End);
w = new BinaryWriter(s);
result.Save(w);
}
finally
{
if (w != null)
w.Close();
if (s != null)
s.Close();
}
}
public IEnumerator GetEnumerator()
{
return Results.GetEnumerator();
}
public GameResult this[int index]
{
get
{
return (GameResult) Results[index];
}
}
public int Count
{
get
{
return Results.Count;
}
}
}
}