forked from triple9dice/999dice.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiceWebAPI.cs
878 lines (862 loc) · 40.4 KB
/
DiceWebAPI.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Threading;
using System.Web.Script.Serialization;
using System.Linq;
using System.Security.Cryptography;
using System.Diagnostics;
using System.Globalization;
#if !NET_35
using System.Threading.Tasks;
#endif
namespace Dice.Client.Web
{
/// <summary>
/// All methods that interact with the API are here.
/// </summary>
public static class DiceWebAPI
{
#region Constants, Variables, Helpers
/// <summary>
/// All bets are between 0 and 999,999
/// </summary>
const long GuessSpan = 1000000;
/// <summary>
/// 99.9% payout. 0.1% house edge.
/// </summary>
const decimal HousePayout = 0.999M;
#if !NET_35
static readonly ThreadLocal<JavaScriptSerializer> Serializers = new ThreadLocal<JavaScriptSerializer>(() => new JavaScriptSerializer());
static JavaScriptSerializer Serializer { get { return Serializers.Value; } }
#else
static JavaScriptSerializer Serializer { get { return new JavaScriptSerializer(); } }
#endif
public static Uri WebUri = new Uri("https://www.999dice.com/api/web.aspx");
static SHA512 CreateSHA512()
{
try
{
#if !NET_35
if (!CryptoConfig.AllowOnlyFipsAlgorithms)
#endif
return new SHA512Managed();
}
catch
{
}
try
{
return new SHA512Cng();
}
catch
{
}
return new SHA512CryptoServiceProvider();
}
#endregion
#region Request methods
static T Request<T>(NameValueCollection formData) where T : DiceResponse, new()
{
T response = new T();
try
{
using (WebClient client = new WebClient())
response.SetRawResponse((IDictionary<string, object>)Serializer.DeserializeObject(Encoding.UTF8.GetString(client.UploadValues(WebUri, "POST", formData))));
response.WebStatusCode = 200;
}
catch (WebException e)
{
response.ErrorMessage = e.Message;
HttpWebResponse r = e.Response as HttpWebResponse;
if (r != null)
response.WebStatusCode = (int)r.StatusCode;
}
return response;
}
#if !NET_35
static async Task<T> RequestAsync<T>(NameValueCollection formData) where T : DiceResponse, new()
{
T response = new T();
try
{
using (WebClient client = new WebClient())
response.SetRawResponse((IDictionary<string, object>)Serializer.DeserializeObject(Encoding.UTF8.GetString(await client.UploadValuesTaskAsync(WebUri, "POST", formData))));
response.WebStatusCode = 200;
}
catch (WebException e)
{
response.ErrorMessage = e.Message;
HttpWebResponse r = e.Response as HttpWebResponse;
if (r != null)
response.WebStatusCode = (int)r.StatusCode;
}
return response;
}
#endif
#endregion
#region Form data creation
static NameValueCollection GetFormDataCreateAccount(string apiKey)
{
return new NameValueCollection
{
{ "a", "CreateAccount" },
{ "Key", apiKey }
};
}
static NameValueCollection GetFormDataBeginSession(string apiKey, string accountCookie)
{
return new NameValueCollection
{
{ "a", "BeginSession" },
{ "Key", apiKey },
{ "AccountCookie", accountCookie }
};
}
static NameValueCollection GetFormDataLogin(string apiKey, string username, string password)
{
return new NameValueCollection
{
{ "a", "Login" },
{ "Key", apiKey },
{ "Username", username },
{ "Password", password }
};
}
static NameValueCollection GetFormDataLogin(string apiKey, string username, string password, int totp)
{
return new NameValueCollection
{
{ "a", "Login" },
{ "Key", apiKey },
{ "Username", username },
{ "Password", password },
{ "Totp", totp.ToString() }
};
}
static NameValueCollection GetFormDataCreateUser(string sessionCookie, string username, string password)
{
return new NameValueCollection
{
{ "a", "CreateUser" },
{ "s", sessionCookie },
{ "Username", username },
{ "Password", password }
};
}
static NameValueCollection GetFormDataWithdraw(string sessionCookie, decimal amount, string address, Currencies currency)
{
return new NameValueCollection
{
{ "a", "Withdraw" },
{ "s", sessionCookie },
{ "Amount", ((long)(amount*100000000)).ToString() },
{ "Address", address },
{ "Currency", currency.ToString() }
};
}
static NameValueCollection GetFormDataWithdraw(string sessionCookie, decimal amount, string address, int totp, Currencies currency)
{
return new NameValueCollection
{
{ "a", "Withdraw" },
{ "s", sessionCookie },
{ "Amount", ((long)(amount*100000000)).ToString() },
{ "Address", address },
{ "Totp", totp.ToString() },
{ "Currency", currency.ToString() }
};
}
static NameValueCollection GetFormDataChangePassword(string sessionCookie, string oldPassword, string newPassword)
{
return new NameValueCollection
{
{ "a", "ChangePassword" },
{ "s", sessionCookie },
{ "OldPassword", oldPassword },
{ "NewPassword", newPassword }
};
}
static NameValueCollection GetFormDataGetServerSeedHash(string sessionCookie)
{
return new NameValueCollection
{
{ "a", "GetServerSeedHash" },
{ "s", sessionCookie }
};
}
static NameValueCollection GetFormDataUpdateEmail(string sessionCookie, string email)
{
return new NameValueCollection
{
{ "a", "UpdateEmail" },
{ "s", sessionCookie },
{ "Email", email }
};
}
static NameValueCollection GetFormDataUpdateEmergencyAddress(string sessionCookie, string address)
{
return new NameValueCollection
{
{ "a", "UpdateEmergencyAddress" },
{ "s", sessionCookie },
{ "Address", address }
};
}
static NameValueCollection GetFormDataGetBalance(string sessionCookie, Currencies currency)
{
return new NameValueCollection
{
{ "a", "GetBalance" },
{ "s", sessionCookie },
{ "Currency", currency.ToString() }
};
}
static NameValueCollection GetFormDataGetDepositAddress(string sessionCookie, Currencies currency)
{
return new NameValueCollection
{
{ "a", "GetDepositAddress" },
{ "s", sessionCookie },
{ "Currency", currency.ToString() }
};
}
static NameValueCollection GetFormDataPlaceBet(string sessionCookie, decimal payIn, long guessLow, long guessHigh, int clientSeed, Currencies currency)
{
return new NameValueCollection
{
{ "a", "PlaceBet" },
{ "s", sessionCookie },
{ "PayIn", ((long)(payIn*100000000)).ToString() },
{ "Low", guessLow.ToString() },
{ "High", guessHigh.ToString() },
{ "ClientSeed", clientSeed.ToString() },
{ "Currency", currency.ToString() }
};
}
static NameValueCollection GetFormDataPlaceAutomatedBets(string sessionCookie, AutomatedBetsSettings settings)
{
return new NameValueCollection
{
{ "a", "PlaceAutomatedBets" },
{ "s", sessionCookie },
{ "BasePayIn", ((long)(settings.BasePayIn*100000000)).ToString() },
{ "Low", settings.GuessLow.ToString() },
{ "High", settings.GuessHigh.ToString() },
{ "MaxBets", settings.MaxBets.ToString() },
{ "ResetOnWin", settings.ResetOnWin.ToString() },
{ "ResetOnLose", settings.ResetOnLose.ToString() },
{ "IncreaseOnWinPercent", settings.IncreaseOnWinPercent.ToString() },
{ "IncreaseOnLosePercent", settings.IncreaseOnLosePercent.ToString() },
{ "MaxPayIn", ((long)(settings.MaxAllowedPayIn*100000000)).ToString() },
{ "ResetOnLoseMaxBet", settings.ResetOnLoseMaxBet.ToString() },
{ "StopOnLoseMaxBet", settings.StopOnLoseMaxBet.ToString() },
{ "StopMaxBalance", ((long)(settings.StopMaxBalance*100000000)).ToString() },
{ "StopMinBalance", ((long)(settings.StopMinBalance*100000000)).ToString() },
{ "StartingPayIn", ((long)(settings.StartingPayIn*100000000)).ToString() },
{ "Compact", "1" },
{ "ClientSeed", settings.ClientSeed.ToString() },
{ "Currency", settings.Currency.ToString() }
};
}
#endregion
#region Processing API Results
static BeginSessionResponse Process(BeginSessionResponse res, string username)
{
if (res.Success && res.Session != null)
res.Session.Username = username;
return res;
}
static CreateUserResponse Process(SessionInfo session, CreateUserResponse res, string username)
{
if (res.Success)
session.Username = username;
return res;
}
static GetBalanceResponse Process(SessionInfo session, GetBalanceResponse res, Currencies currency)
{
res.Currency = currency;
if (res.Success)
session[currency].Balance = res.Balance;
return res;
}
static WithdrawResponse Process(SessionInfo session, WithdrawResponse res, Currencies currency)
{
res.Currency = currency;
if (res.Success)
session[currency].Balance -= res.WithdrawalPending;
return res;
}
static UpdateEmailResponse Process(SessionInfo session, UpdateEmailResponse res, string email)
{
if (res.Success)
session.Email = email;
return res;
}
static UpdateEmergencyAddressResponse Process(SessionInfo session, UpdateEmergencyAddressResponse res, string emergencyAddress)
{
if (res.Success)
session.EmergencyAddress = emergencyAddress;
return res;
}
static GetDepositAddressResponse Process(SessionInfo session, GetDepositAddressResponse res, Currencies currency)
{
res.Currency = currency;
if (res.Success)
session[currency].DepositAddress = res.DepositAddress;
return res;
}
static PlaceBetResponse Process(SessionInfo session, PlaceBetResponse res, decimal payIn, long guessLow, long guessHigh, int clientSeed, Currencies currency)
{
res.Currency = currency;
if (res.Success)
{
session.PauseUpdates();
try
{
++session[currency].BetCount;
session[currency].BetPayIn += payIn;
session[currency].BetPayOut += res.PayOut;
session[currency].Balance = res.StartingBalance + res.PayOut + payIn;
if (res.Secret >= guessLow && res.Secret <= guessHigh)
++session[currency].BetWinCount;
}
finally
{
session.UnpauseUpdates();
}
}
return res;
}
static PlaceAutomatedBetsResponse Process(SessionInfo session, PlaceAutomatedBetsResponse res, AutomatedBetsSettings settings)
{
res.Currency = settings.Currency;
if (res.Success)
{
byte[] seed = new byte[res.ServerSeed.Length / 2];
for (int x = 0; x < seed.Length; ++x)
seed[x] = byte.Parse(res.ServerSeed.Substring(x * 2, 2), System.Globalization.NumberStyles.HexNumber);
byte[] client = BitConverter.GetBytes(settings.ClientSeed).Reverse().ToArray();
decimal payin = settings.StartingPayIn;
decimal bal = res.StartingBalance;
for (int x = 0; x < res.BetCount; ++x)
{
byte[] data = seed.Concat(client).Concat(BitConverter.GetBytes(x).Reverse()).ToArray();
using (SHA512 sha512 = CreateSHA512())
{
byte[] hash = sha512.ComputeHash(sha512.ComputeHash(data));
bool found = false;
while (!found)
{
for (int y = 0; y <= 61; y += 3)
{
long result = (hash[y] << 16) | (hash[y + 1] << 8) | hash[y + 2];
if (result < 16000000)
{
res.Secrets[x] = result % 1000000;
found = true;
break;
}
}
}
}
res.PayIns[x] = payin;
bal += payin;
bool win = res.Secrets[x] >= settings.GuessLow && res.Secrets[x] <= settings.GuessHigh;
if (win)
{
res.PayOuts[x] = CalculateWinPayout(payin, settings.GuessLow, settings.GuessHigh);
bal += res.PayOuts[x];
if (settings.ResetOnWin)
payin = settings.BasePayIn;
else
payin = TruncateSatoshis(payin * (1 + settings.IncreaseOnWinPercent));
}
else
{
if (settings.ResetOnLose || (settings.ResetOnLoseMaxBet && payin == settings.MaxAllowedPayIn))
payin = settings.BasePayIn;
else
payin = TruncateSatoshis(payin * (1 + settings.IncreaseOnLosePercent));
}
if (payin < settings.MaxAllowedPayIn && settings.MaxAllowedPayIn != 0)
payin = settings.MaxAllowedPayIn;
if (payin < -bal)
payin = -bal;
}
Debug.Assert(res.TotalPayIn == res.PayIns.Sum());
Debug.Assert(res.TotalPayOut == res.PayOuts.Sum());
session[settings.Currency].PauseUpdates();
try
{
for (int x = 0; x < res.BetCount; ++x)
if (res.Secrets[x] >= settings.GuessLow && res.Secrets[x] <= settings.GuessHigh)
++session[settings.Currency].BetWinCount;
session[settings.Currency].BetCount += res.BetCount;
session[settings.Currency].Balance = res.StartingBalance + res.TotalPayIn + res.TotalPayOut;
session[settings.Currency].BetPayIn += res.TotalPayIn;
session[settings.Currency].BetPayOut += res.TotalPayOut;
}
finally
{
session[settings.Currency].UnpauseUpdates();
}
}
return res;
}
#endregion
#region Validation methods before making API calls
static void Validate(string apiKey)
{
if (apiKey.IsNullOrWhiteSpace())
throw new ArgumentNullException();
}
static void Validate(string apiKey, string other)
{
if (apiKey.IsNullOrWhiteSpace() || other.IsNullOrWhiteSpace())
throw new ArgumentNullException();
}
static void Validate(SessionInfo session)
{
if (session == null)
throw new ArgumentNullException();
}
static void Validate(Currencies currency)
{
if (currency == Currencies.None || !Enum.IsDefined(typeof(Currencies), currency))
throw new ArgumentOutOfRangeException();
}
static void Validate(SessionInfo session, string other)
{
if (session == null || other.IsNullOrWhiteSpace())
throw new ArgumentNullException();
}
static void Validate(SessionInfo session, string other1, string other2)
{
if (session == null ||
other1.IsNullOrWhiteSpace() ||
other2.IsNullOrWhiteSpace())
throw new ArgumentNullException();
}
static void Validate(SessionInfo session, long guessLow, long guessHigh, Currencies currency)
{
if (session == null)
throw new ArgumentNullException();
if (guessLow < 0 || guessLow > guessHigh || guessHigh >= GuessSpan)
throw new ArgumentOutOfRangeException("0 <= GuessLow <= GuessHigh <= " + GuessSpan.ToString());
Validate(currency);
}
static void Validate(SessionInfo session, AutomatedBetsSettings settings)
{
if (settings == null)
throw new ArgumentNullException();
Validate(session, settings.GuessLow, settings.GuessHigh, settings.Currency);
if (settings.MaxBets < 1 || settings.IncreaseOnWinPercent < 0 || settings.IncreaseOnLosePercent < 0)
throw new ArgumentOutOfRangeException();
}
static void Validate(decimal maxAllowedPayIn, decimal basePayIn)
{
if (maxAllowedPayIn != 0 && maxAllowedPayIn > basePayIn)
throw new ArgumentOutOfRangeException();
}
#endregion
#region Public helper methods
public static decimal TruncateSatoshis(decimal amt)
{
if (amt < 0)
return decimal.Truncate(amt * -100000000M) / -100000000M;
return decimal.Truncate(amt * 100000000M) / 100000000M;
}
public static decimal CalculateWinPayout(decimal payIn, long guessLow, long guessHigh)
{
if (payIn < 0) payIn = -payIn;
payIn = TruncateSatoshis(payIn);
decimal mul = CalculatePayoutMultiplier(guessLow, guessHigh);
decimal payout = payIn * mul;
return TruncateSatoshis(payout);
}
public static decimal CalculateWinProfit(decimal payIn, long guessLow, long guessHigh)
{
if (payIn < 0) payIn = -payIn;
payIn = TruncateSatoshis(payIn);
decimal payout = CalculateWinPayout(payIn, guessLow, guessHigh);
return payout - payIn;
}
public static decimal CalculateChanceToWin(long guessLow, long guessHigh)
{
decimal odds = guessHigh - guessLow + 1;
return odds / GuessSpan;
}
public static decimal CalculatePayoutMultiplier(long guessLow, long guessHigh)
{
return decimal.Floor((HousePayout / CalculateChanceToWin(guessLow, guessHigh)) * 1000000M) / 1000000M;
}
public static int GenerateBetResult(string serverSeed, int clientSeed, int betNumber)
{
if (serverSeed == null)
throw new ArgumentNullException();
if (betNumber < 0 || serverSeed.Length != 64)
throw new ArgumentOutOfRangeException();
Func<string, byte[]> strtobytes = s => Enumerable
.Range(0, s.Length / 2)
.Select(x => byte.Parse(s.Substring(x * 2, 2), NumberStyles.HexNumber))
.ToArray();
byte[] server = strtobytes(serverSeed);
byte[] client = BitConverter.GetBytes(clientSeed).Reverse().ToArray();
byte[] num = BitConverter.GetBytes(betNumber).Reverse().ToArray();
byte[] data = server.Concat(client).Concat(num).ToArray();
using (SHA512 sha512 = new SHA512Managed())
{
byte[] hash = sha512.ComputeHash(sha512.ComputeHash(data));
while (true)
{
for (int x = 0; x <= 61; x += 3)
{
long result = (hash[x] << 16) | (hash[x + 1] << 8) | hash[x + 2];
if (result < 16000000)
return (int)(result % 1000000);
}
hash = sha512.ComputeHash(hash);
}
}
}
#endregion
#region API calls
#region Synchronous API calls
public static BeginSessionResponse BeginSession(string apiKey)
{
Validate(apiKey);
return Request<BeginSessionResponse>(GetFormDataCreateAccount(apiKey));
}
public static BeginSessionResponse BeginSession(string apiKey, string accountCookie)
{
Validate(apiKey, accountCookie);
return Request<BeginSessionResponse>(GetFormDataBeginSession(apiKey, accountCookie));
}
public static BeginSessionResponse BeginSession(string apiKey, string username, string password)
{
Validate(apiKey, username);
return Process(Request<BeginSessionResponse>(GetFormDataLogin(apiKey, username, password)), username);
}
public static BeginSessionResponse BeginSession(string apiKey, string username, string password, int totp)
{
Validate(apiKey, username);
return Process(Request<BeginSessionResponse>(GetFormDataLogin(apiKey, username, password, totp)), username);
}
public static CreateUserResponse CreateUser(SessionInfo session, string username, string password)
{
Validate(session, username, password);
username = username.Trim();
return Process(session, Request<CreateUserResponse>(GetFormDataCreateUser(session.SessionCookie, username, password)), username);
}
public static GetBalanceResponse GetBalance(SessionInfo session, Currencies currency)
{
Validate(session);
Validate(currency);
return Process(session, Request<GetBalanceResponse>(GetFormDataGetBalance(session.SessionCookie, currency)), currency);
}
public static WithdrawResponse WithdrawAll(SessionInfo session, string address, Currencies currency)
{
return Withdraw(session, 0, address, currency);
}
public static WithdrawResponse WithdrawAll(SessionInfo session, string address, int totp, Currencies currency)
{
return Withdraw(session, 0, address, totp, currency);
}
public static WithdrawResponse Withdraw(SessionInfo session, decimal amount, string address, Currencies currency)
{
Validate(session, address);
Validate(currency);
return Process(session, Request<WithdrawResponse>(GetFormDataWithdraw(session.SessionCookie, amount, address, currency)), currency);
}
public static WithdrawResponse Withdraw(SessionInfo session, decimal amount, string address, int totp, Currencies currency)
{
Validate(session, address);
Validate(currency);
return Process(session, Request<WithdrawResponse>(GetFormDataWithdraw(session.SessionCookie, amount, address, totp, currency)), currency);
}
public static ChangePasswordResponse ChangePassword(SessionInfo session, string oldPassword, string newPassword)
{
Validate(session);
return Request<ChangePasswordResponse>(GetFormDataChangePassword(session.SessionCookie, oldPassword, newPassword));
}
public static GetServerSeedHashResponse GetServerSeedHash(SessionInfo session)
{
Validate(session);
return Request<GetServerSeedHashResponse>(GetFormDataGetServerSeedHash(session.SessionCookie));
}
public static UpdateEmailResponse UpdateEmail(SessionInfo session, string email)
{
Validate(session);
if (email != null)
email = email.Trim();
return Process(session, Request<UpdateEmailResponse>(GetFormDataUpdateEmail(session.SessionCookie, email)), email);
}
public static UpdateEmergencyAddressResponse UpdateEmergencyAddress(SessionInfo session, string emergencyAddress)
{
Validate(session);
if (emergencyAddress != null)
emergencyAddress = emergencyAddress.Trim();
return Process(session, Request<UpdateEmergencyAddressResponse>(GetFormDataUpdateEmergencyAddress(session.SessionCookie, emergencyAddress)), emergencyAddress);
}
public static GetDepositAddressResponse GetDepositAddress(SessionInfo session, Currencies currency)
{
Validate(session);
Validate(currency);
return Process(session, Request<GetDepositAddressResponse>(GetFormDataGetDepositAddress(session.SessionCookie, currency)), currency);
}
public static PlaceBetResponse PlaceBet(SessionInfo session, decimal payIn, long guessLow, long guessHigh, int clientSeed, Currencies currency)
{
Validate(session, guessLow, guessHigh, currency);
if (payIn > 0) payIn = -payIn;
return Process(session, Request<PlaceBetResponse>(GetFormDataPlaceBet(session.SessionCookie, payIn, guessLow, guessHigh, clientSeed, currency)), payIn, guessLow, guessHigh, clientSeed, currency);
}
public static PlaceAutomatedBetsResponse PlaceAutomatedBets(SessionInfo session, AutomatedBetsSettings settings)
{
Validate(session, settings);
return Process(session, Request<PlaceAutomatedBetsResponse>(
GetFormDataPlaceAutomatedBets(session.SessionCookie, settings)), settings);
}
#endregion
#region Asynchronous API calls (await/async - .NET 4.5)
#if !NET_35
public static async Task<BeginSessionResponse> BeginSessionAsync(string apiKey)
{
Validate(apiKey);
return await RequestAsync<BeginSessionResponse>(GetFormDataCreateAccount(apiKey));
}
public static async Task<BeginSessionResponse> BeginSessionAsync(string apiKey, string accountCookie)
{
Validate(apiKey, accountCookie);
return await RequestAsync<BeginSessionResponse>(GetFormDataBeginSession(apiKey, accountCookie));
}
public static async Task<BeginSessionResponse> BeginSessionAsync(string apiKey, string username, string password)
{
Validate(apiKey, username);
return Process(await RequestAsync<BeginSessionResponse>(GetFormDataLogin(apiKey, username, password)), username);
}
public static async Task<BeginSessionResponse> BeginSessionAsync(string apiKey, string username, string password, int totp)
{
Validate(apiKey, username);
return Process(await RequestAsync<BeginSessionResponse>(GetFormDataLogin(apiKey, username, password, totp)), username);
}
public static async Task<CreateUserResponse> CreateUserAsync(SessionInfo session, string username, string password)
{
Validate(session, username, password);
username = username.Trim();
return Process(session, await RequestAsync<CreateUserResponse>(GetFormDataCreateUser(session.SessionCookie, username, password)), username);
}
public static async Task<GetBalanceResponse> GetBalanceAsync(SessionInfo session, Currencies currency)
{
Validate(session);
Validate(currency);
return Process(session, await RequestAsync<GetBalanceResponse>(GetFormDataGetBalance(session.SessionCookie, currency)), currency);
}
public static async Task<WithdrawResponse> WithdrawAllAsync(SessionInfo session, string address, Currencies currency)
{
return await WithdrawAsync(session, 0, address, currency);
}
public static async Task<WithdrawResponse> WithdrawAllAsync(SessionInfo session, string address, int totp, Currencies currency)
{
return await WithdrawAsync(session, 0, address, totp, currency);
}
public static async Task<WithdrawResponse> WithdrawAsync(SessionInfo session, decimal amount, string address, Currencies currency)
{
Validate(session, address);
return Process(session, await RequestAsync<WithdrawResponse>(GetFormDataWithdraw(session.SessionCookie, amount, address, currency)), currency);
}
public static async Task<WithdrawResponse> WithdrawAsync(SessionInfo session, decimal amount, string address, int totp, Currencies currency)
{
Validate(session, address);
return Process(session, await RequestAsync<WithdrawResponse>(GetFormDataWithdraw(session.SessionCookie, amount, address, totp, currency)), currency);
}
public static async Task<ChangePasswordResponse> ChangePasswordAsync(SessionInfo session, string oldPassword, string newPassword)
{
Validate(session);
return await RequestAsync<ChangePasswordResponse>(GetFormDataChangePassword(session.SessionCookie, oldPassword, newPassword));
}
public static async Task<GetServerSeedHashResponse> GetServerSeedHashAsync(SessionInfo session)
{
Validate(session);
return await RequestAsync<GetServerSeedHashResponse>(GetFormDataGetServerSeedHash(session.SessionCookie));
}
public static async Task<UpdateEmailResponse> UpdateEmailAsync(SessionInfo session, string email)
{
Validate(session);
if (email != null)
email = email.Trim();
return Process(session, await RequestAsync<UpdateEmailResponse>(GetFormDataUpdateEmail(session.SessionCookie, email)), email);
}
public static async Task<UpdateEmergencyAddressResponse> UpdateEmergencyAddressAsync(SessionInfo session, string emergencyAddress)
{
Validate(session);
if (emergencyAddress != null)
emergencyAddress = emergencyAddress.Trim();
return Process(session, await RequestAsync<UpdateEmergencyAddressResponse>(GetFormDataUpdateEmergencyAddress(session.SessionCookie, emergencyAddress)), emergencyAddress);
}
public static async Task<GetDepositAddressResponse> GetDepositAddressAsync(SessionInfo session, Currencies currency)
{
Validate(session);
return Process(session, await RequestAsync<GetDepositAddressResponse>(GetFormDataGetDepositAddress(session.SessionCookie, currency)), currency);
}
public static async Task<PlaceBetResponse> PlaceBetAsync(SessionInfo session, decimal payIn, long guessLow, long guessHigh, int clientSeed, Currencies currency)
{
Validate(session, guessLow, guessHigh, currency);
if (payIn > 0) payIn = -payIn;
return Process(session, await RequestAsync<PlaceBetResponse>(GetFormDataPlaceBet(session.SessionCookie, payIn, guessLow, guessHigh, clientSeed, currency)), payIn, guessLow, guessHigh, clientSeed, currency);
}
public static async Task<PlaceAutomatedBetsResponse> PlaceAutomatedBetsAsync(SessionInfo session, AutomatedBetsSettings settings)
{
Validate(session, settings);
return Process(session, await RequestAsync<PlaceAutomatedBetsResponse>(
GetFormDataPlaceAutomatedBets(session.SessionCookie, settings)), settings);
}
#endif
#endregion
#region Asynchronous API calls (begin/end pattern)
static DiceAsyncResult<T> QueueWork<T>(AsyncCallback callback, object state, Func<T> work) where T : DiceResponse
{
DiceAsyncResult<T> resp = new DiceAsyncResult<T>(callback, state);
ThreadPool.QueueUserWorkItem(x =>
{
try
{
resp.Complete(work());
}
catch (Exception e)
{
resp.Complete(e);
}
});
return resp;
}
static T GetWorkResult<T>(IAsyncResult i) where T : DiceResponse
{
using (DiceAsyncResult<T> res = (DiceAsyncResult<T>)i)
{
if (!res.IsCompleted)
res.AsyncWaitHandle.WaitOne();
Exception e = res.Exception;
if (e != null)
throw e;
return res.Response;
}
}
public static IAsyncResult BeginBeginSession(string apiKey, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => BeginSession(apiKey));
}
public static IAsyncResult BeginBeginSession(string apiKey, string accountCookie, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => BeginSession(apiKey, accountCookie));
}
public static IAsyncResult BeginBeginSession(string apiKey, string username, string password, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => BeginSession(apiKey, username, password));
}
public static IAsyncResult BeginBeginSession(string apiKey, string username, string password, int totp, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => BeginSession(apiKey, username, password, totp));
}
public static BeginSessionResponse EndBeginSession(IAsyncResult i)
{
return GetWorkResult<BeginSessionResponse>(i);
}
public static IAsyncResult BeginCreateUser(SessionInfo session, string username, string password, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => CreateUser(session, username, password));
}
public static CreateUserResponse EndCreateUser(IAsyncResult i)
{
return GetWorkResult<CreateUserResponse>(i);
}
public static IAsyncResult BeginGetBalance(SessionInfo session, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => GetBalance(session, currency));
}
public static GetBalanceResponse EndGetBalance(IAsyncResult i)
{
return GetWorkResult<GetBalanceResponse>(i);
}
public static IAsyncResult BeginWithdrawAll(SessionInfo session, string address, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => WithdrawAll(session, address, currency));
}
public static IAsyncResult BeginWithdrawAll(SessionInfo session, string address, int totp, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => WithdrawAll(session, address, totp, currency));
}
public static IAsyncResult BeginWithdraw(SessionInfo session, decimal amount, string address, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => Withdraw(session, amount, address, currency));
}
public static IAsyncResult BeginWithdraw(SessionInfo session, decimal amount, string address, int totp, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => Withdraw(session, amount, address, totp, currency));
}
public static WithdrawResponse EndWithdraw(IAsyncResult i)
{
return GetWorkResult<WithdrawResponse>(i);
}
public static IAsyncResult BeginChangePassword(SessionInfo session, string oldPassword, string newPassword, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => ChangePassword(session, oldPassword, newPassword));
}
public static ChangePasswordResponse EndChangePassword(IAsyncResult i)
{
return GetWorkResult<ChangePasswordResponse>(i);
}
public static IAsyncResult BeginGetServerSeedHash(SessionInfo session, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => GetServerSeedHash(session));
}
public static GetServerSeedHashResponse EndGetServerSeedHash(IAsyncResult i)
{
return GetWorkResult<GetServerSeedHashResponse>(i);
}
public static IAsyncResult BeginUpdateEmail(SessionInfo session, string email, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => UpdateEmail(session, email));
}
public static UpdateEmailResponse EndUpdateEmail(IAsyncResult i)
{
return GetWorkResult<UpdateEmailResponse>(i);
}
public static IAsyncResult BeginUpdateEmergencyAddress(SessionInfo session, string emergencyAddress, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => UpdateEmergencyAddress(session, emergencyAddress));
}
public static UpdateEmergencyAddressResponse EndUpdateEmergencyAddress(IAsyncResult i)
{
return GetWorkResult<UpdateEmergencyAddressResponse>(i);
}
public static IAsyncResult BeginGetDepositAddress(SessionInfo session, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => GetDepositAddress(session, currency));
}
public static GetDepositAddressResponse EndGetDepositAddress(IAsyncResult i)
{
return GetWorkResult<GetDepositAddressResponse>(i);
}
public static IAsyncResult BeginPlaceBet(SessionInfo session, decimal payIn, long guessLow, long guessHigh, int clientSeed, Currencies currency, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => PlaceBet(session, payIn, guessLow, guessHigh, clientSeed, currency));
}
public static PlaceBetResponse EndPlaceBet(IAsyncResult i)
{
return GetWorkResult<PlaceBetResponse>(i);
}
public static IAsyncResult BeginPlaceAutomatedBets(SessionInfo session, AutomatedBetsSettings settings, AsyncCallback complete, object state)
{
return QueueWork(complete, state, () => PlaceAutomatedBets(session, settings));
}
public static PlaceAutomatedBetsResponse EndPlaceAutomatedBets(IAsyncResult i)
{
return GetWorkResult<PlaceAutomatedBetsResponse>(i);
}
#endregion
#endregion
}
}