mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 17:58:26 -04:00
78 lines
1.8 KiB
C#
78 lines
1.8 KiB
C#
using Discord;
|
|
using NadekoBot.Common;
|
|
|
|
namespace NadekoBot.Modules.Gambling.Common;
|
|
|
|
public class CurrencyRaffleGame
|
|
{
|
|
public enum Type {
|
|
Mixed,
|
|
Normal
|
|
}
|
|
|
|
public class User
|
|
{
|
|
public IUser DiscordUser { get; set; }
|
|
public long Amount { get; set; }
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return DiscordUser.GetHashCode();
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
return obj is User u
|
|
? u.DiscordUser == DiscordUser
|
|
: false;
|
|
}
|
|
}
|
|
|
|
private readonly HashSet<User> _users = new HashSet<User>();
|
|
public IEnumerable<User> Users => _users;
|
|
public Type GameType { get; }
|
|
|
|
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 User
|
|
{
|
|
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)];
|
|
}
|
|
} |