txlyre 23 ore în urmă
părinte
comite
36a34d5f3b
1 a modificat fișierele cu 17 adăugiri și 2 ștergeri
  1. 17 2
      rand.py

+ 17 - 2
rand.py

@@ -1,6 +1,7 @@
 import re
 import random
 import struct
+import inspect
 import operator
 
 import aiohttp
@@ -9,6 +10,7 @@ import aiohttp
 async def _pyrandom(min_val, max_val, is_dice=False):
     if is_dice:
         return [random.randint(1, max_val) for _ in range(min_val)]
+
     return random.randint(min_val, max_val)
 
 
@@ -54,6 +56,7 @@ async def generate(source, min_val, max_val, count=1):
 
     if count == 1:
         return [await _pyrandom(min_val, max_val)]
+
     return await _pyrandom(min_val, max_val, is_dice=True)
 
 
@@ -69,6 +72,13 @@ async def _trng_yebisu(min_val, max_val, is_dice=False):
     return numbers if is_dice else numbers[0]
 
 
+async def randint(min_val, max_val):
+    try:
+        return await _trng_yebisu(min_val, max_val)
+    except Exception:
+        return await _pyrandom(min_val, max_val)
+
+
 async def rolldices(count, sides):
     try:
         return await _trng_yebisu(count, sides, is_dice=True)
@@ -93,8 +103,10 @@ OPS = {
     "/": operator.truediv,
     "%": operator.mod,
     "^": operator.pow,
+    "..": randint,
 }
-OPS_KEYS = "".join(OPS.keys()).replace("-", r"\-")
+
+OPS_KEYS = "|".join(map(lambda k: "(" + k.replace("-", r"\-").replace(".", r"\.") + ")", OPS.keys()))
 
 T_NAME = re.compile(r"([abce-zа-ге-йл-яA-ZА-Я]+)")
 T_COLON = re.compile(r"(:)")
@@ -301,7 +313,10 @@ class Dices:
             op = OPS[op[0]]
             right = await self._parse_expr()
 
-            left = left.apply(op, right)
+            if inspect.iscoroutinefunction(op):
+                left = Value(await op(int(left), int(right)))
+            else:
+                left = left.apply(op, right)
         elif self._match(T_COLON):
             right = self._expect(T_NAME)[0].upper()