Exemple #1
0
    def make_player_move(self, move_x, move_y):
        player_move = (move_x, move_y)
        if player_move not in self.possible_moves:
            raise error.EInternalError(error.ERROR_INVALID_MOVE)

        self.game.make_move(self.live_player, move_x, move_y)
        del self.possible_moves[player_move]
    def do_broker_callback_tasks(self, queue):
        while True:
            try:
                msg = yield queue.get()
                log.msg("DEBUG: do_broker_callback_tasks: got msg %s" % msg)
                unpacked_msg = self.service.unpack(msg.content.body)
                log.msg("DEBUG: do_broker_callback_tasks: unpacked_msg is %s" % str(unpacked_msg))
                corr_id = msg.content.properties['correlation id']
                if corr_id not in self.broker_callback_map:
                    log.err("do_broker_callback_tasks: invalid corr_id %s" % corr_id)
                    continue

                d, f_name, exchange = self.broker_callback_map[corr_id]
                if self.is_broker_error(unpacked_msg):
                    if unpacked_msg["err_id"] == error.ERROR_INTERNAL_SERVICE_ERROR:
                        d.errback(BrokerError(unpacked_msg["err_id"], unpacked_msg["err_msg"],
                                              f_name=f_name, exchange=exchange))
                    else:
                        d.errback(error.EInternalError(unpacked_msg["err_id"]))
                else:
                    d.callback(unpacked_msg)

                del self.broker_callback_map[corr_id]
            except Closed, e_inst:
                log.msg("do_broker_callback_tasks: queue is closed: %s" % str(e_inst))
                break
            except Exception:
                log.error(str(msg))
                formatted_traceback = traceback.format_exc()
                log.error(formatted_traceback)
Exemple #3
0
 def make_computer_move(self):
     random_move = random.choice(self.possible_moves.keys())
     if random_move not in self.possible_moves:
         raise error.EInternalError(error.ERROR_INVALID_MOVE)
     self.game.make_move(self.computer_player, random_move[0],
                         random_move[1])
     del self.possible_moves[random_move]
Exemple #4
0
    def post(self):
        sid = self.get_argument("sid")
        validate_sid(sid)
        x = int(self.get_argument("x"))
        y = int(self.get_argument("y"))
        player = get_player(sid)

        if player.id not in service.games:
            raise error.EInternalError(error.ERROR_NO_ACTIVE_GAMES)

        simple_game = service.games[player.id]
        simple_game.make_player_move(x, y)
        is_game_over = simple_game.check_game_over()
        if not is_game_over:
            simple_game.make_computer_move()
        is_game_over = simple_game.check_game_over()
        if is_game_over:
            winner = simple_game.get_winner()
            if winner is None:
                #draw, do nothing
                pass
            elif winner == simple_game.computer_player:
                yield player.take_exp(player_model.SIMPLE_GAME_LOOSE_EXP_DELTA)
            elif winner == simple_game.live_player:
                yield player.give_exp(player_model.SIMPLE_GAME_WIN_EXP_DELTA)

        resp = {
            "game": simple_game.as_dict(),
        }
        self.write(service.pack(resp))
Exemple #5
0
    def load_by_id(self):
        sql_cmd = """SELECT id, nickname, xp, email, created, updated FROM
                     players WHERE id=%(id)s"""
        sql_data = {
            "id": self.id,
        }
        player_info = yield service.sql_db.runQuery(sql_cmd, sql_data)
        if not player_info:
            raise error.EInternalError(error.ERROR_INVALID_PLAYER_ID)

        player_info = player_info[0]
        for attr_name, value in player_info.iteritems():
            setattr(self, attr_name, value)
Exemple #6
0
    def post(self):
        email = self.get_argument("email")
        nickname = self.get_argument("nickname")
        password = self.get_argument("password")
        password_repeat = self.get_argument("password_repeat")

        log.msg("email is {}".format(email))
        log.msg("password is {}".format(password))

        if password != password_repeat:
            raise error.EInternalError(error.ERROR_PASSWORD_NOT_MATCH)

        new_player = player_model.Player(
                        nickname=nickname,
                        email=email,
                        password=password,
                     )
        is_exists = yield new_player.is_exists()
        if is_exists:
            raise error.EInternalError(error.ERROR_PLAYER_IS_EXISTS)

        yield new_player.insert()
        new_session = session_model.Session(player_id=new_player.id)
        yield new_session.insert()

        msg = {
            "player_id": new_player.id
        }
        res = yield service.broker.send_broker_msg(exchange="bind_fanout",
                                                   f_name="bind", msg=msg)
        game_service_id = res["game_service_id"]
        game_service_info = yield service.redis_db.get_service_info("game", game_service_id)

        resp = {
            "sid": new_session.sid,
            "game_service_info": game_service_info,

        }
        self.write(service.pack(resp))
Exemple #7
0
    def load_by_sid(self):
        sql_cmd = """SELECT id, player_id, ip_addr, created, updated FROM
                     sessions WHERE sid=%(sid)s"""
        sql_data = {
            "sid": self.sid,
        }
        session_info = yield service.sql_db.runQuery(sql_cmd, sql_data)
        if not session_info:
            raise error.EInternalError(error.ERROR_INVALID_SID)

        session_info = session_info[0]
        for attr_name, value in session_info.iteritems():
            setattr(self, attr_name, value)
Exemple #8
0
    def can_login(self):
        if self.password_hash is None:
            raise error.EInternalError(error.ERROR_NO_PASSWORD_HASH)

        sql_cmd = """SELECT COUNT(*) as player_count FROM players WHERE
                     email=%(email)s AND password_hash=%(password_hash)s"""
        sql_args = {
            "email": self.email,
            "password_hash": self.password_hash,
        }
        res = yield service.sql_db.runQuery(sql_cmd, sql_args)
        can_login = False
        if res[0]["player_count"] > 0:
            can_login = True

        defer.returnValue(can_login)
Exemple #9
0
    def post(self):
        sid = self.get_argument("sid")

        session = session_model.Session(sid=sid)
        yield session.load_by_sid()
        log.msg("session.player_id is {}".format(session.player_id))
        is_my_player = yield service.redis_db.is_my_player(session.player_id)
        if not is_my_player:
            raise error.EInternalError(error.ERROR_GAME_SERVER_NOT_MY_PLAYER,
                                       player_id=session.player_id)

        service.sessions[sid] = session

        player = player_model.Player(id=session.player_id)
        yield player.load_by_id()

        service.players[session.player_id] = player
        resp = {
            "player_id": player.id,
        }
        self.write(service.pack(resp))
 def do_queue_tasks(self, queue):
     while True:
         try:
             msg = yield queue.get()
             log.msg("DEBUG: do_queue_tasks: got msg %s" % msg)
             unpacked_msg = self.service.unpack(msg.content.body)
             log.msg("DEBUG: do_queue_tasks: unpacked_msg is %s" % unpacked_msg)
             f_name = unpacked_msg["f_name"]
             log.msg("DEBUG: self.handlers_map are {}".format(self.handlers_map))
             if f_name not in self.handlers_map:
                 raise error.EInternalError(error.ERROR_NO_BROKER_METHOD)
             f = self.handlers_map[f_name]
             d = f(unpacked_msg)
             d.addCallback(self.do_callback, msg)
             d.addErrback(self.do_errback, msg)
         except Closed, e_inst:
             log.msg("do_queue_tasks: queue is closed: %s" % str(e_inst))
             break
         except Exception:
             log.err(str(msg))
             formatted_traceback = traceback.format_exc()
             log.err(formatted_traceback)
Exemple #11
0
    def post(self):
        email = self.get_argument("email")
        password = self.get_argument("password")

        log.msg("email is {}".format(email))
        log.msg("password is {}".format(password))


        player = player_model.Player(
                     email=email,
                     password=password,
                 )
        can_login = yield player.can_login()
        if not can_login:
            raise error.EInternalError(error.ERROR_BAD_LOGIN_OR_PASSWORD)

        yield player.load_by_email()

        new_session = session_model.Session(player_id=player.id)
        yield new_session.insert()

        msg = {
            "player_id": player.id
        }
        game_service_id = yield service.redis_db.get_player_server(player.id)
        if game_service_id is None:
            res = yield service.broker.send_broker_msg(exchange="bind_fanout",
                                                       f_name="bind", msg=msg)
            game_service_id = res["game_service_id"]

        game_service_info = yield service.redis_db.get_service_info("game", game_service_id)

        resp = {
            "sid": new_session.sid,
            "game_service_info": game_service_info,
        }
        self.write(service.pack(resp))
Exemple #12
0
def validate_sid(sid):
    if sid not in service.sessions:
        raise error.EInternalError(error.ERROR_INVALID_SID)