1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- public static class GameTool
- {
- //用于获取多层级Dictionary中的value
- public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
- {
- Dictionary<string, object> current = root;
- int count = 0;
- foreach (string key in path)
- {
- string rawValue = current[key].ToString();
- rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
- if (count == path.Length - 1)
- {
- return rawValue;
- }
- Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
- current = value;
- count++;
- }
- return null;
- }
- // 属性数值0-100范围内随机计算函数
- public static bool Random100Check(float inputValue, float thresHold = 0.6f)
- {
- // 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
- // 原始公式 x/(x+99)=threshold
- if (thresHold <= 0 || thresHold >= 1)
- throw new ArgumentException("threshold must be in (0, 1)");
- if (inputValue<=0 || inputValue > 100)
- {
- throw new ArgumentException("inputValue must be in (0,100)");
- }
- float factor = (99*thresHold)/(1-thresHold);
- float result = (factor + inputValue) / (factor + 100f);
- return result > thresHold;
- }
- }
|