forked from InitialDet/AutoHook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdate_bite_timers.py
128 lines (101 loc) · 3.73 KB
/
update_bite_timers.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
import os
import requests
import json
def fetch_bite_data():
url = "https://gubal.ffxivteamcraft.com/graphql"
query = """
query BiteTimesPerFishPerSpotQuery($spotId: Int) {
biteTimes: bite_time_per_fish_per_spot(
where: {
spot: {_eq: $spotId},
flooredBiteTime: {_gt: 1, _lt: 600},
occurences: {_gte: 3}
}
) {
itemId
spot
flooredBiteTime
occurences
}
}
"""
payload = {
"operationName": "BiteTimesPerFishPerSpotQuery",
"variables": {},
"query": query
}
response = requests.post(
url,
json=payload,
headers={"Content-Type": "application/json"}
)
if response.status_code != 200:
raise RuntimeError(f"API request failed: {response.text}")
return response.json()
def calculate_quantile(data_points, target_quantile):
if not data_points:
return None
total_count = sum(count for _, count, _ in data_points)
cumulative_count = 0
for value, count, _ in data_points:
cumulative_count += count
if cumulative_count > total_count * target_quantile:
return value
return None
def calculate_statistics(bite_time_series):
first_quartile = calculate_quantile(bite_time_series, 0.25)
third_quartile = calculate_quantile(bite_time_series, 0.75)
lower_percentile = calculate_quantile(bite_time_series, 0.02)
upper_percentile = calculate_quantile(bite_time_series, 0.98)
bite_times = [time for time, _, _ in bite_time_series]
counts = [count for _, count, _ in bite_time_series]
min_time = min(bite_times)
max_time = max(bite_times)
median = calculate_quantile(bite_time_series, 0.5)
total_weighted_time = sum(time * count for time, count, _ in bite_time_series)
total_count = sum(counts)
mean = total_weighted_time / total_count
iqr = third_quartile - first_quartile
whisker_min = max(first_quartile - 1.5 * iqr, min_time)
whisker_max = min(third_quartile + 1.5 * iqr, max_time)
return {
"itemId": bite_time_series[0][2],
"min": min_time,
"median": median,
"mean": mean,
"max": max_time,
"whiskerMin": whisker_min,
"whiskerMax": whisker_max,
"q1": lower_percentile,
"q3": upper_percentile
}
def process_bite_data(bite_data):
fish_ids = set(entry['itemId'] for entry in bite_data['data']['biteTimes'])
bite_time_series = []
for fish_id in fish_ids:
fish_bites = sorted(
[(entry['flooredBiteTime'], entry['occurences'], entry['itemId'])
for entry in bite_data['data']['biteTimes']
if entry['itemId'] == fish_id],
key=lambda x: x[0]
)
if fish_bites:
bite_time_series.append(fish_bites)
return [calculate_statistics(series) for series in bite_time_series]
def main():
try:
bite_data = fetch_bite_data()
statistics = process_bite_data(bite_data)
statistics = sorted(statistics, key=lambda x: x['itemId'])
output_dir = os.path.join('AutoHook', 'Data', 'FishData')
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, 'bitetimers.json')
# Write the sorted statistics to the JSON file
with open(output_path, 'w', encoding='utf-8') as file:
json.dump(statistics, file, indent=2)
print("Successfully updated bitetimers.json")
except Exception as error:
print(f"Error: {error}")
raise
if __name__ == "__main__":
main()