Skip to content

Instantly share code, notes, and snippets.

@NotAFile
Created November 27, 2019 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotAFile/955d333e3e37fd2fa2fa09f7b4b54daa to your computer and use it in GitHub Desktop.
Save NotAFile/955d333e3e37fd2fa2fa09f7b4b54daa to your computer and use it in GitHub Desktop.
Piqueserver Config converter
import pkgutil
import piqueserver.scripts
import json
import toml
import sys
def time_unit(dic, key, unit="min"):
if dic is not None and key in dic:
dic[key] = "{}{}".format(int(dic[key]), unit)
def group_keys(dic, prefix):
result = {}
for key, value in list(dic.items()):
if key.startswith(prefix):
result[key.replace(prefix, "")] = value
del dic[key]
if not result:
return None
return result
with open(sys.argv[1]) as f:
data = json.load(f)
upstream_script_list = [
pkg.name for pkg in pkgutil.iter_modules(piqueserver.scripts.__path__)
]
new_scripts = []
for i in data["scripts"]:
if i in upstream_script_list:
new_scripts.append("piqueserver.scripts.{}".format(i))
else:
new_scripts.append(i)
data["scripts"] = new_scripts
data["rotation"] = data.pop("maps")
data["votekick"] = group_keys(data, "votekick_")
data["votemap"] = group_keys(data, "votemap_")
data["spectator_control"] = group_keys(data, "spectator_")
data["squad"] = {**group_keys(data, "squad_"), **{"auto_squad":
data.pop("auto_squad")}}
data["afk"] = group_keys(data, "afk_")
if data.pop("rollback_on_game_end", False):
data["rollback"] = {"rollback_on_game_end": True}
if data.pop("load_saved_map", False):
data["savemap"] = {"load_saved_map": True}
if not "bans" in data:
new_bans = {}
new_bans["default_duration"] = data.pop("default_ban_duration")
new_bans["publish"] = data["ban_publish"]["enabled"]
new_bans["publish_port"] = data["ban_publish"]["port"]
del data["ban_publish"]
new_bans["bansubscribe"] = [
{"url": i, "whitelist": []} for i in data["ban_subscribe"]["urls"]
]
data["bans"] = new_bans
if not "logging" in data:
new_logging = {}
new_logging["loglevel"] = "info"
new_logging["logfile"] = data.pop("logfile")
del data["debug_log"]
new_logging["profile"] = data.pop("profile")
new_logging["rotate_daily"] = data.pop("rotate_daily")
data["logging"] = new_logging
time_unit(data, "tip_frequency")
time_unit(data, "respawn_time", "sec")
time_unit(data, "grief_friendly_fire_time", "sec")
time_unit(data, "teamswitch_interval", "sec")
time_unit(data, "default_time_limit", "hours")
time_unit(data["votekick"], "ban_duration", "min")
time_unit(data["votemap"], "extension_time", "min")
time_unit(data["bans"], "default_duration", "min")
time_unit(data["spectator_control"], "kick_time", "sec")
time_unit(data["afk"], "time_limit", "sec")
print("# Auto-converted using piqueserver convert-config")
print()
print("# for a complete config reference, see:")
print("# http://docs.piqueserver.org/en/latest/config-options.html")
print()
print(toml.dumps(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment