达摩接口

混江龙づ霸主 提交于 2020-03-02 14:02:59
import os
import time
import json
from google.cloud import bigtable
from google.cloud import happybase


os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/liufuchang/Desktop/bigdata-group.json"


class status(object):
    def __init__(self):
        self.client = bigtable.Client(project="heidao-market", admin=True)
        self.instance = self.client.instance("yotta-bigtable-tw")
        self.connection = happybase.Connection(instance=self.instance)
        self.table = self.connection.table("mafia1_SnapShot_Status_result")
        pass

    def get_scan_list(self, player_id):
        end_time = round(time.time() * 1000)
        start_time = end_time - 86400000
        player_id = player_id
        row_key = str(player_id)[::-1]
        row = self.table.row(row_key)
        result = {}
        if len(row) == 0:
            return result
        last_vals = {}
        last_times = {}
        for i in range(len(row)):
            col = bytes("cf1:status_" + str(i), encoding="utf8")
            if col not in row.keys():
                continue
            row_dic = json.loads(row[col])
            time_str = row_dic["time"]
            time_list = time_str.split('#')
            for key in row_dic:
                if key != "time":
                    val = row_dic[key]
                    val_list = val.split("#")
                    sums = 0.0
                    last_val = val_list[0]
                    last_time = start_time
                    if key in last_vals:
                        last_val = last_vals[key]
                    if key in last_times:
                        last_time = last_times[key]
                    for j in range(len(time_list)):
                        cur_time = int(time_list[j])
                        if cur_time >= last_time:
                            sums += (cur_time - last_time) * last_val
                            last_time = cur_time
                        last_val = float(val_list[j])
                    last_vals[key] = last_val
                    last_times[key] = last_time
                    if key in result:
                        result[key] = result[key] + sums
                    else:
                        result[key] = sums
                    if i == len(row) - 1:
                        result[key] = result[key] + (end_time - last_time) * last_val
                        result[key] = result[key] / 86400000
        return result

    # 根据时间寻找状态
    def get_status_by_time(self, player_id, player_time):
        row_key = str(player_id)[::-1]
        end_time = player_time
        row = self.table.row(row=row_key)
        result = {}
        if len(row) == 0:
            return result
        for i in range(len(row)):
            col = bytes("cf1:status_" + str(i), encoding="utf8")
            if col not in row.keys():
                continue
            row_dic = json.loads(row[col])
            time_str = row_dic["time"]
            time_list = time_str.split('#')
            index = -1
            for j in range(len(time_list)):
                if int(time_list[j]) >= end_time:
                    break
                index = j
            if index == -1:
                return result
            for key in row_dic:
                val = row_dic[key]
                val_list = val.split('#')
                result[key] = val_list[index]
        return result


if __name__ == "__main__":
    status = status()
    # dic = status.get_scan_list(1014223004)
    # print(dic)
    dic = status.get_status_by_time(1014223004, 1582442660353)
    print(dic)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!