Fixed issue in game loop. Unique key issue in database.
This commit is contained in:
parent
6eb18990a2
commit
416cc4511b
@ -50,12 +50,10 @@ class Database:
|
|||||||
def save_game_state(self, game_state):
|
def save_game_state(self, game_state):
|
||||||
"""Save complete game state to database"""
|
"""Save complete game state to database"""
|
||||||
with self._get_connection() as conn:
|
with self._get_connection() as conn:
|
||||||
# Save players
|
# Save players using INSERT OR REPLACE to handle existing nicknames
|
||||||
conn.execute('DELETE FROM players')
|
|
||||||
|
|
||||||
for player in game_state.players.values():
|
for player in game_state.players.values():
|
||||||
conn.execute('''
|
conn.execute('''
|
||||||
INSERT INTO players (player_id, nickname, money, population, color, last_online)
|
INSERT OR REPLACE INTO players (player_id, nickname, money, population, color, last_online)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
''', (
|
''', (
|
||||||
player.player_id,
|
player.player_id,
|
||||||
|
|||||||
@ -20,16 +20,20 @@ database = Database()
|
|||||||
async def game_loop():
|
async def game_loop():
|
||||||
"""Main game loop: economy ticks every 10 seconds, DB save every 10 seconds"""
|
"""Main game loop: economy ticks every 10 seconds, DB save every 10 seconds"""
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(10)
|
try:
|
||||||
|
await asyncio.sleep(10)
|
||||||
|
|
||||||
# Economy tick
|
# Economy tick
|
||||||
economy_engine.tick()
|
economy_engine.tick()
|
||||||
|
|
||||||
# Save to database
|
# Save to database
|
||||||
database.save_game_state(game_state)
|
database.save_game_state(game_state)
|
||||||
|
|
||||||
# Broadcast state to all players
|
# Broadcast state to all players
|
||||||
await ws_manager.broadcast_game_state(game_state.get_state())
|
await ws_manager.broadcast_game_state(game_state.get_state())
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in game loop: {e}")
|
||||||
|
# Continue running even if there's an error
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user