From 942a9152549e3e9acf7ba779eeb087a9d1ef36d7 Mon Sep 17 00:00:00 2001 From: PintTheDragon Date: Mon, 14 Sep 2020 16:14:36 -0700 Subject: [PATCH] Add minimum value to spawn --- EasyEvents/Commands/Spawn.cs | 16 ++++++++-------- EasyEvents/EasyEvents.cs | 2 +- EasyEvents/ScriptActions.cs | 9 +++++++-- EasyEvents/Types/SpawnData.cs | 4 +++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/EasyEvents/Commands/Spawn.cs b/EasyEvents/Commands/Spawn.cs index 4095352..05bde7b 100644 --- a/EasyEvents/Commands/Spawn.cs +++ b/EasyEvents/Commands/Spawn.cs @@ -19,11 +19,11 @@ public static void Run(List args, int i) { var argEls = args[y].Split(','); - if(argEls.Length < 1) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); + if(argEls.Length < 1) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); if (argEls.Length == 1) { - if (y != args.Count - 1 || finalClassId != -1) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); + if (y != args.Count - 1 || finalClassId != -1) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); var classId = -1; @@ -31,23 +31,23 @@ public static void Run(List args, int i) classId = roleInfo.classId; finalClassRole = roleInfo.roleID; - if(classId < 0 || classId > 17) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); + if(classId < 0 || classId > 17) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); finalClassId = classId; } - else if (argEls.Length == 2) + else if (argEls.Length == 3) { var roleInfo = RoleInfo.parseRole(argEls[0], "spawn", i, y); var classId = roleInfo.classId; string role = roleInfo.roleID; - if(!int.TryParse(argEls[1], out var chance)) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); - if(classId < 0 || classId > 17 || chance < 0 || chance > 100) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); + if(!int.TryParse(argEls[1], out var chance) || !int.TryParse(argEls[2], out var min)) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); + if(classId < 0 || classId > 17 || chance < 0 || chance > 100 || min < 0) throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); sum += chance; - classIds.Add(new SpawnData(chance, new RoleInfo(role, classId))); + classIds.Add(new SpawnData(chance, min, new RoleInfo(role, classId))); } - else throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100)\" but got \""+args[y]+"\"."); + else throw new InvalidArgumentException("Invalid argument for command \"spawn\" on line "+i+", argument "+y+". Expected \"(0-17),(0-100),(0+)\" but got \""+args[y]+"\"."); } if(sum > 100) throw new InvalidArgumentException("Invalid arguments for command \"spawn\" on line "+i+", argument. The sum of spawn chances should never exceed 100. Got "+sum+"."); diff --git a/EasyEvents/EasyEvents.cs b/EasyEvents/EasyEvents.cs index c741e93..116c2c1 100644 --- a/EasyEvents/EasyEvents.cs +++ b/EasyEvents/EasyEvents.cs @@ -9,7 +9,7 @@ public class EasyEvents : Plugin { public override string Name => "EasyEvents"; public override string Author => "PintTheDragon"; - public override Version Version => new Version("1.0.2"); + public override Version Version => new Version("1.0.3"); public override PluginPriority Priority => PluginPriority.Highest; public static EasyEvents Singleton; diff --git a/EasyEvents/ScriptActions.cs b/EasyEvents/ScriptActions.cs index 8d8c889..e5982c1 100644 --- a/EasyEvents/ScriptActions.cs +++ b/EasyEvents/ScriptActions.cs @@ -7,11 +7,14 @@ using Exiled.API.Features; using MEC; using UnityEngine; +using Random = System.Random; namespace EasyEvents { public static class ScriptActions { + private static Random random = new Random(); + public static bool eventRan = false; private static List classIds = null; @@ -155,14 +158,16 @@ private static void SetRoles() foreach (var data in classIds) { - + var num = 0; + for (var i = 0; i < players.Count; i++) { - if ((i != 0) && (i * data.chance / 100) <= ((i - 1) * data.chance / 100)) continue; + if (random.Next(0, 101) > data.chance && num > data.min) continue; players[i].SetRole(data.role.GetRole()); CustomRoles.ChangeRole(players[i], data.role.GetCustomRole()); players.RemoveAt(i); + num++; } players.Shuffle(); diff --git a/EasyEvents/Types/SpawnData.cs b/EasyEvents/Types/SpawnData.cs index 7614152..2a6fa77 100644 --- a/EasyEvents/Types/SpawnData.cs +++ b/EasyEvents/Types/SpawnData.cs @@ -3,12 +3,14 @@ public class SpawnData { public int chance = -1; + public int min = -1; public RoleInfo role = null; - public SpawnData(int c, RoleInfo r) + public SpawnData(int c, int m, RoleInfo r) { chance = c; role = r; + min = m; } } } \ No newline at end of file