Spaces:
Running
Running
Create game_logic.py
Browse files- game_logic.py +66 -0
game_logic.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from src.database import interactions
|
| 3 |
+
|
| 4 |
+
class RPGGame:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.current_session_id = None
|
| 7 |
+
self.turn_number = 0
|
| 8 |
+
|
| 9 |
+
def start_game(self, player_name: str, genre: str, scenario: str) -> str:
|
| 10 |
+
session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{player_name}"
|
| 11 |
+
self.current_session_id = session_id
|
| 12 |
+
self.turn_number = 0
|
| 13 |
+
|
| 14 |
+
# Create initial interaction with all session data
|
| 15 |
+
interactions.insert([{
|
| 16 |
+
'session_id': session_id,
|
| 17 |
+
'player_name': player_name,
|
| 18 |
+
'genre': genre,
|
| 19 |
+
'initial_scenario': scenario,
|
| 20 |
+
'turn_number': 0,
|
| 21 |
+
'player_input': "Game starts",
|
| 22 |
+
'timestamp': datetime.now()
|
| 23 |
+
}])
|
| 24 |
+
|
| 25 |
+
# Get initial story and options
|
| 26 |
+
initial_response = interactions.select(interactions.story_text).where(
|
| 27 |
+
(interactions.session_id == session_id) &
|
| 28 |
+
(interactions.turn_number == 0)
|
| 29 |
+
).collect()['story_text'][0]
|
| 30 |
+
|
| 31 |
+
return session_id, initial_response
|
| 32 |
+
|
| 33 |
+
def process_action(self, action: str) -> str:
|
| 34 |
+
if not self.current_session_id:
|
| 35 |
+
return "No active game session. Please start a new game."
|
| 36 |
+
|
| 37 |
+
self.turn_number += 1
|
| 38 |
+
|
| 39 |
+
# Get session info from previous turn
|
| 40 |
+
prev_turn = interactions.select(
|
| 41 |
+
interactions.player_name,
|
| 42 |
+
interactions.genre,
|
| 43 |
+
interactions.initial_scenario
|
| 44 |
+
).where(
|
| 45 |
+
(interactions.session_id == self.current_session_id) &
|
| 46 |
+
(interactions.turn_number == 0)
|
| 47 |
+
).collect()
|
| 48 |
+
|
| 49 |
+
# Record player action with session data
|
| 50 |
+
interactions.insert([{
|
| 51 |
+
'session_id': self.current_session_id,
|
| 52 |
+
'player_name': prev_turn['player_name'][0],
|
| 53 |
+
'genre': prev_turn['genre'][0],
|
| 54 |
+
'initial_scenario': prev_turn['initial_scenario'][0],
|
| 55 |
+
'turn_number': self.turn_number,
|
| 56 |
+
'player_input': action,
|
| 57 |
+
'timestamp': datetime.now()
|
| 58 |
+
}])
|
| 59 |
+
|
| 60 |
+
# Get AI response
|
| 61 |
+
response = interactions.select(interactions.story_text).where(
|
| 62 |
+
(interactions.session_id == self.current_session_id) &
|
| 63 |
+
(interactions.turn_number == self.turn_number)
|
| 64 |
+
).collect()['story_text'][0]
|
| 65 |
+
|
| 66 |
+
return response
|