Esempio n. 1
0
 def syncDB(self):
     """同步到数据库
     """
     # state = int(self.get('_state'))
     state = self.get('_state')
     tablename = self._name.split(':')[0]
     if state == MMODE_STATE_ORI:
         return
     elif state == MMODE_STATE_NEW:
         props = self.get('data')
         props = self.dumps(props)
         pk = self.get('_pk')
         result = util.InsertIntoDB(tablename, props)
     elif state == MMODE_STATE_UPDATE:
         props = self.get('data')
         pk = self.get('_pk')
         props = self.dumps(props)
         prere = {pk: props.get(pk)}
         del (props[pk])
         util.UpdateWithDict(tablename, props, prere)
         result = True
     else:
         pk = self.get('_pk')
         props = self.get('data')
         props = self.dumps(props)
         prere = {pk: props.get(pk)}
         result = util.DeleteFromDB(tablename, prere)
     if result:
         RedisObject.update(self, '_state', MMODE_STATE_ORI)
Esempio n. 2
0
def user_bind():
    user_name = request.args.get('name')
    user_pwd = request.args.get('pwd')
    tourist_id = request.args.get('tid')
    logger.info('bind name:%s pwd:%s %s', user_name, user_pwd, tourist_id)

    if not user_name.isalnum():
        return json.dumps(
            dict(result=False, result_no=-2, message='error name'))

    if len(user_name) > 32:
        return json.dumps(
            dict(result=False, result_no=-3, message='error name len'))

    if len(user_pwd) > 32:
        return json.dumps(
            dict(result=False, result_no=-4, message='error pwd len'))

    get_result = util.GetOneRecordInfo(
        USER_TABLE_NAME,
        dict(account_name=tourist_id, account_password=TOURIST_PWD))
    if get_result is None:
        return json.dumps(
            dict(result=False, message='account name or password error!'))

    result = util.UpdateWithDict(
        USER_TABLE_NAME,
        dict(account_name=user_name, account_password=user_pwd, device_id=''),
        dict(id=get_result['id']))
    logger.info('bind result:%s', result)
    return json.dumps(dict(result=result))
Esempio n. 3
0
def refresh_rank_data(player, rank_id, skill, skill_level):
    red_units = cPickle.dumps(player.fight_cache_component.red_unit)
    slots = cPickle.dumps(line_up_info(player))
    hero_nos = player.line_up_component.hero_nos
    best_skill = player.line_up_component.get_skill_id_by_unpar(skill)
    rank_data = dict(hero_ids=cPickle.dumps(hero_nos),
                     level=player.level.level,
                     nickname=player.base_info.base_name,
                     best_skill=best_skill,
                     unpar_skill=skill,
                     unpar_skill_level=skill_level,
                     ap=player.line_up_component.combat_power,
                     character_id=player.base_info.id,
                     units=red_units,
                     slots=slots)

    prere = dict(id=rank_id)
    result = util.UpdateWithDict(PVP_TABLE_NAME, rank_data, prere)
    if not result:
        raise Exception('update pvp fail!! id:%s' % rank_id)
Esempio n. 4
0
def pvp_fight_request_1505(data, player):
    request = pvp_rank_pb2.PvpFightRequest()
    request.ParseFromString(data)
    __skill = request.skill
    __best_skill = player.line_up_component.get_skill_id_by_unpar(__skill)
    __skill_level = 0
    if __skill in player.line_up_component.unpars:
        __skill_level = player.line_up_component.unpars[request.skill]
    else:
        logger.error('error skill level:%s,%s', request.skill,
                     player.line_up_component.unpars.keys())

    line_up = {}  # {hero_id:pos}
    for line in request.lineup:
        if not line.hero_id:
            continue
        line_up[line.hero_id] = line.pos

    player.line_up_component.line_up_order = line_up
    player.line_up_component.save_data()

    prere = dict(character_id=player.base_info.id)
    record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere, ['id'])
    before_player_rank = 0
    if record:
        before_player_rank = record.get('id')
        refresh_rank_data(player, before_player_rank,
                          __skill, __skill_level)

    if before_player_rank == request.challenge_rank:
        logger.error('cant not fight self')
        return False

    prere = dict(id=request.challenge_rank)
    record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere,
                                   ['character_id',
                                    'nickname',
                                    'level',
                                    'ap',
                                    'best_skill',
                                    'unpar_skill',
                                    'unpar_skill_level',
                                    'units',
                                    'slots',
                                    'hero_ids'])
    blue_units = record.get('units')
    # print "blue_units:", blue_units
    blue_units = cPickle.loads(blue_units)
    # print "blue_units:", blue_units
    red_units = player.fight_cache_component.red_unit
    player.fight_cache_component.awake_hero_units(blue_units)
    player.fight_cache_component.awake_hero_units(red_units)

    process = BattlePVPProcess(red_units, __best_skill, player.level.level, blue_units,
                               record.get('best_skill', 0), record.get('level', 1))
    fight_result = process.process()

    logger.debug("fight result:%s" % fight_result)

    if fight_result:
        logger.debug("fight result:True:%s:%s",
                     before_player_rank,
                     request.challenge_rank)
        if before_player_rank != 0:
            if request.challenge_rank < before_player_rank:
                prere = dict(id=before_player_rank)
                result = util.UpdateWithDict(PVP_TABLE_NAME, record, prere)
                logger.info('update result:%s', result)
                refresh_rank_data(player, request.challenge_rank,
                                  __skill, __skill_level)
        else:
            refresh_rank_data(player, request.challenge_rank,
                              __skill, __skill_level)
    else:
        logger.debug("fight result:False")

    response = pvp_rank_pb2.PvpFightResponse()
    response.res.result = True
    for slot_no, red_unit in red_units.items():
        if not red_unit:
            continue
        red_add = response.red.add()
        assemble(red_add, red_unit)
    for slot_no, blue_unit in blue_units.items():
        if not blue_unit:
            continue
        blue_add = response.blue.add()
        assemble(blue_add, blue_unit)
    response.red_skill = __skill
    response.red_skill_level = __skill_level
    response.blue_skill = record.get('unpar_skill')
    response.blue_skill_level = record.get('unpar_skill_level')

    lively_event = CountEvent.create_event(EventType.SPORTS, 1, ifadd=True)
    tstatus = player.tasks.check_inter(lively_event)
    if tstatus:
        task_data = task_status(player)
        remote_gate.push_object_remote(1234, task_data, [player.dynamic_id])
    return response.SerializeToString()
Esempio n. 5
0
def check_mem_db(delta):
    """同步内存数据到数据库
    """
    new_character_ids = tb_character_info.smem('new')
    new_character_ids = filter(lambda x: x >= 10000, new_character_ids)
    for char_id in new_character_ids:
        character_obj = tb_character_info.getObj(char_id)
        character_info = character_obj.hgetall()
        character_equipments = character_obj.getObj('equipments').hgetall()
        character_mails = character_obj.getObj('mails').hgetall()
        character_heroes = character_obj.getObj('heroes').hgetall()
        user_data = dict(id=char_id,
                         base_info=cPickle.dumps(character_info),
                         heroes=cPickle.dumps(character_heroes),
                         mails=cPickle.dumps(character_mails),
                         equipments=cPickle.dumps(character_equipments))

        insert_result = util.InsertIntoDB(CHARACTER_TABLE_NAME, user_data)
        if not insert_result:
            logger.error('insert id:%s error', char_id)
            result = util.GetOneRecordInfo(CHARACTER_TABLE_NAME,
                                           dict(id=char_id), ['id'])
            if result:
                tb_character_info.sadd('all', char_id)
                tb_character_info.srem('new', char_id)
        else:
            tb_character_info.sadd('all', char_id)
            tb_character_info.srem('new', char_id)
            logger.info('new character:%s', char_id)
        break

    global ALL_CHARACTER_IDS
    if not ALL_CHARACTER_IDS:
        ALL_CHARACTER_IDS = tb_character_info.smem('all')

    for char_id in ALL_CHARACTER_IDS:
        character_obj = tb_character_info.getObj(char_id)
        character_info = character_obj.hgetall()
        character_equipments = character_obj.getObj('equipments').hgetall()
        character_mails = character_obj.getObj('mails').hgetall()
        character_heroes = character_obj.getObj('heroes').hgetall()
        user_data = dict(base_info=cPickle.dumps(character_info),
                         heroes=cPickle.dumps(character_heroes),
                         mails=cPickle.dumps(character_mails),
                         equipments=cPickle.dumps(character_equipments))
        pwere = dict(id=char_id)
        result = util.GetOneRecordInfo(CHARACTER_TABLE_NAME, pwere, ['id'])
        if not result:
            logger.error('all insert id:%s error', char_id)
            tb_character_info.sadd('new', char_id)
        else:
            result = util.UpdateWithDict(CHARACTER_TABLE_NAME, user_data,
                                         pwere)
            if not result:
                logger.error('update id:%s error', char_id)
            # else:
            #     logger.info('save character:%s', char_id)

        ALL_CHARACTER_IDS.remove(char_id)
        break

    if not new_character_ids and not ALL_CHARACTER_IDS:
        reactor.callLater(delta * 10, check_mem_db, delta)
    else:
        reactor.callLater(delta, check_mem_db, delta)