forked from qrakhen/sqript-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cs
More file actions
86 lines (73 loc) · 2.81 KB
/
Random.cs
File metadata and controls
86 lines (73 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using Qrakhen.Sqript;
namespace Qrakhen.SqriptLib {
public class RandomInterface : Interface {
private Random _random;
public RandomInterface() : base("random") {
_random = new Random();
}
public override void Load() {
Define(new Call(set_seed, new string[] { "seed" }, Sqript.ValueType.Null));
Define(new Call(range, new string[] { "min", "max" }, Sqript.ValueType.Integer));
Define(new Call(rangeD, Sqript.ValueType.Decimal));
}
public QValue set_seed(Dictionary<string, QValue> parameters) {
if(!parameters.ContainsKey("seed")) {
throw new ArgumentException("The needed parameter 'seed' is missing!");
}
_random = new Random(ToInt(parameters, "seed"));
return null;
}
public QValue range(Dictionary<string, QValue> parameters) {
if(!parameters.ContainsKey("min")) {
throw new ArgumentException("The needed parameter 'min' is missing!");
}
if(!parameters.ContainsKey("max")) {
throw new ArgumentException("The needed parameter 'max' is missing!");
}
int min = ToInt(parameters, "min");
int max = ToInt(parameters, "max");
return new QValue(
_random.Next(min, max + 1),
Sqript.ValueType.Integer
);
}
public QValue rangeD() {
return new QValue(
_random.NextDouble(),
Sqript.ValueType.Decimal
);
}
#region Helper
private int ToInt(Dictionary<string, QValue> parameters, string name) {
if(parameters[name].Type != Sqript.ValueType.Integer
&& parameters[name].Type != Sqript.ValueType.Decimal
&& parameters[name].Type != Sqript.ValueType.Number
&& parameters[name].Type != Sqript.ValueType.Any
&& parameters[name].Type != Sqript.ValueType.String){
throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type);
}
try {
return int.Parse(parameters[name].Value.ToString());
} catch(FormatException) {
throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type);
}
}
private decimal ToDecimal(Dictionary<string, QValue> parameters, string name) {
if(parameters[name].Type != Sqript.ValueType.Integer
&& parameters[name].Type != Sqript.ValueType.Decimal
&& parameters[name].Type != Sqript.ValueType.Number
&& parameters[name].Type != Sqript.ValueType.Any
&& parameters[name].Type != Sqript.ValueType.String) {
throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type);
}
try {
return decimal.Parse(parameters[name].Value.ToString());
} catch(FormatException) {
throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type);
}
}
#endregion
}
}