Utility scripts for a TES3MP server administrator.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
2.5 KiB

#!/usr/bin/env python3
import argparse
import json
import os
import sys
DESC = "Add or remove player capabilities and rank."
PROG = "tes3mp-playerctl"
VERSION = 1
# TODO: dehardcode this
TES3MP_DIR = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "..")
)
# TODO: dehardcode this
PLAYER_FILES = os.path.join(TES3MP_DIR, "lua", "CoreScripts", "data", "player")
def parse_argv():
"""Set up args and parse them."""
parser = argparse.ArgumentParser(description=DESC, prog=PROG)
parser.add_argument(
"--version",
action="version",
version=PROG + ": v" + str(VERSION),
help=argparse.SUPPRESS,
)
options = parser.add_argument_group("Options")
options.add_argument(
"-P",
"--player",
required=True,
help="The account name of the player to modify.",
)
options.add_argument(
"-R",
"--rank",
choices=["0", "1", "2", "3"],
required=True,
help="Set the player rank, one of 0, 1, 2, or 3. Default 0.",
)
options.add_argument(
"-C", "--allow-console", action="store_true", help="Allow console use."
)
options.add_argument(
"-w",
"--write-changes",
action="store_true",
help="Write changes to player file. Default: no.",
)
return parser.parse_args()
def main():
allow_console = "default"
player = None
rank = 0
write = False
parsed = parse_argv()
if parsed.allow_console:
allow_console = True
if parsed.write_changes:
write = True
player = parsed.player
player_file = os.path.join(PLAYER_FILES, player + ".json")
rank = int(parsed.rank)
if os.path.isfile(player_file):
with open(player_file, "r") as f:
jsonlines = f.readlines()
jsonstring = " ".join(jsonlines)
player_dict = json.loads(jsonstring)
player_dict["settings"]["consoleAllowed"] = allow_console
player_dict["settings"]["staffRank"] = rank
new_player_string = json.dumps(player_dict, sort_keys=True, indent=4)
if write:
with open(player_file, "w") as f:
for line in new_player_string:
f.write(line)
print("Player file written.")
else:
print(new_player_string)
else:
sys.stderr.write("The provided player doesn't exist.\n")
sys.exit(1)
if __name__ == "__main__":
main()