Require.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using DeepCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. namespace DeepMMO.Server.Require
  6. {
  7. public class Require
  8. {
  9. /// <summary>
  10. /// 弃用的
  11. /// </summary>
  12. /// <param name="key"></param>
  13. /// <param name="min"></param>
  14. /// <param name="max"></param>
  15. /// <returns></returns>
  16. public delegate Tuple<bool, int> CheckHandler(string key, int min, int max);
  17. public delegate Task<Tuple<bool, int>> CheckHandlerAsync(string key, int min, int max);
  18. HashMap<string, CheckHandler> map = new HashMap<string, CheckHandler>();
  19. HashMap<string, CheckHandlerAsync> mapAsync = new HashMap<string, CheckHandlerAsync>();
  20. public void RegistHandler(string key, CheckHandler handler)
  21. {
  22. try
  23. {
  24. map.Add(key, handler);
  25. }
  26. catch (Exception e)
  27. {
  28. string err = string.Format("RegistHandler Error Key = [{0}] {1}", key, e.ToString());
  29. throw new Exception(err);
  30. }
  31. }
  32. public void RegistHandler(string key, CheckHandlerAsync handler)
  33. {
  34. try
  35. {
  36. mapAsync.Add(key, handler);
  37. }
  38. catch (Exception e)
  39. {
  40. string err = string.Format("RegistHandler Error Key = [{0}] {1}", key, e.ToString());
  41. throw new Exception(err);
  42. }
  43. }
  44. public void UnRegistHandler(string key)
  45. {
  46. map.Remove(key);
  47. mapAsync.Remove(key);
  48. }
  49. public void Dispose()
  50. {
  51. map.Clear();
  52. mapAsync.Clear();
  53. }
  54. public bool CheckRequire(RequireData data, out string reason)
  55. {
  56. reason = null;
  57. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  58. return true;
  59. string[] key = data.Key;
  60. int[] min = data.Minval;
  61. int[] max = data.Maxval;
  62. string[] text = data.Text;
  63. CheckHandler handler;
  64. string prefix = null;
  65. string k = null;
  66. for (int i = 0; i < key.Length; i++)
  67. {
  68. try
  69. {
  70. k = key[i];
  71. if (string.IsNullOrEmpty(k) || k == "0")
  72. continue;
  73. prefix = k.Substring(0, 1);
  74. k = k.Substring(1);
  75. handler = map.Get(prefix);
  76. if (handler == null)
  77. return false;
  78. var tuple = handler.Invoke(k, min[i], max[i]);
  79. if (tuple.Item1 == false)
  80. {
  81. if (text != null && i < text.Length)
  82. reason = text[i];
  83. return false;
  84. }
  85. }
  86. catch (Exception)
  87. {
  88. string info = string.Format("checkRequire Error key={0},min={1},max={2},reason{3}",
  89. key[i], min[i], max[i]);
  90. throw new Exception(info);
  91. }
  92. }
  93. return true;
  94. }
  95. public bool CheckRequireExceptNullHander(RequireData data, out string reason)
  96. {
  97. reason = null;
  98. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  99. return true;
  100. string[] key = data.Key;
  101. int[] min = data.Minval;
  102. int[] max = data.Maxval;
  103. string[] text = data.Text;
  104. CheckHandler handler;
  105. string prefix = null;
  106. string k = null;
  107. for (int i = 0; i < key.Length; i++)
  108. {
  109. try
  110. {
  111. k = key[i];
  112. if (string.IsNullOrEmpty(k) || k == "0")
  113. continue;
  114. prefix = k.Substring(0, 1);
  115. k = k.Substring(1);
  116. handler = map.Get(prefix);
  117. if (handler == null)
  118. continue;
  119. var tuple = handler.Invoke(k, min[i], max[i]);
  120. if (tuple.Item1 == false)
  121. {
  122. if (text != null && i < text.Length)
  123. reason = text[i];
  124. return false;
  125. }
  126. }
  127. catch (Exception)
  128. {
  129. string info = string.Format("checkRequire Error key={0},min={1},max={2},reason{3}",
  130. key[i], min[i], max[i]);
  131. throw new Exception(info);
  132. }
  133. }
  134. return true;
  135. }
  136. public async Task<RequireResult> CheckRequireAsync(RequireData data)
  137. {
  138. RequireResult result = new RequireResult();
  139. result.Result = true;
  140. result.Reason = string.Empty;
  141. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  142. {
  143. return result;
  144. }
  145. string[] key = data.Key;
  146. int[] min = data.Minval;
  147. int[] max = data.Maxval;
  148. string[] text = data.Text;
  149. CheckHandlerAsync handler;
  150. string prefix = null;
  151. string k = null;
  152. for (int i = 0; i < key.Length; i++)
  153. {
  154. try
  155. {
  156. k = key[i];
  157. if (string.IsNullOrEmpty(k) || k == "0")
  158. {
  159. return result;
  160. }
  161. prefix = k.Substring(0, 1);
  162. k = k.Substring(1);
  163. handler = mapAsync.Get(prefix);
  164. if (handler == null)
  165. {
  166. result.Result = false;
  167. return result;
  168. }
  169. var tuple = await handler.Invoke(k, min[i], max[i]);
  170. if (tuple.Item1 == false)
  171. {
  172. if (text != null && i < text.Length)
  173. {
  174. result.Reason = text[i];
  175. }
  176. result.Result = false;
  177. return result;
  178. }
  179. }
  180. catch (Exception)
  181. {
  182. string info = string.Format("checkRequire Error key={0},min={1},max={2},reason{3}",
  183. key[i], min[i], max[i]);
  184. throw new Exception(info);
  185. }
  186. }
  187. return result;
  188. }
  189. public void CheckRequire(RequireData data, ref List<RequireResult> resultlist)
  190. {
  191. RequireResult rr = null;
  192. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  193. {
  194. rr = new RequireResult();
  195. rr.Result = true;
  196. resultlist.Add(rr);
  197. return;
  198. }
  199. string[] key = data.Key;
  200. int[] min = data.Minval;
  201. int[] max = data.Maxval;
  202. string[] text = data.Text;
  203. CheckHandler handler;
  204. string prefix = null;
  205. string k = null;
  206. for (int i = 0; i < key.Length; i++)
  207. {
  208. try
  209. {
  210. k = key[i];
  211. if (string.IsNullOrEmpty(k) || k == "0")
  212. {
  213. continue;
  214. }
  215. rr = new RequireResult();
  216. prefix = k.Substring(0, 1);
  217. k = k.Substring(1);
  218. handler = map.Get(prefix);
  219. if (handler == null)
  220. {
  221. rr.Result = false;
  222. rr.Reason = "can not find require handler";
  223. }
  224. else
  225. {
  226. var ret = handler.Invoke(k, min[i], max[i]);
  227. rr.Result = ret.Item1;
  228. rr.CurVal = ret.Item2;
  229. rr.MaxVal = max[i];
  230. rr.MinVal = min[i];
  231. if (text != null)
  232. rr.Reason = text[i];
  233. }
  234. }
  235. catch (Exception)
  236. {
  237. string info = string.Format("checkRequire Error key={0},min={1},max={2},reason{3}",
  238. key[i], min[i], max[i], text[i]);
  239. throw new Exception(info);
  240. }
  241. resultlist.Add(rr);
  242. }
  243. }
  244. public bool CheckDetailRequireByAppendCount(RequireData data, int index, int count, out string reason)
  245. {
  246. reason = null;
  247. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  248. return true;
  249. if (data.Key.Length <= index)
  250. return false;
  251. string[] key = data.Key;
  252. int[] min = data.Minval;
  253. int[] max = data.Maxval;
  254. string[] text = data.Text;
  255. CheckHandler handler;
  256. string k = key[index];
  257. if (string.IsNullOrEmpty(k) || k == "0")
  258. return true;
  259. string prefix = k.Substring(0, 1);
  260. handler = map.Get(prefix);
  261. if (handler == null)
  262. return false;
  263. var tuple = handler.Invoke(k, min[index], max[index]);
  264. if (tuple.Item1 == false)
  265. {
  266. if (text != null && index < text.Length)
  267. reason = text[index];
  268. return false;
  269. }
  270. else
  271. {
  272. var checkVal = tuple.Item2 + count;
  273. if (max[index] == -1)
  274. {
  275. return (checkVal >= min[index]);
  276. }
  277. else
  278. {
  279. return checkVal >= min[index] && checkVal < max[index];
  280. }
  281. }
  282. }
  283. public async Task<RequireResult> CheckDetailRequireByAppendCountAsync(RequireData data, int index, int count)
  284. {
  285. RequireResult result = new RequireResult();
  286. result.Result = true;
  287. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  288. {
  289. return result;
  290. }
  291. if (data.Key.Length <= index)
  292. {
  293. result.Result = false;
  294. return result;
  295. }
  296. string[] key = data.Key;
  297. int[] min = data.Minval;
  298. int[] max = data.Maxval;
  299. string[] text = data.Text;
  300. CheckHandlerAsync handler;
  301. string k = key[index];
  302. if (string.IsNullOrEmpty(k) || k == "0")
  303. {
  304. return result;
  305. }
  306. string prefix = k.Substring(0, 1);
  307. handler = mapAsync.Get(prefix);
  308. if (handler == null)
  309. {
  310. result.Result = false;
  311. return result;
  312. }
  313. var tuple = await handler.Invoke(k, min[index], max[index]);
  314. if (tuple.Item1 == false)
  315. {
  316. result.Result = false;
  317. if (text != null && index < text.Length)
  318. result.Reason = text[index];
  319. return result;
  320. }
  321. else
  322. {
  323. var checkVal = tuple.Item2 + count;
  324. if (max[index] == -1)
  325. {
  326. if (checkVal < min[index])
  327. {
  328. result.Result = false;
  329. if (text != null && index < text.Length)
  330. result.Reason = text[index];
  331. }
  332. }
  333. else
  334. {
  335. if (!(checkVal >= min[index] && checkVal < max[index]))
  336. {
  337. result.Result = false;
  338. if (text != null && index < text.Length)
  339. result.Reason = text[index];
  340. }
  341. }
  342. return result;
  343. }
  344. }
  345. public async Task<List<RequireResult>> CheckRequireListAsync(RequireData data)
  346. {
  347. List<RequireResult> resultlist = new List<RequireResult>();
  348. RequireResult rr = null;
  349. if (data == null || data.Key == null || data.Maxval == null || data.Minval == null)
  350. {
  351. rr = new RequireResult();
  352. rr.Result = true;
  353. rr.Result = true;
  354. resultlist.Add(rr);
  355. return resultlist;
  356. }
  357. string[] key = data.Key;
  358. int[] min = data.Minval;
  359. int[] max = data.Maxval;
  360. string[] text = data.Text;
  361. CheckHandlerAsync handler;
  362. string prefix = null;
  363. string k = null;
  364. for (int i = 0; i < key.Length; i++)
  365. {
  366. try
  367. {
  368. k = key[i];
  369. if (string.IsNullOrEmpty(k) || k == "0")
  370. {
  371. continue;
  372. }
  373. rr = new RequireResult();
  374. prefix = k.Substring(0, 1);
  375. k = k.Substring(1);
  376. handler = mapAsync.Get(prefix);
  377. if (handler == null)
  378. {
  379. rr.Result = false;
  380. rr.Reason = "can not find require handler";
  381. }
  382. else
  383. {
  384. var ret = await handler.Invoke(k, min[i], max[i]);
  385. rr.Result = ret.Item1;
  386. rr.CurVal = ret.Item2;
  387. rr.MaxVal = max[i];
  388. rr.MinVal = min[i];
  389. if (text != null)
  390. rr.Reason = text[i];
  391. }
  392. }
  393. catch (Exception)
  394. {
  395. string info = string.Format("checkRequire Error key={0},min={1},max={2},reason{3}",
  396. key[i], min[i], max[i], text[i]);
  397. throw new Exception(info);
  398. }
  399. resultlist.Add(rr);
  400. }
  401. return resultlist;
  402. }
  403. public class RequireResult
  404. {
  405. public bool Result;
  406. public int CurVal;
  407. public int MinVal;
  408. public int MaxVal;
  409. public string Reason;
  410. }
  411. public class RequireData
  412. {
  413. public string[] Key;
  414. public int[] Minval;
  415. public int[] Maxval;
  416. public string[] Text;
  417. }
  418. }
  419. }