-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_call.py
102 lines (88 loc) · 3.21 KB
/
server_call.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
#Code for filling Server with needed information from Riot API
#Created by Ashley Dodson
#March 2017, for CSCI 344 API Project
#Uses Pymongo and Cassiopeia to quickly access resources
import os
import time
from cassiopeia import riotapi
from cassiopeia.type.core.common import LoadPolicy, StatSummaryType
from pymongo import MongoClient
import pymongo
def request_data(summoner_request):
#Set Riot API information
riotapi.set_region("NA")
riotapi.print_calls(False)
riotapi.set_api_key(os.environ["RIOT_DEV"])
riotapi.set_load_policy(LoadPolicy.lazy)
#Establish database connection
client = MongoClient("mongodb://ruler:Katarina7!@ds135700.mlab.com:35700/summonertest")
db = client.summonertest
#Declare variables
kills = 0
deaths = 0
assists = 0
kda = 0
avg_time = 0
avg_cs = 0
avg_game_cs = 0
avg_time = 0
cs = 0
try :
name_request = summoner_request #CHANGE TO DESIRED LOOKUP NAME
except:
return None
summoner = riotapi.get_summoner_by_name(name_request)
match_list = summoner.match_list()
#Number of matches to pull from Riot Servers
num_matches = 5 #CHANGE THIS TO NUMBER INTERESTED IN
#Basic stats collection
for i, match_reference in enumerate(match_list[0:num_matches]):
match = match_reference.match()
for participant in match.participants:
if participant.summoner_id == summoner.id:
cs += participant.stats.cs
kills += participant.stats.kills
deaths += participant.stats.deaths
assists += participant.stats.assists
avg_time += match.duration.total_seconds()
if(avg_time > 0):
avg_cs += (float)(participant.stats.cs / (match.duration.total_seconds() / 60))
#Matchwide averages
if(deaths >0):
kda = (float)(kills + assists) / deaths
else:
kda = kills + assists
if(num_matches > 0):
avg_time = (float)((avg_time / 60) / num_matches)
avg_game_cs = (float)(avg_cs / num_matches)
if(avg_time > 0):
avg_cs = (float)(cs / num_matches)
#TODO Create scores for various items
#Verify user not already on server, delete if pre-existing
if(db.summoners.count() > 0):
if(db.summoners.find({"summoner": summoner.name})):
clear = db.summoners.delete_many({"summoner": summoner.name})
#Push to server
pushVal = db.summoners.insert_one(
{
"summoner": summoner.name,
"level": summoner.level,
"matches_polled": num_matches,
"kda": round(kda, 3),
"kills": kills,
"deaths": deaths,
"assists": assists,
"average_cs_per_min": round(avg_game_cs, 1),
"average_cs": (int)(avg_cs),
"average_time": round(avg_time, 2),
"date_created": time.asctime(time.localtime(time.time()))
}
)
#TODO Adjust so that data sometimes updates instead of always deleting
#Report data on server to console
"""display = db.summoners.find().sort([
("summoner", pymongo.ASCENDING)
])
for summoners in display:
print(summoners)"""
request_data("Rhazik")