-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.py
198 lines (160 loc) · 7.84 KB
/
analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import configparser
from dao import ActivityType, VRChatActivityLogsDao
import datetime
import heapq
import operator
import sys
def is_near_time(a_str, b_str):
date_dt1 = datetime.datetime.strptime(a_str, "%Y-%m-%d %H:%M:%S")
date_dt2 = datetime.datetime.strptime(b_str, "%Y-%m-%d %H:%M:%S")
return (date_dt2 - date_dt1).seconds <= 1
def diff_seconds(from_time, to_time):
date_dt1 = datetime.datetime.strptime(from_time, "%Y-%m-%d %H:%M:%S")
date_dt2 = datetime.datetime.strptime(to_time, "%Y-%m-%d %H:%M:%S")
return (date_dt2 - date_dt1).seconds
def pickup(d, limit):
L = sorted(list(d.items()), key=operator.itemgetter(1), reverse=True)[:limit]
res = ""
for _, (name, count) in enumerate(L):
res += f"{name}[{count}], "
return res
class Analyzer:
__invalid_datetime = "9999-99-99 23:59:59"
def __init__(self, orner_name, home_world):
self.__dao = VRChatActivityLogsDao()
self.__orner_name = orner_name
self.__home_world = home_world
def construct(self, by_time=False):
self.fetch_world_stay_time()
self.fetch_players_in_world()
if by_time:
self.fetch_players_relation_by_time()
else:
self.fetch_players_relation()
def fetch_world_stay_time(self):
df = self.__dao.fetch_world_join_time()
L = df.values.tolist()
L.append(["", self.__invalid_datetime])
world_data = [(
L[i][0],
L[i][1],
L[i + 1][1],
) for i in range(len(L) - 1)]
self.world_data = world_data
def complete_player_data(self, players):
buffer = {}
player_data = []
for user_name, timestamp, activity_type in players:
if activity_type == int(ActivityType.MET_PLAYER):
if user_name in buffer:
print(f"[error] already added: {user_name} at {timestamp}", file=sys.stderr)
continue
buffer[user_name] = timestamp
elif activity_type == int(ActivityType.LEAVE_PLAYER):
if user_name not in buffer:
print(f"[error] not exist: {user_name} at {timestamp}", file=sys.stderr)
continue
player_data.append((buffer.pop(user_name), timestamp, user_name))
else:
print(f"[error] unknown type: {activity_type} of {user_name} at {timestamp}", file=sys.stderr)
for user_name, timestamp in buffer.items():
print(f"[warn] not left: {user_name} from {timestamp}", file=sys.stderr)
player_data.append((timestamp, self.__invalid_datetime, user_name))
player_data.sort()
# print(player_data)
return player_data
def fetch_players_in_world(self):
players = []
for world_name, time_start, time_end in self.world_data:
table = self.__dao.fetch_user_meet_data(time_start, time_end)
time_in_list = table[table["UserName"] == self.__orner_name]["Timestamp"].values.tolist()
if not time_in_list:
print(f"[error] not found {self.__orner_name} in world '{world_name}'\ntable: {table}", file=sys.stderr)
continue
time_in = time_in_list[0]
users = self.complete_player_data(table.values.tolist())
if not users:
continue
players.append((world_name, time_in, users))
self.players = players
def show_join_or_joined(self, players):
meet_to_me = {}
i_meet_who = {}
for i, (world, time, pls) in enumerate(players):
if world == self.__home_world:
continue
already_player = set(pl for pl, time_in, _ in pls if is_near_time(time, time_in))
for pl in already_player:
if pl == self.__orner_name:
continue
i_meet_who[pl] = i_meet_who.setdefault(pl, 0) + 1
comming_player = set(pl for pl, time_in, _ in pls if not is_near_time(time, time_in))
for pl in comming_player:
meet_to_me[pl] = meet_to_me.setdefault(pl, 0) + 1
already_count = len(already_player) - 1
comming_count = len(comming_player)
assert(already_count >= 0 and comming_count >= 0)
# print(f"[{i}] {world}: すでにいた人: {already_count}人, 入ってきた人: {comming_count}人")
S1 = sorted(list(i_meet_who.items()), key=operator.itemgetter(1), reverse=True)
S2 = sorted(list(meet_to_me.items()), key=operator.itemgetter(1), reverse=True)
print("I meet to: ")
for i in range(10):
print(f"[{i + 1}] {S1[i][0]}: {S1[i][1]}回")
print("")
print("who meet to me(?): ")
for i in range(10):
print(f"[{i + 1}] {S2[i][0]}: {S2[i][1]}回")
def fetch_players_relation(self):
valid_player_data = filter(lambda x: x[2] != self.__invalid_datetime, self.players)
relation = {self.__orner_name: dict()}
for i, (world, time, pls) in enumerate(valid_player_data):
queue = []
joinning_player = {self.__orner_name}
for in_time, out_time, pl in pls:
if pl == self.__orner_name:
continue
while queue and queue[0][0] < in_time:
joinning_player.remove(heapq.heappop(queue)[1])
if is_near_time(time, in_time):
relation[self.__orner_name][pl] = relation[self.__orner_name].setdefault(pl, 0) + 1
else:
for pl2 in joinning_player:
if pl2 == self.__orner_name:
continue
relation[pl][pl2] = relation.setdefault(pl, dict()).setdefault(pl2, 0) + 1
heapq.heappush(queue, (out_time, pl))
joinning_player.add(pl)
for pl, to_join_list in relation.items():
if max(to_join_list.values()) < 2:
continue
print(f"{pl}: {pickup(to_join_list, 5)}")
# print(relation)
self.relation = relation
def fetch_players_relation_by_time(self):
valid_player_data = filter(lambda x: x[1] != self.__invalid_datetime, self.players)
relation = {self.__orner_name: dict()}
for i, (world, time, pls) in enumerate(valid_player_data):
for in_time1, out_time1, pl1 in pls:
for in_time2, out_time2, pl2 in pls:
if pl1 == pl2:
continue
if self.__invalid_datetime in (in_time1, out_time1, in_time2, out_time2):
continue
if in_time1 <= in_time2 <= out_time1 or in_time2 <= in_time1 <= out_time2:
sec = diff_seconds(max(in_time1, in_time2), min(out_time1, out_time2))
relation[pl1][pl2] = relation.setdefault(pl1, dict()).setdefault(pl2, 0) + sec
relation[pl2][pl1] = relation.setdefault(pl2, dict()).setdefault(pl1, 0) + sec
for pl, to_join_list in relation.items():
if max(to_join_list.values()) < 2:
continue
print(f"{pl}: {pickup(to_join_list, 5)}")
# print(relation)
self.relation = relation
if __name__ == "__main__":
config_ini = configparser.ConfigParser()
config_ini.read("./config.ini", encoding="utf-8")
orner_name = config_ini["setting"]["orner_name"]
home_world = config_ini["setting"]["home_world"]
analyzer = Analyzer(orner_name, home_world)
analyzer.construct(by_time=True)
print(analyzer.relation)