Exemplo n.º 1
0
    def start_replay(self, rep, event_cb):
        assert self.state == 'initial'

        self.state = 'replay'

        from client.core import PeerPlayer, TheLittleBrother
        from thb import modes

        g = modes[rep.game_mode]()
        self.server = ReplayEndpoint(rep, g)

        pl = [PeerPlayer.parse(i) for i in rep.users]

        g.players = BatchList(pl)
        me = g.players[rep.me_index]
        me.__class__ = TheLittleBrother
        me.server = self.server
        g.me = me
        g.game_params = rep.game_params
        g.game_items = rep.game_items
        log.info('=======REPLAY STARTED=======')

        # g.start()  Starts by UI

        @g.link_exception
        def crash(*a):
            self.state = 'initial'
            event_cb('game_crashed', g)

        @g.link_value
        def finish(*a):
            self.state = 'initial'
            v = g.get()
            if not isinstance(v, ForcedKill):
                event_cb('client_game_finished', g)

            event_cb('end_game', g)

        return g
Exemplo n.º 2
0
    def start_replay(self, rep, event_cb):
        assert self.state == 'initial'

        self.state = 'replay'

        from client.core import PeerPlayer, TheLittleBrother
        from thb import modes

        g = modes[rep.game_mode]()
        self.server = ReplayEndpoint(rep, g)

        pl = [PeerPlayer.parse(i) for i in rep.users]

        g.players = BatchList(pl)
        me = g.players[rep.me_index]
        me.__class__ = TheLittleBrother
        me.server = self.server
        g.me = me
        g.game_params = rep.game_params
        g.game_items = rep.game_items
        log.info('=======REPLAY STARTED=======')

        # g.start()  Starts by UI

        @g.link_exception
        def crash(*a):
            self.state = 'initial'
            event_cb('game_crashed', g)

        @g.link_value
        def finish(*a):
            self.state = 'initial'
            v = g.get()
            if not isinstance(v, ForcedKill):
                event_cb('client_game_finished', g)

            event_cb('end_game', g)

        return g
Exemplo n.º 3
0
class Executive(object):
    def __init__(self):
        self.state = 'initial'  # initial connected

    def connect_server(self, addr, event_cb):
        if not self.state == 'initial':
            return 'server_already_connected'

        try:
            self.state = 'connecting'
            s = socket.create_connection(addr)
            self.server = svr = Server.spawn(s, addr)
            svr.link_exception(lambda *a: event_cb('server_dropped'))
            self.gamemgr = GameManager(event_cb)
            self.state = 'connected'
            self.gamemgr.start()
            return None

            # return 'server_connected'
        except Exception:
            self.state = 'initial'
            log.exception('Error connecting server')
            return 'server_connect_failed'

    def disconnect(self):
        if self.state != 'connected':
            return 'not_connected'
        else:
            self.state = 'dying'
            loop = gevent.getcurrent() is self.gamemgr

            @gevent.spawn
            def kill():
                self.server.shutdown()
                self.gamemgr.join()
                self.server = self.gamemgr = None
                self.state = 'initial'

            if not loop:
                kill.join()

            return 'disconnected'

    def update(self, update_cb):
        from options import options
        import settings
        if options.no_update:
            return 'update_disabled'

        errord = [False]

        def do_update(name, path, server):
            up = Autoupdate(path)
            try:
                for p in up.update(server):
                    update_cb(name, p)
            except Exception as e:
                log.exception(e)
                errord[0] = True
                update_cb('error', e)

        pool = Pool(2)

        pool.spawn(do_update, 'logic_progress', settings.LOGIC_UPDATE_BASE,
                   settings.LOGIC_UPDATE_SERVER)
        if settings.INTERPRETER_UPDATE_BASE:
            pool.spawn(do_update, 'interpreter_progress',
                       settings.INTERPRETER_UPDATE_BASE,
                       settings.INTERPRETER_UPDATE_SERVER)

        pool.join()

        return 'updated' if not errord[0] else 'error'

    def switch_version(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.switch(version)

    def is_version_match(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.is_version_match(version)

    def get_current_version(self):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.get_current_version()

    def is_version_present(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.is_version_present(version)

    def save_replay(self, filename):
        with open(filename, 'wb') as f:
            f.write(self.gamemgr.last_replay.dumps())

    def start_replay(self, rep, event_cb):
        assert self.state == 'initial'

        self.state = 'replay'

        from client.core import PeerPlayer, TheLittleBrother
        from thb import modes

        g = modes[rep.game_mode]()
        self.server = ReplayEndpoint(rep, g)

        pl = [PeerPlayer.parse(i) for i in rep.users]

        g.players = BatchList(pl)
        me = g.players[rep.me_index]
        me.__class__ = TheLittleBrother
        me.server = self.server
        g.me = me
        g.game_params = rep.game_params
        g.game_items = rep.game_items
        log.info('=======REPLAY STARTED=======')

        # g.start()  Starts by UI

        @g.link_exception
        def crash(*a):
            self.state = 'initial'
            event_cb('game_crashed', g)

        @g.link_value
        def finish(*a):
            self.state = 'initial'
            v = g.get()
            if not isinstance(v, ForcedKill):
                event_cb('client_game_finished', g)

            event_cb('end_game', g)

        return g

    def end_replay(self):
        assert self.state == 'replay'
        self.server.end_replay()
        self.server = None

    def _op(_type):
        def wrapper(self, *args):
            if not (self.state == 'connected'):
                return 'connect_first'

            self.server.write([_type, args])

        wrapper.__name__ = _type
        return wrapper

    def _l2op(category, _type):
        def wrapper(self, *args):
            if not (self.state == 'connected'):
                return 'connect_first'

            self.server.write([category, [_type, args]])

        wrapper.__name__ = _type
        return wrapper

    auth = _op('auth')
    pong = _op('pong')
    heartbeat = _op('heartbeat')

    cancel_ready = _l2op('lobby', 'cancel_ready')
    change_location = _l2op('lobby', 'change_location')
    chat = _l2op('lobby', 'chat')
    create_game = _l2op('lobby', 'create_game')
    exit_game = _l2op('lobby', 'exit_game')
    get_lobbyinfo = _l2op('lobby', 'get_lobbyinfo')
    get_ready = _l2op('lobby', 'get_ready')
    invite_grant = _op('invite_grant')
    invite_user = _l2op('lobby', 'invite_user')
    join_game = _l2op('lobby', 'join_game')
    kick_observer = _l2op('lobby', 'kick_observer')
    kick_user = _l2op('lobby', 'kick_user')
    observe_grant = _op('observe_grant')
    observe_user = _l2op('lobby', 'observe_user')
    query_gameinfo = _l2op('lobby', 'query_gameinfo')
    quick_start_game = _l2op('lobby', 'quick_start_game')
    set_game_param = _l2op('lobby', 'set_game_param')
    speaker = _l2op('lobby', 'speaker')
    use_ingame_item = _l2op('lobby', 'use_item')

    item_backpack = _l2op('item', 'backpack')
    item_use = _l2op('item', 'use')
    item_drop = _l2op('item', 'drop')
    item_exchange = _l2op('item', 'exchange')
    item_buy = _l2op('item', 'buy')
    item_sell = _l2op('item', 'sell')
    item_cancel_sell = _l2op('item', 'cancel_sell')
    item_lottery = _l2op('item', 'lottery')

    del _op, _l2op
Exemplo n.º 4
0
class Executive(object):
    def __init__(self):
        self.state = 'initial'  # initial connected

    def connect_server(self, addr, event_cb):
        if not self.state == 'initial':
            return 'server_already_connected'

        try:
            self.state = 'connecting'
            s = socket.create_connection(addr)
            self.server = svr = Server.spawn(s, addr)
            svr.link_exception(lambda *a: event_cb('server_dropped'))
            self.gamemgr = GameManager(event_cb)
            self.state = 'connected'
            self.gamemgr.start()
            return None

            # return 'server_connected'
        except Exception:
            self.state = 'initial'
            log.exception('Error connecting server')
            return 'server_connect_failed'

    def disconnect(self):
        if self.state != 'connected':
            return 'not_connected'
        else:
            self.state = 'dying'
            loop = gevent.getcurrent() is self.gamemgr

            @gevent.spawn
            def kill():
                self.server.shutdown()
                self.gamemgr.join()
                self.server = self.gamemgr = None
                self.state = 'initial'

            if not loop:
                kill.join()

            return 'disconnected'

    def update(self, update_cb):
        from options import options
        import settings
        if options.no_update:
            return 'update_disabled'

        errord = [False]

        def do_update(name, path, server):
            up = Autoupdate(path)
            try:
                for p in up.update(server):
                    update_cb(name, p)
            except Exception as e:
                log.exception(e)
                errord[0] = True
                update_cb('error', e)

        pool = Pool(2)

        pool.spawn(do_update, 'logic_progress', settings.LOGIC_UPDATE_BASE, settings.LOGIC_UPDATE_SERVER)
        if settings.INTERPRETER_UPDATE_BASE:
            pool.spawn(do_update, 'interpreter_progress', settings.INTERPRETER_UPDATE_BASE, settings.INTERPRETER_UPDATE_SERVER)

        pool.join()

        return 'updated' if not errord[0] else 'error'

    def switch_version(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.switch(version)

    def is_version_match(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.is_version_match(version)

    def get_current_version(self):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.get_current_version()

    def is_version_present(self, version):
        import settings
        up = Autoupdate(settings.LOGIC_UPDATE_BASE)
        return up.is_version_present(version)

    def save_replay(self, filename):
        with open(filename, 'wb') as f:
            f.write(self.gamemgr.last_replay.dumps())

    def start_replay(self, rep, event_cb):
        assert self.state == 'initial'

        self.state = 'replay'

        from client.core import PeerPlayer, TheLittleBrother
        from thb import modes

        g = modes[rep.game_mode]()
        self.server = ReplayEndpoint(rep, g)

        pl = [PeerPlayer.parse(i) for i in rep.users]

        g.players = BatchList(pl)
        me = g.players[rep.me_index]
        me.__class__ = TheLittleBrother
        me.server = self.server
        g.me = me
        g.game_params = rep.game_params
        g.game_items = rep.game_items
        log.info('=======REPLAY STARTED=======')

        # g.start()  Starts by UI

        @g.link_exception
        def crash(*a):
            self.state = 'initial'
            event_cb('game_crashed', g)

        @g.link_value
        def finish(*a):
            self.state = 'initial'
            v = g.get()
            if not isinstance(v, ForcedKill):
                event_cb('client_game_finished', g)

            event_cb('end_game', g)

        return g

    def end_replay(self):
        assert self.state == 'replay'
        self.server.end_replay()
        self.server = None

    def _op(_type):
        def wrapper(self, *args):
            if not (self.state == 'connected'):
                return 'connect_first'

            self.server.write([_type, args])
        wrapper.__name__ = _type
        return wrapper

    def _l2op(category, _type):
        def wrapper(self, *args):
            if not (self.state == 'connected'):
                return 'connect_first'

            self.server.write([category, [_type, args]])
        wrapper.__name__ = _type
        return wrapper

    auth      = _op('auth')
    pong      = _op('pong')
    heartbeat = _op('heartbeat')

    cancel_ready     = _l2op('lobby', 'cancel_ready')
    change_location  = _l2op('lobby', 'change_location')
    chat             = _l2op('lobby', 'chat')
    create_game      = _l2op('lobby', 'create_game')
    exit_game        = _l2op('lobby', 'exit_game')
    get_lobbyinfo    = _l2op('lobby', 'get_lobbyinfo')
    get_ready        = _l2op('lobby', 'get_ready')
    invite_grant     = _op('invite_grant')
    invite_user      = _l2op('lobby', 'invite_user')
    join_game        = _l2op('lobby', 'join_game')
    kick_observer    = _l2op('lobby', 'kick_observer')
    kick_user        = _l2op('lobby', 'kick_user')
    observe_grant    = _op('observe_grant')
    observe_user     = _l2op('lobby', 'observe_user')
    query_gameinfo   = _l2op('lobby', 'query_gameinfo')
    quick_start_game = _l2op('lobby', 'quick_start_game')
    set_game_param   = _l2op('lobby', 'set_game_param')
    speaker          = _l2op('lobby', 'speaker')
    use_ingame_item  = _l2op('lobby', 'use_item')

    item_backpack    = _l2op('item', 'backpack')
    item_use         = _l2op('item', 'use')
    item_drop        = _l2op('item', 'drop')
    item_exchange    = _l2op('item', 'exchange')
    item_buy         = _l2op('item', 'buy')
    item_sell        = _l2op('item', 'sell')
    item_cancel_sell = _l2op('item', 'cancel_sell')
    item_lottery     = _l2op('item', 'lottery')

    del _op, _l2op