Example #1
0
 def add_plotbands_phase(self):
     """ Add PlotBands related to show Phase"""
     plotBands = []
     last_entry = len(self.entries) - 1
     n = 1
     while n < last_entry and\
     self.entries[n].phase is not None and\
     self.entries[n] is not None and\
     self.entries[n].next().phase is not None:
         begin = self.entries[n].dt
         phase = self.entries[n].phase
         n += 1
         while self.entries[n] is not None and\
         self.entries[n].phase is not None and\
         self.entries[n].phase == phase and\
         n < last_entry:
             n += 1
         end = self.entries[n].dt
         plotBand = {
             'color': PhaseColor[phase],
             'from': datetime_to_timestamp(begin),
             'to': datetime_to_timestamp(end)
         }
         plotBands.append(plotBand)
     self.json['xAxis']['plotBands'] = plotBands
Example #2
0
    def get_opt_domain(self):
        """获取需要操作域名"""
        filter_opt = {
            "status": {
                '$in': [
                    SyncDomainStatus.CREATING, SyncDomainStatus.EDITING,
                    SyncDomainStatus.ACTIVATING
                ]
            }
        }

        task_list = self.task_db.find(filter_opt)

        now_timestamp = datetime_to_timestamp(datetime.datetime.now())

        for task in task_list:

            channel = task.get('channel', '')
            domain = task.get('domain', '')
            task_id = task.get('task_id', '')
            status = task.get('status', '')
            cname = task.get('cname', '')
            modify_time = task.get('modify_time', '')
            dis_cname = task.get('dis_cname', False)

            # 超时检查
            if modify_time:
                modify_time = datetime_to_timestamp(modify_time)
                if now_timestamp - modify_time > MODIFY_TIME_LIMIT:
                    doc = {
                        'status': SyncDomainStatus.ERROR,
                        'err_msg': 'is timeout'
                    }
                    self.set_task_doc(channel, doc)
                    continue

            task_info = {
                'channel': channel,
                'domain': domain,
                'task_id': task_id,
                'status': status,
                'cname': cname,
                'modify_time': modify_time,
                'dis_cname': dis_cname
            }

            self.opt_domain_list.append(task_info)
Example #3
0
 def start_wait_until_time(self, until_time, auto_fix=False):
     """
     执行wait。使用传入时间
     :param until_time: 格式例子。'2018-09-28 22:45:50.000'
     :param auto_fix: 是否自动校正时间。默认false
     :return:
     """
     until_time_milli_sec = datetime_to_timestamp(datetime.strptime(until_time, '%Y-%m-%d %H:%M:%S.%f'))
     if auto_fix:
         # 自动将基于服务器的时间,切换到基于本地时钟的时间
         until_time_milli_sec = until_time_milli_sec - self.jd_time_sync.local_diff_server_time_microseconds()
     self.timer.start(until_time_milli_sec)
Example #4
0
def server_time(request, template='game/server_time.html'):
    server_id = int(request.REQUEST.get('server_id', '') or 0)
    server_time = diff_time = 0
    if server_id:
        gmp = GMProtocol(server_id)
        result = gmp.query_server_time()
        server_time = result[0]
        diff_time = result[2]

        if request.method == 'POST':
            modify_time = request.REQUEST.get('modify_time', '')
            server_time_str = request.REQUEST.get('server_time_str', '')
            if server_time_str and modify_time:
                modify_timestamp = datetime_to_timestamp(modify_time)
                server_timestamp = datetime_to_timestamp(server_time_str)
                add_sec = modify_timestamp - server_timestamp
                add_sec = 0 if add_sec < 0 else add_sec
                #            day = int(request.REQUEST.get('day','') or 0)
                #            hour = int(request.REQUEST.get('hour','') or 0)
                #            minute = int(request.REQUEST.get('minute','') or 0)
                #            sec = int(request.REQUEST.get('sec','') or 0)
                #            add_sec = day * 86400 + hour * 3600 + minute * 60 + sec
                if add_sec >= 10 * 86400:
                    err_msg = '嘿嘿嘿 增加时间不能大于 10天!'
                else:
                    result = gmp.add_server_time(add_sec)
                    #                    server_time = result[0]
                    #                    diff_time = result[2]
                    info_msg = '成功!' if server_time else '失败!'
        else:
            server_time = result[0]
            diff_time = result[2]
        real_time = server_time - diff_time
        add_day = diff_time / 86400
        add_hour = (diff_time % 86400) / 3600
        add_minute = ((diff_time % 86400) % 3600) / 60
        add_second = ((diff_time % 86400) % 3600) % 60

    return render_to_response(template, locals())
Example #5
0
  def find_or_create(self, player):

    player = self._players.find_one_and_update(
      {"summonerId": player["summonerId"]},
      {
        "$setOnInsert": {"last_update": datetime_to_timestamp(PlayerDb.EARLIEST_UPDATE)},
        "$set": player
      },
      upsert=True,
      return_document=ReturnDocument.AFTER
    )

    return player
Example #6
0
def bookmarks(uid, since=None):
    q = Bookmark.query(Bookmark.uid == uid).order(-Bookmark.last_modified)
    if since: q = q.filter(Bookmark.last_modified >= util.datetime_from_timestamp(since))
    bookmarks = q.fetch(200)
    articles = ndb.get_multi([b.article for b in bookmarks])
    def to_json(bookmark, article):
        j = bookmark.json()
        j['article'] = article.json() if article else None
        return j
    return {
        "bookmarks": [to_json(bookmark, article) for bookmark, article in zip(bookmarks, articles)],
        "since": util.datetime_to_timestamp(datetime.datetime.now()),
        "partial": since is not None
    }
Example #7
0
    def check_dis_cname(self, task):
        """检查隐藏cname"""

        status = task.get('status')
        cname = task.get('cname', '')
        channel = task.get('channel', '')
        modify_time = task.get('modify_time', '')

        modify_time = datetime_to_timestamp(modify_time)
        sub_time = time.time() - modify_time

        if status == SyncDomainStatus.DIS_CNAME:

            if sub_time > DIG_CACHE_TIME:

                dig_test_str = 'dig %s |grep "CNAME" |wc -l' % cname
                print(dig_test_str)
                output = os.popen(dig_test_str)
                check_dig_result = output.read()

                check_dig_result = check_dig_result.strip()

            if sub_time > DIG_CACHE_TIMEOUT and int(check_dig_result) != 0:

                doc = {
                    'status': SyncDomainStatus.ERROR,
                    'err_msg': 'cname diss error'
                }
                self.set_task_doc(channel, doc)

            elif check_dig_result == '0':

                doc = {
                    'status': SyncDomainStatus.DNS_TASK_EFFECT,
                    "modify_time": datetime.datetime.now()
                }
                self.set_task_doc(channel, doc)

        elif status == SyncDomainStatus.DNS_TASK_EFFECT:

            if sub_time > ALL_DNS_SUCC:

                doc = {
                    'status': SyncDomainStatus.SERVING,
                    'err_msg': '',
                    'task_id': '',
                    'modify_time': ''
                }
                self.set_task_doc(channel, doc)
Example #8
0
def main(argv):
  mongo_url = "mongodb://localhost:27017"
  last_update = datetime_to_timestamp(PlayerDb.EARLIEST_UPDATE) + 1
  initial_seed = 60783
  update_old = False
  workers = [1, 1]

  # Parse commandline for last_update date, and thread numbers
  parser = argparse.ArgumentParser(description='Collect data from Riot API')
  parser.add_argument("-i", default=initial_seed, type=int, help="Initial summoner ID")
  parser.add_argument("-n", default=workers, nargs=2, type=int, help="# of pworkers and mworkers, respectively")
  parser.add_argument("-d", help="Starting date to fetch data for")   # TODO add commandline args
  parser.add_argument("--update_old", action='store_true', help="Whether to prioritize gathering of new player data")
  parser.add_argument("--mongo", default=mongo_url, help="URL of MongoDB")
  parser.add_argument("--api_key", default=API_KEY, help="Riot API Key")
  args = parser.parse_args()

  if args.d is not None:
    last_update = datetime_to_timestamp(datetime.datetime.strptime(args.d, "%m/%d/%y"))
  if args.api_key == "":
    print "API key not found. Please add your key to api_key.py or use --api_key"
    sys.exit(2)
  else:
    RiotApi.set_api_key(args.api_key)

  # Initialize MongoDB
  mongo_client = MongoClient(args.mongo)
  outliers_db = mongo_client.outliers

  # Initialize components
  api_scheduler = RiotApiScheduler()
  player_db = PlayerDb(outliers_db.players)
  match_db = MatchDb(outliers_db.matches)
  build_db = BuildDb(outliers_db)
  player_queue = Queue(maxsize=MAX_PLAYER_QSIZE)
  match_queue = Queue(maxsize=MAX_MATCH_QSIZE)

  riot_items = RiotItems()
  match_processor = MatchProcessor(riot_items)

  workers = []

  # Initial seed
  player_queue.put({
      "profileIcon": 588,
      "matchHistoryUri": "/v1/stats/player_history/NA/60783",
      "summonerName": "-INITIAL_SEED- (TheOddOne)",
      "summonerId": args.i,
      "last_update": datetime_to_timestamp(PlayerDb.EARLIEST_UPDATE),
      "league": "GOLD"
   })

  # Register stop signal handler
  def stop(sig, frame):
    api_scheduler.stop()
    for worker in workers:
      worker.stop()
    print "[STOP] Players left: %r, Matches left: %r" % (player_queue.qsize(), match_queue.qsize())
  signal.signal(signal.SIGINT, stop)

  # Start threads
  api_scheduler.start()

  for i in xrange(args.n[0]):
    worker = PlayerWorker(
      is_prioritize_new=not args.update_old,
      api_scheduler=api_scheduler,
      player_db=player_db,
      match_db=match_db,
      last_update=last_update,
      player_queue=player_queue,
      match_queue=match_queue,
    )
    workers.append(worker)
    worker.start()

  for i in xrange(args.n[1]):
    worker = MatchWorker(
      last_update=last_update,
      api_scheduler=api_scheduler,
      player_db=player_db,
      match_db=match_db,
      build_db=build_db,
      player_queue=player_queue,
      match_queue=match_queue,
      match_processor=match_processor
    )
    workers.append(worker)
    worker.start()

  # Janky way to quit program
  line = raw_input()
  if line == "q":
    stop(2, None)
Example #9
0
def shop_daily_save(request):
    import urllib
    '''
    每日限购保存
    '''
    MIN_ITEM_ID = 10000

    server_id = int(request.GET.get('server_id', 0))
    usm = request.admin
    server_ids = request.POST.getlist('server_id')
    server_ids = list(set([int(i) for i in server_ids]))

    item_id = request.POST.get('item_id', 0)
    item_name = request.POST.get('item_name', '')
    item_desc = request.POST.get('item_desc', '')
    # 处理 % 符号
    item_desc = urllib.quote(item_desc.encode('utf-8'))
    item_type = int(request.POST.get('item_type', 0))
    content = int(request.POST.get('content', 0))
    currency = int(request.POST.get('currency', -1))
    tag = request.POST.get('tag', '')
    discount = int(request.POST.get('discount', 10))
    price = float(request.POST.get('price', 0))
    limit_num = int(request.POST.get('limit_num', 0))
    limit_vip = int(request.POST.get('limit_vip', 0))
    limit_rank = int(request.POST.get('limit_rank', 0))
    begin_time = request.POST.get('begin_time', '0')
    end_time = request.POST.get('end_time', '0')

    reward_desc = json.loads(request.POST.get('reward_desc', ''))
    output_path = request.POST.get('output_path', '')

    level_ratio = json.loads(request.POST.get('level_ratio', ''))
    res_info = convert_res_info(reward_desc, level_ratio)

    if begin_time and end_time:
        begin_time = datetime_to_timestamp(begin_time)
        end_time = datetime_to_timestamp(end_time)
    elif not begin_time and not end_time:
        begin_time = 0
        end_time = 0
    else:
        return HttpResponse("日期错误")

    ishot = False
    if tag and tag == "ishot":
        ishot = True
        discount = 10
    elif discount not in range(1, 11):
        return HttpResponse('折扣数据非法')

    item = [
        None, item_name, item_desc, content, currency, ishot, price, item_type,
        discount, limit_num, limit_vip, limit_rank, begin_time, end_time,
        reward_desc, output_path, level_ratio
    ]

    allow_create = True
    #  权限判断   //如果不是管理员账号
    if not usm.is_manager:
        user_server_list = usm.get_resource(
            'server').all()  # 获取当前登陆的管理员账号有权限管理的服务器列表
        user_server_id = []
        for user_server in user_server_list:
            user_server_id.append(user_server.id)

    if not allow_create:
        return HttpResponse('没有权限添加')

    if server_ids:
        for new_server_id in server_ids:
            gmp = GMProtocol(new_server_id)
            if new_server_id == server_id and item_id:
                item_id = int(item_id)
            else:
                # 添加商品ID
                _s, _p = gmp.get_shop_list(1)
                shop_list, _p = gmp.get_shop_list(_p)
                item_ids = [i[0] for i in shop_list]
                item_id = max(max(item_ids), MIN_ITEM_ID) + 1
            item[0] = item_id
            msg = [
                item,
            ]
            try:
                result = gmp.set_shop_list(msg)
            except:
                return HttpResponse('{"code":1, "data": "添加出错"}')
            else:
                if len(result) > 1 and not result[1]:
                    return HttpResponse('{"code":1, "data": "添加出错"}')

    server_time = None
    if server_id != 0:
        gmp = GMProtocol(server_id)
        server_time = gmp.query_server_time()[0]

    template_path = 'game/shop_daily_edit.html'
    context = {}
    item[2] = urllib.unquote(item[2])
    context["item"] = item
    context["item_id"] = item_id
    context["res_info"] = res_info
    context["server_id"] = server_id
    context["server_time"] = server_time
    context["group_servers_dict"] = get_group_servers_dict(request)
    context["select_server_ids"] = server_ids

    ajax = request.GET.get('ajax', '')
    if ajax:
        return HttpResponse('{"code":0}')
    return render_to_response(template_path, context)
Example #10
0
def server_active_cfg_list(request):
    _now = int(time.time())
    g = lambda x, y: request.GET.get(x, request.POST.get(x, y))
    status = int(g('status', '1'))
    sdate = g('sdate', '')
    edate = g('edate', '')
    page_num = int(g('page_num', '1'))

    _result_list = load_active_task_data()

    if sdate and edate:
        _sd = datetime_to_timestamp(sdate)
        _ed = datetime_to_timestamp(edate)
    else:
        sdate = edate = timestamp_to_datetime_str(_now)
        _sd = _ed = 0

    result_list = []
    for item in _result_list:
        _ststamp = datetime_to_timestamp(item['sdate'])
        _etstamp = datetime_to_timestamp(item['edate'])

        if _sd and _ed:  #时间搜索
            _is_append = (_ed > _ststamp > _sd)
        else:
            if status == 1:  #活动中
                _is_append = _now < _etstamp
            elif status == 2:  #过期
                _is_append = _now > _etstamp
            else:
                _is_append = True
        if _is_append:
            result_list.append(item)

    page_size = int(g('page_size', '20'))
    total_record = len(result_list)
    sp = (page_num - 1) * page_size
    result_list = result_list[sp:sp + page_size]

    list_len = result_list.__len__()
    param_dict = {}
    for p in get_server_param():
        param_dict[p[0]] = p[1]
    i = 0
    while list_len > i:
        k = 1 + i
        item1 = result_list[i]
        while list_len > (k):
            item2 = result_list[k]
            sdate1 = datetime.datetime.strptime(item1['sdate'],
                                                '%Y-%m-%d %H:%M:%S')
            info1 = item1['server_info']
            edate1 = datetime.datetime.strptime(item1['edate'],
                                                '%Y-%m-%d %H:%M:%S')
            rev_info1 = item1['recover_server_info']

            sdate2 = datetime.datetime.strptime(item2['sdate'],
                                                '%Y-%m-%d %H:%M:%S')
            info2 = item2['server_info']
            edate2 = datetime.datetime.strptime(item2['edate'],
                                                '%Y-%m-%d %H:%M:%S')
            rev_info2 = item2['recover_server_info']

            error_dict = {}

            def set_msg(p1, p2):
                for key1 in p1:
                    value1 = p1.get(key1)
                    value2 = p2.get(key1, None)

                    if None != p2.get(key1, None) and value1 != value2:
                        d = error_dict.get(i, {})
                        if None == d.get(key1, None):
                            msg_list = item1.get('msg_list', [])
                            msg = '%s(%s)' % (item2['title'].encode('utf-8'),
                                              param_dict.get(key1, ''))
                            msg_list.append(msg)
                            item1['msg_list'] = msg_list
                            d[key1] = 1
                        error_dict[i] = d

            s1 = set([int(server_id) for server_id in item1["server_list"]])
            s2 = set([int(server_id) for server_id in item2["server_list"]])

            if (datetime.datetime.now() -
                    edate1).days < 2 and (s1 & s2).__len__() != 0:
                if abs((sdate1 - sdate2).days) <= 1:
                    set_msg(info1, info2)

                if abs((sdate1 - edate2).days) <= 1:
                    set_msg(info1, rev_info2)

                if abs((edate1 - edate2).days) <= 1:
                    set_msg(rev_info1, rev_info2)

                if abs((edate1 - sdate2).days) <= 1:
                    set_msg(rev_info1, info2)

            k += 1

        i += 1

    return render_to_response('game/cfg_server_active_list.html', locals())
Example #11
0
def battle_server_time(request, template='game/battle_server_time.html'):
    server_id = int(request.REQUEST.get('server_id', '') or 0)
    server_time = diff_time = 0
    if server_id:
        group_servers_dict = get_group_servers_dict(request)
        gmp = GM_battleProtocol(server_id)
        result = gmp.query_battle_server_time()
        server_time = result[0]
        diff_time = result[2]

        if request.method == 'POST':
            modify_time = request.REQUEST.get('modify_time', '')
            server_time_str = request.REQUEST.get('server_time_str', '')
            if server_time_str and modify_time:
                modify_timestamp = datetime_to_timestamp(modify_time)
                server_timestamp = datetime_to_timestamp(server_time_str)
                add_sec = modify_timestamp - server_timestamp
                add_sec = 0 if add_sec < 0 else add_sec
                #            day = int(request.REQUEST.get('day','') or 0)
                #            hour = int(request.REQUEST.get('hour','') or 0)
                #            minute = int(request.REQUEST.get('minute','') or 0)
                #            sec = int(request.REQUEST.get('sec','') or 0)
                #            add_sec = day * 86400 + hour * 3600 + minute * 60 + sec
                if add_sec >= 10 * 86400:
                    err_msg = '嘿嘿嘿 增加时间不能大于 10天!'
                else:
                    result = gmp.add_battle_server_time(add_sec)
                    #                    server_time = result[0]
                    #                    diff_time = result[2]
                    info_msg = '成功!' if server_time else '失败!'

            kid = request.REQUEST.get('kid', '')
            sids = request.REQUEST.get('sids', '')
            if sids:
                sids = json.loads(sids)

            if kid and len(sids) > 0:

                _r = {'success': [], 'failed': []}
                for s in sids:
                    try:
                        gmp = GMProtocol(s)
                        stime_res = gmp.query_server_time()
                        sid_time = stime_res[0]
                        dif_time = server_time - sid_time
                        result = -1
                        while 1:
                            if dif_time >= 10 * 86400:
                                _dif_time = 10 * 86300
                                try:
                                    result = gmp.add_server_time(_dif_time)
                                except:
                                    break
                                dif_time = dif_time - _dif_time
                            else:
                                result = gmp.add_server_time(dif_time)
                                break

                        if result == 0:
                            _r['success'].append(s)
                        else:
                            _r['failed'].append(s)

                    except Exception, e:
                        print trace_msg()
                        _r['failed'].append(s)
                        pass

                return HttpResponse(json.dumps(_r))
        else:
            server_time = result[0]
            diff_time = result[2]
        real_time = server_time - diff_time
        add_day = diff_time / 86400
        add_hour = (diff_time % 86400) / 3600
        add_minute = ((diff_time % 86400) % 3600) / 60
        add_second = ((diff_time % 86400) % 3600) % 60