Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Add minimum value to spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Sep 14, 2020
1 parent 493a40b commit 942a915
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
16 changes: 8 additions & 8 deletions EasyEvents/Commands/Spawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ public static void Run(List<string> 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;

var roleInfo = RoleInfo.parseRole(argEls[0], "spawn", i, y);
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+".");
Expand Down
2 changes: 1 addition & 1 deletion EasyEvents/EasyEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EasyEvents : Plugin<Config>
{
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;
Expand Down
9 changes: 7 additions & 2 deletions EasyEvents/ScriptActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpawnData> classIds = null;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion EasyEvents/Types/SpawnData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 942a915

Please sign in to comment.