Compare commits

..

7 commits

View file

@ -2,27 +2,32 @@ import os.path
import json import json
from rcon.source import Client from rcon.source import Client
# Declare variables that are needed everywhere.
servers_file = "./.midnight_servers.json" servers_file = "./.midnight_servers.json"
servers = [] servers = []
isfirstrun = False
# Loads servers in from the servers_file, if it does not exist, trigger adding first server via create_servers.
def load_servers(): def load_servers():
global servers global servers
if os.path.isfile(servers_file): if os.path.isfile(servers_file):
with open(servers_file,'r') as openfile: with open(servers_file,'r') as openfile:
servers = json.load(openfile) servers = json.load(openfile)
else: else:
create_server(True) print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n')
create_server()
# Write the contents to the servers variable to the servers_file.
def write_servers(): def write_servers():
json_obj = json.dumps(servers,indent=4) json_obj = json.dumps(servers,indent=4) # Add indentation when dumping to string.
with open(servers_file, "w") as outfile: with open(servers_file, "w") as outfile:
outfile.write(json_obj) outfile.write(json_obj)
def list_servers(mode=int(1)): # Lists servers, default action is to list all information minus password. Second option lists only ID and name.
def list_servers(mode=int(1)): # make sure a default is set.
if(mode==1): if(mode==1):
index = int(0) index = int(0)
for x in servers: for x in servers: # for each dict in the servers array as x.
strindex = str(index) strindex = str(index)
print('ID: '+strindex+' ===') print('ID: '+strindex+' ===')
print(' Name:'+x['name']) print(' Name:'+x['name'])
@ -30,7 +35,6 @@ def list_servers(mode=int(1)):
print(' Port:'+x['port']) print(' Port:'+x['port'])
print(' Password: **********\n') print(' Password: **********\n')
index += 1 index += 1
initial_choice()
elif(mode==2): elif(mode==2):
index = int(0) index = int(0)
for x in servers: for x in servers:
@ -38,27 +42,18 @@ def list_servers(mode=int(1)):
print('ID: '+strindex+' ('+x['name']+')') print('ID: '+strindex+' ('+x['name']+')')
index += 1 index += 1
def create_server(isfirstrun=False): # Creates new server dicts in the server array.
global servers def create_server():
if(isfirstrun): global servers # Global so that we can write to it.
print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n')
print('\n=== Add Server ===') print('\n=== Add Server ===')
srv_name = input('Name: ') srv_name = input('Name: ')
srv_addr = input('Address: ') srv_addr = input('Address: ')
srv_port = input('Port: ') srv_port = input('Port: ')
srv_pass = input('Password: ') srv_pass = input('Password: ')
print('OK\n') print('OK\n')
new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}] new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}] # Build new dict in array.
servers = servers + new_server # Append new array to existing array.
if(isfirstrun): write_servers() # Write to the file.
json_obj = json.dumps(new_server,indent=4)
with open(servers_file, "w") as outfile:
outfile.write(json_obj)
else:
servers = servers + new_server
write_servers()
initial_choice()
def pop_server(): def pop_server():
global servers global servers
@ -85,7 +80,7 @@ def send_commands():
with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client: with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client:
response = client.run(command) response = client.run(command)
print(servers[server]['name']+': '+response) print(servers[server]['name']+': '+response+'\n')
else: else:
proceed = False proceed = False
print('No command was entered. Exiting.\n') print('No command was entered. Exiting.\n')
@ -98,7 +93,7 @@ def send_commands():
with Client(x['address'], int(x['port']), passwd=x['password']) as client: with Client(x['address'], int(x['port']), passwd=x['password']) as client:
response = client.run(command) response = client.run(command)
print(x['name']+': '+response) print(x['name']+': '+response+'\n')
else: else:
proceed = False proceed = False
print('No command was entered. Exiting.\n') print('No command was entered. Exiting.\n')
@ -110,10 +105,12 @@ def initial_choice():
match choose_func: match choose_func:
case "1": case "1":
create_server() create_server()
initial_choice()
case "2": case "2":
pop_server() pop_server()
case "3": case "3":
list_servers() list_servers()
initial_choice()
case "4": case "4":
send_commands() send_commands()
case "5": case "5":
@ -123,9 +120,4 @@ def initial_choice():
initial_choice() initial_choice()
load_servers() load_servers()
initial_choice() initial_choice()
#with Client('10.100.4.60', 30580, passwd='orrQLZvXH6jgD@wt') as client:
# response = client.run('tps')
#
#print(response)