forked from triple9dice/999dice.net
/
DiceResponse.cs
406 lines (384 loc) · 15.2 KB
/
DiceResponse.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Linq;
namespace Dice.Client.Web
{
/// <summary>
/// The base class from which all API response classes are derived.
/// </summary>
public abstract class DiceResponse
{
/// <summary>
/// True, if the API call was successful.
/// </summary>
public bool Success { get; internal set; }
/// <summary>
/// The HTTP status code for the API call, if available.
/// </summary>
public int WebStatusCode { get; internal set; }
/// <summary>
/// An error message from the API call, if available.
/// Standard errors like "too fast" do not return error messages, but rather set boolean values (like DiceResponse.RateLimited).
/// </summary>
public string ErrorMessage { get; internal set; }
/// <summary>
/// The raw API response converted from JSON.
/// </summary>
public IDictionary<string, object> RawResponse { get; private set; }
/// <summary>
/// True if you have exceeded rate limits.
/// </summary>
public bool RateLimited { get; private set; }
internal DiceResponse()
{
}
/// <summary>
/// Stores the response object, converted from JSON.
/// Checks for common result codes.
/// </summary>
/// <param name="resp">The response object, converted from JSON</param>
virtual internal void SetRawResponse(IDictionary<string, object> resp)
{
RawResponse = resp;
if (resp.ContainsKey("error"))
ErrorMessage = (string)resp["error"];
if (resp.ContainsKey("TooFast"))
RateLimited = true;
if (resp.ContainsKey("success"))
Success = true;
}
}
public sealed class BeginSessionResponse : DiceResponse
{
public SessionInfo Session { get; private set; }
public bool InvalidApiKey { get; private set; }
public bool LoginRequired { get; private set; }
public bool WrongUsernameOrPassword { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("InvalidApiKey"))
{
InvalidApiKey = true;
return;
}
if (resp.ContainsKey("LoginRequired"))
{
LoginRequired = true;
return;
}
if (resp.ContainsKey("LoginInvalid"))
{
WrongUsernameOrPassword = true;
return;
}
if (resp.ContainsKey("SessionCookie") &&
resp.ContainsKey("AccountId") &&
resp.ContainsKey("MaxBetBatchSize"))
{
Success = true;
Session = new SessionInfo(
(string)resp["SessionCookie"],
Convert.ToInt64(resp["AccountId"]),
Convert.ToInt32(resp["MaxBetBatchSize"]));
if (resp.ContainsKey("AccountCookie"))
Session.AccountCookie = (string)resp["AccountCookie"];
if (resp.ContainsKey("ClientSeed"))
Session.ClientSeed = Convert.ToInt64(resp["ClientSeed"]);
if (resp.ContainsKey("BetCount"))
Session.BetCount = Convert.ToInt64(resp["BetCount"]);
if (resp.ContainsKey("BetPayIn"))
Session.BetPayIn = Convert.ToDecimal(resp["BetPayIn"]) / 100000000M;
if (resp.ContainsKey("BetPayOut"))
Session.BetPayOut = Convert.ToDecimal(resp["BetPayOut"]) / 100000000M;
if (resp.ContainsKey("BetWinCount"))
Session.BetWinCount = Convert.ToInt64(resp["BetWinCount"]);
if (resp.ContainsKey("Balance"))
Session.Balance = Convert.ToDecimal(resp["Balance"]) / 100000000M;
if (resp.ContainsKey("Email"))
Session.Email = (string)resp["Email"];
if (resp.ContainsKey("EmergencyAddress"))
Session.EmergencyAddress = (string)resp["EmergencyAddress"];
if (resp.ContainsKey("DepositAddress"))
Session.DepositAddress = (string)resp["DepositAddress"];
}
}
}
public sealed class CreateUserResponse : DiceResponse
{
/// <summary>
/// True if the account already has a username and password.
/// </summary>
public bool AccountAlreadyHasUser { get; private set; }
/// <summary>
/// True if the username is already in use by someone else.
/// </summary>
public bool UsernameAlreadyTaken { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("AccountHasUser"))
AccountAlreadyHasUser = true;
else if (resp.ContainsKey("UsernameTaken"))
UsernameAlreadyTaken = true;
}
}
public sealed class WithdrawResponse : DiceResponse
{
/// <summary>
/// The amount of the withdrawal which is now pending.
/// </summary>
public decimal WithdrawalPending { get; private set; }
/// <summary>
/// True if the amount of the withdrawal is too small to process.
/// </summary>
public bool WithdrawalTooSmall { get; private set; }
/// <summary>
/// True if there are insufficient funds to make the requested withdrawal.
/// </summary>
public bool InsufficientFunds { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("TooSmall"))
WithdrawalTooSmall = true;
else if (resp.ContainsKey("InsufficientFunds"))
InsufficientFunds = true;
else if (resp.ContainsKey("Pending"))
{
WithdrawalPending = Convert.ToDecimal(resp["Pending"]) / 100000000M;
Success = true;
}
}
}
public sealed class ChangePasswordResponse : DiceResponse
{
/// <summary>
/// True if the wrong password was supplied.
/// </summary>
public bool WrongPassword { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("WrongPassword"))
WrongPassword = true;
}
}
public sealed class GetServerSeedHashResponse : DiceResponse
{
/// <summary>
/// The server seed hash (SHA-256)
/// </summary>
public string ServerSeedHash { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("Hash"))
{
Success = true;
ServerSeedHash = (string)resp["Hash"];
}
}
}
public sealed class GetBalanceResponse : DiceResponse
{
/// <summary>
/// The current balance of the user.
/// </summary>
public decimal Balance { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("Balance"))
{
Success = true;
Balance = Convert.ToDecimal(resp["Balance"]) / 100000000M;
}
}
}
public sealed class SetClientSeedResponse : DiceResponse
{
}
public sealed class UpdateEmailResponse : DiceResponse
{
}
public sealed class UpdateEmergencyAddressResponse : DiceResponse
{
}
public sealed class GetDepositAddressResponse : DiceResponse
{
/// <summary>
/// A bitcoin address accepting deposits for the user.
/// </summary>
public string DepositAddress { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("Address"))
{
Success = true;
DepositAddress = (string)resp["Address"];
}
}
}
public sealed class PlaceBetResponse : DiceResponse
{
/// <summary>
/// True if the chance to win is too high.
/// </summary>
public bool ChanceTooHigh { get; private set; }
/// <summary>
/// True if the chance to win is too low.
/// </summary>
public bool ChanceTooLow { get; private set; }
/// <summary>
/// True if there are insufficient funds to place the bet.
/// </summary>
public bool InsufficientFunds { get; private set; }
/// <summary>
/// True if there is no possible way the user could profit from a bet that has a cost.
/// </summary>
public bool NoPossibleProfit { get; private set; }
/// <summary>
/// True if the payout for this bet could be greater than what the site is willing to risk.
/// </summary>
public bool MaxPayoutExceeded { get; private set; }
/// <summary>
/// The bet's ID.
/// </summary>
public long BetId { get; private set; }
/// <summary>
/// The payout for a winning bet.
/// </summary>
public decimal PayOut { get; private set; }
/// <summary>
/// The secret value generated for the bet.
/// </summary>
public long Secret { get; private set; }
/// <summary>
/// The user's balance immediately before placing this bet.
/// </summary>
public decimal StartingBalance { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("ChanceTooHigh"))
ChanceTooHigh = true;
else if (resp.ContainsKey("ChanceTooLow"))
ChanceTooLow = true;
else if (resp.ContainsKey("InsufficientFunds"))
InsufficientFunds = true;
else if (resp.ContainsKey("NoPossibleProfit"))
NoPossibleProfit = true;
else if (resp.ContainsKey("MaxPayoutExceeded"))
MaxPayoutExceeded = true;
else if (resp.ContainsKey("BetId") &&
resp.ContainsKey("PayOut") &&
resp.ContainsKey("Secret") &&
resp.ContainsKey("StartingBalance"))
{
Success = true;
BetId = Convert.ToInt64(resp["BetId"]);
PayOut = Convert.ToDecimal(resp["PayOut"]) / 100000000M;
Secret = Convert.ToInt64(resp["Secret"]);
StartingBalance = Convert.ToDecimal(resp["StartingBalance"]) / 100000000M;
}
}
}
public sealed class PlaceAutomatedBetsResponse : DiceResponse
{
/// <summary>
/// True if the chance to win is too high.
/// </summary>
public bool ChanceTooHigh { get; private set; }
/// <summary>
/// True if the chance to win is too low.
/// </summary>
public bool ChanceTooLow { get; private set; }
/// <summary>
/// True if there are insufficient funds to place the bet.
/// </summary>
public bool InsufficientFunds { get; private set; }
/// <summary>
/// True if there is no possible way the user could profit from a bet that has a cost.
/// </summary>
public bool NoPossibleProfit { get; private set; }
/// <summary>
/// True if the payout for this bet could be greater than what the site is willing to risk.
/// </summary>
public bool MaxPayoutExceeded { get; private set; }
/// <summary>
/// The ID's of the bets in the batch.
/// </summary>
public long[] BetIds { get; private set; }
/// <summary>
/// The amounts paid in for each bet in the batch.
/// These are always negative numbers.
/// </summary>
public decimal[] PayIns { get; private set; }
/// <summary>
/// The payouts from each bet in the batch.
/// </summary>
public decimal[] PayOuts { get; private set; }
/// <summary>
/// The secret numbers generated for the bets.
/// </summary>
public long[] Secrets { get; private set; }
/// <summary>
/// The number of bets placed in this batch.
/// </summary>
public int BetCount { get; private set; }
/// <summary>
/// The total amount paid to place the bets in this batch.
/// This value, plus TotalPayOut will equal the net profit.
/// This is always a negative number.
/// </summary>
public decimal TotalPayIn { get; private set; }
/// <summary>
/// The total amount paid out from bets in this batch.
/// This value, plus TotalPayIn will equal the net profit.
/// </summary>
public decimal TotalPayOut { get; private set; }
/// <summary>
/// The server seed used to generate these bets.
/// </summary>
public string ServerSeed { get; private set; }
/// <summary>
/// The user's balance immediately before placing the first bet of this batch.
/// </summary>
public decimal StartingBalance { get; private set; }
internal override void SetRawResponse(IDictionary<string, object> resp)
{
base.SetRawResponse(resp);
if (resp.ContainsKey("ChanceTooHigh"))
ChanceTooHigh = true;
else if (resp.ContainsKey("ChanceTooLow"))
ChanceTooLow = true;
else if (resp.ContainsKey("InsufficientFunds"))
InsufficientFunds = true;
else if (resp.ContainsKey("NoPossibleProfit"))
NoPossibleProfit = true;
else if (resp.ContainsKey("MaxPayoutExceeded"))
MaxPayoutExceeded = true;
else if (resp.ContainsKey("BetId") &&
resp.ContainsKey("PayIn") &&
resp.ContainsKey("PayOut") &&
resp.ContainsKey("Seed") &&
resp.ContainsKey("BetCount") &&
resp.ContainsKey("StartingBalance"))
{
Success = true;
long BetId = Convert.ToInt64(resp["BetId"]);
BetCount = Convert.ToInt32(resp["BetCount"]);
ServerSeed = (string)resp["Seed"];
TotalPayIn = Convert.ToDecimal(resp["PayIn"]) / 100000000M;
TotalPayOut = Convert.ToDecimal(resp["PayOut"]) / 100000000M;
StartingBalance = Convert.ToDecimal(resp["StartingBalance"]) / 100000000M;
BetIds = Enumerable.Range(0, BetCount).Select(x => BetId + x).ToArray();
Secrets = new long[BetCount];
PayIns = new decimal[BetCount];
PayOuts = new decimal[BetCount];
}
}
}
}