From 98df6c4a9811105b0d1178b8e382be5e9fce3730 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:36:02 -0500 Subject: [PATCH 1/7] Clean up server responses --- mc_control.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mc_control.py b/mc_control.py index 3605e12..b858c6e 100644 --- a/mc_control.py +++ b/mc_control.py @@ -85,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) + print(servers[server]['name']+': '+response+'\n') else: proceed = False print('No command was entered. Exiting.\n') @@ -98,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) + print(x['name']+': '+response+'\n') else: proceed = False print('No command was entered. Exiting.\n') From ee36d0dce9a0267a38c25464171e0537630317ca Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:41:47 -0500 Subject: [PATCH 2/7] Remove old comment --- mc_control.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/mc_control.py b/mc_control.py index b858c6e..f9cd012 100644 --- a/mc_control.py +++ b/mc_control.py @@ -123,9 +123,4 @@ def initial_choice(): initial_choice() load_servers() -initial_choice() - -#with Client('10.100.4.60', 30580, passwd='orrQLZvXH6jgD@wt') as client: -# response = client.run('tps') -# -#print(response) \ No newline at end of file +initial_choice() \ No newline at end of file From 9562deae3db48caaba01e571418a8d5d8984b451 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:43:12 -0500 Subject: [PATCH 3/7] remove unnecessary variable --- mc_control.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mc_control.py b/mc_control.py index f9cd012..b7fc486 100644 --- a/mc_control.py +++ b/mc_control.py @@ -2,9 +2,10 @@ import os.path import json from rcon.source import Client +# Declare variables that are needed everywhere. + servers_file = "./.midnight_servers.json" servers = [] -isfirstrun = False def load_servers(): global servers From 91a3a2bde66780cacede63486a0be93287d3ef3d Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:48:38 -0500 Subject: [PATCH 4/7] Start adding some comments, fix an error. --- mc_control.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mc_control.py b/mc_control.py index b7fc486..2281e9c 100644 --- a/mc_control.py +++ b/mc_control.py @@ -7,6 +7,7 @@ from rcon.source import Client servers_file = "./.midnight_servers.json" servers = [] +# 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): @@ -15,15 +16,17 @@ def load_servers(): else: create_server(True) +# Write the contents to the servers variable to the servers_file. 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: 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): index = int(0) - for x in servers: + for x in servers: # for each dict in the servers array as x. strindex = str(index) print('ID: '+strindex+' ===') print(' Name:'+x['name']) @@ -31,7 +34,6 @@ def list_servers(mode=int(1)): print(' Port:'+x['port']) print(' Password: **********\n') index += 1 - initial_choice() elif(mode==2): index = int(0) for x in servers: @@ -39,6 +41,7 @@ def list_servers(mode=int(1)): print('ID: '+strindex+' ('+x['name']+')') index += 1 +# Creates new server dicts in the server array. def create_server(isfirstrun=False): global servers if(isfirstrun): @@ -115,6 +118,7 @@ def initial_choice(): pop_server() case "3": list_servers() + initial_choice() case "4": send_commands() case "5": From 53c000cb14a61b7ca6741f8bd6ad99d1be65ef96 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:50:35 -0500 Subject: [PATCH 5/7] Remove unnecessary code --- mc_control.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mc_control.py b/mc_control.py index 2281e9c..4b89488 100644 --- a/mc_control.py +++ b/mc_control.py @@ -54,14 +54,8 @@ def create_server(isfirstrun=False): srv_pass = input('Password: ') print('OK\n') 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() + servers = servers + new_server + write_servers() initial_choice() def pop_server(): From a87b03a5899c55c25f98f89ae21180afef313ec1 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:52:21 -0500 Subject: [PATCH 6/7] Move code out of function into where it should be --- mc_control.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mc_control.py b/mc_control.py index 4b89488..f503b7b 100644 --- a/mc_control.py +++ b/mc_control.py @@ -14,7 +14,8 @@ def load_servers(): with open(servers_file,'r') as openfile: servers = json.load(openfile) 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(): @@ -42,11 +43,8 @@ def list_servers(mode=int(1)): # make sure a default is set. index += 1 # Creates new server dicts in the server array. -def create_server(isfirstrun=False): +def create_server(): 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: ') From 8ec78fd01b4a07dd6f91e1a29c6d39b9d5684119 Mon Sep 17 00:00:00 2001 From: Subnet_Masked Date: Sun, 4 Feb 2024 16:55:49 -0500 Subject: [PATCH 7/7] more comments, remove unnecessary function call. --- mc_control.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mc_control.py b/mc_control.py index f503b7b..7d338f4 100644 --- a/mc_control.py +++ b/mc_control.py @@ -44,17 +44,16 @@ def list_servers(mode=int(1)): # make sure a default is set. # Creates new server dicts in the server array. def create_server(): - global servers + global servers # Global so that we can write to it. 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}] - servers = servers + new_server - write_servers() - initial_choice() + 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. def pop_server(): global servers @@ -106,6 +105,7 @@ def initial_choice(): match choose_func: case "1": create_server() + initial_choice() case "2": pop_server() case "3":