-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclash.py
More file actions
686 lines (571 loc) · 24.1 KB
/
clash.py
File metadata and controls
686 lines (571 loc) · 24.1 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
from flask import Flask, request, jsonify
from pymongo import MongoClient
import requests
from datetime import datetime
import re
from collections import defaultdict
from datetime import datetime, timedelta
from pymongo import MongoClient
import json
from flask_cors import CORS
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import random
from itertools import combinations
# Configuração do MongoDB
# client = MongoClient("mongodb://localhost:27017/")
uri = "mongodb+srv://brendofcg:qwer1234Bb@agrupamentobanco.zb2av.mongodb.net/?appName=AgrupamentoBanco"
client = MongoClient(uri, server_api=ServerApi('1'))
db = client["clash_royale"]
players_collection = db["players"]
battles_collection = db["battles"]
# Configuração da API Flask
app = Flask(__name__)
cors = CORS(app)
API_KEY = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjVhMWI0NjgwLTZjMmQtNDg1ZC1hZWNkLTM2ZDU2MTU4MmM1NSIsImlhdCI6MTc0NDgwNjczNSwic3ViIjoiZGV2ZWxvcGVyLzhmNzA4NDY0LWIxMmYtMDdiMy0zN2FlLTU0NWY4MTM2YmEyMSIsInNjb3BlcyI6WyJyb3lhbGUiXSwibGltaXRzIjpbeyJ0aWVyIjoiZGV2ZWxvcGVyL3NpbHZlciIsInR5cGUiOiJ0aHJvdHRsaW5nIn0seyJjaWRycyI6WyIxNzkuMjQwLjI2LjIxMiJdLCJ0eXBlIjoiY2xpZW50In1dfQ.MfU9NuHVs-kHMbtmUTz6g2qB30o6eMildy4J58tKeU0xSF0AWbb2hE7rsXM2xUXkVvo562gyFvgqQmsthHQ2ww" # Substitua pelo seu token real
HEADERS = {"Authorization": f"Bearer {API_KEY}"}
# Função para buscar e armazenar dados de um jogador
def fetch_player_data(tag):
url = f"https://api.clashroyale.com/v1/players/%23{tag}"
response = requests.get(url, headers=HEADERS)
if response.status_code == 200:
player_data = response.json()
# Salva ou atualiza os dados do jogador
players_collection.update_one({"tag": player_data["tag"]}, {"$set": player_data}, upsert=True)
return player_data
return None
# Função para buscar e armazenar batalhas de um jogador
# Função para buscar e armazenar batalhas de um jogador
def fetch_battle_log(tag):
url = f"https://api.clashroyale.com/v1/players/%23{tag}/battlelog"
response = requests.get(url, headers=HEADERS)
if response.status_code == 200:
battles = response.json()
for battle in battles:
# Converter battleTime para datetime antes de salvar
try:
battle_time = datetime.strptime(battle["battleTime"], "%Y%m%dT%H%M%S.000Z")
battle["battleTime"] = battle_time
except Exception as e:
print(f"Erro ao converter battleTime para datetime: {e}")
continue
# Gerar uma duração entre 2 e 10 minutos (em segundos)
duration_minutes = random.randint(2, 10)
duration_seconds = random.randint(0, 59)
total_duration = timedelta(minutes=duration_minutes, seconds=duration_seconds).total_seconds()
battle["duration"] = total_duration
# Salvar no banco
battles_collection.update_one(
{"battleTime": battle["battleTime"]},
{"$set": battle},
upsert=True
)
return battles
return None
# Função para validar se uma data está no formato correto
def validate_date(date_str):
"""Valida e converte uma string de data no formato ISO para um objeto datetime."""
try:
if isinstance(date_str, datetime): # Evita conversões duplicadas
return date_str
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S")
except ValueError:
return None
# Consulta 1: Porcentagem de vitórias com uma carta específica
def calculate_win_rate(card_name, start_time, end_time):
start_time = validate_date(start_time)
end_time = validate_date(end_time)
if not start_time or not end_time:
return {"error": "Formato de data inválido. Use 'YYYY-MM-DDTHH:MM:SS'"}
# Total de batalhas em que a carta participou
total_battles = battles_collection.count_documents({
"battleTime": {"$gte": start_time, "$lte": end_time},
"$or": [
{"team.cards.name": card_name},
{"opponent.cards.name": card_name}
]
})
# Vitórias com a carta no time vencedor
wins_as_team = battles_collection.count_documents({
"battleTime": {"$gte": start_time, "$lte": end_time},
"team.cards.name": card_name,
"$expr": {"$gt": ["$team.crowns", "$opponent.crowns"]}
})
# Vitórias com a carta no oponente vencedor
wins_as_opponent = battles_collection.count_documents({
"battleTime": {"$gte": start_time, "$lte": end_time},
"opponent.cards.name": card_name,
"$expr": {"$gt": ["$opponent.crowns", "$team.crowns"]}
})
total_wins = wins_as_team + wins_as_opponent
total_losses = total_battles - total_wins
win_rate = round((total_wins / total_battles * 100), 2) if total_battles > 0 else 0
loss_rate = round((total_losses / total_battles * 100), 2) if total_battles > 0 else 0
return {
"card_name": card_name,
"start_time": start_time.strftime("%Y-%m-%dT%H:%M:%S"),
"end_time": end_time.strftime("%Y-%m-%dT%H:%M:%S"),
"total_battles": total_battles,
"total_wins": total_wins,
"total_losses": total_losses,
"win_rate": win_rate,
"loss_rate": loss_rate
}
# Consulta 2: Decks completos com taxa de vitória superior a X%
def get_successful_decks(win_threshold, start_time, end_time):
start_time = validate_date(start_time)
end_time = validate_date(end_time)
if not start_time or not end_time:
return {"error": "Invalid date format. Use 'YYYY-MM-DDTHH:MM:SS'"}
pipeline = [
# Filtro para o intervalo de tempo
{"$match": {"battleTime": {"$gte": start_time, "$lte": end_time}}},
# Desenrolar o deck da equipe para contar vitórias por cada deck
{"$unwind": "$team.deck"},
# Determinar se o time venceu com base na quantidade de crowns
{"$group": {
"_id": "$team.deck",
"total_battles": {"$sum": 1},
"wins": {
"$sum": {
"$cond": [{"$gt": ["$team.crowns", "$opponent.crowns"]}, 1, 0]
}
}
}},
# Calcular a taxa de vitória
{"$addFields": {
"win_rate": {"$multiply": [{"$divide": ["$wins", "$total_battles"]}, 100]}
}},
# Filtrar decks com taxa de vitória acima do limite definido
{"$match": {
"win_rate": {"$gte": win_threshold}
}}
]
resultado = list(battles_collection.aggregate(pipeline))
print("Resultado da Query:", resultado) # Debug
return resultado
# Consulta 3: Derrotas utilizando um combo de cartas
def calculate_losses_with_combo(cards, start_time, end_time):
# Validando datas
start_time = validate_date(start_time)
end_time = validate_date(end_time)
if not start_time or not end_time:
return {"error": "Invalid date format. Use 'YYYY-MM-DDTHH:MM:SS'"}
return battles_collection.count_documents({
"battleTime": {"$gte": start_time, "$lte": end_time},
"team.cards.name": {"$all": cards},
"winner": "opponent"
})
def calculate_disadvantage_wins(card_name, trophy_percent):
trophy_factor = 1 - (trophy_percent / 100)
query = {
"$expr": {
"$and": [
# Partida durou menos de 120 segundos
{"$lt": ["$duration", 120]},
# Vencedor teve menos troféus que o perdedor, com Z% de diferença
{
"$cond": [
{"$gt": [{"$arrayElemAt": ["$team.crowns", 0]}, {"$arrayElemAt": ["$opponent.crowns", 0]}]},
{
"$lt": [
{"$arrayElemAt": ["$team.startTrophies", 0]},
{
"$multiply": [
{"$arrayElemAt": ["$opponent.startTrophies", 0]},
trophy_factor
]
}
]
},
{
"$lt": [
{"$arrayElemAt": ["$opponent.startTrophies", 0]},
{
"$multiply": [
{"$arrayElemAt": ["$team.startTrophies", 0]},
trophy_factor
]
}
]
}
]
},
# Perdedor destruiu 2 ou mais torres
{
"$cond": [
{"$gt": [{"$arrayElemAt": ["$team.crowns", 0]}, {"$arrayElemAt": ["$opponent.crowns", 0]}]},
{"$gte": [{"$arrayElemAt": ["$opponent.crowns", 0]}, 2]},
{"$gte": [{"$arrayElemAt": ["$team.crowns", 0]}, 2]}
]
}
]
},
"$or": [
{"team.cards.name": card_name},
{"opponent.cards.name": card_name}
]
}
result = battles_collection.count_documents(query)
return {"card": card_name, "disadvantage_victories": result}
def top_combos(n, min_win_rate, start_time, end_time):
pipeline = [
{
"$match": {
"battleTime": {"$gte": start_time, "$lte": end_time},
"team.cards": {"$exists": True},
"opponent.cards": {"$exists": True}
}
},
{
"$project": {
"team_cards": "$team.cards.name",
"team_crowns": {"$arrayElemAt": ["$team.crowns", 0]},
"opponent_crowns": {"$arrayElemAt": ["$opponent.crowns", 0]}
}
},
{
"$project": {
"combo": {
"$slice": [
{"$setUnion": ["$team_cards"]}, n
]
},
"is_win": {"$gt": ["$team_crowns", "$opponent_crowns"]}
}
},
{
"$group": {
"_id": "$combo",
"total": {"$sum": 1},
"wins": {"$sum": {"$cond": ["$is_win", 1, 0]}}
}
},
{
"$project": {
"total": 1,
"wins": 1,
"win_rate": {
"$cond": [
{"$eq": ["$total", 0]},
0,
{"$multiply": [{"$divide": ["$wins", "$total"]}, 100]}
]
}
}
},
{
"$match": {
"win_rate": {"$gte": min_win_rate}
}
},
{
"$sort": {"win_rate": -1}
},
{
"$limit": 10
}
]
result = battles_collection.aggregate(pipeline)
return [
{
"combo": r["_id"],
"wins": r["wins"],
"total_battles": r["total"],
"win_rate": round(r["win_rate"], 2)
}
for r in result
]
# Rota para calcular taxa de vitória
@app.route("/winrate", methods=["GET"])
def win_rate():
card_name = request.args.get('card_name')
start_time = request.args.get('start_time')
end_time = request.args.get('end_time')
if not card_name or not start_time or not end_time:
return jsonify({"error": "Missing required parameters"}), 400
# Validar datas
start_time = validate_date(start_time)
end_time = validate_date(end_time)
if not start_time or not end_time:
return jsonify({"error": "Invalid date format. Use 'YYYY-MM-DDTHH:MM:SS'"}), 400
# Calcular win rate
result = calculate_win_rate(card_name, start_time, end_time)
return jsonify(result)
# Rota para obter decks vitoriosos
@app.route('/winning-decks', methods=['GET'])
def get_winning_decks():
try:
win_threshold = float(request.args.get('win_threshold', 50))
start_time = request.args.get('start_time', '2000-01-01T00:00:00')
end_time = request.args.get('end_time', '2100-01-01T00:00:00')
start_time = datetime.strptime(start_time, "%Y-%m-%dT%H:%M:%S")
end_time = datetime.strptime(end_time, "%Y-%m-%dT%H:%M:%S")
print(f"Iniciando consulta de decks vencedores...")
print(f"Parâmetros recebidos - win_threshold: {win_threshold}, start_time: {start_time}, end_time: {end_time}")
battles = list(battles_collection.find({"battleTime": {"$gte": start_time, "$lte": end_time}}))
print(f"Total de batalhas encontradas: {len(battles)}")
deck_stats = defaultdict(lambda: {'wins': 0, 'total': 0, 'players': set()})
for battle in battles:
team = battle.get('team', [{}])[0]
opponent = battle.get('opponent', [{}])[0]
if 'crowns' not in team or 'crowns' not in opponent:
continue
if team['crowns'] > opponent['crowns']:
winner = team
loser = opponent
elif opponent['crowns'] > team['crowns']:
winner = opponent
loser = team
else:
continue # Empate
# Decks como tupla de strings
winner_deck = tuple(sorted([str(card) for card in winner.get('cards', [])]))
loser_deck = tuple(sorted([str(card) for card in loser.get('cards', [])]))
# Captura nome do jogador vencedor
winner_name = winner.get('name', 'Desconhecido')
deck_stats[winner_deck]['wins'] += 1
deck_stats[winner_deck]['total'] += 1
deck_stats[winner_deck]['players'].add(winner_name)
deck_stats[loser_deck]['total'] += 1
# Montagem do resultado final
resultados = []
for deck, stats in deck_stats.items():
winrate = (stats['wins'] / stats['total']) * 100 if stats['total'] > 0 else 0
if abs(winrate - win_threshold) <= 5: # ±5% margem
resultados.append({
'deck_cartas': list(deck),
'winrate_percentual': round(winrate, 2),
'vitorias': stats['wins'],
'jogadores_vencedores': list(stats['players']),
'batalhas_total': stats['total']
})
# Organiza por maior taxa de vitória
resultados = sorted(resultados, key=lambda d: d['winrate_percentual'], reverse=True)
return jsonify(resultados)
except Exception as e:
print(f"Erro ao buscar os decks vencedores: {e}")
return jsonify({'erro': 'Erro ao buscar os decks vencedores'}), 500
# Rota para calcular derrotas com combo de cartas
@app.route("/losses_with_combo", methods=["GET"])
def losses_with_combo():
try:
cards = request.args.getlist("cards")
start_time = request.args.get("start_time", "2000-01-01T00:00:00")
end_time = request.args.get("end_time", "2100-01-01T00:00:00")
start_time = datetime.strptime(start_time, "%Y-%m-%dT%H:%M:%S")
end_time = datetime.strptime(end_time, "%Y-%m-%dT%H:%M:%S")
losses = 0
battles = battles_collection.find({"battleTime": {"$gte": start_time, "$lte": end_time}})
for battle in battles:
team = battle.get("team", [{}])[0]
opponent = battle.get("opponent", [{}])[0]
if 'crowns' not in team or 'crowns' not in opponent:
continue
if team['crowns'] < opponent['crowns']:
loser = team
elif opponent['crowns'] < team['crowns']:
loser = opponent
else:
continue # empate
loser_cards = [card.get("name") if isinstance(card, dict) else card for card in loser.get("cards", [])]
if all(card in loser_cards for card in cards):
losses += 1
return jsonify({"losses": losses})
except Exception as e:
print(f"Erro ao calcular derrotas com combo: {e}")
return jsonify({"erro": "Erro ao calcular derrotas com combo"}), 500
# Rota para obter dados de um jogador
@app.route("/player/<tag>", methods=["GET"])
def get_player(tag):
data = fetch_player_data(tag)
if data:
return jsonify(data)
return jsonify({"error": "Player not found"}), 404
# Rota para obter o histórico de batalhas de um jogador
@app.route("/battlelog/<tag>", methods=["GET"])
def get_battle_log(tag):
data = fetch_battle_log(tag)
if data:
return jsonify(data)
return jsonify({"error": "No battle logs found"}), 404
# Rota Para Obter as Vitoais Estranhas;
@app.route('/vitorias-desvantagem', methods=['GET'])
def vitorias_com_desvantagem():
# Recuperar os parâmetros de entrada
card = request.args.get('card', '')
percent = request.args.get('percent', '')
try:
trophy_percent = float(percent) # Percentual de desvantagem
except ValueError:
trophy_percent = 0 # Caso o valor seja inválido, usamos 0 como padrão
# Simular a obtenção dos dados das partidas (substitua pelo seu código de busca)
partidas = buscar_partidas_com_card(card) # Função fictícia para buscar as partidas
# Filtrar partidas que atendem aos critérios (com base no percentual de desvantagem)
partidas_validas = []
for partida in partidas:
# Checar as condições: vencedor tem menos troféus com base no percentual de desvantagem
if partida['winner']['trophies'] < partida['loser']['trophies'] * (1 - trophy_percent / 100):
partidas_validas.append(partida)
# Cálculo da média de duração e a mais próxima de 120 segundos
total_duracao = 0
closest_to_120s = None
for partida in partidas_validas:
total_duracao += partida['duration']
if closest_to_120s is None or abs(partida['duration'] - 120) < abs(closest_to_120s - 120):
closest_to_120s = partida['duration']
# Calcular a quantidade de vitórias com desvantagem
disadvantage_victories = len(partidas_validas)
# Calcular a média de duração
if len(partidas_validas) > 0:
average_duration = total_duracao / len(partidas_validas)
else:
average_duration = 0
# Criar a resposta
resultado = {
"average_duration": average_duration,
"card": card,
"closest_to_120s": closest_to_120s,
"disadvantage_victories": disadvantage_victories
}
# Se não houver partidas válidas, adiciona uma mensagem explicativa
if disadvantage_victories == 0:
resultado["message"] = "Nenhuma partida atende aos critérios especificados."
return jsonify(resultado)
def buscar_partidas_com_card(card):
# Função fictícia para simular a busca de partidas
# Você deve substituir isso com a lógica real para recuperar as partidas de sua base de dados
return [
# Exemplo de partida
{
'winner': {'trophies': 3000},
'loser': {'trophies': 3500},
'duration': 115
},
# Outra partida
{
'winner': {'trophies': 2800},
'loser': {'trophies': 3000},
'duration': 125
},
# Mais partidas podem ser adicionadas aqui
]
# Rota Para obter os cambos vencedores
@app.route('/combos-vencedores', methods=['GET'])
def combos_vencedores():
try:
n = int(request.args.get('n'))
min_win_rate = float(request.args.get('percent'))
start = datetime.fromisoformat(request.args.get('start'))
end = datetime.fromisoformat(request.args.get('end'))
except:
return jsonify({"error": "Parâmetros inválidos"}), 400
partidas = list(battles_collection.find({
"battleTime": {"$gte": start, "$lte": end},
"team.cards": {"$exists": True},
"opponent.cards": {"$exists": True}
}))
stats = {}
for partida in partidas:
try:
team = partida["team"][0]
opponent = partida["opponent"][0]
team_cards = team["cards"]
cartas = sorted(set([c["name"] for c in team_cards]))
if len(cartas) < n:
continue # ignora combos menores que o esperado
is_win = team["crowns"] > opponent["crowns"]
for combo in combinations(cartas, n):
combo = tuple(sorted(combo))
if combo not in stats:
stats[combo] = {"wins": 0, "total": 0}
stats[combo]["total"] += 1
if is_win:
stats[combo]["wins"] += 1
except Exception as e:
print(f"Erro ao processar partida: {e}")
continue
resultados = []
for combo, dados in stats.items():
total = dados["total"]
wins = dados["wins"]
if total == 0:
continue
win_rate = (wins / total) * 100
if win_rate >= min_win_rate:
resultados.append({
"combo": list(combo),
"wins": wins,
"total_battles": total,
"win_rate": round(win_rate, 2)
})
resultados.sort(key=lambda x: x["win_rate"], reverse=True)
return jsonify(resultados[:1000])
# rota para elixit
@app.route('/cartas-menor-elixir', methods=['GET'])
def cartas_menor_elixir():
pipeline = [
{"$unwind": "$team"},
{"$unwind": "$team.cards"},
{"$group": {
"_id": "$team.cards.name",
"elixir": {"$avg": "$team.cards.elixirCost"} # <--- Corrigido aqui
}},
{"$sort": {"elixir": 1}}
]
resultado = list(battles_collection.aggregate(pipeline))
return jsonify(resultado)
# rotas cartas comuns
@app.route('/cartas-mais-comuns', methods=['GET'])
def cartas_mais_comuns():
pipeline = [
{"$unwind": "$team"},
{"$unwind": "$team.cards"},
{"$group": {
"_id": "$team.cards.name",
"decks": {"$sum": 1}
}},
{"$sort": {"decks": -1}},
{"$limit": 10}
]
resultado = list(battles_collection.aggregate(pipeline))
return jsonify(resultado)
# Jogadores com mais vitorias e seus decks
@app.route('/top-jogadores', methods=['GET'])
def top_jogadores():
pipeline = [
{
"$project": {
"player": "$team.name", # Corrigido aqui
"cartas": "$team.cards.name",
"vitoria": {
"$gt": [
{"$arrayElemAt": ["$team.crowns", 0]},
{"$arrayElemAt": ["$opponent.crowns", 0]}
]
}
}
},
{"$match": {"vitoria": True}},
{
"$group": {
"_id": {"jogador": "$player", "deck": "$cartas"},
"vitorias_com_deck": {"$sum": 1}
}
},
{"$sort": {"vitorias_com_deck": -1}},
{
"$group": {
"_id": "$_id.jogador",
"vitorias_totais": {"$sum": "$vitorias_com_deck"},
"deck_mais_usado": {"$first": "$_id.deck"},
"vitorias_com_deck": {"$first": "$vitorias_com_deck"}
}
},
{"$sort": {"vitorias_totais": -1}},
{"$limit": 3}
]
resultado = list(battles_collection.aggregate(pipeline))
return jsonify(resultado)
if __name__ == "__main__":
app.run(debug=True)