Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystalwarrior committed May 14, 2024
2 parents a79026a + cf2768d commit b2fe9d8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 33 deletions.
54 changes: 37 additions & 17 deletions server/commands/hubs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shlex

import oyaml as yaml # ordered yaml

Expand Down Expand Up @@ -100,16 +101,17 @@ def ooc_cmd_save_hub(client, arg):
"""
Save the current Hub in the server's storage/hubs/<name>.yaml file.
If blank and you're a mod, it will save to server's config/areas_new.yaml for the server owner to approve.
Usage: /save_hub <name>
Usage: /save_hub <name> <read_only>
"""
args = shlex.split(arg)
if not client.is_mod:
if arg == "":
if args[0] == "":
raise ArgumentError(
"You must be authorized to save the default hub!")
if len(arg) < 3:
if len(args[0]) < 3:
raise ArgumentError("Filename must be at least 3 symbols long!")
try:
if arg != "":
if args[0] != "":
path = "storage/hubs"
num_files = len(
[f for f in os.listdir(path) if os.path.isfile(
Expand All @@ -120,26 +122,28 @@ def ooc_cmd_save_hub(client, arg):
"Server storage full! Please contact the server host to resolve this issue."
)
try:
arg = f"{path}/{derelative(arg)}.yaml"
if os.path.isfile(arg):
with open(arg, "r", encoding="utf-8") as stream:
args[0] = f"{path}/{derelative(args[0])}.yaml"
if os.path.isfile(args[0]):
with open(args[0], "r", encoding="utf-8") as stream:
hub = yaml.safe_load(stream)
if "read_only" in hub and hub["read_only"] is True:
raise ArgumentError(
f"Hub {arg} already exists and it is read-only!"
f"Hub {args[0]} already exists and it is read-only!"
)
with open(arg, "w", encoding="utf-8") as stream:
hub = client.area.area_manager.save(ignore=["can_gm", "max_areas"])
if len(args) == 2 and args[1] == "read_only":
hub["read_only"] = True
with open(args[0], "w", encoding="utf-8") as stream:
yaml.dump(
client.area.area_manager.save(
ignore=["can_gm", "max_areas"]),
hub,
stream,
default_flow_style=False,
)
except ArgumentError:
raise
except Exception:
raise AreaError(f"File path {arg} is invalid!")
client.send_ooc(f"Saving as {arg}...")
raise AreaError(f"File path {args[0]} is invalid!")
client.send_ooc(f"Saving as {args[0]}...")
else:
client.server.hub_manager.save("config/areas_new.yaml")
client.send_ooc(
Expand Down Expand Up @@ -227,18 +231,34 @@ def ooc_cmd_overlay_hub(client, arg):
client.send_ooc("Success, sending ARUP and refreshing music...")


@mod_only()
def ooc_cmd_list_hubs(client, arg):
"""
Show all the available hubs for loading in the storage/hubs/ folder.
Usage: /list_hubs
"""
text = "Available hubs:"
hubs_editable = []
hubs_read_only = []
for F in os.listdir("storage/hubs/"):
if F.lower().endswith(".yaml"):
text += "\n- {}".format(F[:-5])
with open(f"storage/hubs/{F}", "r", encoding="utf-8") as stream:
hub = yaml.safe_load(stream)
if "read_only" in hub and hub["read_only"] is True:
hubs_read_only.append(F[:-5])
else:
hubs_editable.append(F[:-5])

hubs_read_only.sort()
msg = "\n⛩️ Available Read Only Hubs: ⛩️\n"
for hub in hubs_read_only:
msg += f"\n🌎 [👀]{hub}"

if client.is_mod:
hubs_editable.sort()
msg += "\n\n⛩️ Available Editable Hubs: ⛩️\n"
for hub in hubs_editable:
msg += f"\n🌎 {hub}"

client.send_ooc(text)
client.send_ooc(msg)


@mod_only(hub_owners=True)
Expand Down
58 changes: 42 additions & 16 deletions server/commands/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,34 @@ def ooc_cmd_musiclists(client, arg):
Displays all the available music lists.
Usage: /musiclists
"""
text = "Available musiclists:"
from os import listdir

for F in listdir("storage/musiclists/"):
musiclist_editable = []
musiclist_read_only = []
for F in os.listdir("storage/musiclists/"):
if F.lower().endswith(".yaml"):
text += "\n- {}".format(F[:-5])
with open(f"storage/musiclists/{F}", "r", encoding="utf-8") as stream:
musiclist = yaml.safe_load(stream)
read_only = False
for item in musiclist:
if "read_only" in item and item["read_only"] is True:
musiclist_read_only.append(F[:-5])
read_only = True
break
if not read_only:
musiclist_editable.append(F[:-5])

musiclist_read_only.sort()
msg = "\n🎶 Available Read Only Musiclists: 🎶\n"
for ml in musiclist_read_only:
msg += f"\n🎜 [👀]{ml}"

client.send_ooc(text)
if client.is_mod:
musiclist_editable.sort()
msg += "\n\n🎶 Available Editable Musiclists: 🎶\n"
for ml in musiclist_editable:
msg += f"\n🎜 {ml}"

client.send_ooc(msg)


def ooc_cmd_musiclist(client, arg):
Expand Down Expand Up @@ -375,17 +395,18 @@ def ooc_cmd_musiclist_save(client, arg):
"""
Allow you to save a musiclist on server list!
If the musiclist you're editing is already in the server list, you don't have to add [MusiclistName]
Usage: /musiclist_save <local/area/hub> [MusiclistName]
Argument "read_only" is optional
Usage: /musiclist_save <local/area/hub> [MusiclistName] <read_only>
"""
if arg == "":
client.send_ooc("Usage: /musiclist_save <local/area/hub> <MusiclistName>")
client.send_ooc("Usage: /musiclist_save <local/area/hub> <MusiclistName> <read_only>")
return

args = shlex.split(arg)
if args[0] not in ["local", "area", "hub"]:
client.send_ooc("Usage: /musiclist_save <local/area/hub> <MusiclistName>")
client.send_ooc("Usage: /musiclist_save <local/area/hub> <MusiclistName> <read_only>")
return

if args[0] == "local":
musiclist = client.music_list
name = client.music_ref
Expand All @@ -397,20 +418,25 @@ def ooc_cmd_musiclist_save(client, arg):
name = client.area.area_manager.music_ref

if name == "unsaved":
if len(args) == 2:
if len(args) >= 2:
name = args[1]
else:
client.send_ooc("This is a new musiclist, you should give it a name")
return

if len(args) > 2 and args[2].lower() == "read_only":
musiclist.append({"read_only": True})

filepath = f"storage/musiclists/{name}.yaml"

with open(filepath, "r", encoding="utf-8") as stream:
test = yaml.safe_load(stream)
if "read_only" in test and test["read_only"] is True:
raise ArgumentError(
f"Musiclist '{name}' already exists and it is read-only!"
)
if os.path.isfile(filepath):
with open(filepath, "r", encoding="utf-8") as stream:
test = yaml.safe_load(stream)
for item in test:
if "read_only" in item and item["read_only"] is True:
raise ArgumentError(
f"Musiclist '{name}' already exists and it is read-only!"
)
with open(filepath, "w", encoding="utf-8") as yaml_save:
yaml.dump(musiclist, yaml_save)
client.send_ooc(f"Musiclist '{name}' saved on server list!")
Expand Down

0 comments on commit b2fe9d8

Please sign in to comment.