mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
#nullable disable
|
|
namespace NadekoBot.Modules.Gambling.Common;
|
|
|
|
public class CurrencyRaffleGame
|
|
{
|
|
public enum Type
|
|
{
|
|
Mixed,
|
|
Normal
|
|
}
|
|
|
|
public IEnumerable<User> Users
|
|
=> _users;
|
|
|
|
public Type GameType { get; }
|
|
|
|
private readonly HashSet<User> _users = new();
|
|
|
|
public CurrencyRaffleGame(Type type)
|
|
=> GameType = type;
|
|
|
|
public bool AddUser(IUser usr, long amount)
|
|
{
|
|
// if game type is normal, and someone already joined the game
|
|
// (that's the user who created it)
|
|
if (GameType == Type.Normal && _users.Count > 0 && _users.First().Amount != amount)
|
|
return false;
|
|
|
|
if (!_users.Add(new() { DiscordUser = usr, Amount = amount }))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public User GetWinner()
|
|
{
|
|
var rng = new NadekoRandom();
|
|
if (GameType == Type.Mixed)
|
|
{
|
|
var num = rng.NextLong(0L, Users.Sum(x => x.Amount));
|
|
var sum = 0L;
|
|
foreach (var u in Users)
|
|
{
|
|
sum += u.Amount;
|
|
if (sum > num)
|
|
return u;
|
|
}
|
|
}
|
|
|
|
var usrs = _users.ToArray();
|
|
return usrs[rng.Next(0, usrs.Length)];
|
|
}
|
|
|
|
public class User
|
|
{
|
|
public IUser DiscordUser { get; set; }
|
|
public long Amount { get; set; }
|
|
|
|
public override int GetHashCode()
|
|
=> DiscordUser.GetHashCode();
|
|
|
|
public override bool Equals(object obj)
|
|
=> obj is User u ? u.DiscordUser == DiscordUser : false;
|
|
}
|
|
} |