import socket
from packet import Packet

# command class
class Command:
    def __init__(self, name, callback):
        self.name = name
        self.callback = callback
    
# command array
commands = []

# adds commands into the command array
def add(name,callback):
    commands.append(Command(name, callback))

# command thread
def cmdthread(srv):
    while srv.isActive:
        command = input("> ")
        
        cmddata = []
        if len(command):
            if " " in command:
                cmddata = command.split(" ")
            else:
                cmddata.append(command)

            # find the correct command in the array
            for cmd in commands:
                if cmd.name == cmddata[0]:
                    # execute the callback
                    cmd.callback(srv, cmddata)
                    break

# command functions
def Cmd_Start_f(srv, args):
    srv.start_match()

# maybe later...
def Cmd_Kick_f(srv, args):
    pass

# move command
def Cmd_Move_f(srv, args):
    srv.send_all(Packet("clientmove:{}:{}:{}".format(args[1], args[2], args[3])).get())

# set name command
def Cmd_Setname_f(srv, args):
    print("[DEBUG]: Changed name of client {} to {}".format(args[1], args[2]))
    srv.send_all(Packet("namechange:{}:{}".format(args[1], args[2])).get())

# Stop the current match command
def Cmd_Stop_f(srv, args):
    srv.stop_match()

# register commands here
def init():
    add("start", Cmd_Start_f)
    add("kick", Cmd_Kick_f)
    add("clientmove", Cmd_Move_f)
    add("setname", Cmd_Setname_f)
    add("stop", Cmd_Stop_f)

@@ -1377,16 +1377,129 @@
 atch()%0A%0A
+# Exit dedicated server%0Adef Cmd_Exit_f(srv, args):%0A    srv.isActive = False%0A    srv.sock.close()%0A    sys.exit()%0A%0A
 # regist
@@ -1677,12 +1677,40 @@
 Cmd_Stop_f)%0A
+    add(%22exit%22, Cmd_Exit_f)%0A

Exit command? should fix the socket issues of before...