|
| 1 | +using Exiled.API.Features; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.Remoting.Messaging; |
| 6 | + |
| 7 | +namespace UncomplicatedCustomTeams.API.Features |
| 8 | +{ |
| 9 | + public class SummonedTeam |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Gets a list of every spawned <see cref="Team"/> as <see cref="SummonedTeam"/> |
| 13 | + /// </summary> |
| 14 | + public static List<SummonedTeam> List { get; } = new(); |
| 15 | + |
| 16 | + public string Id { get; } |
| 17 | + |
| 18 | + public List<SummonedCustomRole> Players { get; } = new(); |
| 19 | + |
| 20 | + public Team Team { get; } |
| 21 | + |
| 22 | + public long Time { get; } |
| 23 | + |
| 24 | + public SummonedTeam(Team team) |
| 25 | + { |
| 26 | + Team = team; |
| 27 | + Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 28 | + Id = Guid.NewGuid().ToString(); |
| 29 | + |
| 30 | + List.Add(this); |
| 31 | + } |
| 32 | + |
| 33 | + public void Destroy() |
| 34 | + { |
| 35 | + foreach (SummonedCustomRole role in Players) { role.Destroy(); } |
| 36 | + Players.Clear(); |
| 37 | + List.Remove(this); |
| 38 | + } |
| 39 | + |
| 40 | + public static SummonedTeam Summon(Team team, IEnumerable<Player> players) |
| 41 | + { |
| 42 | + SummonedTeam SummonedTeam = new(team); |
| 43 | + |
| 44 | + foreach (Player Player in players) |
| 45 | + { |
| 46 | + foreach (CustomRole Role in team.Roles) |
| 47 | + { |
| 48 | + if (SummonedTeam.SummonedPlayersCount(Role) < Role.MaxPlayers) |
| 49 | + { |
| 50 | + SummonedTeam.Players.Add(new(SummonedTeam, Player, Role)); |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return SummonedTeam; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public int SummonedPlayersCount(CustomRole role) |
| 61 | + { |
| 62 | + return Players.Where(cr => cr.CustomRole == role).Count(); |
| 63 | + } |
| 64 | + |
| 65 | + public IEnumerable<SummonedCustomRole> SummonedPlayersGet(CustomRole role) => Players.Where(cr => cr.CustomRole == role); |
| 66 | + |
| 67 | + public SummonedCustomRole SummonedPlayersGet(Player player) => Players.Where(cr => cr.Player.Id == player.Id).FirstOrDefault(); |
| 68 | + |
| 69 | + public bool SummonedPlayersTryGet(Player player, out SummonedCustomRole role) |
| 70 | + { |
| 71 | + role = SummonedPlayersGet(player); |
| 72 | + return role != null; |
| 73 | + } |
| 74 | + |
| 75 | + public void TrySpawnPlayer(Player player) => SummonedPlayersGet(player)?.AddRole(); |
| 76 | + |
| 77 | + public static SummonedTeam Get(string Id) => List.Where(st => st.Id == Id).FirstOrDefault(); |
| 78 | + |
| 79 | + public static bool TryGet(string Id, out SummonedTeam team) |
| 80 | + { |
| 81 | + team = Get(Id); |
| 82 | + return team != null; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments