Update battle_arena.py
Browse files- battle_arena.py +27 -10
battle_arena.py
CHANGED
|
@@ -10,8 +10,12 @@ from typing import Dict, List, Tuple, Optional
|
|
| 10 |
|
| 11 |
|
| 12 |
async def init_battle_arena_db(db_path: str):
|
| 13 |
-
"""๋ฐฐํ ์๋ ๋ ํ
์ด๋ธ ์ด๊ธฐํ"""
|
| 14 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# ๋ฐฐํ ๋ฐฉ ํ
์ด๋ธ
|
| 16 |
await db.execute("""
|
| 17 |
CREATE TABLE IF NOT EXISTS battle_rooms (
|
|
@@ -90,7 +94,9 @@ async def create_battle_room(
|
|
| 90 |
if is_npc and battle_type != 'opinion':
|
| 91 |
return False, "โ NPC๋ ๋ค์๊ฒฐ ์ฃผ์ ๋ง ๊ฐ๋ฅ", None
|
| 92 |
|
| 93 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
| 94 |
# GPU ์ฐจ๊ฐ
|
| 95 |
if is_npc:
|
| 96 |
cursor = await db.execute(
|
|
@@ -165,7 +171,8 @@ async def place_bet(
|
|
| 165 |
if bet_amount < 1 or bet_amount > 100:
|
| 166 |
return False, "โ ๋ฒ ํ
1~100 GPU"
|
| 167 |
|
| 168 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
| 169 |
db.row_factory = aiosqlite.Row
|
| 170 |
|
| 171 |
# ๋ฐฉ ํ์ธ
|
|
@@ -249,7 +256,8 @@ async def place_bet(
|
|
| 249 |
|
| 250 |
async def resolve_battle(db_path: str, room_id: int) -> Tuple[bool, str]:
|
| 251 |
"""๋ฐฐํ ํ์ (50.01% ์ด์ ๋ํํ ์ชฝ ์น๋ฆฌ)"""
|
| 252 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
| 253 |
db.row_factory = aiosqlite.Row
|
| 254 |
|
| 255 |
cursor = await db.execute(
|
|
@@ -366,7 +374,8 @@ async def resolve_battle(db_path: str, room_id: int) -> Tuple[bool, str]:
|
|
| 366 |
|
| 367 |
async def get_active_battles(db_path: str, limit: int = 20) -> List[Dict]:
|
| 368 |
"""์งํ ์ค์ธ ๋ฐฐํ ๋ชฉ๋ก"""
|
| 369 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
| 370 |
db.row_factory = aiosqlite.Row
|
| 371 |
|
| 372 |
cursor = await db.execute(
|
|
@@ -419,7 +428,9 @@ async def get_active_battles(db_path: str, limit: int = 20) -> List[Dict]:
|
|
| 419 |
|
| 420 |
async def auto_resolve_expired_battles(db_path: str):
|
| 421 |
"""๋ง๋ฃ๋ ๋ฐฐํ ์๋ ํ์ """
|
| 422 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
| 423 |
cursor = await db.execute(
|
| 424 |
"""SELECT id FROM battle_rooms
|
| 425 |
WHERE status='active' AND end_time <= ?""",
|
|
@@ -516,7 +527,9 @@ async def npc_create_battle(db_path: str) -> Tuple[bool, str]:
|
|
| 516 |
num_battles = random.randint(1, 2) # 1-2๊ฐ ๋๋ค ์์ฑ
|
| 517 |
|
| 518 |
# ๐ ํ์ฌ ํ์ฑํ๋ ๋ฐฐํ๋ฐฉ ์ ๋ชฉ ์กฐํ (์ค๋ณต ๋ฐฉ์ง)
|
| 519 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
| 520 |
cursor = await db.execute("""
|
| 521 |
SELECT title FROM battle_rooms
|
| 522 |
WHERE status='active'
|
|
@@ -524,7 +537,9 @@ async def npc_create_battle(db_path: str) -> Tuple[bool, str]:
|
|
| 524 |
active_titles = {row[0] for row in await cursor.fetchall()}
|
| 525 |
|
| 526 |
for _ in range(num_battles):
|
| 527 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
| 528 |
# ํ์ฑ NPC ์ค GPU 50 ์ด์์ธ NPC ์ ํ
|
| 529 |
cursor = await db.execute("""
|
| 530 |
SELECT agent_id, ai_identity, gpu_dollars
|
|
@@ -602,7 +617,9 @@ async def npc_auto_bet(db_path: str) -> int:
|
|
| 602 |
"""
|
| 603 |
total_bet_count = 0
|
| 604 |
|
| 605 |
-
async with aiosqlite.connect(db_path) as db:
|
|
|
|
|
|
|
| 606 |
# ์งํ์ค์ธ ๋ฐฐํ ๊ฐ์ ธ์ค๊ธฐ
|
| 607 |
cursor = await db.execute("""
|
| 608 |
SELECT id, title, option_a, option_b, battle_type
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
async def init_battle_arena_db(db_path: str):
|
| 13 |
+
"""๋ฐฐํ ์๋ ๋ ํ
์ด๋ธ ์ด๊ธฐํ (DB ๋ฝ ๋ฐฉ์ง)"""
|
| 14 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 15 |
+
# ๐ง WAL ๋ชจ๋ ํ์ฑํ (๋์ ์ ๊ทผ ์ต์ ํ)
|
| 16 |
+
await db.execute("PRAGMA journal_mode=WAL")
|
| 17 |
+
await db.execute("PRAGMA busy_timeout=30000") # 30์ด ๋๊ธฐ
|
| 18 |
+
|
| 19 |
# ๋ฐฐํ ๋ฐฉ ํ
์ด๋ธ
|
| 20 |
await db.execute("""
|
| 21 |
CREATE TABLE IF NOT EXISTS battle_rooms (
|
|
|
|
| 94 |
if is_npc and battle_type != 'opinion':
|
| 95 |
return False, "โ NPC๋ ๋ค์๊ฒฐ ์ฃผ์ ๋ง ๊ฐ๋ฅ", None
|
| 96 |
|
| 97 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 98 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 99 |
+
|
| 100 |
# GPU ์ฐจ๊ฐ
|
| 101 |
if is_npc:
|
| 102 |
cursor = await db.execute(
|
|
|
|
| 171 |
if bet_amount < 1 or bet_amount > 100:
|
| 172 |
return False, "โ ๋ฒ ํ
1~100 GPU"
|
| 173 |
|
| 174 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 175 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 176 |
db.row_factory = aiosqlite.Row
|
| 177 |
|
| 178 |
# ๋ฐฉ ํ์ธ
|
|
|
|
| 256 |
|
| 257 |
async def resolve_battle(db_path: str, room_id: int) -> Tuple[bool, str]:
|
| 258 |
"""๋ฐฐํ ํ์ (50.01% ์ด์ ๋ํํ ์ชฝ ์น๋ฆฌ)"""
|
| 259 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 260 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 261 |
db.row_factory = aiosqlite.Row
|
| 262 |
|
| 263 |
cursor = await db.execute(
|
|
|
|
| 374 |
|
| 375 |
async def get_active_battles(db_path: str, limit: int = 20) -> List[Dict]:
|
| 376 |
"""์งํ ์ค์ธ ๋ฐฐํ ๋ชฉ๋ก"""
|
| 377 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 378 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 379 |
db.row_factory = aiosqlite.Row
|
| 380 |
|
| 381 |
cursor = await db.execute(
|
|
|
|
| 428 |
|
| 429 |
async def auto_resolve_expired_battles(db_path: str):
|
| 430 |
"""๋ง๋ฃ๋ ๋ฐฐํ ์๋ ํ์ """
|
| 431 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 432 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 433 |
+
|
| 434 |
cursor = await db.execute(
|
| 435 |
"""SELECT id FROM battle_rooms
|
| 436 |
WHERE status='active' AND end_time <= ?""",
|
|
|
|
| 527 |
num_battles = random.randint(1, 2) # 1-2๊ฐ ๋๋ค ์์ฑ
|
| 528 |
|
| 529 |
# ๐ ํ์ฌ ํ์ฑํ๋ ๋ฐฐํ๋ฐฉ ์ ๋ชฉ ์กฐํ (์ค๋ณต ๋ฐฉ์ง)
|
| 530 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 531 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 532 |
+
|
| 533 |
cursor = await db.execute("""
|
| 534 |
SELECT title FROM battle_rooms
|
| 535 |
WHERE status='active'
|
|
|
|
| 537 |
active_titles = {row[0] for row in await cursor.fetchall()}
|
| 538 |
|
| 539 |
for _ in range(num_battles):
|
| 540 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 541 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 542 |
+
|
| 543 |
# ํ์ฑ NPC ์ค GPU 50 ์ด์์ธ NPC ์ ํ
|
| 544 |
cursor = await db.execute("""
|
| 545 |
SELECT agent_id, ai_identity, gpu_dollars
|
|
|
|
| 617 |
"""
|
| 618 |
total_bet_count = 0
|
| 619 |
|
| 620 |
+
async with aiosqlite.connect(db_path, timeout=30.0) as db:
|
| 621 |
+
await db.execute("PRAGMA busy_timeout=30000")
|
| 622 |
+
|
| 623 |
# ์งํ์ค์ธ ๋ฐฐํ ๊ฐ์ ธ์ค๊ธฐ
|
| 624 |
cursor = await db.execute("""
|
| 625 |
SELECT id, title, option_a, option_b, battle_type
|