GameTool.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. public static class GameTool
  6. {
  7. //用于获取多层级Dictionary中的value
  8. public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
  9. {
  10. Dictionary<string, object> current = root;
  11. int count = 0;
  12. foreach (string key in path)
  13. {
  14. string rawValue = current[key].ToString();
  15. rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
  16. if (count == path.Length - 1)
  17. {
  18. return rawValue;
  19. }
  20. Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
  21. current = value;
  22. count++;
  23. }
  24. return null;
  25. }
  26. // 属性数值0-100范围内随机计算函数
  27. public static bool Random100Check(float inputValue, float thresHold = 0.6f)
  28. {
  29. // 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
  30. // 原始公式 x/(x+99)=threshold
  31. if (thresHold <= 0 || thresHold >= 1)
  32. throw new ArgumentException("threshold must be in (0, 1)");
  33. if (inputValue<=0 || inputValue > 100)
  34. {
  35. throw new ArgumentException("inputValue must be in (0,100)");
  36. }
  37. float factor = (99*thresHold)/(1-thresHold);
  38. float result = (factor + inputValue) / (factor + 100f);
  39. return result > thresHold;
  40. }
  41. }