vrobot3 is a chat bot for IRC and Discord.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.4KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Useful utilities.
  10. //
  11. //-----------------------------------------------------------------------------
  12. using System;
  13. using System.Net;
  14. using System.Text;
  15. using System.Collections.Generic;
  16. namespace ProjectGolan.Vrobot3
  17. {
  18. //
  19. // Utils
  20. //
  21. public static partial class Utils
  22. {
  23. private static long RNDHash = 0x7f083dfd7f083dfd;
  24. //
  25. // List.AddItem
  26. //
  27. public static T AddItem<T>(this List<T> list, T item)
  28. {
  29. list.Add(item);
  30. return item;
  31. }
  32. //
  33. // GetRND
  34. //
  35. public static Random GetRND()
  36. {
  37. RNDHash *= DateTime.UtcNow.ToFileTime();
  38. var rnd = new Random(unchecked((int)(RNDHash & 0x7fffffff)));
  39. RNDHash ^= 0x7f8f8f8f8f8f8f8f;
  40. RNDHash >>= 4;
  41. RNDHash += 0x7f0000007f000000;
  42. return rnd;
  43. }
  44. //
  45. // GetArguments
  46. //
  47. public static String[] GetArguments(String msg, String help, int min,
  48. int max = 0, char splitchr = ',')
  49. {
  50. char[] splitseq = { splitchr };
  51. String[] split;
  52. if(min == 1 && msg == String.Empty)
  53. throw new CommandArgumentException(help);
  54. split = max == 0 ? msg.Split(splitseq) : msg.Split(splitseq, max);
  55. if(min >= 0 && split.Length < min)
  56. throw new CommandArgumentException(help);
  57. return split;
  58. }
  59. //
  60. // SetRange
  61. //
  62. public static double SetRange(double x, double min, double max)
  63. => ((max - min) * x) + min;
  64. //
  65. // FuzzyRelativeDate
  66. //
  67. public static String FuzzyRelativeDate(DateTime then, DateTime now)
  68. {
  69. var span = now.Subtract(then);
  70. if(span.Seconds == 0)
  71. return "now";
  72. var denom = span.Days > 0 ? "day" :
  73. span.Hours > 0 ? "hour" :
  74. span.Minutes > 0 ? "minute" :
  75. "second";
  76. int number;
  77. switch(denom)
  78. {
  79. default: number = 0; break;
  80. case "second": number = span.Seconds; break;
  81. case "minute": number = span.Minutes; break;
  82. case "hour": number = span.Hours; break;
  83. case "day": number = span.Days; break;
  84. }
  85. return $"{number} {denom}{number != 1 ? "s" : String.Empty} ago";
  86. }
  87. //
  88. // FuzzyRelativeDate
  89. //
  90. public static String FuzzyRelativeDate(DateTime then)
  91. => FuzzyRelativeDate(then, DateTime.Now);
  92. //
  93. // GetResponseString
  94. //
  95. public static String GetResponseString(WebResponse resp, int maxsize)
  96. {
  97. try
  98. {
  99. var bufp = new byte[maxsize];
  100. var read = 0;
  101. using(var stream = resp.GetResponseStream())
  102. if(stream != null)
  103. read = stream.Read(bufp, 0, maxsize);
  104. return Encoding.Default.GetString(bufp, 0, read);
  105. }
  106. catch(Exception exc)
  107. {
  108. Console.WriteLine("URL request error: {0}",
  109. exc.Message ?? "[unknown]");
  110. return null;
  111. }
  112. }
  113. //
  114. // GetResponseString
  115. //
  116. public static String GetResponseString(String uri, int maxsize,
  117. String referer = null)
  118. {
  119. try
  120. {
  121. var req = WebRequest.Create(uri);
  122. if(referer != null)
  123. {
  124. var req_ = req as HttpWebRequest;
  125. req_.Referer = referer;
  126. }
  127. using(var resp = req.GetResponse())
  128. return GetResponseString(resp, maxsize);
  129. }
  130. catch(Exception exc)
  131. {
  132. Console.WriteLine("URL request error: {0}",
  133. exc.Message ?? "[unknown]");
  134. return null;
  135. }
  136. }
  137. //
  138. // TryParse
  139. //
  140. public static void TryParse(String str, String err, out double outp)
  141. {
  142. if(!double.TryParse(str, out outp))
  143. throw new CommandArgumentException(err);
  144. }
  145. //
  146. // TryParse
  147. //
  148. public static void TryParse(String str, String err, out int outp)
  149. {
  150. if(!int.TryParse(str, out outp))
  151. throw new CommandArgumentException(err);
  152. }
  153. }
  154. }
  155. // EOF