# Path: cuwo/script.py
# class ServerScript(BaseScript):
#     connection_class = ConnectionScript
#     commands = None
#     aliases = None
# 
#     def __init__(self, server):
#         self.script_name = self.__module__.split('.')[1]
# 
#         self.server = server
#         self.world = server.world
#         self.loop = server.loop
#         server.scripts.add(self)
#         self.children = []
#         self.on_load()
#         for connection in server.connections:
#             self.call('on_existing_connection', connection=connection)
# 
#     def on_new_connection(self, event):
#         if self.connection_class is None:
#             return
#         script = self.connection_class(self, event.connection)
#         script.call('on_connect')
# 
#     def on_existing_connection(self, event):
#         if self.connection_class is None:
#             return
#         self.connection_class(self, event.connection)
# 
#     def on_command(self, event):
#         return self.call_command(event.user, event.command, event.args)
# 
#     def unload(self):
#         if self.server is None:
#             return
#         self.on_unload()
#         self.server.scripts.remove(self)
#         for script in self.children[:]:
#             script.unload()
#         self.children = None
#         self.server = None
#         self.world = None
# 
#     def call_command(self, user, command, args):
#         if self.commands is None:
#             return
#         command = command.lower()
#         command = self.aliases.get(command, command)
#         command = self.commands.get(command, None)
#         if not command:
#             return
#         user.parent = self  # for ScriptInterface
# 
#         if len(args) < command.min_args:
#             return command.get_syntax()
#         if command.max_args is not None and len(args) > command.max_args:
#             return command.get_syntax()
# 
#         try:
#             ret = command(user, *args) or ''
#         except InvalidPlayer:
#             ret = 'Invalid player specified'
#         except InsufficientRights:
#             ret = 'Insufficient rights'
#         except Exception:
#             import traceback
#             traceback.print_exc()
#             ret = ''
#         return ret
# 
# Path: cuwo/exceptions.py
# class InvalidData(Exception):
#     """
#     Raised when invalid data is received
#     """


from cuwo.script import ServerScript
from cuwo.exceptions import InvalidData
import zlib
import json
import asyncio

    def connection_made(self, transport):
        self.transport = transport
        self.task = asyncio.get_event_loop().create_task(self.send_loop())

    @asyncio.coroutine
    def send_loop(self):
        config = self.server.config.base
        data = ServerData()

        while True:
            data.name = config.server_name
            data.max = config.max_players
            data.ip = self.server.config.master.hostname
            data.players = len(self.server.players)
            data.mode = self.server.get_mode()
            self.send_packet(data.get(), self.address)
            yield from asyncio.sleep(UPDATE_RATE)

    def on_packet(self, data, addr):
        if data.get('ack', False):
            self.on_ack()

    def on_ack(self):
        if self.has_response:
            return
        self.has_response = True
        print('Received response from master server')



# Path: cuwo/script.py
# class ServerScript(BaseScript):
#     connection_class = ConnectionScript
#     commands = None
#     aliases = None
# 
#     def __init__(self, server):
#         self.script_name = self.__module__.split('.')[1]
# 
#         self.server = server
#         self.world = server.world
#         self.loop = server.loop
#         server.scripts.add(self)
#         self.children = []
#         self.on_load()
#         for connection in server.connections:
#             self.call('on_existing_connection', connection=connection)
# 
#     def on_new_connection(self, event):
#         if self.connection_class is None:
#             return
#         script = self.connection_class(self, event.connection)
#         script.call('on_connect')
# 
#     def on_existing_connection(self, event):
#         if self.connection_class is None:
#             return
#         self.connection_class(self, event.connection)
# 
#     def on_command(self, event):
#         return self.call_command(event.user, event.command, event.args)
# 
#     def unload(self):
#         if self.server is None:
#             return
#         self.on_unload()
#         self.server.scripts.remove(self)
#         for script in self.children[:]:
#             script.unload()
#         self.children = None
#         self.server = None
#         self.world = None
# 
#     def call_command(self, user, command, args):
#         if self.commands is None:
#             return
#         command = command.lower()
#         command = self.aliases.get(command, command)
#         command = self.commands.get(command, None)
#         if not command:
#             return
#         user.parent = self  # for ScriptInterface
# 
#         if len(args) < command.min_args:
#             return command.get_syntax()
#         if command.max_args is not None and len(args) > command.max_args:
#             return command.get_syntax()
# 
#         try:
#             ret = command(user, *args) or ''
#         except InvalidPlayer:
#             ret = 'Invalid player specified'
#         except InsufficientRights:
#             ret = 'Insufficient rights'
#         except Exception:
#             import traceback
#             traceback.print_exc()
#             ret = ''
#         return ret
# 
# Path: cuwo/exceptions.py
# class InvalidData(Exception):
#     """
#     Raised when invalid data is received
#     """


# Path: scripts/master.py
from cuwo.script import ServerScript
from cuwo.exceptions import InvalidData
import zlib
import json
import asyncio


    def connection_made(self, transport):
        self.transport = transport
        self.task = asyncio.get_event_loop().create_task(self.send_loop())

    @asyncio.coroutine
    def send_loop(self):
        config = self.server.config.base
        data = ServerData()

        while True:
            data.name = config.server_name
            data.max = config.max_players
            data.ip = self.server.config.master.hostname
            data.players = len(self.server.players)
            data.mode = self.server.get_mode()
            self.send_packet(data.get(), self.address)
            yield from asyncio.sleep(UPDATE_RATE)

    def on_packet(self, data, addr):
        if data.get('ack', False):
            self.on_ack()

    def on_ack(self):
        if self.has_response:
            return
        self.has_response = True
        print('Received response from master server')



class MasterRelay(ServerScript):