File size: 4,735 Bytes
52a4f3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Chess Master GUI - Setup & Launch

## Quick Start

### 1. Set Gemini API Key

```bash
# Windows (PowerShell)
$env:GEMINI_API_KEY = "your_api_key_here"

# Windows (Command Prompt)
set GEMINI_API_KEY=your_api_key_here

# Linux/Mac
export GEMINI_API_KEY=your_api_key_here
```

Get a free key: https://aistudio.google.com/app/apikey

### 2. Run the GUI

**Windows:**
```bash
run_gui.bat
```

**Linux/Mac:**
```bash
bash run_gui.sh
```

**Or manually:**
```bash
python app.py
```

### 3. Open in Browser

```
http://localhost:5000
```

## What's Included

### Files
- `app.py` - Flask server with WebSocket support
- `templates/index.html` - Game UI
- `static/style.css` - Styling
- `static/game.js` - Client-side logic
- `requirements-gui.txt` - Python dependencies
- `run_gui.bat` / `run_gui.sh` - Quick start scripts

### Features
✓ Modern, responsive web interface  
✓ Chess board with legal move highlighting  
✓ Real-time chat sidebar  
✓ Agent responds naturally (chooses when to speak)  
✓ Game statistics (moves, duration, messages)  
✓ Player persistence (remembers you)  
✓ Full trigger system (BEFORE_MATCH, ON_USER_MOVE, etc.)  

## How It Works

### Game Flow

1. **Start** → Enter name → Click "Start Game"
2. **BEFORE_MATCH trigger** → Agent greets you (if it chooses to speak)
3. **Make moves** → Type or click legal moves
4. **ON_USER_MOVE trigger** → Agent responds (if interesting)
5. **Agent moves** → Random legal move (automated)
6. **Repeat** → Until game ends
7. **AFTER_MATCH trigger** → Agent reflects on game

### Natural Conversation

The agent will:
- **Speak when:**
  - You make an interesting move
  - There's a critical moment (check, threat)
  - You ask a question in chat
  - Something unusual happens
  
- **Stay silent when:**
  - Routine opening moves
  - Nothing interesting to add
  - You're just thinking

This is controlled by the system prompt which encourages the agent to use `"action": "stop"` for silence.

## Architecture

### Backend
- **Flask** - Web server
- **Flask-SocketIO** - Real-time WebSocket communication
- **GameSession** - Per-client game state management
- **Agent System** - Full Chess Master with triggers

### Frontend
- **HTML5** - Semantic markup
- **CSS3** - Modern styling with flexbox
- **JavaScript** - WebSocket client, game logic
- **Socket.IO** - Real-time communication

### Async Flow

```
Client                    Server                      Agent
  |                         |                           |
  |--send move----------->  |                           |
  |                         |--trigger(ON_USER_MOVE)->  |
  |                         |                      [respond]
  |                         |<--response------------|
  |<--move_made------------|
  |                    [agent moves]
  |<--board update--------|
  |
  |--send message------->  |
  |                         |--trigger(ON_USER_INPUT)->|
  |                         |                     [respond]
  |                         |<--response------------|
  |<--message sent---------|
```

## Troubleshooting

### Port Already in Use
```bash
# Change port in app.py
socketio.run(app, debug=True, host="0.0.0.0", port=5001)  # Use 5001 instead
```

### API Key Issues
- Verify key is set: `echo %GEMINI_API_KEY%` (Windows) or `echo $GEMINI_API_KEY%` (Unix)
- Get new key: https://aistudio.google.com/app/apikey
- Key should start with `AIzaSy...`

### WebSocket Connection Failed
- Check browser console (F12 → Console)
- Ensure server is running: `http://localhost:5000`
- Check firewall settings

## Customization

### Change Port
Edit `app.py`:
```python
socketio.run(app, debug=True, host="0.0.0.0", port=YOUR_PORT)
```

### Change UI Colors
Edit `static/style.css`:
```css
/* Primary color */
#1e3c72  /* Dark blue */
#2a5298  /* Light blue */
#6a1b9a  /* Agent purple */
```

### Agent Personality
Edit `agent/main_agent.py` system prompt:
```python
system_prompt = f"""You are Viktor Petrov...
```

## Performance Notes

- **Single player per session** - Game state is per WebSocket connection
- **No database persistence** - Chat/moves only persist during game
- **Random moves** - Agent doesn't evaluate position (not a real chess engine)
- **Threading async** - WebSocket runs on thread pool

For production, consider:
- Redis for session management
- Database for move history
- Real chess engine for moves
- Authentication system

## Next Steps

1. Play a full game and check agent responses
2. Test natural conversation (agent staying silent)
3. Play multiple games - agent should remember you
4. (Optional) Implement real chess AI for better moves

---

**Ready to play?**

```bash
python app.py
```

Then open: http://localhost:5000