|
1
|
|
|
import json |
|
2
|
|
|
import os.path |
|
3
|
|
|
|
|
4
|
|
|
import pandas as pd |
|
5
|
|
|
from google.auth.transport.requests import Request |
|
6
|
|
|
from google.oauth2.credentials import Credentials |
|
7
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow |
|
8
|
|
|
from googleapiclient.discovery import build |
|
9
|
|
|
from numpyencoder import NumpyEncoder |
|
10
|
|
|
|
|
11
|
|
|
# from main import MyBot |
|
12
|
|
|
from modules import get_settings |
|
13
|
|
|
|
|
14
|
|
|
SCOPES = ['https://www.googleapis.com/auth/spreadsheets'] |
|
15
|
|
|
SAMPLE_RANGE_NAME = 'A1:AA200' |
|
16
|
|
|
|
|
17
|
|
|
SAMPLE_SPREADSHEET_ID_input = get_settings.get_settings("EXCEL_ID") |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def handle_creds(creds, token: str) -> None: |
|
21
|
|
|
if creds and creds.expired and creds.refresh_token: |
|
22
|
|
|
creds.refresh(Request()) |
|
23
|
|
|
else: |
|
24
|
|
|
flow = InstalledAppFlow.from_client_secrets_file( |
|
25
|
|
|
'pull_config/credentials/client_secret.json', SCOPES) |
|
26
|
|
|
creds = flow.run_local_server(port=0) |
|
27
|
|
|
# Save the credentials for the next run |
|
28
|
|
|
with open(token, 'w') as token: |
|
29
|
|
|
token.write(creds.to_json()) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def import_from_sheets(): |
|
33
|
|
|
token: str = "token.json" |
|
34
|
|
|
creds = None |
|
35
|
|
|
# The file token.json stores the user's access and refresh tokens, and is |
|
36
|
|
|
# created automatically when the authorization flow completes for the first time |
|
37
|
|
|
if os.path.exists(token): |
|
38
|
|
|
creds = Credentials.from_authorized_user_file(token, SCOPES) |
|
39
|
|
|
# If there are no (valid) credentials available, let the user log in |
|
40
|
|
|
if not creds or not creds.valid: |
|
41
|
|
|
handle_creds(creds, token) |
|
42
|
|
|
|
|
43
|
|
|
service = build('sheets', 'v4', credentials=creds) |
|
44
|
|
|
|
|
45
|
|
|
# Call the Sheets API |
|
46
|
|
|
sheet = service.spreadsheets() |
|
47
|
|
|
result_input = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID_input, range=SAMPLE_RANGE_NAME).execute() |
|
48
|
|
|
values_input = result_input.get('values', []) |
|
49
|
|
|
|
|
50
|
|
|
# if not values_input: |
|
51
|
|
|
# dt_string = MyBot.get_current_time() |
|
52
|
|
|
# print(f"({dt_string})\t[{get_config.__name__}]: No data found.") |
|
53
|
|
|
return values_input |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def create_trigger_list(triggers) -> list: |
|
57
|
|
|
triggers_list = [] |
|
58
|
|
|
for row in triggers.itertuples(index=False): |
|
59
|
|
|
help_ser = pd.Series(row) |
|
60
|
|
|
help_ser = help_ser[~help_ser.isna()] |
|
61
|
|
|
# Drop empty strings |
|
62
|
|
|
help_ser = pd.Series(filter(None, help_ser)) |
|
63
|
|
|
# Copy strings with spaces without keeping them |
|
64
|
|
|
for trigger in help_ser: |
|
65
|
|
|
trigger_nospace = trigger.replace(' ', '') |
|
66
|
|
|
help_ser = help_ser.append(pd.Series(trigger_nospace)) |
|
67
|
|
|
help_ser = help_ser.drop_duplicates() |
|
68
|
|
|
triggers_list.append(help_ser) |
|
69
|
|
|
return triggers_list |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def create_output(monsters_df: pd.DataFrame) -> dict: |
|
73
|
|
|
types = {'id': [4, 3, 2, 1, 0], 'label': ["Common", "Event2", "Event1", "Legendary", "Rare"]} |
|
74
|
|
|
types_df = pd.DataFrame(data=types) |
|
75
|
|
|
total_milestones = {"Sunday Spotter I": [100], "Sunday Spotter II": [200], "Sunday Spotter III": [300], |
|
76
|
|
|
"Rare Spotter I": [500], "Rare Spotter II": [750], "Rare Spotter III": [1000], |
|
77
|
|
|
"Pro Spotter I": [1500], "Pro Spotter II": [2000], "Pro Spotter III": [2500], |
|
78
|
|
|
"Legendary Spotter I": [3500], "Legendary Spotter II": [4500], "Legendary Spotter III": [5500], |
|
79
|
|
|
"Mythic Spotter I": [7500], "Mythic Spotter II": [9500], "Mythic Spotter III": [11500], |
|
80
|
|
|
"Pogmare Spotter": [15000]} |
|
81
|
|
|
total_milestones_df = pd.DataFrame(data=total_milestones) |
|
82
|
|
|
common_milestones = {"Common Spotter": [100], "Common Killer": [500], "Common Slayer": [1000], |
|
83
|
|
|
"Common Destroyer": [1500], "Common Annihilator": [2500]} |
|
84
|
|
|
common_milestones_df = pd.DataFrame(data=common_milestones) |
|
85
|
|
|
json_final = {'total_milestones': total_milestones_df, 'common_milestones': common_milestones_df, |
|
86
|
|
|
'types': types_df, 'commands': monsters_df} |
|
87
|
|
|
return { |
|
88
|
|
|
key: json_final[key].to_dict(orient='records') |
|
89
|
|
|
for key in json_final |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
def create_trigger_structure(triggers_list: list) -> pd.Series: |
|
94
|
|
|
triggers_def = [list(i) for i in triggers_list] |
|
95
|
|
|
return pd.Series(triggers_def) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def get_config() -> None: |
|
99
|
|
|
pd.set_option('mode.chained_assignment', None) |
|
100
|
|
|
values_input = import_from_sheets() |
|
101
|
|
|
df = pd.DataFrame(values_input[1:], columns=values_input[0]) |
|
102
|
|
|
|
|
103
|
|
|
monsters_df = df[["name", "type"]] |
|
104
|
|
|
monsters_df["type"] = pd.to_numeric(df["type"]) |
|
105
|
|
|
|
|
106
|
|
|
triggers = df.drop(['name', 'role', 'type', 'id'], axis=1) |
|
107
|
|
|
triggers = triggers.applymap(lambda s: s.lower() if type(s) == str else s) |
|
108
|
|
|
|
|
109
|
|
|
triggers_list = create_trigger_list(triggers) |
|
110
|
|
|
|
|
111
|
|
|
triggers_def = create_trigger_structure(triggers_list) |
|
112
|
|
|
monsters_df.insert(loc=0, column='triggers', value=triggers_def) |
|
113
|
|
|
|
|
114
|
|
|
data_dict = create_output(monsters_df) |
|
115
|
|
|
|
|
116
|
|
|
# write to disk |
|
117
|
|
|
with open('server_files/config.json', 'w', encoding='utf8') as f: |
|
118
|
|
|
json.dump(data_dict, f, indent=4, ensure_ascii=False, sort_keys=False, cls=NumpyEncoder) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
def main() -> None: |
|
122
|
|
|
get_config() |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
if __name__ == "__main__": |
|
126
|
|
|
main() |
|
127
|
|
|
|