Server & Game refactor basics
This commit is contained in:
49
server.py
49
server.py
@@ -11,7 +11,7 @@ from serializable import Serializable
|
||||
import json
|
||||
import os
|
||||
mcp = FastMCP("wyvern-castle")
|
||||
game = Game()
|
||||
game: Game = None
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -21,8 +21,53 @@ logging.basicConfig(
|
||||
]
|
||||
)
|
||||
|
||||
# History file path
|
||||
# Constants
|
||||
HISTORY_FILE = "game_history.json"
|
||||
SAVE_PATH = "save_"
|
||||
|
||||
@mcp.tool()
|
||||
async def load_game(slot:int):
|
||||
"""Loads an already existing game.
|
||||
|
||||
Args:
|
||||
slot: Integer id of the save slot.
|
||||
"""
|
||||
global game
|
||||
path = SAVE_PATH + str(slot) + ".json"
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
game.deserialize(f.read())
|
||||
return {
|
||||
"success": True,
|
||||
"msg": f"{path} as been successfully loaded!"
|
||||
}
|
||||
except OSError as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
@mcp.tool()
|
||||
async def save_game(slot:int):
|
||||
"""Saves the current game to the given slot.
|
||||
|
||||
Args:
|
||||
slot: Integer id of the save slot.
|
||||
"""
|
||||
global game
|
||||
path = SAVE_PATH + str(slot) + ".json"
|
||||
try:
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(game.serialize())
|
||||
return {
|
||||
"success": True,
|
||||
"msg": f"{path} as been successfully saved!"
|
||||
}
|
||||
except OSError as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def append_to_history(event: Dict[str, Any]):
|
||||
"""Append a game event to the history file."""
|
||||
|
||||
Reference in New Issue
Block a user