Javier-Jimenez99's picture
Actualizar README.md con secciones de demostración y arquitectura, y mejorar la interfaz de usuario en app.py para manejar bloques HTML. También se corrigen comentarios y traducciones en obr.py y test.py.
18f4e71
import requests as request
import os
import urllib3
# Suppress warnings about insecure SSL connections
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
ASSETS_NAMES = {
"BOOK": "book.png",
"CHEST": "chest.png",
"SWORD": "sword.png",
"TORCH": "torch.png",
"ARCHER": "halfElfBard.png",
"HUMAN": "human.png",
"WOMAN": "woman.png",
"CHILD": "child.png",
"ORC": "halfOrcFighter2.png",
"KNIGHT": "knight1.png",
"DRAGON": "redDragon.png",
"GOBLIN": "goblin.png",
"GOLEM": "rockGolem.png",
"FOREST": "forestMap.jpg",
"VILLAGE": "villageMap.jpg",
"LONELY_CABIN": "lonelyCabinMap.jpg",
"BATTLE_ARENA": "battleArenaMap.jpg",
"CABIN": "cabinMap.jpeg",
}
def execute_action(tab_id, action, params={}):
"""
Execute an action on a specific tab.
Args:
tab_id (str): The ID of the tab where the action will be executed.
action (str): The action to perform (e.g., "createShape", "createText").
params (json): Parameters required for the action.
Returns:
dict: A dictionary containing the game state.
"""
payload = {
"action": action,
"args": params
}
url = f"{os.getenv('FRONTEND_SERVER')}/execute/{tab_id}"
response = request.post(
url,
json=payload,
headers={'Content-Type': 'application/json'},
verify=False
)
if action != "getGameState" and response.status_code == 200 and response.json()["success"]:
return execute_action(tab_id=tab_id, action="getGameState")
else:
return response.json()
def create_token(name:str, type:str, x:int, y:int, size:int, tab_id:str)->dict:
"""
Create a token with specified properties, they can be items or characters.
Args:
name (str): Name of the token.
type (str): Type of the token. It can be "BOOK" | "CHEST" | "SWORD" | "TORCH" | "ARCHER" | "HUMAN" | "ORC" | "KNIGHT" | "DRAGON" | "GOBLIN" | "GOLEM".
x (int): X coordinate of the token in cells.
y (int): Y coordinate of the token in cells.
size (int): Width of the token in cells.
tab_id (str): The ID of the tab where the token will be created. This is the most important parameter.
Returns:
dict: A dictionary containing the game state.
"""
asset_url = f"{os.getenv('ASSETS_URL')}/{ASSETS_NAMES[type.upper()]}"
params = {
"name": name,
"imageUrl": asset_url,
"x": x,
"y": y,
"size": size
}
return execute_action(tab_id=tab_id, action="createToken", params=params)
def create_shape(width:int, height:int, x:int, y:int, shape_type:str, fill_color:str, stroke_color:str, tab_id:str) -> dict:
"""
Create a shape item with specified properties.
Args:
width (int): Width of the shape in cells.
height (int): Height of the shape in cells.
x (int): X coordinate of the shape in cells.
y (int): Y coordinate of the shape in cells.
shape_type (str): Type of the shape. It can be "RECTANGLE" | "CIRCLE" | "TRIANGLE" | "HEXAGON".
fill_color (str): Fill color in hex format (e.g., "#ff0000").
stroke_color (str): Stroke color in hex format (e.g., "#ff0000").
tab_id (str): The ID of the tab where the shape will be created. This is the most important parameter.
Returns:
dict: A dictionary containing the game state.
"""
params = {
"width": width,
"height": height,
"x": x,
"y": y,
"shapeType": shape_type.upper(),
"strokeColor": stroke_color,
"fillColor": fill_color
}
return execute_action(tab_id=tab_id, action="createShape", params=params)
def game_state(tab_id:str) -> dict:
"""
Get the current game state.
Args:
tab_id (str): The ID of the tab to retrieve the game state for.
Returns:
dict: A dictionary containing the game state.
"""
return execute_action(tab_id=tab_id, action="getGameState")
def move_item(tab_id:str, item_id:str, x:int, y:int) -> dict:
"""
Move an item to a new position.
Args:
tab_id (str): The ID of the tab where the item is located.
item_id (str): The ID of the item to move.
x (int): New X coordinate in cells.
y (int): New Y coordinate in cells.
Returns:
dict: A dictionary confirming the moved item.
"""
params = {
"id": item_id,
"x": x,
"y": y
}
return execute_action(tab_id=tab_id, action="moveItem", params=params)
def delete_item(tab_id:str, item_id:str) -> dict:
"""
Delete an item from the game.
Args:
tab_id (str): The ID of the tab where the item is located.
item_id (str): The ID of the item to delete.
Returns:
dict: A dictionary containing the game state.
"""
params = {
"id": item_id
}
return execute_action(tab_id=tab_id, action="deleteItem", params=params)
def fill_fog(tab_id:str) -> dict:
"""
Fill the entire map with fog of war.
Args:
tab_id (str): The ID of the tab where the fog will be applied.
Returns:
dict: A dictionary containing the game state.
"""
return execute_action(tab_id=tab_id, action="fillFog")
def clear_fog(tab_id:str) -> dict:
"""
Clear the fog of war from the entire map.
Args:
tab_id (str): The ID of the tab where the fog will be cleared.
Returns:
dict: A dictionary containing the game state.
"""
return execute_action(tab_id=tab_id, action="removeFog")
def add_token_light(tab_id:str, item_id:str, light_radius:int) -> dict:
"""
Add light to a token. This will allow to show it inside the fog of war.
It can be used to add light to items like torches or characters.
It will be attached so that the light moves with the token.
Args:
tab_id (str): The ID of the tab where the item is located.
item_id (str): The ID of the item to add light to.
light_radius (int): The radius of the light in cells.
Returns:
dict: A dictionary containing the game state.
"""
params = {
"targetId": item_id,
"radiusCells": light_radius
}
return execute_action(tab_id=tab_id, action="addLightSource", params=params)
def animate_token_viewport(tab_id:str, item_id:str) -> dict:
"""
Animate the viewport to focus on a specific token.
Args:
tab_id (str): The ID of the tab where the item is located.
item_id (str): The ID of the item to focus on.
Returns:
dict: A dictionary containing the game state.
"""
params = {
"itemsId": item_id
}
return execute_action(tab_id=tab_id, action="animateViewport", params=params)
def insert_map(tab_id:str, map_type:str) -> dict:
"""
Insert a map into the game.
The available maps are:
- FOREST: A forest with multiple paths and trees.
- VILLAGE: A small village with 3 houses and a well in the middle.
- LONELY_CABIN: A lonely cabin in the middle of a plain.
- CABIN: The interior of a cabin.
- BATTLE_ARENA: A roman battle arena with a central platform and walls around it.
Args:
tab_id (str): The ID of the tab where the map will be inserted.
map_type (str): The type of the map to insert. It can be: "FOREST" | "VILLAGE" | "LONELY_CABIN" | "BATTLE_ARENA" | "CABIN".
Returns:
dict: A dictionary containing the game state.
"""
map_type = map_type.upper()
asset_url = f"{os.getenv('ASSETS_URL')}/{ASSETS_NAMES[map_type]}"
params = {
"mapUrl": asset_url,
}
return execute_action(tab_id=tab_id, action="insertMap", params=params)
def clean_map(tab_id:str) -> dict:
"""
Clean the map by removing all items and shapes.
Args:
tab_id (str): The ID of the tab where the map will be cleaned.
Returns:
dict: A dictionary confirming the map cleaning.
"""
return execute_action(tab_id=tab_id, action="emptyAll")