Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeVar: Add variable RandomVar that holds a random value #121

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 55 additions & 7 deletions 7thWrapperLib/RuntimeVar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ The original developer is Iros <irosff@outlook.com>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _7thWrapperLib {

Expand Down Expand Up @@ -50,13 +49,28 @@ public enum VarType {
Random,
CounterRnd,
RandomVarOnce,
RandomVar
}

class RandomVar
{
public RandomVar(int randomValue)
{
value = randomValue;
lastGeneratedTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
locked = true;
}

public int value { get; set; }
public bool locked { get; set; }
public long lastGeneratedTimestamp { get; set; }
}

static class RuntimeVar {

private static Dictionary<string, Func<int>> _sys = new Dictionary<string, Func<int>>(StringComparer.InvariantCultureIgnoreCase);
private static Dictionary<string, int> _counters = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
private static Dictionary<string, int> _vars = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
private static Dictionary<string, RandomVar> _vars = new Dictionary<string, RandomVar>(StringComparer.InvariantCultureIgnoreCase);
private static Random _r = new Random();

static RuntimeVar() {
Expand All @@ -82,6 +96,22 @@ private static Func<bool> GetSys(string which, int size, string value, string sp
return CompareInt(_sys[which], value, spec);
}

private static Func<bool, bool> GetUnlockRandomVarWrapper(string runtimeVarSpec)
{
return isActive => {
if (isActive) {
string[] parts = runtimeVarSpec.Split(':');
if (Enum.TryParse<VarType>(parts[0], true, out VarType type)) {
if (type == VarType.RandomVar) {
if (_vars.TryGetValue(parts[1], out RandomVar randomVar))
randomVar.locked = false;
}
}
}
return isActive;
};
}

private static Func<bool> CompareInt(Func<int> reader, string value, string spec) {
Func<int, bool> compare;
int last = 0;
Expand All @@ -100,7 +130,9 @@ private static Func<bool> CompareInt(Func<int> reader, string value, string spec
return values.Contains(i);
};
}
return () => compare(reader());

Func<bool, bool> unlockRandomVarWrapper = GetUnlockRandomVarWrapper(spec);
return () => unlockRandomVarWrapper(compare(reader()));
}

public static Func<bool> MakeRuntimeVar(string spec, string value) {
Expand Down Expand Up @@ -138,11 +170,27 @@ public static Func<bool> MakeRuntimeVar(string spec, string value) {
return CompareInt(() => _r.Next(size), value, spec);
case VarType.RandomVarOnce:
return CompareInt(() => {
if (_vars.TryGetValue(parts[1], out int randomValue))
return randomValue;
if (_vars.TryGetValue(parts[1], out RandomVar randomValue))
return randomValue.value;

_vars[parts[1]] = _r.Next(size);
return _vars[parts[1]];
_vars[parts[1]] = new RandomVar(_r.Next(size));
return _vars[parts[1]].value;
}, value, spec);
case VarType.RandomVar:
return CompareInt(() => {
int timeToLiveSeconds = parts.Length > 3 ? int.Parse(parts[3]) : 0;
if (_vars.TryGetValue(parts[1], out RandomVar randomVar)) {
if (!randomVar.locked && DateTimeOffset.Now.ToUnixTimeSeconds() > randomVar.lastGeneratedTimestamp + timeToLiveSeconds) {
randomVar.lastGeneratedTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
randomVar.value = _r.Next(size);
}

randomVar.locked = true;
return randomVar.value;
}

_vars[parts[1]] = new RandomVar(_r.Next(size));
return _vars[parts[1]].value;
}, value, spec);
}

Expand Down