using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EggLink.DanhengServer.Util { /// /// A list that can be used to randomly select an element with a certain weight from it. /// /// public class RandomList { private readonly List _List = []; public RandomList() { } public RandomList(IEnumerable collection) { _List.AddRange(collection); } public void Add(T item, int weight) { for (int i = 0; i < weight; i++) { _List.Add(item); } } public void Remove(T item) { var temp = _List.ToList(); _List.Clear(); foreach (var i in temp) { if (i?.Equals(item) == false) { _List.Add(i); } } } public void AddRange(IEnumerable collection, IEnumerable weights) { var list = collection.ToList(); for (int i = 0; i < list.Count; i++) { Add(list[i], weights.ElementAt(i)); } } public T? GetRandom() { if (_List.Count == 0) { return default; } return _List[Random.Shared.Next(_List.Count)]; } public void Clear() { _List.Clear(); } public int GetCount() { return _List.Count; } } }