Skip to content

Commit

Permalink
Refactor commands, fix possible crashes if map items don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerroth committed Dec 20, 2022
1 parent a4521cb commit 68b16e3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
77 changes: 49 additions & 28 deletions command.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
frame = args[3]
string = args[4]

if str(item_index) not in map["items"]:
item = map_get_item(map, item_index)
if not item:
print("Error: item not found.")
return

# Move item
item = map["items"][str(item_index)]
item[1] = x
item[2] = y
print("Move", str(get_name_from_item_id(item[0])), "to", f"({x},{y})")
Expand All @@ -135,26 +135,27 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
item_index = args[0]
reason = args[1]

if str(item_index) not in map["items"]:
item = map_get_item(map, item_index)
if not item:
print("Error: item not found.")
return

# Delete item
name = str(get_name_from_item_id(map["items"][str(item_index)][0]))
del map["items"][str(item_index)]
name = str(get_name_from_item_id(item[0]))
map_delete_item(map, item_index)

print(f"Remove {name}. Reason: {reason}")

elif cmd == "kill":
item_index = args[0]
reason = args[1]

if str(item_index) not in map["items"]:
item = map_get_item(map, item_index)
if not item:
print("Error: item not found.")
return

# Delete item
name = str(get_name_from_item_id(map["items"][str(item_index)][0]))
del map["items"][str(item_index)]
name = str(get_name_from_item_id(item[0]))
map_delete_item(map, item_index)

print(f"Kill {name}. Reason: {reason}")

Expand All @@ -169,22 +170,22 @@ def do_command(USERID, map_id, cmd, args, resources_changed):

# Delete items
for index in index_list:
if str(index) in map["items"]:
del map["items"][str(index)]
map_delete_item(map, index)

print(f"Removed {len(index_list)} items.")

elif cmd == "orient":
item_index = args[0]
orientation = args[1]

if str(item_index) not in map["items"]:
item = map_get_item(map, item_index)
if not item:
print("Error: item not found.")
return

map["items"][str(item_index)][4] = int(orientation)
item[4] = int(orientation)

print("Rotate", str(get_name_from_item_id(map["items"][str(item_index)][0])))
print("Rotate", str(get_name_from_item_id(item[0])))

elif cmd == "expand":
expansion = args[0]
Expand All @@ -196,17 +197,15 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
elif cmd == "store_item":
item_index = args[0]

if str(item_index) not in map["items"]:
item = map_pop_item(map, item_index)
if not item:
print("Error: item not found.")
return

item_id = map["items"][str(item_index)][0]
item_id = item[0]
name = str(get_name_from_item_id(item_id))

add_store_item(map, item_id)

# Delete item from map
del map["items"][str(item_index)]

print(f"Store {name}.")

Expand All @@ -222,7 +221,6 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
name = str(get_name_from_item_id(item_id))

remove_store_item(map, item_id)

map_add_item(map, item_index, item_id, x, y)

print(f"Placed stored {name}.")
Expand Down Expand Up @@ -296,16 +294,21 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
if len(args) > 2:
level = args[2]

item_properties = map["items"][str(item_index)][6]
if "xp" not in item_properties:
item_properties["xp"] = xp_gain
item = map_get_item(map, item_index)
if not item:
print("Error: item not found.")
return

attr = item[6]
if "xp" not in attr:
attr["xp"] = xp_gain
else:
item_properties["xp"] += xp_gain
attr["xp"] += xp_gain

if level:
item_properties["level"] = level
attr["level"] = level

print("Added", xp_gain, "XP to", get_name_from_item_id(map["items"][str(item_index)][0]))
print("Added", xp_gain, "XP to", get_name_from_item_id(item[0]))

elif cmd == "set_variables":
pass
Expand Down Expand Up @@ -335,6 +338,14 @@ def do_command(USERID, map_id, cmd, args, resources_changed):

unit = map_pop_item(map, index_unit)
building = map_get_item(map, index_building)

if not unit:
print("Error: unit not found.")
return
if not building:
print("Error: building not found.")
return

push_unit(unit, building)

print("Pushed", str(get_name_from_item_id(unit[0])), "to", str(get_name_from_item_id(building[0])))
Expand All @@ -349,7 +360,14 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
unknown = args[6] # unknown

building = map_get_item(map, index_building)
if not building:
print("Error: building not found.")
return

unit = pop_unit(building)
if not unit:
print("Error: no units in building.")
return

# modify item data
unit[0] = item_id
Expand All @@ -365,7 +383,10 @@ def do_command(USERID, map_id, cmd, args, resources_changed):
item_id = args[0]
activate = args[1]

item = map["items"][str(item_id)]
item = map_get_item(map, item_id)
if not item:
print("Error: item not found.")
return

if activate > 0:
item[3] = time_now
Expand Down
20 changes: 16 additions & 4 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ def map_add_item_from_item(map: dict, index: int, item: list):
map["items"][str(index)] = item

def map_get_item(map: dict, index: int):
return map["items"][str(index)]
itemstr = str(index)
if itemstr not in map["items"]:
return None
return map["items"][itemstr]

def map_pop_item(map: dict, index: int):
return map["items"].pop(str(index))
itemstr = str(index)
if itemstr not in map["items"]:
return None
return map["items"].pop(itemstr)

def map_delete_item(map: dict, index: int):
del map["items"][str(index)]
itemstr = str(index)
if itemstr not in map["items"]:
return None
del map["items"][str(itemstr)]

def push_unit(unit: dict, building: dict):
building[5].append(unit)

def pop_unit(building: dict):
return building[5].pop()
if len(building[5]) > 0:
return building[5].pop()
else:
return None

def add_store_item(map: dict, item: int, quantity: int = 1):
itemstr = str(item)
Expand Down

0 comments on commit 68b16e3

Please sign in to comment.