File size: 8,164 Bytes
b3ea2a5
 
 
 
18f4e71
b3ea2a5
 
 
 
 
 
 
 
 
dca6d40
 
b3ea2a5
 
 
 
 
61c131b
e584cde
61c131b
 
990ddaa
b3ea2a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18f4e71
b3ea2a5
 
18f4e71
dca6d40
 
 
 
 
b3ea2a5
 
 
dca6d40
b3ea2a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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")