Last active
August 29, 2015 14:25
-
-
Save pigeonhands/f27a6155f90d42ac3722 to your computer and use it in GitHub Desktop.
PrimeDiceAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
using System.Threading.Tasks; | |
/// <summary> | |
/// PrimeDice Api By BahNahNah | |
/// uid=2388291 | |
/// </summary> | |
public class PrimeDiceAPI | |
{ | |
private HttpWebRequest _httpRequest; | |
private string username, password; | |
public WebProxy Proxy { get; set; } | |
bool isLoggedin = false; | |
public string AccessToken{get; private set; } | |
#region " SubClasses" | |
public class LoginData | |
{ | |
public bool admin { get; set; } | |
public bool mod { get; set; } | |
public string access_token { get; set; } | |
} | |
public class BetInfo | |
{ | |
public string id { get; set; } | |
public string player { get; set; } | |
public string player_id { get; set; } | |
public string amount { get; set; } | |
public string target { get; set; } | |
public string profit { get; set; } | |
public bool win { get; set; } | |
public string condition { get; set; } | |
public double roll { get; set; } | |
public string nonce { get; set; } | |
public string client { get; set; } | |
public double multiplier { get; set; } | |
public string timestamp { get; set; } | |
public bool jackpot { get; set; } | |
public string server { get; set; } | |
public bool revealed { get; set; } | |
} | |
public class UserInfo | |
{ | |
public string id { get; set; } | |
public string userid { get; set; } | |
public string username { get; set; } | |
public string balance { get; set; } | |
public bool password { get; set; } | |
public object address { get; set; } | |
public string registered { get; set; } | |
public string otp_enabled { get; set; } | |
public string email_enabled { get; set; } | |
public bool address_enabled { get; set; } | |
public int wagered { get; set; } | |
public string profit { get; set; } | |
public string bets { get; set; } | |
public string wins { get; set; } | |
public string losses { get; set; } | |
public string win_risk { get; set; } | |
public string lose_risk { get; set; } | |
public string messages { get; set; } | |
public string referred { get; set; } | |
public string affiliate_total { get; set; } | |
public string nonce { get; set; } | |
public string client { get; set; } | |
public object previous_server { get; set; } | |
public object previous_client { get; set; } | |
public object previous_server_hashed { get; set; } | |
public string next_seed { get; set; } | |
public string server { get; set; } | |
} | |
public class BetData | |
{ | |
public BetInfo bet { get; set; } | |
public UserInfo user { get; set; } | |
} | |
#endregion | |
public PrimeDiceAPI(string _username, string _password) | |
{ | |
username = _username; | |
password = _password; | |
AccessToken = ""; | |
} | |
public bool Login(string Gauth) | |
{ | |
string responce = PostData("https://api.primedice.com/api/login", "username={0}&password={1}&otp={2}", username, password, Gauth); | |
if (responce == string.Empty || responce == "Unauthorized") | |
return false; | |
try | |
{ | |
LoginData ld = Deserialize<LoginData>(responce); | |
AccessToken = ld.access_token; | |
isLoggedin = true; | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
public BetData Bet(int ammountSatoshis, double winChance) | |
{ | |
if (!isLoggedin) | |
return null; | |
string responce = PostData(string.Format("https://api.primedice.com/api/bet?access_token={0}", AccessToken), "amount={0}&condition=%3C&target={1}", ammountSatoshis, winChance); | |
if (responce == string.Empty || responce == "Insufficient funds") | |
return null; | |
BetData bd = Deserialize<BetData>(responce); | |
return bd; | |
} | |
private string PostData(string url, string data, params object[] formatting) | |
{ | |
try | |
{ | |
string fStr = string.Format(data, formatting); | |
_httpRequest = (HttpWebRequest) WebRequest.Create(url); | |
_httpRequest.Method = "POST"; | |
_httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; | |
_httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"; | |
_httpRequest.Accept = "*/*"; | |
_httpRequest.UnsafeAuthenticatedConnectionSharing = true; | |
if (Proxy != null) | |
_httpRequest.Proxy = Proxy; | |
byte[] contentBytes = Encoding.UTF8.GetBytes(fStr); | |
_httpRequest.ContentLength = contentBytes.Length; | |
using (Stream _uploadStream = _httpRequest.GetRequestStream()) | |
{ | |
_uploadStream.Write(contentBytes, 0, contentBytes.Length); | |
_uploadStream.Flush(); | |
_uploadStream.Close(); | |
} | |
HttpWebResponse _responce = (HttpWebResponse) _httpRequest.GetResponse(); | |
using (StreamReader _responceStream = new StreamReader(_responce.GetResponseStream())) | |
{ | |
return _responceStream.ReadToEnd(); | |
} | |
} | |
catch(Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
return string.Empty; | |
} | |
} | |
private static T Deserialize<T>(string json) | |
{ | |
var s = new DataContractJsonSerializer(typeof(T)); | |
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) | |
{ | |
return (T)s.ReadObject(ms); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment