File size: 13,417 Bytes
9d4e305 a3ef353 9d4e305 a3ef353 9d4e305 a3ef353 9d4e305 a3ef353 | 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 361 362 363 364 365 366 367 | # WordGame - Verbumix Code Structure Documentation
## Project Overview
**WordGame** (Verbumix) is a Hungarian word guessing game similar to Wordle, built with FastAPI backend and HTML/CSS/JavaScript frontend. The application features multiple game categories, user authentication, multiplayer rooms, leaderboards, and premium membership.
**Tech Stack:**
- Backend: FastAPI (Python)
- Frontend: HTML, Tailwind CSS, HTMX, JavaScript
- Database/Auth: Supabase
- Payment: Paddle
---
## File Structure
```
wordGame/
βββ main.py # Main FastAPI application, core game logic
βββ database.py # Supabase client configuration
βββ room.py # Multiplayer room management
βββ gameChange.py # Game category selection router
βββ paddle.py # Paddle payment webhook handler
βββ slang.py # Slang word import utility
βββ static/ # Static assets (CSS, JS, images)
βββ templates/ # Jinja2 HTML templates
βββ base.html # Base layout with navigation and game logic
βββ login.html # Login form
βββ register.html # Registration form
βββ forgot-password.html # Password recovery request form
βββ reset-password.html # Password reset form with token extraction
βββ jatek-valaszto.html # Game category selection screen
βββ jatek.html # Main game interface (Wordle-style)
βββ rangletra.html # Leaderboard display
βββ premium.html # Premium membership page (Paddle integration)
```
---
## Python Files Detailed Breakdown
### 1. `database.py` - Supabase Configuration
| Variable/Function | Type | Description |
|-------------------|------|-------------|
| `SUPABASE_URL` | Constant | Supabase project URL |
| `SUPABASE_KEY` | Constant | Supabase publishable API key |
| `supabase` | Client | Supabase client instance for database operations |
**Purpose:** Centralized database connection configuration used by all other modules.
---
### 2. `main.py` - Main Application (277 lines)
#### Imports & Setup
| Variable/Function | Type | Description |
|-------------------|------|-------------|
| `app` | FastAPI | Main application instance |
| `templates` | Jinja2Templates | Template renderer for HTML files |
| `KATEGORIA_NEVEK` | Dict | Category name mappings (szleng, matek, gamer) |
#### Routes & Functions
| Route/Function | Method | Path | Description |
|----------------|--------|------|-------------|
| `index` | GET | `/` | Home page, displays base.html with user context |
| `ellenoriz_tipp` | POST | `/ellenorzes` | **Core game logic** - validates guess against answer word |
| `rangletra` | GET | `/rangletra` | Displays leaderboard for selected category |
| `premium_oldal` | GET | `/premium` | Shows premium membership offer page |
| `login_page` | GET | `/login` | Displays login form |
| `register_page` | GET | `/register` | Displays registration form |
| `auth_register` | POST | `/auth/register` | Handles user registration via Supabase Auth |
| `auth_login` | POST | `/auth/login` | Handles user login, sets access_token cookie |
| `auth_logout` | GET | `/auth/logout` | Logs out user, deletes cookie |
| `forgot_password_page` | GET | `/forgot-password` | Displays password recovery form |
| `auth_forgot_password` | POST | `/auth/forgot-password` | Sends password reset email |
| `reset_password_page` | GET | `/reset-password` | Displays new password form |
| `auth_reset_password` | POST | `/auth/reset-password` | Updates user password with token |
| `get_current_user` | Async | (helper) | Extracts and validates user from access_token cookie |
| `ment_rangletra_pont` | Sync | (helper) | Saves score to leaderboard table |
#### Key Variables in Game Logic (`ellenoriz_tipp`)
| Variable | Type | Description |
|----------|------|-------------|
| `tipp` | str | User's guess (uppercased) |
| `megfejtes` | str | Answer word from database |
| `statuszok` | list | Array of "szurke"/"zold"/"sarga" for each letter |
| `megfejtes_szamlalo` | Counter | Letter frequency counter for handling duplicates |
#### Game Logic Flow
1. Fetch answer word from `szavak` table by category
2. Compare guess length to answer length
3. First pass: Mark exact matches (green/zold)
4. Second pass: Mark correct letters in wrong position (yellow/sarga)
5. Generate HTML grid with colored cells
6. If all green, show victory message
---
### 3. `room.py` - Multiplayer Room Management (138 lines)
| Route/Function | Method | Path | Description |
|----------------|--------|------|-------------|
| `generate_room_code` | Sync | (helper) | Generates random 6-character room code |
| `letrehoz_szoba` | POST | `/szoba/letrehozas` | Creates new room, adds owner as member |
| `csatlakozik_szoba` | POST | `/szoba/csatlakozas` | Join existing room by code |
| `szoba_statusz` | GET | `/szoba/statusz/{szoba_id}` | Returns current room member list (polled every 3s) |
#### Database Tables Used
| Table | Purpose |
|-------|---------|
| `szobak` | Stores room data (id, tulajdonos_id, kod) |
| `szoba_tagok` | Stores room members (szoba_id, user_id) |
| `profilok` | User profiles (checked for is_premium to allow >4 members) |
#### Key Variables
| Variable | Type | Description |
|----------|------|-------------|
| `kod` | str | 6-character alphanumeric room code |
| `szoba_id` | str | UUID of created/joined room |
| `tulajdonos_id` | str | Room owner's user ID |
| `jelenlegi_tagok_szama` | int | Current member count |
---
### 4. `gameChange.py` - Game Category Router (20 lines)
| Route/Function | Method | Path | Description |
|----------------|--------|------|-------------|
| `jatek_valaszto` | GET | `/jatek-valaszto` | Displays game category selection screen |
| `indit_jatek` | GET | `/jatek/{kategoria}` | Loads game for specific category |
**Note:** `indit_jatek` currently fetches word from database but returns empty string (incomplete implementation).
---
### 5. `paddle.py` - Payment Webhook (21 lines)
| Route/Function | Method | Path | Description |
|----------------|--------|------|-------------|
| `paddle_webhook` | POST | `/paddle-webhook` | Receives Paddle payment events |
#### Webhook Logic
- Listens for `transaction.completed` events
- Extracts `user_id` from `custom_data`
- Updates `profilok` table: sets `is_premium = True`
---
### 6. `slang.py` - Slang Word Importer (49 lines)
| Route/Function | Method | Path | Description |
|----------------|--------|------|-------------|
| `import_szleng_kifejezesek` | POST | `/szoba/import/szleng` | Imports slang words from GitHub repository |
#### Import Logic
1. Fetches raw text from `SLANG_URL` (GitHub)
2. Filters words: must be 5 letters, alphabetic only
3. Inserts unique words into `szavak` table with category "szleng"
4. Silently skips duplicates
---
## HTML Templates Detailed Breakdown
### `templates/base.html` - Base Layout (103 lines)
#### Structure
- Header with navigation (JΓ‘tΓ©kok, RanglΓ©tra, PrΓ©mium, Login/Logout)
- Main content area (`#jatek-terulet`) loaded via HTMX
- JavaScript for Wordle-style keyboard input handling
#### Key JavaScript Variables
| Variable | Type | Description |
|----------|------|-------------|
| `aktualisSor` | int | Current row (0-5) |
| `aktualisBetuIndex` | int | Current letter position (0-4) |
| `SZO_HOSSZ` | const | Word length (5) |
| `MAX_PROBA` | const | Maximum attempts (6) |
#### Key JavaScript Functions
| Function | Description |
|----------|-------------|
| `handleInput(key)` | Processes keyboard input, updates grid, submits on Enter |
#### HTMX Integration
- Uses `hx-get`, `hx-post`, `hx-target`, `hx-swap` for dynamic content
- Polls room status every 3 seconds with `hx-trigger="every 3s"`
---
### `templates/login.html` - Login Form (36 lines)
- Email and password fields
- HTMX form submission to `/auth/login`
- Error display in `#login-error`
- Links to register and forgot password
---
### `templates/register.html` - Registration Form (32 lines)
- Email and password (min 6 chars) fields
- HTMX form submission to `/auth/register`
- Status display in `#register-status`
- Link to login
---
### `templates/forgot-password.html` - Password Recovery (24 lines)
- Email input field
- HTMX form submission to `/auth/forgot-password`
- Status display in `#forgot-status`
---
### `templates/reset-password.html` - Password Reset (41 lines)
- New password input field
- Hidden field for `access_token` extracted from URL hash
- JavaScript `extractToken()` parses URL hash for Supabase token
- HTMX form submission to `/auth/reset-password`
---
### `templates/jatek-valaszto.html` - Game Selection (14 lines)
Three category buttons:
- Mai Szleng `/jatek/szleng`
- Matek (Nerdle mΓ³d) `/jatek/matek`
- Gamer / Geek `/jatek/gamer`
---
### `templates/jatek.html` - Game Interface (53 lines)
#### Variables from Context
| Variable | Description |
|----------|-------------|
| `kategoria_tipus` | Category identifier (szleng/matek/gamer) |
| `kategoria_nev` | Display name of category |
#### Layout
- 6 rows Γ 5 columns grid for guesses
- Category-specific virtual keyboard:
- **Matek**: Numbers 0-9 and operators (+, -, *, /, =)
- **Others**: Hungarian keyboard layout with special characters (Γ, Γ, Γ, Γ, Γ, Ε, Γ, Γ, Ε°)
---
### `templates/rangletra.html` - Leaderboard (51 lines)
#### Context Variable
| Variable | Description |
|----------|-------------|
| `toplista` | List of (sorszam, jatekos) tuples |
#### Jatekos Object Properties
| Property | Description |
|----------|-------------|
| `nev` | Username |
| `ido` | Time in seconds |
| `pont` | Score |
| `is_premium` | Premium badge display |
#### Features
- Category filter buttons (Szleng, Matek, Gamer)
- Medal emojis for top 3 ()
- Premium crown badge ()
---
### `templates/premium.html` - Premium Page (22 lines)
- Paddle.js integration template
- Placeholder for checkout initialization
- Note: Currently a template/example, not fully integrated
---
## Database Schema (Supabase Tables)
Based on code analysis, the following tables are used:
### `szavak` - Words Table
| Column | Type | Description |
|--------|------|-------------|
| `szo` | text | The word/answer |
| `kategoria` | text | Category (szleng, matek, gamer) |
### `szobak` - Rooms Table
| Column | Type | Description |
|--------|------|-------------|
| `id` | uuid | Primary key |
| `tulajdonos_id` | uuid | Owner's user ID |
| `kod` | text | 6-character room code |
### `szoba_tagok` - Room Members Table
| Column | Type | Description |
|--------|------|-------------|
| `id` | uuid | Primary key |
| `szoba_id` | uuid | Room ID (foreign key) |
| `user_id` | uuid | Member's user ID |
### `rangletra` - Leaderboard Table
| Column | Type | Description |
|--------|------|-------------|
| `username` | text | Player name |
| `pontszam` | int | Score |
| `ido_masodperc` | int | Time in seconds |
| `kategoria` | text | Category |
| `is_premium` | bool | Premium status |
### `profilok` - User Profiles Table
| Column | Type | Description |
|--------|------|-------------|
| `id` | uuid | User ID (from Supabase Auth) |
| `is_premium` | bool | Premium membership status |
---
## API Endpoints Summary
| Method | Path | File | Description |
|--------|------|------|-------------|
| GET | `/` | main.py | Home page |
| POST | `/ellenorzes` | main.py | Submit guess |
| GET | `/rangletra` | main.py | Leaderboard |
| GET | `/premium` | main.py | Premium page |
| GET | `/login` | main.py | Login form |
| GET | `/register` | main.py | Register form |
| POST | `/auth/register` | main.py | Register user |
| POST | `/auth/login` | main.py | Login user |
| GET | `/auth/logout` | main.py | Logout user |
| GET | `/forgot-password` | main.py | Password recovery form |
| POST | `/auth/forgot-password` | main.py | Send reset email |
| GET | `/reset-password` | main.py | Reset password form |
| POST | `/auth/reset-password` | main.py | Update password |
| GET | `/jatek-valaszto` | gameChange.py | Game selection |
| GET | `/jatek/{kategoria}` | gameChange.py | Load game |
| POST | `/szoba/letrehozas` | room.py | Create room |
| POST | `/szoba/csatlakozas` | room.py | Join room |
| GET | `/szoba/statusz/{szoba_id}` | room.py | Room status poll |
| POST | `/paddle-webhook` | paddle.py | Payment webhook |
| POST | `/szoba/import/szleng` | slang.py | Import slang words |
---
## Security Considerations
1. **API Keys Exposed**: `SUPABASE_KEY` in `database.py` is a publishable key (safe for client-side), but should still be protected
2. **Cookie Security**: `secure=False` in login cookie - should be `True` in production (HTTPS)
3. **CSRF Protection**: HTMX handles CSRF with cookies, but additional protection may be needed
4. **Input Validation**: Basic validation present, but could be strengthened
5. **Rate Limiting**: No rate limiting visible - consider adding for auth endpoints
---
## Known Issues / TODOs
1. `gameChange.py` - `indit_jatek()` returns empty string (incomplete)
2. `premium.html` - Paddle integration is template-only, not fully functional
3. `main.py` line 166, 191, 226, 252 - `AuthApiError` is referenced but not imported
4. No error handling for network failures in word fetching
5. Leaderboard save function (`ment_rangletra_pont`) is defined but never called |