-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
265 lines (216 loc) · 8.01 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import logging
import os
import uvicorn
from fastapi import BackgroundTasks, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response
from constants import REGIONS
from database_pg import Database, ProcessPerformance
from extract_pg import main as extract_main
from riot_api import RiotAPI
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
app = FastAPI(
title="Esports Playmaker",
description="A FastAPI application for the Esports Playmaker plugin.",
version="0.1.0",
servers=[
{"url": "http://0.0.0.0:8000/"},
{"url": "https://lacralabs.replit.app"},
],
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
db = Database(os.getenv("DATABASE_URL")).get_connection()
# API endpoints
@app.on_event("startup")
async def startup_event():
global riot_api
riot_api = RiotAPI(db)
@app.get("/summoner/{summoner_name}")
async def get_summoner_info(summoner_name: str, region: str = "na1"):
"""
Retrieves summoner information based on the summoner name and region.
Args:
summoner_name (str): The summoner name.
region (str, optional): The region of the summoner. Defaults to "na1".
Returns:
dict: The summoner information.
"""
if region not in REGIONS:
return {"error": "Invalid region"}
try:
summoner_info = riot_api.summoner_info(summoner_name, region)
return summoner_info
except Exception as e:
return {"error": f"Error retrieving summoner information: {e}"}
@app.get("/summoner/leagues/{summoner_id}")
async def get_summoner_leagues(summoner_id: str, region: str = "na1"):
"""
Retrieves a list of league information for the given summoner ID.
Args:
summoner_id (str): The summoner ID.
region (str, optional): The region of the summoner. Defaults to "na1".
Returns:
dict: A dictionary containing the league information for the given summoner ID.
"""
try:
riot_api = RiotAPI(db)
summoner_leagues = riot_api.summoner_leagues(summoner_id, region)
return summoner_leagues
except Exception as e:
return {"error": f"Error retrieving summoner leagues: {e}"}
@app.get("/champion_mastery/{puuid}")
async def get_champion_mastery(puuid: str, region: str = "na1"):
"""
Retrieves the champion mastery for a given player.
Args:
puuid (str): The unique identifier for the player.
region (str, optional): The region where the player is located. Defaults to "na1".
Returns:
dict: A dictionary containing the champion mastery information, or an error message if an exception occurs.
"""
try:
riot_api = RiotAPI(db)
champion_mastery = riot_api.champion_mastery(puuid, region)
return champion_mastery
except Exception as e:
return {"error": f"Error retrieving champion mastery: {e}"}
@app.get("/summoner/champion_mastery/scores/{puuid}")
async def get_champion_mastery_total_score(puuid: str, region: str = "na1"):
"""
Retrieves the total champion mastery score for a given player.
Args:
puuid (str): The player's unique identifier.
region (str, optional): The region where the player is located. Defaults to "na1".
Returns:
dict: A dictionary containing the total champion mastery score, or an error message if an exception occurs.
"""
try:
riot_api = RiotAPI(db)
champion_mastery_score = riot_api.champion_mastery_total_score(puuid, region)
return champion_mastery_score
except Exception as e:
return {"error": f"Error retrieving total champion mastery score: {e}"}
@app.get("/summoner/match_list/{puuid}")
async def get_match_list(
puuid: str,
num_matches: int = 10,
queue_type: str = "ranked",
region: str = "americas",
):
"""
Retrieves a list of match IDs based on the player UUID, number of matches, queue type, and region.
"""
logging.info(
f"Fetching match list for PUUID: {puuid}, Num Matches: {num_matches}, Queue Type: {queue_type}, Region: {region}"
)
try:
riot_api = RiotAPI(db)
match_list = riot_api.match_ids(puuid, num_matches, queue_type, region)
if not match_list:
logging.warning("Received empty match list.")
return match_list
except Exception as e:
logging.error(f"Error retrieving match list: {e}")
return {"error": f"Error retrieving match list: {e}"}
@app.get("/match/detail/{match_id}")
async def get_match_detail(match_id: str, db):
"""
Retrieves the match detail for a given match ID.
"""
try:
match_detail = (
db.cursor()
.execute("SELECT * FROM match_detail WHERE match_id = ?", (match_id,))
.fetchone()
)
if match_detail is not None:
return match_detail
else:
return {"error": "Match not found"}
except Exception as e:
return {"error": f"Error retrieving match detail: {e}"}
@app.get("/match/timeline/{match_id}")
async def get_match_timeline(match_id: str, db):
"""
Retrieves the match detail for a given match ID.
"""
try:
match_detail = (
db.cursor()
.execute("SELECT * FROM match_detail WHERE match_id = ?", (match_id,))
.fetchone()
)
if match_detail is not None:
return match_detail
else:
return {"error": "Match not found"}
except Exception as e:
return {"error": f"Error retrieving match detail: {e}"}
@app.post("/performance")
async def calculate_performance(match_id: str):
"""
Calculates player performance based on match details.
Args:
match_id (str): The match ID.
db: The database connection.
Returns:
dict: The calculated performance data.
"""
try:
match_detail = db.cursor().execute(
"SELECT * FROM match_detail WHERE match_id = ?", (match_id,)
)
if match_detail:
performance_calculator = ProcessPerformance(db)
performance_data = performance_calculator.process_player_performance(
match_detail[0], db
)
return performance_data
else:
return {"error": "Match not found"}
except Exception as e:
return {"error": f"Error calculating performance: {e}"}
@app.post("/data_mining/{mode}")
async def data_mining(mode: str, background_tasks: BackgroundTasks):
valid_modes = [
"player_list",
"match_list",
"match_download_standard",
"match_download_detail",
]
if mode not in valid_modes:
raise HTTPException(status_code=400, detail="Invalid mode specified")
# Use BackgroundTasks to run the data mining process without blocking the API response
background_tasks.add_task(extract_main, mode)
return {"message": "Data mining process initiated", "mode": mode}
@app.get("/")
async def home():
return {"message": "Welcome to the Esports Playmaker"}
@app.get("/public/logo.png")
async def plugin_logo():
return FileResponse("logo.png", media_type="image/png")
@app.get("/public/favicon.ico")
async def plugin_favicon():
return FileResponse("favicon.ico", media_type="image/x-icon")
@app.get("/.well-known/ai-plugin.json")
async def plugin_manifest():
with open("ai-plugin.json", "r") as f:
json_content = f.read()
return Response(content=json_content, media_type="application/json")
@app.get("/openapi.yaml")
async def openapi_spec(request: Request):
host = request.client.host if request.client else "localhost"
with open("openapi.yaml", "r") as f:
yaml_content = f.read()
yaml_content = yaml_content.replace("PLUGIN_HOSTNAME", f"https://{host}")
return Response(content=yaml_content, media_type="application/yaml")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)