# Path: pyspades/protocol.py
# class BaseConnection(object):
#     disconnected = False
#     timeout_call = None
#     def __init__(self, protocol, peer):
#         self.protocol = protocol
#         self.peer = peer
#     
#     def timed_out(self):
#         self.disconnect()
#     
#     def disconnect(self, data = 0):
#         if self.disconnected:
#             return
#         self.disconnected = True
#         self.peer.disconnect(data);
#         self.protocol.remove_peer(self.peer)
#         self.on_disconnect()
#     
#     def loader_received(self, loader):
#         raise NotImplementedError('loader_received() not implemented')
#     
#     def send_contained(self, contained, sequence = False):
#         if self.disconnected:
#             return
#         if sequence:
#             flags = enet.PACKET_FLAG_UNSEQUENCED
#         else:
#             flags = enet.PACKET_FLAG_RELIABLE
#         data = ByteWriter()
#         contained.write(data)
#         packet = enet.Packet(str(data), flags)
#         self.peer.send(0, packet)
#     
#     # events
# 
#     def on_connect(self):
#         pass
#     
#     def on_disconnect(self):
#         pass
#     
#     # properties
#         
#     @property
#     def latency(self):
#         return self.peer.roundTripTime
# 
# Path: pyspades/constants.py
# MASTER_VERSION = 31
# 
# Path: pyspades/web.py
# def getPage(url, bindAddress = None, *arg, **kw):
#     # reimplemented here to insert bindAddress
# 
#     # _parse() deprecated in twisted 13.1.0 in favor of the _URI class
#     if hasattr(client, '_parse'):
#         scheme, host, port, path = client._parse(url)
#     else:
#         # _URI class renamed to URI in 15.0.0
#         try:
#             from twisted.web.client import _URI as URI
#         except ImportError:
#             from twisted.web.client import URI
#         
#         uri = URI.fromBytes(url)
#         scheme = uri.scheme
#         host = uri.host
#         port = uri.port
#         path = uri.path
# 
#     factory = HTTPClientFactory(url, *arg, **kw)
#     factory.noisy = False
# 	
#     if scheme == 'https':
#         from twisted.internet import ssl
#         context = ssl.ClientContextFactory()
#         reactor.connectSSL(host, port, factory, context, 
#             bindAddress = bindAddress)
#     else:
#         reactor.connectTCP(host, port, factory, bindAddress = bindAddress)
#     return factory.deferred


from pyspades.loaders import Loader
from pyspades.protocol import BaseConnection
from pyspades.loaders import *
from pyspades.common import *
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from pyspades.bytes import ByteReader
from pyspades.constants import MASTER_VERSION
from pyspades.web import getPage
import random
MAX_GAME_MODE_SIZE = 7

HOST = 'master.buildandshoot.com'

if STAGING:
    PORT = 32885

class AddServer(Loader):
    __slots__ = ['count', 'max_players', 'name', 'port', 'game_mode', 'map']
    
    id = 4
    
    def read(self, reader):
        if reader.dataLeft() == 1:
            self.count = reader.readByte(True)
        else:
            self.max_players = reader.readByte(True)
            self.port = reader.readShort(True, False)
            self.name = reader.readString()
            self.game_mode = reader.readString()
            self.map = reader.readString()
    
    def write(self, reader):
        if self.count is None:
            reader.writeByte(self.max_players)
            reader.writeShort(self.port, True, False)
            reader.writeString(self.name)
            reader.writeString(self.game_mode)
            reader.writeString(self.map)
        else:

# Path: pyspades/protocol.py
# class BaseConnection(object):
#     disconnected = False
#     timeout_call = None
#     def __init__(self, protocol, peer):
#         self.protocol = protocol
#         self.peer = peer
#     
#     def timed_out(self):
#         self.disconnect()
#     
#     def disconnect(self, data = 0):
#         if self.disconnected:
#             return
#         self.disconnected = True
#         self.peer.disconnect(data);
#         self.protocol.remove_peer(self.peer)
#         self.on_disconnect()
#     
#     def loader_received(self, loader):
#         raise NotImplementedError('loader_received() not implemented')
#     
#     def send_contained(self, contained, sequence = False):
#         if self.disconnected:
#             return
#         if sequence:
#             flags = enet.PACKET_FLAG_UNSEQUENCED
#         else:
#             flags = enet.PACKET_FLAG_RELIABLE
#         data = ByteWriter()
#         contained.write(data)
#         packet = enet.Packet(str(data), flags)
#         self.peer.send(0, packet)
#     
#     # events
# 
#     def on_connect(self):
#         pass
#     
#     def on_disconnect(self):
#         pass
#     
#     # properties
#         
#     @property
#     def latency(self):
#         return self.peer.roundTripTime
# 
# Path: pyspades/constants.py
# MASTER_VERSION = 31
# 
# Path: pyspades/web.py
# def getPage(url, bindAddress = None, *arg, **kw):
#     # reimplemented here to insert bindAddress
# 
#     # _parse() deprecated in twisted 13.1.0 in favor of the _URI class
#     if hasattr(client, '_parse'):
#         scheme, host, port, path = client._parse(url)
#     else:
#         # _URI class renamed to URI in 15.0.0
#         try:
#             from twisted.web.client import _URI as URI
#         except ImportError:
#             from twisted.web.client import URI
#         
#         uri = URI.fromBytes(url)
#         scheme = uri.scheme
#         host = uri.host
#         port = uri.port
#         path = uri.path
# 
#     factory = HTTPClientFactory(url, *arg, **kw)
#     factory.noisy = False
# 	
#     if scheme == 'https':
#         from twisted.internet import ssl
#         context = ssl.ClientContextFactory()
#         reactor.connectSSL(host, port, factory, context, 
#             bindAddress = bindAddress)
#     else:
#         reactor.connectTCP(host, port, factory, bindAddress = bindAddress)
#     return factory.deferred


# Path: pyspades/master.py
from pyspades.loaders import Loader
from pyspades.protocol import BaseConnection
from pyspades.loaders import *
from pyspades.common import *
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from pyspades.bytes import ByteReader
from pyspades.constants import MASTER_VERSION
from pyspades.web import getPage
import random

MAX_GAME_MODE_SIZE = 7

HOST = 'master.buildandshoot.com'

if STAGING:
    PORT = 32885

class AddServer(Loader):
    __slots__ = ['count', 'max_players', 'name', 'port', 'game_mode', 'map']
    
    id = 4
    
    def read(self, reader):
        if reader.dataLeft() == 1:
            self.count = reader.readByte(True)
        else:
            self.max_players = reader.readByte(True)
            self.port = reader.readShort(True, False)
            self.name = reader.readString()
            self.game_mode = reader.readString()
            self.map = reader.readString()
    
    def write(self, reader):
        if self.count is None:
            reader.writeByte(self.max_players)
            reader.writeShort(self.port, True, False)
            reader.writeString(self.name)
            reader.writeString(self.game_mode)
            reader.writeString(self.map)
        else:

            reader.writeByte(self.count, True)