Example #1
0
def before_request():
    msg = []
    msg.append("----BEGIN REQUEST----")
    msg.append("Date: %s" % (datetime.datetime.now()))
    msg.append("IP Address: %s" % (request.remote_addr))
    msg.append("Method: %s" % (request.method))
    msg.append("URL: %s" % (request.url))
    if request.args is not None and len(request.args) > 0:
        msg.append("Args: %s" % (request.args))
    if request.json is not None:
        msg.append("JSON: %s" % (request.json))
    if request.form is not None and len(request.form) > 0:
        msg.append("FORM: %s" % (request.form))
    msg.append("----END REQUEST----\n")
    log.log_debug("\n".join(msg))

    if config.get_config("environment") == "PROD":
        if "/static/" in request.url:
            pass
        else:
            if "/pre_signup" not in request.url:
                return redirect("/pre_signup")

    g.total_messages = 0
    if current_user.is_authenticated:
        usr = User.get(User.uuid == current_user.uuid)
        data = ContactMessage.get_user_messages(current_user.uuid)
        g.total_messages = len(data)
Example #2
0
def get_news_list(param):
    ob_url = param[1]
    page = param[0]
    doc = pq(page)
    try:
        trs = doc('div.border_tr td:eq(1) tr:eq(1) tr').items()
        # print(trs)

        news_list = []
        for tr in trs:
            news = {}
            # 新闻日期
            date = tr('td:eq(2)').text().replace('(', '').replace(')',
                                                                  '').strip()
            news['date'] = date
            # print(date)
            # 新闻url
            url = tr('td:eq(1) a').make_links_absolute(
                'http://cmee.nuaa.edu.cn/').attr.href
            #         print(url)
            news['url'] = url
            # 新闻标题
            title = tr('td:eq(1) a').text()
            news['title'] = title

            news_list.append(news)
        if news_list:
            log.log_debug('fetch:解析列表成功' + str(news_list))
            return news_list
        else:
            log.log_error('fetch:新闻列表为空' + ob_url + '网站规则可能发生变化')
    except:
        log.log_exception('fetch:解析列表错误' + ob_url + '网站规则可能发生变化')
Example #3
0
def playing_plot_on_event_playing_create(message_type, channel, channel_type,
                                         serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingCreate()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 关注玩家进入/退出副本事件
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_ENTER, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_plot_on_event_playing_actor_enter")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_LEAVE, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_plot_on_event_playing_actor_leave")

    # 创建副本对象并加入管理器中
    playing = plot_types.Playing(message.playing_, message.template_,
                                 message.scene_)
    plot_types.PlayingManager.add(playing)

    log.log_debug("副本(id=%d,template=%d) 创建成功" %
                  (message.playing_, message.template_))
Example #4
0
def add_award(actor, template):
    log.log_debug("idol.add_award(%d, %d)" % (actor, template))

    request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 获取副本配置
    playing_config = idol_types.Config.get(template)
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % template)
        return None

    # 请求增加副本奖励
    request = ccrequest.playing.ttypes.RequestPlayingAddAward()
    request.actor_ = actor
    request.playing_template_ = template
    # 奖励
    request.awards_ = playing_config.get_awards()
    # 抽奖
    draw_awards = playing_config.get_draw_awards()
    draw_awards_size = len(draw_awards)
    if draw_awards_size > 0:
        request.draw_award_ = draw_awards[random.randint(0, draw_awards_size - 1)]
    else:
        request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField(ccentity.resource.ttypes.ResourceType.MIN, 0, 0)

    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD, request_data, len(request_data))
Example #5
0
def add_award(actor, template):
  log.log_debug("idol.add_award(%d, %d)" % (actor, template))

  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 获取副本配置
  playing_config = idol_types.Config.get(template)
  if playing_config == None:
    log.log_error("获取 副本配置失败(%d)" % template)
    return None

  # 请求增加副本奖励
  request = ccrequest.playing.ttypes.RequestPlayingAddAward()
  request.actor_ = actor
  request.playing_template_ = template
  # 奖励
  request.awards_ = playing_config.get_awards()
  # 抽奖
  draw_awards = playing_config.get_draw_awards()
  draw_awards_size = len(draw_awards)
  if draw_awards_size > 0:
    request.draw_award_ = draw_awards[random.randint(0, draw_awards_size - 1)]
  else:
    request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField(\
        ccentity.resource.ttypes.ResourceType.MIN, 0, 0)

  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD,\
      request_data, len(request_data))
Example #6
0
def generate_qmap_3d(qn, qmax, extrinsic_rotation=None, order='xyz'):
    q = numpy.linspace(-qmax, qmax, qn)
    Qz, Qy, Qx = numpy.meshgrid(q, q, q, indexing='ij')
    qmap = numpy.zeros(shape=(qn, qn, qn, 3), dtype='float')
    if order == 'xyz':
        qmap[:, :, :, 0] = Qx[:, :, :]
        qmap[:, :, :, 1] = Qy[:, :, :]
        qmap[:, :, :, 2] = Qz[:, :, :]
    elif order == 'zyx':
        qmap[:, :, :, 2] = Qx[:, :, :]
        qmap[:, :, :, 1] = Qy[:, :, :]
        qmap[:, :, :, 0] = Qz[:, :, :]
    else:
        log_and_raise_error(
            logger,
            "order=\'%s\' is not a recognised argument for this function." %
            str(order))
        return
    if extrinsic_rotation is not None:
        log_debug(logger, "Applying qmap rotation.")
        intrinsic_rotation = copy.deepcopy(extrinsic_rotation)
        intrinsic_rotation.invert()
        qmap = intrinsic_rotation.rotate_vectors(
            qmap.ravel(), order=order).reshape(qmap.shape)
    return qmap
Example #7
0
def playing_idol_on_event_playing_actor_enter(message_type, channel,
                                              channel_type, serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorEnter()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 关注玩家杀死NPC事件、角色被杀死事件
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_idol_on_event_actor_kill_npc")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_idol_on_event_actor_killed")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
        "playing_idol_on_event_playing_actor_request_complete")

    # 获取副本对象
    playing = idol_types.PlayingManager.get(message.playing_)
    if playing == None:
        log.log_error("idol副本 获取 playing(%d) 失败" % message.playing_)
        return None

    now = time.time()

    # 获取玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        # 玩家不存在时,创建并加入副本管理器中
        actor = idol_types.Actor(message.actor_, now)
        playing.add_actor(actor)

    # 副本管理器中加入一个玩家ID到副本ID的对应关系
    idol_types.PlayingManager.add_actor(message.actor_, message.playing_)

    # 请求初始化玩家
    request = ccrequest.playing.ttypes.RequestPlayingInitializeActor()
    request.actor_ = message.actor_
    request.spend_time_ = now - actor.get_start_time()
    request.scores_ = []
    request.datas_ = []

    # 序列化消息
    request_data = TSerialization.serialize(request)

    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INITIALIZE_ACTOR, \
        request_data, len(request_data))

    log.log_debug("玩家(%d) 进入 idol副本(id=%d,template=%d)" % \
        (message.actor_, message.playing_, message.template_))
Example #8
0
def playing_idol_on_event_playing_create(message_type, channel, channel_type, serialize):
  # 获取事件交互代理
  communicator_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

  # 事件消息
  message = ccevent.playing.ttypes.EventPlayingCreate()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 关注玩家进入/退出副本事件
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_ENTER, \
      message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
      "playing_idol_on_event_playing_actor_enter")
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_LEAVE, \
      message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
      "playing_idol_on_event_playing_actor_leave")

  # 创建副本对象并加入管理器中
  playing = idol_types.Playing(message.playing_, message.template_, message.scene_)
  idol_types.PlayingManager.add(playing)

  # 获取副本配置
  playing_config = idol_types.Config.get(message.template_)
  if playing_config == None:
    log.log_error("获取 副本配置失败(%d)" % message.template_)
    return None

  # 召唤一个BOSS
  facade_request.summon_npc(message.scene_, playing_config.get_pass_kill_npc(),\
      playing_config.get_pass_npc_x(), playing_config.get_pass_npc_y());

  log.log_debug("idol副本(id=%d,template=%d) 创建成功" % (message.playing_, message.template_))
def get_th_crossed_proc():
    try:
        query = """
                SELECT
                    a.id, a.request, time_to_sec(TIMEDIFF(now(),a.start_time))/60 as DIFF, b.th, a.status, b.script
                FROM
                    procmon.request_queue a, procmon.request_to_process b
                WHERE
                    (a.status = 'Starting' OR a.status = 'Running')
                    AND
                    (a.request = b.request_token)
                HAVING
                    DIFF > b.th
                """                
        
        mysqlCursor.execute(query)
    
        if mysqlCursor.rowcount > 0:
            return mysqlCursor.fetchall()
        else:
            return []
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_th_crossed_proc()", 1, -6)        
Example #10
0
def playing_idol_on_event_playing_actor_leave(message_type, channel, channel_type, serialize):
  # 获取事件交互代理
  communicator_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

  # 事件消息
  message = ccevent.playing.ttypes.EventPlayingActorLeave()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 取消关注玩家杀死NPC事件、角色被杀死事件
  communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_killed")
  communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_kill_npc")
  communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
      "playing_idol_on_event_playing_actor_request_complete")

  # 副本管理器中删除玩家对象
  idol_types.PlayingManager.remove_actor(message.actor_)

  log.log_debug("玩家(%d) 离开 idol副本(id=%d,template=%d)" % \
      (message.actor_, message.playing_, message.template_))
Example #11
0
def playing_plot_on_event_actor_killed(message_type, channel, channel_type, serialize):
  # 事件消息
  message = ccevent.role.ttypes.EventRoleKilled()

  # 消息反序列化
  TSerialization.deserialize(message, serialize)

  # 主角类型只能是玩家
  if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
    return None

  # 根据玩家ID获取所在副本ID
  playing_id = plot_types.PlayingManager.get_actor_playing(message.id_)
  if playing_id == None or playing_id == 0:
    proxy.Logging.error("plot_types.PlayingManager.get_actor_playing(%d) failed." % message.id_)
    return None

  # 获取副本对象
  playing = plot_types.PlayingManager.get(playing_id)
  if playing == None:
    proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." % playing_id)
    return None

  # 获取玩家对象
  actor = playing.get_actor(message.id_)
  if actor == None:
    proxy.Logging.error("playing.get_actor(%d) failed." % message.id_)
    return None

  # 玩家死亡次数增加
  actor.inc_dead_count(1)

  log.log_debug("玩家(%d) 死亡次数(%d)" % (message.id_, actor.get_dead_count()))
def check_runtime():
    try:
        proc_list = utility.get_th_crossed_proc()
        
        for proc in proc_list:
            
            r_id = proc[0]
            req_str = proc[1].strip()
            diff = proc[2]
            th = proc[3]
            status = proc[4]
            script = proc[5]
            
            status = status + '..TH Crossed'
            
            utility.update_status(status, r_id)
            
            
            msgBody = "Request:" + req_str + ", " + "ID:" + str(r_id) + ", " + "Script:" + script + "\n"
            msgBody += "Runtime TH: " + str(th) + " min" + "\n" + "\n"
            msgBody += "Total time taken by the process upto now: " + str(diff) + " min" + "\n"
            msgBody += "Runtime has crossed the TH value."
            
            
            email_sender.send(r_id, req_str, msgBody)
            
    
    except Exception as err:
        log.log_debug(err, 1, -4)
        log.log_debug("Error in check_runtime()", 1, -4)
Example #13
0
def add_award(actor, template, score):
    log.log_debug("plot.add_award(%d, %d, %d)" % (actor, template, score))

    playing_config = plot_types.Config.get(template)
    if playing_config == None:
        log.log_error("找不到日常副本配置(%d)" % template)
        return None

    request = ccrequest.playing.ttypes.RequestPlayingAddAward()
    request.actor_ = actor
    request.playing_template_ = template

    award_field = ccentity.playing.ttypes.PlayingAwardField()
    award_field.type_ = ccentity.resource.ttypes.ResourceType.RESOURCE

    request.awards_ = []

    # 奖励
    award_field.type_ = ccentity.resource.ttypes.ResourceType.ITEM
    for i in playing_config.get_awards():
        request.awards_.append(i)

    # 抽奖奖励
    draw_items = playing_config.get_draw_items()
    draw_items_size = len(draw_items)
    if draw_items_size > 0:
        request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField(
            ccentity.resource.ttypes.ResourceType.ITEM, draw_items[random.randint(0, draw_items_size - 1)], 1
        )

    if proxy.Request.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD, request) == False:
        proxy.Logging.error("发送请求 REQUEST_PLAYING_ADD_AWARD 出错")
        return None

    proxy.Logging.debug("增加奖励(玩家=%d) 成功" % actor)
def update_db_lock(status, name, flag):   
    try:
        #mysqlCursor.execute("""
        #                    UPDATE procmon.users
        #                    SET status=%s
        #                    WHERE name=%s
        #                    """, (status, name))
        
        if flag == 'start':                
            mysqlCursor.execute("""
                                UPDATE procmon.users
                                SET status=%s, start_time=now()
                                WHERE name=%s
                               """, (status, name))
        elif flag == 'end':
            mysqlCursor.execute("""
                                UPDATE procmon.users
                                SET status=%s, end_time=now()
                                WHERE name=%s
                                """, (status, name))
        
        mysqlConn.commit()        
                
    except Exception as err:                
        log.log_debug(err, 1, -6)
        log.log_debug("Error in update_db_lock()", 1, -6)
        mysqlConn.rollbak()
Example #15
0
def playing_plot_on_event_playing_actor_leave(message_type, channel,
                                              channel_type, serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorLeave()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 取消关注玩家杀死NPC事件、请求完成副本事件、角色被杀死事件
    communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_plot_on_event_actor_killed")
    communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
        "playing_plot_on_event_playing_actor_request_complete")
    communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_plot_on_event_actor_kill_npc")
    # 副本1需要关注玩家血量改变的事件
    if message.template_ == 1:
        proxy.Communicator.unfollow(ccevent.ttypes.EventType.EVENT_ROLE_CHANGE_HP, \
            message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
            "playing_plot_on_event_role_change_hp")

    # 副本管理器中删除玩家对象
    plot_types.PlayingManager.remove_actor(message.actor_)

    log.log_debug("玩家(%d)离开副本(id=%d,template=%d)" % \
        (message.actor_, message.playing_, message.template_))
Example #16
0
def _check_dep(path):
    if os.path.isfile(path):
        with open(path) as file_dependency:
            lines = file_dependency.readlines()
            for line in lines:
                try:
                    package_version = ''
                    installer_name = ''
                    res = []
                    
                    for str_equal in line.split('='):
                        if str_equal.strip(): # not blank
                            for str_blank in str_equal.split():
                                res.append(str_blank)
                    
                    if len(res) % 2 == 0:
                        if len(res):
                            log_err('util', 'failed to check dependency, invalid format' )
                            return False
                        continue # if it is blank, then continue
                    else:
                        package_name =  res[0]
                        
                        for index_to_match in range(1, len(res), 2):
                            if res[index_to_match] == 'installer':
                                installer_name = res[index_to_match + 1]
                                continue
                            if res[index_to_match] == 'version':
                                package_version = res[index_to_match + 1]
                                continue
                        
                        if installer_name == '':
                            installers = ['pip', 'apt-get']
                            for installer in installers:
                                installer_name = installer
                                if package_version == '': 
                                    cmd = '%s install %s' % (str(installer_name), str(package_name))
                                else :
                                    cmd = '%s install %s==%s' % (str(installer_name), str(package_name), str(package_version))
                                status, output = commands.getstatusoutput(cmd)
                                if status == 0:
                                    log_debug('util', 'check dependency, finished installing %s' % str(package_name))
                                    break
                            if status != 0:
                                log_err('util', 'check dependency, invalid installer, failed to install %s' % str(package_name))
                                return False
                        else:
                            if package_version == '':
                                cmd = '%s install %s' % (str(installer_name), str(package_name))
                            else:
                                cmd = '%s install %s==%s' % (str(installer_name), str(package_name), str(package_version))
                            status, output = commands.getstatusoutput(cmd)
                            if status == 0:
                                log_debug('util', 'check dependency, finished installing %s' % str(package_name))
                            else:
                                log_err('util', 'check dependency, failed to install %s' % str(package_name))
                                return False
                except:
                    continue # if it is blank, continue. else return False
        return True
def running_proc():
    try:
        query = "SELECT count(request) FROM procmon.request_queue WHERE status = 'Running'"
        mysqlCursor.execute(query)  
        return mysqlCursor.rowcount
    
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in running_proc()", 1, -6)
Example #18
0
def playing_idol_on_event_playing_actor_enter(message_type, channel, channel_type, serialize):
  # 获取事件交互代理
  communicator_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 事件消息
  message = ccevent.playing.ttypes.EventPlayingActorEnter()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 关注玩家杀死NPC事件、角色被杀死事件
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_kill_npc")
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_killed")
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
      "playing_idol_on_event_playing_actor_request_complete")

  # 获取副本对象
  playing = idol_types.PlayingManager.get(message.playing_)
  if playing == None:
    log.log_error("idol副本 获取 playing(%d) 失败" % message.playing_)
    return None

  now = time.time()

  # 获取玩家对象
  actor = playing.get_actor(message.actor_)
  if actor == None:
    # 玩家不存在时,创建并加入副本管理器中
    actor = idol_types.Actor(message.actor_, now)
    playing.add_actor(actor)

  # 副本管理器中加入一个玩家ID到副本ID的对应关系
  idol_types.PlayingManager.add_actor(message.actor_, message.playing_)

  # 请求初始化玩家
  request = ccrequest.playing.ttypes.RequestPlayingInitializeActor()
  request.actor_ = message.actor_
  request.spend_time_ = now - actor.get_start_time()
  request.scores_ = []
  request.datas_ = []

  # 序列化消息
  request_data = TSerialization.serialize(request)

  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INITIALIZE_ACTOR, \
      request_data, len(request_data))

  log.log_debug("玩家(%d) 进入 idol副本(id=%d,template=%d)" % \
      (message.actor_, message.playing_, message.template_))
Example #19
0
def playing_idol_on_event_actor_killed(message_type, channel, channel_type,
                                       serialize):
    # 事件消息
    message = ccevent.role.ttypes.EventRoleKilled()

    # 消息反序列化
    TSerialization.deserialize(message, serialize)

    # 主角类型只能是玩家
    if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
        return None

    # 根据玩家ID获取所在副本ID
    playing_id = idol_types.PlayingManager.get_actor_playing(message.id_)
    if playing_id == None or playing_id == 0:
        return None

    # 获取副本对象
    playing = idol_types.PlayingManager.get(playing_id)
    if playing == None:
        return None

    # 获取玩家对象
    actor = playing.get_actor(message.id_)
    if actor == None:
        return None

    # 玩家死亡次数增加
    actor.inc_dead_count()

    log.log_debug("idol副本 玩家(%d) 死亡次数(%d)" %
                  (message.id_, actor.get_dead_count()))

    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 先复活玩家
    request = ccrequest.scene.ttypes.RequestSceneRevive()
    request.actor_ = message.id_
    request.stay_revive_ = True
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
        request_data, len(request_data))

    # 请求失败消息
    request = ccrequest.playing.ttypes.RequestPlayingFailure()
    request.playing_ = playing_id
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE, \
        request_data, len(request_data))

    log.log_debug("idol副本 玩家(%d) 失败" % message.id_)
Example #20
0
def call(command):
    """Call into the system and run Command (string)"""
    s = time.time()
    cmd = join_and_sanitize(command)
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    result, _err = proc.communicate()
    e = time.time()
    log_debug(["subprocess command:", cmd])
    log_debug(["subprocess time:", (e - s)])
    return result
Example #21
0
def generate_qmap(X,
                  Y,
                  pixel_size,
                  detector_distance,
                  wavelength,
                  extrinsic_rotation=None,
                  order="xyz"):
    r"""
    Generate scattering vector map from experimental parameters

    Args:
      :X (array): :math:`x`-coordinates of pixels in unit meter

      :Y (array): :math:`y`-coordinates of pixels in unit meter

      :pixel_size (float): Pixel size (i.e. edge length) in unit meter

      :detector_distance (float): Distance from interaction point to detector plane

      :wavelength (float): Photon wavelength in unit meter

    Kwargs:
      :extrinsic_rotation (:class:`condor.utils.rotation.Rotation`): Extrinsic rotation of the sample. If ``None`` no rotation is applied (default ``None``)

      :order (str): Order of scattering vector coordinates in the output array. Choose either ``'xyz'`` or ``'zyx'`` (default ``'xyz'``)    
    """
    log_debug(logger,
              "Allocating qmap (%i,%i,%i)" % (X.shape[0], X.shape[1], 3))
    R_Ewald = 2 * numpy.pi / wavelength
    p_x = X * pixel_size
    p_y = Y * pixel_size
    p_z = numpy.ones_like(X) * detector_distance
    l = numpy.sqrt(p_x**2 + p_y**2 + p_z**2)
    r_x = p_x / l
    r_y = p_y / l
    r_z = p_z / l - 1.
    qmap = numpy.zeros(shape=(X.shape[0], X.shape[1], 3))
    if order == "xyz":
        qmap[:, :, 0] = r_x * R_Ewald
        qmap[:, :, 1] = r_y * R_Ewald
        qmap[:, :, 2] = r_z * R_Ewald
    elif order == "zyx":
        qmap[:, :, 0] = r_z * R_Ewald
        qmap[:, :, 1] = r_y * R_Ewald
        qmap[:, :, 2] = r_x * R_Ewald
    else:
        log_and_raise_error(logger,
                            "Indexing with order=%s is invalid." % order)
    if extrinsic_rotation is not None:
        log_debug(logger, "Applying qmap rotation.")
        intrinsic_rotation = copy.deepcopy(extrinsic_rotation)
        intrinsic_rotation.invert()
        qmap = intrinsic_rotation.rotate_vectors(
            qmap.ravel(), order=order).reshape(qmap.shape)
    return qmap
Example #22
0
def playing_idol_on_event_actor_killed(message_type, channel, channel_type, serialize):
  # 事件消息
  message = ccevent.role.ttypes.EventRoleKilled()

  # 消息反序列化
  TSerialization.deserialize(message, serialize)

  # 主角类型只能是玩家
  if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
    return None

  # 根据玩家ID获取所在副本ID
  playing_id = idol_types.PlayingManager.get_actor_playing(message.id_)
  if playing_id == None or playing_id == 0:
    return None

  # 获取副本对象
  playing = idol_types.PlayingManager.get(playing_id)
  if playing == None:
    return None

  # 获取玩家对象
  actor = playing.get_actor(message.id_)
  if actor == None:
    return None

  # 玩家死亡次数增加
  actor.inc_dead_count()

  log.log_debug("idol副本 玩家(%d) 死亡次数(%d)" % (message.id_, actor.get_dead_count()))

  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 先复活玩家
  request = ccrequest.scene.ttypes.RequestSceneRevive()
  request.actor_ = message.id_
  request.stay_revive_ = True
  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
      request_data, len(request_data))

  # 请求失败消息
  request = ccrequest.playing.ttypes.RequestPlayingFailure()
  request.playing_ = playing_id
  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE, \
      request_data, len(request_data))

  log.log_debug("idol副本 玩家(%d) 失败" % message.id_)
Example #23
0
 def resize_horizontal(self, increment):
     if not self.parent.resize_horizontal(self, increment):
         log_debug(['Unable to resize. Using alternative'])
         next_child = self.parent.find_earliest_a_but_not_me(self)
         if next_child is not None:
             if not self.parent.all_are_bees(self):
                 log_debug(['All are not bees'])
                 increment *= -1
             elif self.parent.get_plane_type() == PLANE.VERTICAL:
                 increment *= -1
             next_child.resize_horizontal(increment)
def db_lock():
    try:
        query = "SELECT name FROM procmon.users WHERE status='active' AND time_to_sec(TIMEDIFF(now(), start_time))/60 < 5"
        mysqlCursor.execute(query)  
        if mysqlCursor.rowcount > 0:
            return True
        else:
            return False
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in db_lock()", 1, -6)
Example #25
0
 def _shrink_stacks(self, group_prefix="/"):
     for k in self._f[group_prefix].keys():
         name = group_prefix + k
         if isinstance(self._f[name], h5py.Dataset):
             log.log_debug(logger, "Shrinking dataset %s to stack length %i" % (name, self._i))
             s = list(self._f[name].shape)
             s.pop(0)
             s.insert(0, self._i)
             s = tuple(s)
             self._f[name].resize(s)
         else:
             self._shrink_stacks(name + "/")
Example #26
0
 def resize_vertical(self, increment):
     """Request parent to resize window."""
     if not self.parent.resize_vertical(self, increment):
         log_debug(['Unable to resize. Using alternative'])
         next_child = self.parent.find_earliest_a_but_not_me(self)
         if next_child is not None:
             if not self.parent.all_are_bees(self):
                 log_debug(['All are not bees'])
                 increment *= -1
             elif self.parent.get_plane_type() == PLANE.HORIZONTAL:
                 increment *= -1
             next_child.resize_vertical(increment)
Example #27
0
def fetch_map(emd_id):
    url = "ftp://ftp.ebi.ac.uk/pub/databases/emdb/structures/EMD-%s/map/emd_%s.map.gz" % (str(emd_id),str(emd_id))
    log.log_debug(logger, "Downloading file for EMDID %s from URL %s" % (emd_id, url))
    filename = "./emd_%s.map" % str(emd_id)
    response = urllib2.urlopen(url)
    compressedFile = StringIO.StringIO()
    compressedFile.write(response.read())
    compressedFile.seek(0)
    decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
    log.log_debug(logger, "Download of %s ended." % (filename))
    with open(filename, 'w') as outfile:
        outfile.write(decompressedFile.read())
    return read_map(filename)
def get_new_requests():    
    try:
        query = "SELECT id, request, args FROM procmon.request_queue WHERE status is NULL or status=''"
        mysqlCursor.execute(query)
        
        if mysqlCursor.rowcount > 0:
            return mysqlCursor.fetchall()
        else:
            return []
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_new_requests()", 1, -6)
def update_status(flag, id):
    try:                
        mysqlCursor.execute("""
                            UPDATE procmon.request_queue
                            SET status=%s
                            WHERE id=%s
                            """, (flag, id))
        mysqlConn.commit()
                
    except Exception as err:                
        log.log_debug(err, 1, -6)
        log.log_debug("Error in update_status()", 1, -6)
        mysqlConn.rollback()
def get_single_running_proc():
    try:
        query = "SELECT id, request_token, args FROM procmon.request_to_process WHERE status=1 AND timer=0"
        mysqlCursor.execute(query)
    
        if mysqlCursor.rowcount > 0:
            return mysqlCursor.fetchall()
        else:
            return []
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_single_running_proc()", 1, -6)        
Example #31
0
def fetch_map(emd_id):
    url = "ftp://ftp.ebi.ac.uk/pub/databases/emdb/structures/EMD-%s/map/emd_%s.map.gz" % (str(emd_id),str(emd_id))
    log.log_debug(logger, "Downloading file for EMDID %s from URL %s" % (emd_id, url))
    filename = "./emd_%s.map" % str(emd_id)
    response = urllib2.urlopen(url)
    compressedFile = StringIO.StringIO()
    compressedFile.write(response.read())
    compressedFile.seek(0)
    decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
    log.log_debug(logger, "Download of %s ended." % (filename))
    with open(filename, 'w') as outfile:
        outfile.write(decompressedFile.read())
    return read_map(filename)
Example #32
0
def playing_plot_on_event_actor_kill_npc(message_type, channel, channel_type,
                                         serialize):
    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.actor.ttypes.EventActorKillNpc()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 获取玩家所在副本ID
    playing_id = plot_types.PlayingManager.get_actor_playing(message.actor_)
    if playing_id == None or playing_id == 0:
        proxy.Logging.error(
            "plot_types.PlayingManager.get_actor_playing(%d) failed." %
            message.actor_)
        return None

    # 获取副本对象
    playing = plot_types.PlayingManager.get(playing_id)
    if playing == None:
        proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." %
                            playing_id)
        return None

    # 获取副本玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        proxy.Logging.error("playing.get_actor(%d) failed." % message.actor_)
        return None

    # 增加杀死NPC个数
    actor.inc_kill_npc(message.npc_template_)

    # 请求消息
    request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
    # 玩家ID
    request.actor_ = message.actor_
    # 分数对象(得分类型=杀死NPC, key=NPC模板ID, value=当前杀死的个数)
    score_field = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
        message.npc_template_, actor.get_kill_npc(message.npc_template_))
    request.score_ = score_field
    # 序列化消息
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
        request_data, len(request_data))

    log.log_debug("玩家(%d) 杀死 NPC(%d) 个数(%d)" % (message.actor_, score_field.key_, \
          score_field.value_))
Example #33
0
 def _write_without_iterate(self, D, group_prefix="/"):
     for k in D.keys():
         if isinstance(D[k],dict):
             group_prefix_new = group_prefix + k + "/"
             log.log_debug(logger, "Writing group %s" % group_prefix_new)
             if k not in self._f[group_prefix]:
                 self._f.create_group(group_prefix_new)
             self._write_without_iterate(D[k], group_prefix_new)
         else:
             name = group_prefix + k
             log.log_debug(logger, "Writing dataset %s" % name)
             data = D[k]
             if k not in self._f[group_prefix]:
                 if numpy.isscalar(data):
                     maxshape = (None,)
                     shape = (self._chunksize,)
                     dtype = numpy.dtype(type(data))
                     if dtype == "S":
                         dtype = h5py.new_vlen(str)
                     axes = "experiment_identifier:value"
                 else:
                     data = numpy.asarray(data)
                     try:
                         h5py.h5t.py_create(data.dtype, logical=1)
                     except TypeError:
                         log.log_warning(logger, "Could not save dataset %s. Conversion to numpy array failed" % name)
                         continue
                     maxshape = tuple([None]+list(data.shape))
                     shape = tuple([self._chunksize]+list(data.shape))
                     dtype = data.dtype
                     ndim = data.ndim
                     axes = "experiment_identifier"
                     if ndim == 1: axes = axes + ":x"
                     elif ndim == 2: axes = axes + ":y:x"
                     elif ndim == 3: axes = axes + ":z:y:x"
                 log.log_debug(logger, "Create dataset %s [shape=%s, dtype=%s]" % (name,str(shape),str(dtype)))
                 self._f.create_dataset(name, shape, maxshape=maxshape, dtype=dtype, **self._create_dataset_kwargs)
                 self._f[name].attrs.modify("axes",[axes])
             if self._f[name].shape[0] <= self._i:
                 if numpy.isscalar(data):
                     data_shape = []
                 else:
                     data_shape = data.shape
                 new_shape = tuple([self._chunksize*(self._i/self._chunksize+1)]+list(data_shape))
                 log.log_debug(logger, "Resize dataset %s [old shape: %s, new shape: %s]" % (name,str(self._f[name].shape),str(new_shape)))
                 self._f[name].resize(new_shape)
             log.log_debug(logger, "Write to dataset %s at stack position %i" % (name, self._i))
             if numpy.isscalar(data):
                 self._f[name][self._i] = data
             else:
                 self._f[name][self._i,:] = data[:]
def get_host_script_user_pass(req_str):    
    try:
        query = "SELECT host, port, script, username, password FROM procmon.request_to_process WHERE request_token='" + req_str + "'"
        mysqlCursor.execute(query)		
        res = mysqlCursor.fetchone()        
    
        if res != None:
            return res[0].strip(), res[1], res[2].strip(), res[3].strip(), res[4].strip()
        else:
            return None, None, None, None, None
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_host_script_user_pass()", 1, -6)
def get_contact_list(req_str):
    try:
        query = "SELECT contacts FROM procmon.request_to_process WHERE request_token='" + req_str + "'"
        mysqlCursor.execute(query)  
        res = mysqlCursor.fetchone()
    
        if(res!=None):
            return res[0].strip()
        else:
            return config.from_addr
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_contact_list()", 1, -6)
def insert_to_queue(request_name, arguments):
    try:
        mysqlCursor.execute("""
                INSERT INTO procmon.request_queue(request, args)
                VALUES(%s, %s)
                """, (request_name, arguments)                
                )
        
        mysqlConn.commit()
        
    except Exception as err:        
        log.log_debug(err, 1, -6)
        log.log_debug("Error in insert_to_queue()", 1, -6)        
        mysqlConn.rollback()
def get_cmd(ext):
    try:
        query = "SELECT cmd FROM procmon.cmd_format WHERE file_ext = '" + ext.strip() + "'"
        mysqlCursor.execute(query)	
        #return mysqlCursor.fetchone()[0]
        cmd = mysqlCursor.fetchone()
        if cmd != None:
            return cmd[0]            
        else:
            return cmd
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_cmd()", 1, -6)        
def disable_proc(id, pname, status):
    try:
        mysqlCursor.execute("""
                UPDATE procmon.request_to_process
                SET status = %s
                WHERE id = %s
                """, (status, id)
                )
        
        mysqlConn.commit()
        
    except Exception as err:        
        log.log_debug(err, 1, -6)
        log.log_debug("Error in disable_proc()", 1, -6)        
        mysqlConn.rollback()
def update_next_runtime(id):
    try:
        mysqlCursor.execute("""
                UPDATE procmon.request_to_process
                SET next_run_time = DATE_ADD(NOW(), INTERVAL timer SECOND)
                WHERE id = %s
                """, (id)
                )
        
        mysqlConn.commit()
        
    except Exception as err:        
        log.log_debug(err, 1, -6)
        log.log_debug("Error in update_next_runtime()", 1, -6)        
        mysqlConn.rollback()
def connect():
    i = 0
    while i < config.max_attempts:
        try:
            global mysqlConn
            global mysqlCursor
            mysqlConn = MySQLdb.connect(host=config.DB_HOST, port=config.DB_PORT, user=config.DB_USER, passwd=config.DB_PASS)                
            mysqlCursor = mysqlConn.cursor()
            
            break
        except Exception as err:            
            log.log_debug(err, 1, -6)
            log.log_debug("Error in connecting MySQL DB..Trying again", 1, -6)
            i += 1
            time.sleep(2)
Example #41
0
def generate_qmap(X,Y,pixel_size,detector_distance,wavelength,extrinsic_rotation=None, order="xyz"):
    r"""
    Generate scattering vector map from experimental parameters

    Args:
      :X (array): :math:`x`-coordinates of pixels in unit meter

      :Y (array): :math:`y`-coordinates of pixels in unit meter

      :pixel_size (float): Pixel size (i.e. edge length) in unit meter

      :detector_distance (float): Distance from interaction point to detector plane

      :wavelength (float): Photon wavelength in unit meter

    Kwargs:
      :extrinsic_rotation (:class:`condor.utils.rotation.Rotation`): Extrinsic rotation of the sample. If ``None`` no rotation is applied (default ``None``)

      :order (str): Order of scattering vector coordinates in the output array. Choose either ``'xyz'`` or ``'zyx'`` (default ``'xyz'``)    
    """
    log_debug(logger, "Allocating qmap (%i,%i,%i)" % (X.shape[0],X.shape[1],3))
    R_Ewald = 2*numpy.pi/wavelength
    p_x = X*pixel_size
    p_y = Y*pixel_size
    p_z = numpy.ones_like(X)*detector_distance
    l = numpy.sqrt(p_x**2+p_y**2+p_z**2)
    r_x = p_x/l
    r_y = p_y/l
    r_z = p_z/l - 1.
    qmap = numpy.zeros(shape=(X.shape[0],X.shape[1],3))
    if order == "xyz":
        qmap[:,:,0] = r_x * R_Ewald
        qmap[:,:,1] = r_y * R_Ewald
        qmap[:,:,2] = r_z * R_Ewald
    elif order == "zyx":
        qmap[:,:,0] = r_z * R_Ewald
        qmap[:,:,1] = r_y * R_Ewald
        qmap[:,:,2] = r_x * R_Ewald
    else:
        log_and_raise_error(logger, "Indexing with order=%s is invalid." % order)
    if extrinsic_rotation is not None:
        log_debug(logger, "Applying qmap rotation.")
        intrinsic_rotation = copy.deepcopy(extrinsic_rotation)
        intrinsic_rotation.invert()
        qmap = intrinsic_rotation.rotate_vectors(qmap.ravel(), order=order).reshape(qmap.shape)
    return qmap
Example #42
0
def playing_plot_on_event_actor_kill_npc(message_type, channel, channel_type, serialize):
  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 事件消息
  message = ccevent.actor.ttypes.EventActorKillNpc()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 获取玩家所在副本ID
  playing_id = plot_types.PlayingManager.get_actor_playing(message.actor_)
  if playing_id == None or playing_id == 0:
    proxy.Logging.error("plot_types.PlayingManager.get_actor_playing(%d) failed." % message.actor_)
    return None

  # 获取副本对象
  playing = plot_types.PlayingManager.get(playing_id)
  if playing == None:
    proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." % playing_id)
    return None

  # 获取副本玩家对象
  actor = playing.get_actor(message.actor_)
  if actor == None:
    proxy.Logging.error("playing.get_actor(%d) failed." % message.actor_)
    return None

  # 增加杀死NPC个数
  actor.inc_kill_npc(message.npc_template_)

  # 请求消息
  request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
  # 玩家ID
  request.actor_ = message.actor_
  # 分数对象(得分类型=杀死NPC, key=NPC模板ID, value=当前杀死的个数)
  score_field = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
      message.npc_template_, actor.get_kill_npc(message.npc_template_))
  request.score_ = score_field
  # 序列化消息
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
      request_data, len(request_data))

  log.log_debug("玩家(%d) 杀死 NPC(%d) 个数(%d)" % (message.actor_, score_field.key_, \
        score_field.value_))
def get_periodic_running_proc():
    try:
        query = """
                SELECT id, request_token, args, entry_time, next_run_time, timer FROM procmon.request_to_process
                WHERE
                    ( status = 1 AND timer > 0 )
                AND
                    ( next_run_time IS NULL OR ( NOW() - next_run_time ) >= 0 )
                """                
        
        mysqlCursor.execute(query)
    
        if mysqlCursor.rowcount > 0:
            return mysqlCursor.fetchall()
        else:
            return []
        
    except Exception as err:
        log.log_debug(err, 1, -6)
        log.log_debug("Error in get_single_running_proc()", 1, -6)        
Example #44
0
def playing_idol_on_event_playing_destory(message_type, channel, channel_type, serialize):
  # 获取事件交互代理
  communicator_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

  # 事件消息
  message = ccevent.playing.ttypes.EventPlayingDestory()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 取消关注玩家进入/退出副本
  communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_ENTER, \
      message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
      "playing_idol_on_event_playing_actor_enter")
  communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_LEAVE, \
      message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
      "playing_idol_on_event_playing_actor_leave")

  # 从管理器中删除副本对象
  idol_types.PlayingManager.remove(message.playing_)

  log.log_debug("副本(%d) 销毁成功" % message.playing_)
Example #45
0
def make_icosahedron_map(N, nRmax, extrinsic_rotation=None):
    """
    Generate map of a uniform icosahedron (density = 1) on a regular grid

    Orientation: The cartesian grid axis all lie parallel to 2-fold symmetry axes of the icosahedron.

    Args:
      :N (int): Edge length of the grid in unit pixels

      :nRmax (float): Outer radius of the icosahedron in unit pixels

    Kwargs:
      :rotation (:class:`condor.utils.rotation.Rotation`): Rotation instance for extrinsic rotation of the icosahedron. 
    """
    log_debug(logger, "Building icosahedral geometry")
    log_debug(logger, "Grid: %i x %i x %i (%i voxels)" % (N, N, N, N**3))
    t0 = time.time()
    if extrinsic_rotation is not None:
        q = extrinsic_rotation.get_as_quaternion()
        icomap = icosahedron.icosahedron(N, nRmax, q)
    else:
        icomap = icosahedron.icosahedron(N, nRmax)
    t1 = time.time()
    log_debug(logger, "Built map within %f seconds." % (t1 - t0))
    return icomap
Example #46
0
def make_icosahedron_map_slow(N, nRmax, extrinsic_rotation=None):
    """
    Generate map of a uniform icosahedron (density = 1) on a regular grid (*slow python implementation*)

    Orientation: The cartesian grid axis all lie parallel to 2-fold symmetry axes of the icosahedron.

    Args:
      :N (int): Edge length of the grid in unit pixels

      :nRmax (float): Outer radius of the icosahedron in unit pixels

    Kwargs:
    
      :rotation (:class:`condor.utils.rotation.Rotation`): Rotation instance for extrinsic rotation of the icosahedron. 
    """
    na = nRmax / numpy.sqrt(10.0 + 2 * numpy.sqrt(5)) * 4.
    nRmin = numpy.sqrt(3) / 12 * (3.0 + numpy.sqrt(5)) * na  # radius at faces
    log_debug(logger, "Building icosahedral geometry")
    n_list = get_icosahedron_normal_vectors()
    # Rotate
    if extrinsic_rotation is not None:
        n_list = extrinsic_rotation.rotate_vectors(numpy.array(n_list))
    X, Y, Z = 1.0 * numpy.mgrid[0:N, 0:N, 0:N]
    X = X - (N - 1) / 2.
    Y = Y - (N - 1) / 2.
    Z = Z - (N - 1) / 2.
    log_debug(logger, "Grid: %i x %i x %i (%i voxels)" % (N, N, N, N**3))
    icomap = numpy.zeros((len(n_list), N, N, N))
    # calculate distance of all voxels to all faces (negative inside, positive outside icosahedron)
    for i in range(len(n_list)):
        icomap[i, :, :, :] = (X * n_list[i][0] + Y * n_list[i][1] +
                              Z * n_list[i][2]) + nRmin
    s = 1.
    M = icomap.copy()
    temp = abs(M) < 0.5 * s
    icomap[temp] = 0.5 + icomap[temp] / s
    icomap[M < (-0.5) * s] = 0
    icomap[M > 0.5 * s] = 1
    icomap = icomap.min(0)
    return icomap
Example #47
0
def playing_plot_on_event_playing_destory(message_type, channel, channel_type,
                                          serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingDestory()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 取消关注玩家进入/退出副本
    communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_ENTER, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_plot_on_event_playing_actor_enter")
    communicator_proxy.unfollow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_LEAVE, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_plot_on_event_playing_actor_leave")

    # 从管理器中删除副本对象
    plot_types.PlayingManager.remove(message.playing_)

    log.log_debug("副本(%d) 销毁成功" % message.playing_)
Example #48
0
def playing_plot_on_event_actor_killed(message_type, channel, channel_type,
                                       serialize):
    # 事件消息
    message = ccevent.role.ttypes.EventRoleKilled()

    # 消息反序列化
    TSerialization.deserialize(message, serialize)

    # 主角类型只能是玩家
    if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
        return None

    # 根据玩家ID获取所在副本ID
    playing_id = plot_types.PlayingManager.get_actor_playing(message.id_)
    if playing_id == None or playing_id == 0:
        proxy.Logging.error(
            "plot_types.PlayingManager.get_actor_playing(%d) failed." %
            message.id_)
        return None

    # 获取副本对象
    playing = plot_types.PlayingManager.get(playing_id)
    if playing == None:
        proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." %
                            playing_id)
        return None

    # 获取玩家对象
    actor = playing.get_actor(message.id_)
    if actor == None:
        proxy.Logging.error("playing.get_actor(%d) failed." % message.id_)
        return None

    # 玩家死亡次数增加
    actor.inc_dead_count(1)

    log.log_debug("玩家(%d) 死亡次数(%d)" % (message.id_, actor.get_dead_count()))
Example #49
0
def add_award(actor, template, score):
    log.log_debug("plot.add_award(%d, %d, %d)" % (actor, template, score))

    playing_config = plot_types.Config.get(template)
    if playing_config == None:
        log.log_error("找不到日常副本配置(%d)" % template)
        return None

    request = ccrequest.playing.ttypes.RequestPlayingAddAward()
    request.actor_ = actor
    request.playing_template_ = template

    award_field = ccentity.playing.ttypes.PlayingAwardField()
    award_field.type_ = ccentity.resource.ttypes.ResourceType.RESOURCE

    request.awards_ = []

    # 奖励
    award_field.type_ = ccentity.resource.ttypes.ResourceType.ITEM
    for i in playing_config.get_awards():
        request.awards_.append(i)

    # 抽奖奖励
    draw_items = playing_config.get_draw_items()
    draw_items_size = len(draw_items)
    if draw_items_size > 0:
        request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField( \
            ccentity.resource.ttypes.ResourceType.ITEM, \
            draw_items[random.randint(0, draw_items_size - 1)], 1)

    if proxy.Request.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD,\
      request) == False:
        proxy.Logging.error("发送请求 REQUEST_PLAYING_ADD_AWARD 出错")
        return None

    proxy.Logging.debug("增加奖励(玩家=%d) 成功" % actor)
Example #50
0
def playing_idol_on_event_playing_create(message_type, channel, channel_type,
                                         serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingCreate()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 关注玩家进入/退出副本事件
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_ENTER, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_idol_on_event_playing_actor_enter")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_LEAVE, \
        message.template_, ccevent.ttypes.ChannelType.CHANNEL_PLAYING, \
        "playing_idol_on_event_playing_actor_leave")

    # 创建副本对象并加入管理器中
    playing = idol_types.Playing(message.playing_, message.template_,
                                 message.scene_)
    idol_types.PlayingManager.add(playing)

    # 获取副本配置
    playing_config = idol_types.Config.get(message.template_)
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % message.template_)
        return None

    # 召唤一个BOSS
    facade_request.summon_npc(message.scene_, playing_config.get_pass_kill_npc(),\
        playing_config.get_pass_npc_x(), playing_config.get_pass_npc_y())

    log.log_debug("idol副本(id=%d,template=%d) 创建成功" %
                  (message.playing_, message.template_))
Example #51
0
def handler(wsock, message):
    d = dictionary[str(wsock)]
    log_debug(dictionary.keys())
    log_debug(d["size"])
    try:
        obj = json.loads(message)
        print(obj)
        if obj["action"] == "startUploading":
            d["file_size"] = obj.get("fileSize", 0)
            d["name"] = obj.get("name", "")
            d["description"] = obj.get("description", "")

        elif obj["action"] == "getDataList":
            offset = obj.get("offset", 0)
            limit = obj.get("limit")
            res = fm.get_data_list()
            res["action"] = obj["action"]
            wsock.send(json.dumps(res))

        elif obj["action"] == "getData":
            offset = obj.get("offset", 0)
            limit = obj.get("limit", 10)
            data_id = obj.get("dataId")
            res = fm.get_data(data_id, offset, limit)
            res["action"] = obj["action"]
            res["dataId"] = data_id
            wsock.send(json.dumps(res))

        elif obj["action"] == "getRecipeList":
            offset = obj.get("offset", 0)
            limit = obj.get("limit")
            res = fm.get_recipe_list(offset, limit)
            res["action"] = obj["action"]
            wsock.send(json.dumps(res))

        elif obj["action"] == "getModelList":
            offset = obj.get("offset", 0)
            limit = obj.get("limit")
            res = fm.get_model_list()
            res["action"] = obj["action"]
            wsock.send(json.dumps(res))

        elif obj["action"] == "startLearning":

            recipe_id = obj["recipeId"]
            data_id = obj["dataId"]
            model_info = obj["info"]
            config = obj["trainConfig"]
            model = CNN(recipe_id)
            res = model.train(config, data_id, wsock, model_info)
            res["action"] = "finishLearning"
            del model
            wsock.send(json.dumps(res))

        elif obj["action"] == "addRecipe":
            recipe = obj["recipe"]
            res = fm.save_recipe(recipe)
            res["action"] = obj["action"]
            wsock.send(json.dumps(res))

        elif obj["action"] == "deleteModel":
            model_id = obj["modelId"]
            r = fm.delete_model(model_id)
            log_debug(r)
            res = {}
            res["action"] = obj["action"]
            res["modelId"] = model_id
            wsock.send(json.dumps(res))

        elif obj["action"] == "deleteRecipe":
            recipe_id = obj["recipeId"]
            r = fm.delete_recipe(recipe_id)
            log_debug(r)
            res = {}
            res["action"] = obj["action"]
            res["recipeId"] = recipe_id
            wsock.send(json.dumps(res))

        elif obj["action"] == "deleteData":
            data_id = obj["dataId"]
            r = fm.delete_data(data_id)
            log_debug(r)
            res = {}
            res["action"] = obj["action"]
            res["dataId"] = data_id
            wsock.send(json.dumps(res))

        elif obj["action"] == "updateData":
            data = obj["dataInfo"]
            file_id = obj["dataId"]
            new_data = fm.put_data_info(data, file_id)
            res = {
                "data": new_data,
                "action": obj["action"]
            }
            log_debug(res)
            wsock.send(json.dumps(res))

        elif obj["action"] == "updateModel":
            model_id = obj["modelId"]
            model = obj["model"]
            put_res = fm.put_model_info(model, model_id)
            res = {
                "model": put_res["detail"],
                "action": obj["action"]
            }
            log_debug(res)
            wsock.send(json.dumps(res))

        elif obj["action"] == "updateRecipe":
            recipe_id = obj["recipeId"]
            info = obj["info"]
            res = fm.get_recipe(recipe_id)
            if res and res.get("status") == "success":
                body = res["detail"]["body"]
                body["info"]["name"] = info["name"]
                body["info"]["description"] = info["description"]
                new_recipe = fm.update_recipe(recipe_id, body)
                res = {
                    "recipe": new_recipe["detail"],
                    "action": obj["action"]
                }
            else:
                res = {
                    "status": "error",
                    "action": obj["action"]
                }
            log_debug(res)
            wsock.send(json.dumps(res))

    except (UnicodeDecodeError, json.decoder.JSONDecodeError):
        d["size"] += len(message)
        d["uploading_file"] += message
        response = {"status": "loading", "loadedSize": d["size"]}
        print(response)
        time.sleep(0.05) # for the progress bar.
        wsock.send(json.dumps(response))
        if d["size"] == int(d["file_size"]):
            uploading_file = d["uploading_file"]
            file_id = fm.generate_id()
            result = fm.put_zip_file(uploading_file, file_id, is_expanding=True)
            new_data = {
                "name": d["name"],
                "description": d["description"]
            }
            fm.put_data_info(new_data, file_id)
            response = {"action": "uploaded", "fileId": file_id}
            wsock.send(json.dumps(response))
Example #52
0
File: detect.py Project: zbsun/spts
def find_particles(image_scored,
                   image_thresholded,
                   min_dist,
                   n_particles_max,
                   peak_centering="center_of_mass"):

    n_lit = image_thresholded.sum()
    log_debug(logger, "%i pixels above threshold" % n_lit)

    success = False
    return_default = success, [], None, None, None, None, None, None, None, None

    if n_lit == 0:
        return return_default

    else:
        log_debug(logger, "Label image")
        labels, n_labels = scipy.ndimage.measurements.label(image_thresholded)
        i_labels = range(1, n_labels + 1)

        if n_labels > n_particles_max:
            log_info(
                logger,
                "%i labels - (frame overexposed? too many particles?), skipping analysis"
                % n_labels)
            return return_default

        V = [image_scored[i_label == labels].max() for i_label in i_labels]

        nx = image_thresholded.shape[1]
        ny = image_thresholded.shape[0]
        x, y = np.meshgrid(np.arange(nx), np.arange(ny))
        x = x[image_thresholded]
        y = y[image_thresholded]
        l = labels[image_thresholded]
        v = image_scored[image_thresholded]

        if peak_centering == "center_of_mass":
            log_debug(logger, "Determine centers of mass")
            com = scipy.ndimage.measurements.center_of_mass(
                image_thresholded, labels, i_labels)
            X = [com_x for com_y, com_x in com]
            Y = [com_y for com_y, com_x in com]

        elif peak_centering == "center_to_max":
            log_debug(logger, "Determine maximum positions")
            X = []
            Y = []
            for i_label in i_labels:
                i_max = (v * (l == i_label)).argmax()
                X.append(x[i_max])
                Y.append(y[i_max])
        else:
            log_and_raise_error(
                logger, "%s is not a valid argument for peak_centering!" %
                peak_centering)

        dislocation = []
        for i_label, xc, yc in zip(i_labels, X, Y):
            i_max = (v * (l == i_label)).argmax()
            dislocation.append(np.sqrt((x[i_max] - xc)**2 +
                                       (y[i_max] - yc)**2))

        merged = list(np.zeros(len(i_labels), dtype=np.bool))

        # Merge too close peaks
        log_debug(logger, "Merge too close points")
        i_labels, labels, X, Y, V, merged, dists_closest_neighbor = merge_close_points(
            i_labels, labels, X, Y, V, merged, min_dist)
        log_debug(logger, "Clean up labels")
        i_labels, labels = clean_up_labels(i_labels, labels)
        n_labels = len(i_labels)

        log_debug(logger, "Measure size of each peak")
        areas = measure_areas(i_labels, labels)

        success = True
        areas = np.asarray(areas)
        X = np.asarray(X)
        Y = np.asarray(Y)
        dislocation = np.asarray(dislocation)
        V = np.asarray(V)
        merged = np.asarray(merged)
        dists_closest_neighbor = np.asarray(dists_closest_neighbor)

        return success, i_labels, labels, areas, X, Y, V, merged, dists_closest_neighbor, dislocation
Example #53
0
def train():
    TIMESTAMP = "{0:%Y-%m-%d-%H-%M/}".format(datetime.now())
    log.log_info('program start')
    data, num_good, num_bad = util.load_train_data(num_data // 2)
    log.log_debug('Data loading completed')

    # resample
    data, length = util.resample(data, 600)
    data = util.reshape(data, length)
    good_data_origin = data[:num_good, :]
    bad_data_origin = data[num_good:, :]

    # extract bad data for test and train
    permutation = list(np.random.permutation(len(bad_data_origin)))
    shuffled_bad_data = bad_data_origin[permutation, :]
    test_bad_data = shuffled_bad_data[:int(num_bad * 0.3), :]
    train_bad_data_origin = shuffled_bad_data[int(num_bad * 0.3):, :]
    # extract corresponding good data for test and train
    permutation = list(np.random.permutation(len(good_data_origin)))
    shuffled_good_data = good_data_origin[permutation, :]
    test_good_data = shuffled_good_data[:len(test_bad_data), :]
    train_good_data = shuffled_good_data[len(test_bad_data):, :]

    assert len(test_bad_data) == len(test_good_data)
    # construct test data
    test_y = np.array([1.] * len(test_good_data) + [0.] * len(test_bad_data), dtype=np.float).reshape(
        (len(test_bad_data) + len(test_good_data), 1))
    test_x = np.vstack((test_good_data, test_bad_data))

    # expand the number of bad data for train
    train_x = np.vstack((train_good_data, train_bad_data_origin))
    train_y = np.array([1.] * len(train_good_data) + [0.] * len(train_bad_data_origin), dtype=np.float).reshape(
        (len(train_bad_data_origin) + len(train_good_data), 1))

    train_x, train_y, num_expand = util.expand(train_x, train_y)

    # regularize
    for i in range(len(train_x)):
        train_x[i, :, 0] = util.regularize(train_x[i, :, 0])
        train_x[i, :, 1] = util.regularize(train_x[i, :, 1])
        train_x[i, :, 2] = util.regularize(train_x[i, :, 2])
    for i in range(len(test_x)):
        test_x[i, :, 0] = util.regularize(test_x[i, :, 0])
        test_x[i, :, 1] = util.regularize(test_x[i, :, 1])
        test_x[i, :, 2] = util.regularize(test_x[i, :, 2])

    # random
    train_x, train_y = util.shuffle_data(train_x, train_y)

    log.log_debug('prepare completed')
    log.log_info('convolution layers: ' + str(conv_layers))
    log.log_info('filters: ' + str(filters))
    log.log_info('full connected layers: ' + str(fc_layers))
    log.log_info('learning rate: %f' % learning_rate)
    log.log_info('keep prob: ' + str(keep_prob))
    log.log_info('the number of expanding bad data: ' + str(num_expand))
    log.log_info('mini batch size: ' + str(mini_batch_size))

    if mini_batch_size != 0:
        assert mini_batch_size <= len(train_x)

    cnn = Cnn(conv_layers, fc_layers, filters, learning_rate)
    (m, n_W0, n_C0) = train_x.shape
    n_y = train_y.shape[1]

    # construction calculation graph
    cnn.initialize(n_W0, n_C0, n_y)
    cost = cnn.cost()
    optimizer = cnn.get_optimizer(cost)
    predict, accuracy = cnn.predict()

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with tf.Session() as sess:

        # log for tensorboard
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter("resource/tsb/train/" + TIMESTAMP, sess.graph)
        test_writer = tf.summary.FileWriter("resource/tsb/test/" + TIMESTAMP)

        if enable_debug:
            sess = tf_debug.LocalCLIDebugWrapperSession(sess)

        sess.run(init)

        for i in range(1, num_epochs + 1):
            if mini_batch_size != 0:
                num_mini_batches = int(m / mini_batch_size)
                mini_batches = util.random_mini_batches(train_x, train_y, mini_batch_size)

                cost_value = 0
                for mini_batch in mini_batches:
                    (mini_batch_x, mini_batch_y) = mini_batch
                    _, temp_cost = sess.run([optimizer, cost], feed_dict={cnn.x: mini_batch_x, cnn.y: mini_batch_y,
                                                                          cnn.keep_prob: keep_prob})
                    cost_value += temp_cost
                cost_value /= num_mini_batches
            else:
                _, cost_value = sess.run([optimizer, cost],
                                         feed_dict={cnn.x: train_x, cnn.y: train_y, cnn.keep_prob: keep_prob})

            # disable dropout
            summary_train, train_accuracy = sess.run([merged, accuracy],
                                                     feed_dict={cnn.x: train_x, cnn.y: train_y,
                                                                cnn.keep_prob: 1})
            summary_test, test_accuracy = sess.run([merged, accuracy],
                                                   feed_dict={cnn.x: test_x, cnn.y: test_y, cnn.keep_prob: 1})

            train_writer.add_summary(summary_train, i - 1)
            test_writer.add_summary(summary_test, i - 1)

            if print_detail and (i % 10 == 0 or i == 1):
                info = '\nIteration %d\n' % i + \
                       'Cost: %f\n' % cost_value + \
                       'Train accuracy: %f\n' % train_accuracy + \
                       'Test accuracy: %f' % test_accuracy
                log.log_info(info)

            # stop when test>0.95 and train>0.99
            if test_accuracy >= 0.95 and train_accuracy >= 0.99:
                info = '\nIteration %d\n' % i + \
                       'Cost: %f\n' % cost_value + \
                       'Train accuracy: %f\n' % train_accuracy + \
                       'Test accuracy: %f' % test_accuracy
                log.log_info(info)
                saver.save(sess, "resource/model/" + TIMESTAMP)
                break
            saver.save(sess, "resource/model/" + TIMESTAMP)
        train_writer.close()
        test_writer.close()

    log.log_info('program end')
Example #54
0
def playing_idol_on_event_playing_actor_request_complete(
        message_type, channel, channel_type, serialize):
    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorRequestComplete()

    # 消息反序列化
    TSerialization.deserialize(message, serialize)

    # 获取副本对象
    playing = idol_types.PlayingManager.get(message.playing_)
    if playing == None:
        log.log_error("获取 副本对象(%d) 失败" % message.playing_)
        return None

    # 获取玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        log.log_error("获取 玩家对象(%d) 失败" % message.actor_)
        return None

    # 玩家是否完成副本
    if actor.get_finish() == 0:
        log.log_error("玩家(%d) 未成完成idol副本" % message.actor_)
        return None

    # 获取副本配置
    playing_config = idol_types.Config.get(message.template_)
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % message.template_)
        return None

    # 消耗玩家副本次数
    request = ccrequest.playing.ttypes.RequestPlayingIncreaseComplete()
    # request.actor_ = message.actor_
    # request.playing_template_ = message.template_
    request.playing_ = message.playing_
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INCREASE_COMPLETE, \
        request_data, len(request_data))

    # 请求完成消息
    request = ccrequest.playing.ttypes.RequestPlayingComplete()
    # request.actor_ = message.actor_
    request.playing_ = message.playing_

    # 抽奖
    actor.draw_award_item(playing_config.get_draw_awards())
    request.draw_award_ = actor.get_draw_award_item()

    # 奖励
    request.awards_ = playing_config.get_awards()

    # 副本耗时
    playing_result = ccentity.playing.ttypes.PlayingResultField()
    playing_result.awarded_ = False
    playing_result.award_count_ = 1
    playing_result.paid_award_count_ = 0
    playing_result.values_ = []

    # result.value1: 花费时间
    playing_result.values_.append(int(time.time() - actor.get_start_time()))

    # 副本结果
    request.result_ = playing_result

    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_COMPLETE, \
        request_data, len(request_data))

    log.log_debug("玩家完成idol副本(%d)" % message.template_)

    # 请求清除场景中的NPC
    request_clear_npc = ccrequest.scene.ttypes.RequestSceneClearAllNpc()
    request_clear_npc.scene_ = playing.get_scene()
    request_clear_npc.delay_secs_ = 5

    # 序列化
    request_clear_npc_data = TSerialization.serialize(request_clear_npc)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_CLEAR_ALL_NPC, \
        request_clear_npc_data, len(request_clear_npc_data))

    log.log_debug("请求清除 场景(%d) npc" % request_clear_npc.scene_)
Example #55
0
def playing_plot_on_event_playing_actor_request_complete(
        message_type, channel, channel_type, serialize):
    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorRequestComplete()

    # 消息反序列化
    TSerialization.deserialize(message, serialize)

    # 获取副本对象
    playing = plot_types.PlayingManager.get(message.playing_)
    if playing == None:
        log.log_error("获取 副本对象(%d) 失败" % message.playing_)
        return None

    # 获取玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        log.log_error("获取 玩家对象(%d) 失败" % message.actor_)
        return None
    # 玩家得分(星数) 应该为0
    if actor.get_score() != 0:
        log.log_error("玩家 得分(%d) 错误" % actor.get_score())
        return None

    # 获取副本配置
    playing_config = plot_types.Config.get(message.template_)
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % message.template_)
        return None

    # 获取玩家通关需要杀死的NPC
    pass_kill_npc = playing_config.get_pass_kill_npc()
    if actor.get_kill_npc(pass_kill_npc) <= 0:
        log.log_error("玩家 未能杀死NPC(%d) 不能通关" % pass_kill_npc)
        return None

    # 随机抽取一个道具
    actor.draw_award_item(playing_config.get_draw_items())

    # 统计副本花费时间
    spend_time = time.time() - actor.get_start_time()

    # 请求消息
    request = ccrequest.playing.ttypes.RequestPlayingComplete()

    # 玩家星数加1
    actor.inc_score(1)

    # 死亡次数低于配置次数星数加1
    if actor.get_dead_count() <= playing_config.get_dead_count():
        actor.inc_score(1)

    # 花费时间低于配置时间星数加1
    if spend_time <= playing_config.get_spend_time():
        actor.inc_score(1)

    # 杀死NPC个数大于配置个数星数加1
    if actor.get_kill_npc_num() >= playing_config.get_npc_number():
        actor.inc_score(1)

    # 杀死隐藏NPC星数加1
    if actor.get_kill_npc(playing_config.get_hide_npc()):
        actor.inc_score(1)

    # request.actor_ = message.actor_
    request.playing_ = message.playing_
    request.score_ = actor.get_score()
    request.awards_ = []

    award_field = ccentity.playing.ttypes.PlayingAwardField()
    award_field.type_ = ccentity.resource.ttypes.ResourceType.RESOURCE

    score = actor.get_score()

    # 道具
    for award in playing_config.get_awards():
        if award.type_ == ccentity.resource.ttypes.ResourceType.RESOURCE:
            award.number_ = award.number_ * score * 20 / 100
        request.awards_.append(award)

    # 抽奖奖励
    request.draw_award_ = actor.get_draw_award_item()

    # 副本耗时, 分数
    playing_result = ccentity.playing.ttypes.PlayingResultField()
    playing_result.awarded_ = False
    playing_result.award_count_ = 1
    playing_result.paid_award_count_ = 0
    playing_result.values_ = []

    # result.value1: 花费时间
    playing_result.values_.append(int(spend_time))
    # result.value2: 分数
    playing_result.values_.append(score)

    log.log_debug("[plot] spend_time=%d" % spend_time)

    # 副本结果
    request.result_ = playing_result

    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_COMPLETE,\
        request_data, len(request_data))

    # 请求清除场景中的NPC
    request_clear_npc = ccrequest.scene.ttypes.RequestSceneClearAllNpc()
    request_clear_npc.scene_ = playing.get_scene()
    request_clear_npc.delay_secs_ = 5

    # 序列化
    request_clear_npc_data = TSerialization.serialize(request_clear_npc)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_CLEAR_ALL_NPC, \
        request_clear_npc_data, len(request_clear_npc_data))

    log.log_debug("请求清除 场景(%d) npc" % request_clear_npc.scene_)
Example #56
0
@app.route('/images/<image_id>/probability', method="GET")
def get_probability(image_id):
    res = prob.get_prob(image_id)
    return put_response(res)

@app.route('/static/<file_type>/<file>')
def read_static(file_type, file):
    if file_type == "js":
        content_type = "text/javascript"
    elif file_type == "css":
        content_type = "text/css"
    else:
        content_type = "text/html"
    with open('../static/'+file_type+'/'+file) as f:
        data = f.read()
    return put_response(data=data, content_type=content_type)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--port', dest="port", type=int, default=8081)
    parser.add_argument('--debug', dest="debug", action="store_true")
    args = parser.parse_args()
    port = args.port
    if args.debug:
        log_debug("debug mode.")
        app.run(host="0.0.0.0", port=8081, debug=True, reloader=True)
    else:
        server = WSGIServer(("0.0.0.0", port), app, handler_class=WebSocketHandler)
        log_debug("websocket server start. port:{}".format(port))
        server.serve_forever()
Example #57
0
def playing_plot_on_event_playing_actor_enter(message_type, channel,
                                              channel_type, serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorEnter()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 关注玩家杀死NPC事件、请求完成副本事件、角色被杀死事件
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_plot_on_event_actor_kill_npc")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
        "playing_plot_on_event_playing_actor_request_complete")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_plot_on_event_actor_killed")
    # 副本1需要关注玩家血量改变的事件
    if message.template_ == 1:
        proxy.Communicator.follow(ccevent.ttypes.EventType.EVENT_ROLE_CHANGE_HP, \
            message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
            "playing_plot_on_event_role_change_hp")

    # 获取副本对象
    playing = plot_types.PlayingManager.get(message.playing_)
    if playing == None:
        proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." %
                            message.playing_)
        return None

    now = time.time()

    # 获取玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        # 玩家不存在时,创建并加入副本中
        actor = plot_types.Actor(message.actor_, now)
        playing.add_actor(actor)
        # 消耗玩家副本次数
        request = ccrequest.playing.ttypes.RequestPlayingIncreaseComplete()
        # request.actor_ = message.actor_
        # request.playing_template_ = message.template_
        request.playing_ = message.playing_
        # 序列化
        request_data = TSerialization.serialize(request)
        # 发送请求
        request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INCREASE_COMPLETE, \
            request_data, len(request_data))

    # 副本管理器中加入一个玩家ID到副本ID的对应关系
    plot_types.PlayingManager.add_actor(message.actor_, message.playing_)

    # 请求初始化玩家
    request = ccrequest.playing.ttypes.RequestPlayingInitializeActor()
    request.actor_ = message.actor_
    request.spend_time_ = now - actor.get_start_time()
    request.scores_ = []
    request.datas_ = []
    # 循环获取玩家杀死的每个NPC
    for (k, v) in actor.get_kill_npcs().items():
        score_field = ccentity.playing.ttypes.PlayingScoreField()
        score_field.type_ = ccentity.playing.ttypes.PlayingScoreType.KILL_NPC
        score_field.key_ = k
        score_field.value_ = v
        request.scores_.append(score_field)

    # 序列化消息
    request_data = TSerialization.serialize(request)

    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INITIALIZE_ACTOR, \
        request_data, len(request_data))

    log.log_debug("玩家(%d)进入副本(id=%d,template=%d)" % \
        (message.actor_, message.playing_, message.template_))
Example #58
0
 def close(self):
     self._shrink_stacks()
     log.log_debug(logger, "Closing file %s" % self._filename)
     self._f.close()