mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-08-07 05:58:34 -04:00
Added MathNet.Numerics for Cecil. Refactored and fixed some issues
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
using KfChatDotNetBot.Migrations;
|
||||
using MathNet.Numerics;
|
||||
using KfChatDotNetBot.Services;
|
||||
using MathNet.Numerics.Distributions;
|
||||
using RandN;
|
||||
using RandN.Compat;
|
||||
namespace KfChatDotNetBot.Commands.Kasino;
|
||||
|
||||
public static class Cecil
|
||||
{
|
||||
private static RandomShim<StandardRng> _rand = RandomShim.Create<StandardRng>(StandardRng.Create());
|
||||
public static double Consult(Skew skew, double minThreshold = 0)
|
||||
{
|
||||
double r = _rand.NextDouble();
|
||||
if (r < skew.LossRate)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
double winRate = 1 - skew.LossRate;
|
||||
double scaledR = (r - skew.LossRate) / winRate;
|
||||
|
||||
double baseResult;
|
||||
if (skew is BetaSkew betaSkew)
|
||||
{
|
||||
baseResult = Beta.InvCDF(betaSkew.Weight, betaSkew.Beta, scaledR) * betaSkew.CalibratedMaxWin;
|
||||
}
|
||||
else if (skew is GammaSkew gammaSkew)
|
||||
{
|
||||
baseResult = Gamma.InvCDF(gammaSkew.Weight, gammaSkew.Weight, scaledR);
|
||||
}
|
||||
else return 0;
|
||||
|
||||
baseResult /= winRate;
|
||||
|
||||
if (minThreshold == 0) return baseResult;
|
||||
|
||||
double limit = (skew is BetaSkew b) ? b.MaxWin : double.MaxValue;
|
||||
|
||||
return Math.Min(Round(baseResult, minThreshold), limit);
|
||||
|
||||
double Round(double baseR, double minT)
|
||||
{
|
||||
double lower = Math.Floor(baseR / minT) * minT;
|
||||
double upper = lower + minT;
|
||||
double roundChance = (baseR - lower) / minT;
|
||||
return (_rand.NextDouble() < roundChance) ? lower : upper;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Skew
|
||||
{
|
||||
public double Weight;
|
||||
public double LossRate;
|
||||
protected double TargetEv = 1;
|
||||
public abstract void Calibrate(double winRate);
|
||||
|
||||
public void Rig(double desiredEv, double lr)
|
||||
{
|
||||
TargetEv = desiredEv;
|
||||
Calibrate(1-lr);
|
||||
}
|
||||
}
|
||||
|
||||
public class BetaSkew : Skew
|
||||
{
|
||||
public readonly double MaxWin;
|
||||
public double CalibratedMaxWin;
|
||||
public double Beta;
|
||||
public double Alpha;
|
||||
|
||||
public BetaSkew(double vol, double mw, double lr)
|
||||
{
|
||||
MaxWin = mw;
|
||||
Weight = vol;
|
||||
LossRate = lr;
|
||||
Rig(1, lr);
|
||||
}
|
||||
|
||||
public override void Calibrate(double winRate)
|
||||
{
|
||||
CalibratedMaxWin = MaxWin * winRate;
|
||||
double normalizer = TargetEv / CalibratedMaxWin;
|
||||
Alpha = normalizer * Weight;
|
||||
Beta = (1 - normalizer) * Weight;
|
||||
}
|
||||
}
|
||||
|
||||
public class GammaSkew : Skew
|
||||
{
|
||||
public double Alpha;
|
||||
public double B;
|
||||
|
||||
public GammaSkew(double wght, double lr)
|
||||
{
|
||||
Weight = wght;
|
||||
LossRate = lr;
|
||||
Alpha = Math.Pow(Weight, Weight) / SpecialFunctions.Gamma(wght);
|
||||
Rig(1, lr);
|
||||
}
|
||||
|
||||
public override void Calibrate(double winRate)
|
||||
{
|
||||
B = (Weight / TargetEv) * winRate;
|
||||
}
|
||||
}
|
||||
+15
-16
@@ -4,24 +4,24 @@ using KfChatDotNetBot.Models;
|
||||
using KfChatDotNetBot.Models.DbModels;
|
||||
using KfChatDotNetBot.Services;
|
||||
using KfChatDotNetBot.Settings;
|
||||
using KfChatDotNetWsClient.Models.Events;
|
||||
using NLog;
|
||||
|
||||
namespace KfChatDotNetBot.Commands.Kasino;
|
||||
|
||||
[KasinoCommand]
|
||||
[WagerCommand]
|
||||
public class CecilCommand : ICommand
|
||||
{
|
||||
public List<Regex> Patterns => [
|
||||
new Regex(@"^cecil (?<bet>\d+(?:\.\d+)?) (?<difficulty>\d+(?:\.\d+)?) (?<maxwin>\d+(?:\.\d+)?)", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^cecil (?<bet>\d+(?:\.\d+)?) (?<difficulty>\d+(?:\.\d+)?)", RegexOptions.IgnoreCase),
|
||||
new Regex(@"^cecil (?<bet>\d+(?:\.\d+)?)", RegexOptions.IgnoreCase),
|
||||
new Regex("^keno")
|
||||
new Regex("^cecil")
|
||||
];
|
||||
|
||||
public string? HelpText => "!cecil <bet> <optional difficulty> <optional max win>";
|
||||
public UserRight RequiredRight => UserRight.Loser;
|
||||
public TimeSpan Timeout => TimeSpan.FromSeconds(60);
|
||||
public RateLimitOptionsModel? RateLimitOptions => new RateLimitOptionsModel
|
||||
public RateLimitOptionsModel? RateLimitOptions => new()
|
||||
{
|
||||
MaxInvocations = 5,
|
||||
Window = TimeSpan.FromSeconds(10)
|
||||
@@ -41,7 +41,7 @@ public class CecilCommand : ICommand
|
||||
|
||||
if (!arguments.TryGetValue("bet", out var amount)) //if user just enters !keno
|
||||
{
|
||||
await botInstance.SendChatMessageAsync(
|
||||
await botInstance.ReplyToUser(message,
|
||||
$"{user.FormatUsername()}, not enough arguments. !cecil <bet> <optional difficulty> <[i]optional max win > 1[/i] - Cecil Tool: https://i.ddos.lgbt/raw/CecilHelper.html>",
|
||||
true, autoDeleteAfter: cleanupDelay);
|
||||
RateLimitService.RemoveMostRecentEntry(user, this);
|
||||
@@ -54,14 +54,13 @@ public class CecilCommand : ICommand
|
||||
throw new InvalidOperationException($"Caught a null when retrieving gambler for {user.KfUsername}");
|
||||
if (gambler.Balance < wager)
|
||||
{
|
||||
await botInstance.SendChatMessageAsync(
|
||||
await botInstance.ReplyToUser(message,
|
||||
$"{user.FormatUsername()}, your balance of {await gambler.Balance.FormatKasinoCurrencyAsync()} isn't enough for this wager.",
|
||||
true, autoDeleteAfter: cleanupDelay);
|
||||
RateLimitService.RemoveMostRecentEntry(user, this);
|
||||
return;
|
||||
}
|
||||
|
||||
bool beta;
|
||||
double difficulty;
|
||||
|
||||
double result;
|
||||
@@ -76,32 +75,32 @@ public class CecilCommand : ICommand
|
||||
|
||||
if (!arguments.TryGetValue("maxwin", out var maxWin))
|
||||
{
|
||||
GammaSkew skew = new GammaSkew(difficulty, 0);
|
||||
var skew = new GammaSkew(difficulty, 0);
|
||||
result = Cecil.Consult(skew, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
double mWin = Convert.ToDouble(maxWin.Value);
|
||||
var mWin = Convert.ToDouble(maxWin.Value);
|
||||
if (mWin < 1)
|
||||
{
|
||||
await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, max win must be greater than 1.", true, autoDeleteAfter: cleanupDelay);
|
||||
await botInstance.ReplyToUser(message, $"{user.FormatUsername()}, max win must be greater than 1.", true, autoDeleteAfter: cleanupDelay);
|
||||
return;
|
||||
}
|
||||
BetaSkew skew = new BetaSkew(difficulty, mWin, 0);
|
||||
result = Cecil.Consult(skew, 0);
|
||||
var skew = new BetaSkew(difficulty, mWin, 0);
|
||||
result = Cecil.Consult(skew);
|
||||
}
|
||||
|
||||
var payout = wager * Convert.ToDecimal(result);
|
||||
var net = payout - wager;
|
||||
var newBalance = await Money.NewWagerAsync(gambler.Id, wager, net, WagerGame.Cecil);
|
||||
var newBalance = await Money.NewWagerAsync(gambler.Id, wager, net, WagerGame.Cecil, ct: ctx);
|
||||
var colors =
|
||||
await SettingsProvider.GetMultipleValuesAsync([
|
||||
BuiltIn.Keys.KiwiFarmsGreenColor, BuiltIn.Keys.KiwiFarmsRedColor
|
||||
]);
|
||||
var red = $"{colors[BuiltIn.Keys.KiwiFarmsRedColor].Value}";
|
||||
var green = $"{colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}";
|
||||
var red = colors[BuiltIn.Keys.KiwiFarmsRedColor].Value;
|
||||
var green = colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value;
|
||||
var color = (payout > wager) ? green : red;
|
||||
await botInstance.SendChatMessageAsync(
|
||||
await botInstance.ReplyToUser(message,
|
||||
$"{user.FormatUsername()}, Cecil has determined you are due [color={color}]{await payout.FormatKasinoCurrencyAsync()}[/color] from your wager of {await wager.FormatKasinoCurrencyAsync()}. Balance: {await newBalance.FormatKasinoCurrencyAsync()}",
|
||||
true, autoDeleteAfter: cleanupDelay);
|
||||
|
||||
Reference in New Issue
Block a user