Beispiel #1
0
    def _run(self):
        cmds = {}
        self.account = None

        def handler(*state):
            def register(f):
                for s in state:
                    d = cmds.setdefault(s, {})
                    d[f.__name__] = f
            return register

        # --------- Handlers ---------
        @handler('connected')
        def register(self,data):
            self.write(['not_impl', None])

        @handler('connected')
        def auth(self, cred):
            login, password = cred
            acc = Account.authenticate(login, password)
            if acc:
                self.account = acc
                if not acc.available():
                    self.write(['auth_result', 'not_available'])
                    self.close()
                else:
                    self.write(['auth_result', 'success'])
                    self.account = acc
                    hall.new_user(self)

            else:
                self.write(['auth_result', 'invalid_credential'])

        @handler('hang')
        def create_game(self, arg):
            _type, name = arg
            g = hall.create_game(self, _type, name)
            hall.join_game(self, g.gameid)

        @handler('hang')
        def join_game(self, gameid):
            hall.join_game(self, gameid)

        @handler('hang')
        def get_hallinfo(self, _):
            hall.send_hallinfo(self)

        @handler('hang')
        def quick_start_game(self, _):
            hall.quick_start_game(self)

        @handler('hang')
        def observe_user(self, uid):
            hall.observe_user(self, uid)

        @handler('inroomwait', 'ready', 'ingame')
        def observe_grant(self, rst):
            hall.observe_grant(self, rst)

        @handler('hang')
        def query_gameinfo(self, gid):
            hall.query_gameinfo(self, gid)

        @handler('inroomwait')
        def get_ready(self, _):
            hall.get_ready(self)

        @handler('inroomwait', 'ready', 'ingame', 'observing')
        def exit_game(self, _):
            hall.exit_game(self)

        @handler('inroomwait', 'ready')
        def kick_user(self, uid):
            hall.kick_user(self, uid)

        @handler('inroomwait')
        def change_location(self, loc):
            hall.change_location(self, loc)

        @handler('ready')
        def cancel_ready(self, _):
            hall.cancel_ready(self)

        @handler('ingame')
        def gamedata(self, data):
            p = Packet(data)
            p.scan_count = 0
            self.gdqueue.append(p)
            self.gdevent.set()

        @handler('__any__')
        def _disconnect(self, _):
            self.write(['bye', None])
            self.close()

        @handler('hang', 'inroomwait', 'ready', 'ingame', 'observing')
        def chat(self, data):
            hall.chat(self, data)

        @handler('hang', 'inroomwait', 'ready', 'ingame', 'observing')
        def speaker(self, data):
            hall.speaker(self, data)

        # --------- End ---------

        # ----- Banner -----
        from settings import VERSION
        self.write(['thbattle_greeting', VERSION])
        # ------------------

        self.state = 'connected'
        while True:
            try:
                cmd, data = self.read()
                f = cmds[self.state].get(cmd)
                if not f:
                    f = cmds['__any__'].get(cmd)

                if (self.account or cmd == 'auth') and f:
                    f(self, data)
                else:
                    self.write(['invalid_command', [cmd, data]])

            except EndpointDied as e:
                self.gdqueue.append(e)
                self.gdevent.set()
                break

        # client died, do clean ups
        if self.state not in ('connected', 'hang'):
            hall.exit_game(self, drops=True)

        if self.state != 'connected':
            hall.user_exit(self)
Beispiel #2
0
    def _run(self):
        cmds = {}
        self.account = None

        def handler(*state):
            def register(f):
                for s in state:
                    d = cmds.setdefault(s, {})
                    d[f.__name__] = f

            return register

        # --------- Handlers ---------
        @handler('connected')
        def register(self, data):
            self.write(['not_impl', None])

        @handler('connected')
        def auth(self, cred):
            login, password = cred
            acc = Account.authenticate(login, password)
            if acc:
                self.account = acc
                if not acc.available():
                    self.write(['auth_result', 'not_available'])
                    self.close()
                else:
                    self.write(['auth_result', 'success'])
                    self.account = acc
                    hall.new_user(self)

            else:
                self.write(['auth_result', 'invalid_credential'])

        @handler('hang')
        def create_game(self, arg):
            _type, name = arg
            g = hall.create_game(self, _type, name)
            hall.join_game(self, g.gameid)

        @handler('hang')
        def join_game(self, gameid):
            hall.join_game(self, gameid)

        @handler('hang')
        def get_hallinfo(self, _):
            hall.send_hallinfo(self)

        @handler('hang')
        def quick_start_game(self, _):
            hall.quick_start_game(self)

        @handler('hang')
        def observe_user(self, uid):
            hall.observe_user(self, uid)

        @handler('inroomwait', 'ready', 'ingame')
        def observe_grant(self, rst):
            hall.observe_grant(self, rst)

        @handler('hang')
        def query_gameinfo(self, gid):
            hall.query_gameinfo(self, gid)

        @handler('inroomwait')
        def get_ready(self, _):
            hall.get_ready(self)

        @handler('inroomwait', 'ready', 'ingame', 'observing')
        def exit_game(self, _):
            hall.exit_game(self)

        @handler('inroomwait', 'ready')
        def kick_user(self, uid):
            hall.kick_user(self, uid)

        @handler('inroomwait')
        def change_location(self, loc):
            hall.change_location(self, loc)

        @handler('ready')
        def cancel_ready(self, _):
            hall.cancel_ready(self)

        @handler('ingame')
        def gamedata(self, data):
            p = Packet(data)
            p.scan_count = 0
            self.gdqueue.append(p)
            self.gdevent.set()

        @handler('__any__')
        def _disconnect(self, _):
            self.write(['bye', None])
            self.close()

        @handler('hang', 'inroomwait', 'ready', 'ingame', 'observing')
        def chat(self, data):
            hall.chat(self, data)

        @handler('hang', 'inroomwait', 'ready', 'ingame', 'observing')
        def speaker(self, data):
            hall.speaker(self, data)

        # --------- End ---------

        # ----- Banner -----
        from settings import VERSION
        self.write(['thbattle_greeting', VERSION])
        # ------------------

        self.state = 'connected'
        while True:
            try:
                cmd, data = self.read()
                f = cmds[self.state].get(cmd)
                if not f:
                    f = cmds['__any__'].get(cmd)

                if (self.account or cmd == 'auth') and f:
                    f(self, data)
                else:
                    self.write(['invalid_command', [cmd, data]])

            except EndpointDied as e:
                self.gdqueue.append(e)
                self.gdevent.set()
                break

        # client died, do clean ups
        if self.state not in ('connected', 'hang'):
            hall.exit_game(self, drops=True)

        if self.state != 'connected':
            hall.user_exit(self)
Beispiel #3
0
 def exit_game(self, _):
     hall.exit_game(self)
Beispiel #4
0
 def exit_game(self, _):
     hall.exit_game(self)