public static string ToJsonString(this BetCondition betCondition) { switch (betCondition) { case BetCondition.LowerThan: return("<"); case BetCondition.GreaterThan: return(">"); default: return(null); } }
/*public object GetLast30OfSite() { return null; }*/ /// <summary>Creates a new <see cref="Bet"/> on the server, or a simulated one if the current <see cref="PrimediceClient"/> is not authenticated.</summary> /// <param name="amount">Amount of satoshi to bet.</param> /// <param name="condition">Condition of the bet, relative to <paramref name="target"/>.</param> /// <param name="target">Target of the dice roll.</param> /// <returns>An awaitable <see cref="Bet"/> object if the bet was placed successfully.</returns> public async Task<Bet> Create(double amount, BetCondition condition, float target) { // Round the target automatically target = (float)Math.Round(target, 2, MidpointRounding.AwayFromZero); if (WebClient.IsAuthorized) { // Create a new bet on the server var response = await WebClient.Post<ServerResponse>("bet", new Dictionary<string, string> { ["amount"] = amount.ToString(CultureInfo.InvariantCulture), ["target"] = target.ToString(CultureInfo.InvariantCulture), ["condition"] = condition.ToJsonString() }); return response.Bet; } else { // Create a new simulated bet var bet = new Bet { IsSimulated = true, ServerSeedHashed = SimulatedSeedSet.ServerSeedHashed, ClientSeed = SimulatedSeedSet.ClientSeed, Nonce = SimulatedSeedSet.Nonce, Time = DateTime.UtcNow, Amount = amount, Condition = condition, Target = target }; // Calculate the roll of the bet var roll = bet.CalculateRoll(SimulatedSeedSet.ServerSeed); bet.Roll = roll; // Determine whether the bet was won or lost and calculate the multiplier bool isWon; float multiplier; if (condition == BetCondition.LowerThan) { isWon = roll < target; multiplier = 100 / target * (1 - Utils.HouseEdge); } else { isWon = roll > target; multiplier = 100 / (100 - target) * (1 - Utils.HouseEdge); } multiplier = (float)Math.Floor(multiplier * 100000) / 100000; // Set the properties of the bet bet.IsWon = isWon; bet.Multiplier = multiplier; if (isWon) { bet.ProfitAmount = amount * (multiplier - 1); } else { bet.ProfitAmount = -amount; } // Generate a new server seed and reset nonce to 0 if necessary if (SimulatedSeedSet.Nonce == ulong.MaxValue) { SimulatedSeedSet.Nonce = 0; var nextServerSeed = Utils.GenerateRandomServerSeed(); SimulatedSeedSet.PreviousServerSeed = SimulatedSeedSet.ServerSeed; SimulatedSeedSet.PreviousServerSeedHashed = SimulatedSeedSet.ServerSeedHashed; SimulatedSeedSet.ServerSeed = SimulatedSeedSet.NextServerSeed; SimulatedSeedSet.ServerSeedHashed = SimulatedSeedSet.NextServerSeedHashed; SimulatedSeedSet.NextServerSeed = nextServerSeed; SimulatedSeedSet.NextServerSeedHashed = nextServerSeed.GetHashed(); } else { // Increment the simulated nonce by 1 SimulatedSeedSet.Nonce += 1; } // Return the simulated bet object return bet; } }
/*public object GetLast30OfSite() * { * return null; * }*/ /// <summary>Creates a new <see cref="Bet"/> on the server, or a simulated one if the current <see cref="PrimediceClient"/> is not authenticated.</summary> /// <param name="amount">Amount of satoshi to bet.</param> /// <param name="condition">Condition of the bet, relative to <paramref name="target"/>.</param> /// <param name="target">Target of the dice roll.</param> /// <returns>An awaitable <see cref="Bet"/> object if the bet was placed successfully.</returns> public async Task <Bet> Create(double amount, BetCondition condition, float target) { // Round the target automatically target = (float)Math.Round(target, 2, MidpointRounding.AwayFromZero); if (WebClient.IsAuthorized) { // Create a new bet on the server var response = await WebClient.Post <ServerResponse>("bet", new Dictionary <string, string> { ["amount"] = amount.ToString(CultureInfo.InvariantCulture), ["target"] = target.ToString(CultureInfo.InvariantCulture), ["condition"] = condition.ToJsonString() }); return(response.Bet); } else { // Create a new simulated bet var bet = new Bet { IsSimulated = true, ServerSeedHashed = SimulatedSeedSet.ServerSeedHashed, ClientSeed = SimulatedSeedSet.ClientSeed, Nonce = SimulatedSeedSet.Nonce, Time = DateTime.UtcNow, Amount = amount, Condition = condition, Target = target }; // Calculate the roll of the bet var roll = bet.CalculateRoll(SimulatedSeedSet.ServerSeed); bet.Roll = roll; // Determine whether the bet was won or lost and calculate the multiplier bool isWon; float multiplier; if (condition == BetCondition.LowerThan) { isWon = roll < target; multiplier = 100 / target * (1 - Utils.HouseEdge); } else { isWon = roll > target; multiplier = 100 / (100 - target) * (1 - Utils.HouseEdge); } multiplier = (float)Math.Floor(multiplier * 100000) / 100000; // Set the properties of the bet bet.IsWon = isWon; bet.Multiplier = multiplier; if (isWon) { bet.ProfitAmount = amount * (multiplier - 1); } else { bet.ProfitAmount = -amount; } // Generate a new server seed and reset nonce to 0 if necessary if (SimulatedSeedSet.Nonce == ulong.MaxValue) { SimulatedSeedSet.Nonce = 0; var nextServerSeed = Utils.GenerateRandomServerSeed(); SimulatedSeedSet.PreviousServerSeed = SimulatedSeedSet.ServerSeed; SimulatedSeedSet.PreviousServerSeedHashed = SimulatedSeedSet.ServerSeedHashed; SimulatedSeedSet.ServerSeed = SimulatedSeedSet.NextServerSeed; SimulatedSeedSet.ServerSeedHashed = SimulatedSeedSet.NextServerSeedHashed; SimulatedSeedSet.NextServerSeed = nextServerSeed; SimulatedSeedSet.NextServerSeedHashed = nextServerSeed.GetHashed(); } else { // Increment the simulated nonce by 1 SimulatedSeedSet.Nonce += 1; } // Return the simulated bet object return(bet); } }