diff --git a/mc_control.py b/mc_control.py index 3aa8e1d..f78cfe8 100644 --- a/mc_control.py +++ b/mc_control.py @@ -1,6 +1,5 @@ import os.path import json -from termcolor import colored, cprint from rcon.source import Client # Declare variables that are needed everywhere. @@ -15,7 +14,7 @@ def load_servers(): with open(servers_file,'r') as openfile: servers = json.load(openfile) else: - print(colored('\n!! Looks like you do not have any servers yet - Let\'s add one.','white','on_green',attrs=['bold'])) + 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. @@ -26,30 +25,32 @@ def write_servers(): # 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. - output = "" if(mode==1): index = int(0) for x in servers: # for each dict in the servers array as x. strindex = str(index) - output+='ID: '+strindex+' ===\n Name:'+x['name']+'\n Address:'+x['address']+'\n Port:'+x['port']+'\n' + print('ID: '+strindex+' ===') + print(' Name:'+x['name']) + print(' Address:'+x['address']) + print(' Port:'+x['port']) + print(' Password: **********\n') index += 1 elif(mode==2): index = int(0) for x in servers: strindex = str(index) - output+='ID: '+strindex+' ('+x['name']+')\n' + print('ID: '+strindex+' ('+x['name']+')') index += 1 - return output # Creates new server dicts in the server array. def create_server(): global servers # Global so that we can write to it. - print(colored('\n=== Add Server ===','green')) - srv_name = input(colored('Name: ','yellow')) - srv_addr = input(colored('Address: ','yellow')) - srv_port = input(colored('Port: ','yellow')) - srv_pass = input(colored('Password: ','yellow')) - print(colored('OK\n','green')) + 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. @@ -57,58 +58,46 @@ def create_server(): # Used to delete servers from the array and write. def pop_server(): global servers # Global so we can write - print(colored('\n=== Remove Server ===\nPick a server to delete','red')) - print(colored(list_servers(2),'light_blue')) # Lists server ID and Name only. - delete = input(colored('(enter to cancel) ?: ','yellow')) - if(delete==""): - print(colored('Cancelled\n','red')) - return - servers.pop(int(delete)) - print(colored('Deleted\n','light_blue')) + print('\n=== Remove Server ===\nPick a server to delete') + list_servers(2) # Lists server ID and Name only. + delete = int(input('?: ')) + servers.pop(delete) write_servers() # Let's send some commands to the servers! def send_commands(): proceed=True # Used in while loops to determine if we still want to send more commands. - print(colored('\n=== Send commands ===','green')) - choice = input(colored('(Enter to cancel) Single (1) All (2)?: ','yellow')) - if(choice==""): - print(colored('Cancelled\n','red')) - return - if(choice=="1"): # We only want to interact with one server. - print(colored(list_servers(2),'light_blue')) # Show us which servers we can pick from. - server = input(colored('(Enter to cancel) Server?: ','yellow')) - if(server==""): - print(colored('Cancelled\n','red')) - return - server=int(server) + print('\n=== Send commands ===') + choice = int(input('Single Server (1) or All (2)?: ')) + if(choice==1): # We only want to interact with one server. + list_servers(2) # Show us which servers we can pick from. + server = int(input('Server?: ')) while(proceed): - command = input(colored('Input command (None to exit): ','yellow')) + command = input('Input command (None to exit): ') if(command!=""): with Client(servers[server]['address'], int(servers[server]['port']), passwd=servers[server]['password']) as client: response = client.run(command) - print(colored(servers[server]['name']+': '+response,'cyan')) + print(servers[server]['name']+': '+response+'\n') else: proceed = False - print(colored('No command was entered. Exiting.\n','red')) - elif(choice=="2"): + print('No command was entered. Exiting.\n') + elif(choice==2): while(proceed): - command = input(colored('Input command (None to exit): ','yellow')) + command = input('Input command (None to exit): ') for x in servers: # Run for each server. if(command!=""): with Client(x['address'], int(x['port']), passwd=x['password']) as client: response = client.run(command) - print(colored(x['name']+': '+response,'cyan')) + print(x['name']+': '+response+'\n') else: proceed = False - print(colored('No command was entered. Exiting.\n','red')) + print('No command was entered. Exiting.\n') # The main menu. def initial_choice(): - print(colored('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit','green')) - choose_func = input(colored("?: ",'yellow')) + choose_func = input('=== Choose Function ===\n1. Add servers\n2. Remove servers\n3. List servers\n4. Send commands\n5. Exit\n?: ') match choose_func: case "1": @@ -118,7 +107,7 @@ def initial_choice(): pop_server() initial_choice() case "3": - print(colored(list_servers(),'light_blue')) + list_servers() initial_choice() case "4": send_commands() @@ -126,7 +115,7 @@ def initial_choice(): case "5": exit() case _: - print(colored("\n!! Invalid input\n",'red')) + print("\n!! Invalid input\n") initial_choice() load_servers() diff --git a/requirements.txt b/requirements.txt index 67fe5a1..fbd7a88 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -rcon==2.4.4 -termcolor==2.4.0 \ No newline at end of file +rcon==2.4.4 \ No newline at end of file