summaryrefslogtreecommitdiffstatshomepage
path: root/src/oneroll/tui.py
blob: 9e4e4c9c99dbb6af06f675d5c552c706c277c7a7 (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
#!/usr/bin/env python3
"""
OneRoll Terminal User Interface (TUI)

An interactive dice roll interface created using textual.
"""

from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widgets import Header, Footer, Input, Button, Static, DataTable, Tabs, Tab
from textual.reactive import reactive
from textual.message import Message
from typing import List, Dict, Any
import json
from datetime import datetime

from . import OneRoll, roll, roll_statistics, CommonRolls

class RollResult(Message):
    """Throw result message"""
    
    def __init__(self, result: Dict[str, Any], expression: str) -> None:
        self.result = result
        self.expression = expression
        super().__init__()

class ExpressionInput(Input):
    """Expression input box"""
    
    def __init__(self) -> None:
        super().__init__(
            placeholder="输入骰子表达式,如 3d6 + 2",
            id="expression_input"
        )
    
    def on_key(self, event) -> None:
        if event.key == "enter":
            self.post_message(RollResult(roll(self.value), self.value))
            self.value = ""

class QuickRollButton(Button):
    """Quick Throw Button"""
    
    def __init__(self, label: str, expression: str) -> None:
        self.expression = expression
        super().__init__(label, id=f"quick_{expression}")
    
    def on_button_pressed(self) -> None:
        self.post_message(RollResult(roll(self.expression), self.expression))

class RollHistory(DataTable):
    """Throw History Table"""
    
    def __init__(self) -> None:
        super().__init__()
        self.add_columns("时间", "表达式", "总点数", "详情")
    
    def add_roll(self, result: Dict[str, Any], expression: str) -> None:
        """Add throw record"""
        time_str = datetime.now().strftime("%H:%M:%S")
        self.add_row(
            time_str,
            expression,
            str(result["total"]),
            result["details"]
        )
        # keep table at bottom
        self.scroll_end()

class StatisticsPanel(Static):
    """Statistics Panel"""
    
    def __init__(self) -> None:
        super().__init__("统计功能", id="stats_panel")
    
    def show_statistics(self, expression: str, times: int = 100) -> None:
        """Show statistics information"""
        try:
            stats = roll_statistics(expression, times)
            stats_text = f"""
统计结果: {expression} (投掷 {stats['count']} 次)

最小值: {stats['min']}
最大值: {stats['max']}
平均值: {stats['mean']:.2f}
总和: {stats['total']}
            """
            self.update(stats_text)
        except Exception as e:
            self.update(f"统计错误: {e}")

class RollDisplay(Static):
    """The throwing result shows """
    
    def __init__(self) -> None:
        super().__init__("等待投掷...", id="roll_display")
    
    def show_result(self, result: Dict[str, Any], expression: str) -> None:
        """Display throwing result"""
        total = result["total"]
        details = result["details"]
        rolls = result["rolls"]
        
        # a kind of color selection based on result
        if total >= 15:
            color = "green"
        elif total >= 10:
            color = "yellow"
        else:
            color = "red"
        
        display_text = f"""[bold blue]🎲 {expression}[/bold blue]

[bold]总点数:[/bold] [bold {color}]{total}[/bold {color}]
[bold]详情:[/bold] {details}

[bold]投掷结果:[/bold] {rolls}"""

        # show the comment
        comment = result.get("comment", "")
        if comment:
            display_text += f"\n\n[bold]注释:[/bold] [italic blue]{comment}[/italic blue]"
        
        self.update(display_text)

class OneRollTUI(App):
    """OneRoll 终端用户界面"""
    
    CSS = """
    Screen {
        layout: vertical;
    }
    
    #expression_input {
        margin: 1;
    }
    
    #roll_display {
        height: 8;
        border: solid $primary;
        margin: 1;
        padding: 1;
    }
    
    #stats_panel {
        height: 8;
        border: solid $secondary;
        margin: 1;
        padding: 1;
    }
    
    #history_table {
        height: 10;
        margin: 1;
    }
    
    .quick_buttons {
        height: 3;
        margin: 1;
    }
    
    Button {
        margin: 1;
    }
    """
    
    def compose(self) -> ComposeResult:
        """UI components"""
        yield Header()
        
        with Container():
            yield ExpressionInput()
            
            with Horizontal(classes="quick_buttons"):
                yield QuickRollButton("D20", CommonRolls.D20)
                yield QuickRollButton("优势", CommonRolls.D20_ADVANTAGE)
                yield QuickRollButton("劣势", CommonRolls.D20_DISADVANTAGE)
                yield QuickRollButton("属性", CommonRolls.ATTRIBUTE_ROLL)
                yield QuickRollButton("3D6", "3d6")
                yield QuickRollButton("2D6", "2d6")
            
            yield RollDisplay()
            
            with Tabs():
                with Tab("历史记录", id="history_tab"):
                    yield RollHistory(id="history_table")
                
                with Tab("统计", id="stats_tab"):
                    yield StatisticsPanel()
        
        yield Footer()
    
    def on_mount(self) -> None:
        """Initialization during interface mount"""
        self.title = "OneRoll 骰子投掷器"
        self.sub_title = "高性能骰子表达式解析器"
        
        # Set focus to the input box
        self.query_one(ExpressionInput).focus()
    
    def on_roll_result(self, message: RollResult) -> None:
        # display result
        roll_display = self.query_one(RollDisplay)
        roll_display.show_result(message.result, message.expression)
        
        # add history
        history_table = self.query_one(RollHistory)
        history_table.add_roll(message.result, message.expression)
    
    def on_key(self, event) -> None:
        if event.key == "ctrl+q":
            self.exit()
        elif event.key == "ctrl+h":
            self.show_help()
        elif event.key == "ctrl+s":
            self.show_statistics()
    
    def show_help(self) -> None:
        help_text = """
OneRoll 骰子投掷器

支持的表达式格式:
• 基本骰子: 3d6, 1d20, 2d10
• 数学运算: 3d6 + 2, 2d6 * 3, (2d6 + 3) * 2
• 修饰符:
  - ! 爆炸骰子: 2d6!
  - kh 取高: 4d6kh3
  - kl 取低: 4d6kl2
  - dh 丢弃高: 5d6dh1
  - dl 丢弃低: 5d6dl1
  - r 重投: 3d6r1
  - ro 条件重投: 4d6ro1

快捷键:
• Ctrl+Q - 退出程序
• Ctrl+H - 显示帮助
• Ctrl+S - 显示统计
• Enter - 执行投掷
        """
        
        self.notify(help_text, title="帮助信息", timeout=10)
    
    def show_statistics(self) -> None:
        self.notify("统计功能开发中...", title="统计")

def run_tui():
    app = OneRollTUI()
    app.run()

if __name__ == "__main__":
    run_tui()