Compare commits
No commits in common. "8ec78fd01b4a07dd6f91e1a29c6d39b9d5684119" and "81df9ef44374a99509ad5347bf3ca5214938988c" have entirely different histories.
8ec78fd01b
...
81df9ef443
1 changed files with 29 additions and 21 deletions
|
@ -2,32 +2,27 @@ import os.path
|
|||
import json
|
||||
from rcon.source import Client
|
||||
|
||||
# Declare variables that are needed everywhere.
|
||||
|
||||
servers_file = "./.midnight_servers.json"
|
||||
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():
|
||||
global servers
|
||||
if os.path.isfile(servers_file):
|
||||
with open(servers_file,'r') as openfile:
|
||||
servers = json.load(openfile)
|
||||
else:
|
||||
print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n')
|
||||
create_server()
|
||||
create_server(True)
|
||||
|
||||
# Write the contents to the servers variable to the servers_file.
|
||||
def write_servers():
|
||||
json_obj = json.dumps(servers,indent=4) # Add indentation when dumping to string.
|
||||
json_obj = json.dumps(servers,indent=4)
|
||||
with open(servers_file, "w") as outfile:
|
||||
outfile.write(json_obj)
|
||||
|
||||
# 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.
|
||||
def list_servers(mode=int(1)):
|
||||
if(mode==1):
|
||||
index = int(0)
|
||||
for x in servers: # for each dict in the servers array as x.
|
||||
for x in servers:
|
||||
strindex = str(index)
|
||||
print('ID: '+strindex+' ===')
|
||||
print(' Name:'+x['name'])
|
||||
|
@ -35,6 +30,7 @@ def list_servers(mode=int(1)): # make sure a default is set.
|
|||
print(' Port:'+x['port'])
|
||||
print(' Password: **********\n')
|
||||
index += 1
|
||||
initial_choice()
|
||||
elif(mode==2):
|
||||
index = int(0)
|
||||
for x in servers:
|
||||
|
@ -42,18 +38,27 @@ def list_servers(mode=int(1)): # make sure a default is set.
|
|||
print('ID: '+strindex+' ('+x['name']+')')
|
||||
index += 1
|
||||
|
||||
# Creates new server dicts in the server array.
|
||||
def create_server():
|
||||
global servers # Global so that we can write to it.
|
||||
def create_server(isfirstrun=False):
|
||||
global servers
|
||||
if(isfirstrun):
|
||||
print('\n!! Looks like you do not have any servers yet - Let\'s add one.\n')
|
||||
|
||||
print('\n=== Add Server ===')
|
||||
srv_name = input('Name: ')
|
||||
srv_addr = input('Address: ')
|
||||
srv_port = input('Port: ')
|
||||
srv_pass = input('Password: ')
|
||||
print('OK\n')
|
||||
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.
|
||||
write_servers() # Write to the file.
|
||||
new_server = [{"name":srv_name,"address":srv_addr,"port":srv_port,"password":srv_pass}]
|
||||
|
||||
if(isfirstrun):
|
||||
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():
|
||||
global servers
|
||||
|
@ -80,7 +85,7 @@ def send_commands():
|
|||
with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client:
|
||||
response = client.run(command)
|
||||
|
||||
print(servers[server]['name']+': '+response+'\n')
|
||||
print(servers[server]['name']+': '+response)
|
||||
else:
|
||||
proceed = False
|
||||
print('No command was entered. Exiting.\n')
|
||||
|
@ -93,7 +98,7 @@ def send_commands():
|
|||
with Client(x['address'], int(x['port']), passwd=x['password']) as client:
|
||||
response = client.run(command)
|
||||
|
||||
print(x['name']+': '+response+'\n')
|
||||
print(x['name']+': '+response)
|
||||
else:
|
||||
proceed = False
|
||||
print('No command was entered. Exiting.\n')
|
||||
|
@ -105,12 +110,10 @@ def initial_choice():
|
|||
match choose_func:
|
||||
case "1":
|
||||
create_server()
|
||||
initial_choice()
|
||||
case "2":
|
||||
pop_server()
|
||||
case "3":
|
||||
list_servers()
|
||||
initial_choice()
|
||||
case "4":
|
||||
send_commands()
|
||||
case "5":
|
||||
|
@ -121,3 +124,8 @@ def initial_choice():
|
|||
|
||||
load_servers()
|
||||
initial_choice()
|
||||
|
||||
#with Client('10.100.4.60', 30580, passwd='orrQLZvXH6jgD@wt') as client:
|
||||
# response = client.run('tps')
|
||||
#
|
||||
#print(response)
|
Loading…
Reference in a new issue