aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tests/examples/ndice/src/dicer.py
blob: 74bb371457be7eb5d3a31597758f7d3f9f71c3b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from multilogging import multilogger
from typing import List

import abc
import re
import random


ZERO = 0
EMPTY_STRING = ""
EMPTY_LIST = []

logger = multilogger(name="DicerGirl", payload="Dicer")


class BaseDice:
    def __init__(self, roll_string: str = EMPTY_STRING) -> None:
        self.roll_string = roll_string
        self.db = EMPTY_STRING
        self.outcome = ZERO
        self.display = EMPTY_LIST

    def __repr__(self) -> str:
        return self.db.upper()

    @abc.abstractmethod
    def parse(self) -> "BaseDice":
        raise NotImplementedError

    @abc.abstractmethod
    def roll(self) -> int:
        """对骰子进行投掷并给出结果"""
        raise NotImplementedError


class DigitDice(BaseDice):
    """数字骰"""

    def __init__(self, roll_string: str = EMPTY_STRING) -> None:
        super().__init__(roll_string=roll_string)
        if not roll_string.isdigit():
            raise ValueError

        self.parse()

    def parse(self) -> "DigitDice":
        self.a = int(self.roll_string)
        self.b = 1
        self.db = f"{self.a}"
        return self

    def roll(self) -> int:
        self.outcome = self.a
        self.display = [self.a]
        return self.outcome


class Dice(BaseDice):
    """多面骰"""

    def __init__(self, roll_string: str = "", explode: bool = False) -> None:
        super().__init__(roll_string=roll_string)
        self.dices = EMPTY_LIST
        self.great = False
        self.explode = explode
        self.parse()

    def parse(self) -> "Dice":
        self.dices = []
        split = re.split(r"[dD]", self.roll_string)

        if split[0]:
            self.a = int(split[0])
        else:
            self.a = 1

        if split[1]:
            self.b = int(split[1])
        else:
            self.b = 100

        self.db = f"{self.a}D{self.b}"
        self.dices += [f"D{self.b}"] * self.a
        return self

    def roll(self) -> int:
        self.results = []
        self.display = []

        for _ in range(self.a):
            result = random.randint(1, self.b)

            if result == 1 and self.explode:
                result -= 1

            if self.explode and self.b == 8:
                self.dices.append("D10")
                result2 = random.randint(1, 10)
                if result2 == 1:
                    result -= 1
                result += result2
                if result2 == 10:
                    self.dices.append("D12")
                    result3 = random.randint(1, 12)
                    if result3 == 1:
                        result -= 1
                    result += result3
                    if result3 == 12:
                        self.dices.append("D20")
                        result4 = random.randint(1, 20)
                        if result4 == 1:
                            result -= 1
                        result += result4
                        if result4 == 20:
                            self.great = True

            self.results.append(result)
            self.display.append(result)

        self.outcome = sum(self.results)
        return self.outcome


class AwardDice(BaseDice):
    """奖励骰"""

    def __init__(self, roll_string: str = "") -> None:
        super().__init__(roll_string=roll_string)
        self.parse()

    def parse(self) -> "AwardDice":
        split = re.split(r"[bB]", self.roll_string)

        if split[0]:
            self.a = int(split[0])
        else:
            self.a = 1

        self.b = int(split[1])
        self.db = f"{self.a}B{self.b}"
        return self

    def roll(self) -> int:
        self.results = []
        self.display = []

        for _ in range(self.a):
            ten = []
            for _ in range(self.b):
                outcome = Dice("1d10").roll()
                outcome = outcome if outcome != 10 else 0
                ten.append(outcome)

            result = Dice("1d100").roll()
            ten.append(result // 10)
            minten = min(ten)
            ten.remove(result // 10)
            outcome = minten * 10 + (result % 10)
            self.results.append(outcome)
            self.display.append([result, ten])

        self.outcome = sum(self.results)
        return self.outcome


class PunishDice(BaseDice):
    """惩罚骰"""

    def __init__(self, roll_string: str = "") -> None:
        super().__init__(roll_string=roll_string)
        self.parse()

    def parse(self) -> "PunishDice":
        split = re.split(r"[pP]", self.roll_string)

        if split[0]:
            self.a = int(split[0])
        else:
            self.a = 1

        self.b = int(split[1])
        self.db = f"{self.a}P{self.b}"
        return self

    def roll(self) -> int:
        self.results = []
        self.display = []

        for _ in range(self.a):
            ten = []
            for _ in range(self.b):
                outcome = Dice("1d10").roll()
                outcome = outcome if outcome != 10 else 0
                ten.append(outcome)

            result = Dice("1d100").roll()
            ten.append(result // 10)
            maxten = max(ten)
            ten.remove(result // 10)
            outcome = maxten * 10 + (result % 10)
            self.results.append(outcome)
            self.display.append([result, ten])

        self.outcome = sum(self.results)
        return self.outcome


class Dicer:
    """掷骰类
    参数:
        roll_string: 标准掷骰表达式
        explode: 是否启用爆炸骰
    示例:
        ```python
        dice = Dice("1d10")
        dice.roll()
        print(dice.outcome) # 输出`1d10`投掷结果
        ```
    """

    def __init__(self, roll_string: str = EMPTY_STRING, explode: bool = False) -> None:
        self.roll_string: str = roll_string
        self.explode: bool = explode
        self.calc_list: List[str | Dice | DigitDice | AwardDice | PunishDice] = []
        self.results: List[int] = []
        self.display: List[int | List[int]] = []
        self.outcome: int = ZERO
        self.great: bool = False
        self.dices: List[str] = []

    def parse(self, roll_string: str = EMPTY_STRING, explode: bool = False):
        self.roll_string = roll_string if roll_string else self.roll_string
        self.calc_list = []
        self.db = EMPTY_STRING
        matches: List[str] = re.findall(r"\d*[a-zA-Z]\w*|\d+|[-+*/]", self.roll_string)

        for match in matches:
            if match in ("+", "-", "*", "/", "(", ")"):
                self.calc_list.append(match)
                self.db += match
            elif re.match(r"\d*[dD]\d*", match):
                self.calc_list.append(Dice(match, explode=explode))
                self.db += match.upper()
            elif re.match(r"\d*[bB]\d+", match):
                self.calc_list.append(AwardDice(match))
                self.db += match.upper()
            elif re.match(r"\d*[pP]\d+", match):
                self.calc_list.append(PunishDice(match))
                self.db += match.upper()
            elif re.match(r"\d+", match):
                self.calc_list.append(DigitDice(match))
                self.db += match.upper()
            else:
                raise ValueError(f"骰 {match} 不符合规范.")

        if not matches:
            self.calc_list.append(Dice("1d100"))
            self.db = "1D100"

        return self

    def roll(self):
        self.parse(roll_string=self.roll_string, explode=self.explode)
        self.dices = []
        self.display = []
        for index, calc in enumerate(self.calc_list):
            if calc in ("+", "-", "*", "/", "(", ")"):
                continue

            outcome = calc.roll()
            self.calc_list[index] = outcome
            self.results.append(outcome)
            self.display += calc.display

            if isinstance(calc, Dice) and self.explode:
                if calc.great:
                    self.great = True

                self.dices += calc.dices

        self.outcome = eval("".join(map(str, self.calc_list)))
        return self

    def description(self):
        def count_integers(lst: list) -> int:
            count = 0
            for item in lst:
                if isinstance(item, int):
                    count += 1
                elif isinstance(item, list):
                    count += count_integers(item)
            return count

        results = self.display
        len_display = count_integers(self.display)
        len_results = count_integers(self.results)

        if len_display <= 10:
            results = self.display
        elif len_results <= 10:
            results = self.results
        else:
            results = [...]

        return f"{self.db}={results}={self.outcome}"

    def get_results(self):
        return self.results

    def detail_expr(self):
        return str(self.results)

    def calc(self):
        return self.outcome

    def __repr__(self):
        return self.db


if __name__ == "__main__":
    # text = "-10/d2/1d10+2d2-22/2+3p2+2b10-p4/b2/d2"
    # dice = Dicer(text)
    # print(dice.calc_list)
    # dice.roll()
    # print(dice.calc_list)
    # print(dice.results)
    # print(dice.outcome)
    roll_strings = {
        "1": 1,
        "10": 10,
        "100": 100,
        "-1": -1,
        "-10": -10,
        "-100": -100,
        "1d1": 1,
        "10d1": 10,
        "100d1": 100,
        "10d1+10d1": 20,
        "10d1-10d1": 0,
        "10d1+10d1+10d1": 30,
        "10d1-10d1+10d1": 10,
        "10d1-10d1-10d1": -10,
    }
    for roll_string in roll_strings.keys():
        try:
            dice = Dicer().parse(roll_string).roll().roll()
            if dice.outcome != roll_strings[roll_string]:
                print(dice.description())
                raise ValueError(
                    f"对于 {roll_string} dice.toal={dice.outcome} 但期待 {roll_strings[roll_string]}"
                )
        except ValueError as error:
            logger.exception(error)

    try:
        roll_string = "d"
        dice = Dicer(roll_string).roll()
        print(dice.description())
    except ValueError as error:
        logger.exception(error)