Exemple #1
0
class MatchListTest(TestCase):
    def setUp(self):
        gameinfo = getInfoGetter(True)
        self.matchlist = MatchList(gameinfo, 'l am eternal', queue="SOLODUO")

    def tearDown(self):
        self.matchlist = None

    def testMatchListWinrateByTime(self):
        winrates = self.matchlist.winrateByTime()
        self.assertEqual(
            winrates[0].beginTime, 0,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[0].endTime, 6,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[1].beginTime, 6,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[1].endTime, 12,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[2].beginTime, 12,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[2].endTime, 18,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[3].beginTime, 18,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(
            winrates[3].endTime, 24,
            'incorrect range of times in the WinrateByTimeOfDays returned by MatchList.winrateByTime()'
        )
        self.assertEqual(winrates[0].played, 0)
        self.assertEqual(winrates[0].won, 0)
        self.assertEqual(winrates[1].played, 1)
        self.assertEqual(winrates[1].won, 1)
        self.assertEqual(winrates[2].played, 5)
        self.assertEqual(winrates[2].won, 1)
        self.assertEqual(winrates[3].played, 7)
        self.assertEqual(winrates[3].won, 3)

    def testMatchListWinrateByOtherSummoners(self):
        winrates = self.matchlist.winrateByOtherSummoners()
        eternalIn = False
        makotoIn = False
        montaroIn = False

        for wr in winrates:
            if wr.thatSummoner == 'l am eternal':
                eternalIn = True
                self.assertTrue(wr.onThisSummonersTeam,
                                'bad l am eternal winrate values')
                self.assertEqual(wr.thatSummoner, 'l am eternal',
                                 'bad l am eternal winrate values')
                self.assertEqual(wr.played, 13,
                                 'bad l am eternal winrate values')
                self.assertEqual(wr.won, 5, 'bad l am eternal winrate values')
            if wr.thatSummoner == 'lilmontaro':
                montaroIn = True
                self.assertTrue(wr.onThisSummonersTeam,
                                'bad lilmontaro winrate values')
                self.assertEqual(wr.thatSummoner, 'lilmontaro',
                                 'bad lilmontaro winrate values')
                self.assertEqual(wr.played, 5, 'bad lilmontaro winrate values')
                self.assertEqual(wr.won, 1, 'bad lilmontaro winrate values')
            if wr.thatSummoner == 'itou makoto':
                makotoIn = True
                self.assertFalse(wr.onThisSummonersTeam,
                                 'bad itou makoto winrate values')
                self.assertEqual(wr.thatSummoner, 'itou makoto',
                                 'bad itou makoto winrate values')
                self.assertEqual(wr.played, 2,
                                 'bad itou makoto winrate values')
                self.assertEqual(wr.won, 1, 'bad itou makoto winrate values')
        self.assertTrue(eternalIn, 'l am eternal not in winrates')
        self.assertTrue(makotoIn, 'itou makoto not in winrates')
        self.assertTrue(montaroIn, 'lilmontaro not in winrates')
        self.assertEqual(len(winrates), 3)

    #TODO: write this test. use python shell in root of project directory to generate these stats.
    '''
Exemple #2
0
def search(request, name):
    '''gameinfo = getInfoGetter(True)
    print('creating MatchList')
    ml = MatchList(gameinfo, 'l am eternal')
    print("Searching for " + name)
    id = gameinfo.getSummonerByName(name)['accountId']
    matchtimes = []
    allmatches = []
    matchdetails = []
    for i in range(0,HUNDREDS_OF_MATCHES):
        matches = gameinfo.getMatchlistBySummonerId(id, i*100, 50) 
            
        for match in matches['matches']:
            allmatches.append(match)
            matchdetails.append(gameinfo.getMatchById(match['gameId']))
    i = 0
    winrateByTime = [[0 for x in range(2)] for y in range(4)]
    for match in allmatches:
        if match['queue'] == 420:
            #NA: match['timestamp']/1000 IS PST OF BEGINNING OF MATCH
            #VERIFIED 
            utctime = datetime.fromtimestamp(match['timestamp']/1000)
            offset = timedelta(hours=0)
            time = utctime - offset
            matchtimes.append(str(time) + ' timestamp: ' +str(match['timestamp']) + '   '+ str(match['gameId']) + '    ' + str(matchdetails[i]['queueId']) + ' ' + str(win(name, matchdetails[i])))
            
            if win(name, matchdetails[i]):
                if time.hour >= 18:
                    winrateByTime[3][1]+=1
                    winrateByTime[3][0]+=1
                elif time.hour >= 12:
                    winrateByTime[2][1]+=1
                    winrateByTime[2][0]+=1
                elif time.hour >= 6:
                    winrateByTime[1][1]+=1
                    winrateByTime[1][0]+=1
                else: #0-6
                    winrateByTime[0][1]+=1
                    winrateByTime[0][0]+=1
            else:
                if time.hour >= 18:
                    winrateByTime[3][0]+=1
                elif time.hour >= 12:
                    winrateByTime[2][0]+=1
                elif time.hour >= 6:
                    winrateByTime[1][0]+=1
                else: #0-6
                    winrateByTime[0][0]+=1
        i+=1
        
            #print("not a ranked game.")
    onPlayersTeam = dict()
    vsPlayersTeam = dict()
    for match in matchdetails:
        if match['queueId'] == 420:
            thisSummonersTeam = []
            enemyTeam = []
            thisSummonerWon = win(name, match)
            thisSummonerId = 0
            
            thisSummonersTeamId = 0
            for player in match['participantIdentities']:
                if player['player']['summonerName'].lower() == name:
                    thisSummonerId = player['participantId']
                    break
            for participant in match['participants']:
                
                if participant['participantId'] == thisSummonerId:
                    thisSummonersTeamId = participant['teamId']
                    break
            
            participantIds = dict()
            for player in match['participantIdentities']:
                participantIds[player['participantId']] = player['player']['summonerName']
            
            for participant in match['participants']:
                if participant['teamId'] == thisSummonersTeamId:
                    thisSummonersTeam.append(participant['participantId'])
                else:
                    enemyTeam.append(participant['participantId'])
            if thisSummonerWon:
                for particId in thisSummonersTeam:
                    if participantIds[particId] in onPlayersTeam:
                        wg = onPlayersTeam[participantIds[particId]]
                        onPlayersTeam[participantIds[particId]] = (wg[0]+1, wg[1]+1)
                    else:
                        onPlayersTeam[participantIds[particId]] = (1,1)
                for particId in enemyTeam:
                    if participantIds[particId] in vsPlayersTeam:
                        wg = vsPlayersTeam[participantIds[particId]]
                        vsPlayersTeam[participantIds[particId]] = (wg[0], wg[1]+1)
                    else:
                        vsPlayersTeam[participantIds[particId]] = (0,1)
            else:
                for particId in thisSummonersTeam:
                    if participantIds[particId] in onPlayersTeam:
                        wg = onPlayersTeam[participantIds[particId]]
                        onPlayersTeam[participantIds[particId]] = (wg[0], wg[1]+1)
                    else:
                        onPlayersTeam[participantIds[particId]] = (0,1)
                for particId in enemyTeam:
                    if participantIds[particId] in vsPlayersTeam:
                        wg = vsPlayersTeam[participantIds[particId]]
                        vsPlayersTeam[participantIds[particId]] = (wg[0]+1, wg[1]+1)
                    else:
                        vsPlayersTeam[participantIds[particId]] = (1,1)
    onPlayersTeamList = []
    for item in onPlayersTeam.items():
        if item[1][1] > 1:
            onPlayersTeamList.append(item)
    onPlayersTeamList.sort(key=lambda x: x[1][1], reverse=True)

    vsPlayersTeamList = []
    for item in vsPlayersTeam.items():
        if item[1][1] > 1:
            vsPlayersTeamList.append(item)
    vsPlayersTeamList.sort(key=lambda x: x[1][1], reverse=True)
    
    print(onPlayersTeamList)
    print(vsPlayersTeamList)
    
    winratesByTime = []
    if winrateByTime[3][0] != 0:
        winratesByTime.append('18:00-24:00: ' + str(winrateByTime[3][1]/winrateByTime[3][0]*100) + '% of ' + str(winrateByTime[3][0]) + ' games.')
    else:
        winratesByTime.append('18:00-24:00: no games')
    if winrateByTime[2][0] != 0:
        winratesByTime.append('12:00-18:00: ' + str(winrateByTime[2][1]/winrateByTime[2][0]*100) + '% of ' + str(winrateByTime[2][0]) + ' games.')
    else:
        winratesByTime.append('12:00-18:00: no games')
    if winrateByTime[1][0] != 0:
        winratesByTime.append('6:00-12:00: ' + str(winrateByTime[1][1]/winrateByTime[1][0]*100) + '% of ' + str(winrateByTime[1][0]) + ' games.')
    else:
        winratesByTime.append('6:00-12:00: no games')
    if winrateByTime[0][0] != 0:
        winratesByTime.append('0:00-6:00: ' + str(winrateByTime[0][1]/winrateByTime[0][0]*100) + '% of ' + str(winrateByTime[0][0]) + ' games.')
    else: 
        winratesByTime.append('0:00-6:00: no games')
    
    
    print(winratesByTime)
    return render(request, 'search/search.html', {
        'name'           : name             ,  
        'matchtimes'     : matchtimes       , 
        'winratesByTime' : winratesByTime   ,
        'onPlayersTeam'  : onPlayersTeamList,
        'vsPlayersTeam'  : vsPlayersTeamList,
    })
    '''
    maxMatches = int(request.session['numMatches'])
    champions = request.session['champions']
    role = request.session['role']
    queue = request.session['queue']
    if champions == "":
        champions = None
    if role == "":
        role = None
    if queue == "":
        queue = None
    try:
        matchList = MatchList(getInfoGetter(), name, maxMatches, None, queue,
                              champions, role)
    except RuntimeError as e:
        return HttpResponse("Error: " + str(e))
    #remember that you wrote the text 'in PST only' into search.html
    if matchList.size() == 0:
        return HttpResponse(
            'no matches found with those parameters. Try a different search or include more matches.'
        )

    return render(
        request,
        'search/search.html',
        {
            'name':
            name,
            'overview':
            str(matchList),
            'numMatches':
            maxMatches,
            'matchIds':
            matchList.matchIdList(),
            'summonerId':
            matchList.id,

            #By time
            'wrByTime':
            matchList.winrateByTime(),

            #OtherSummoners:
            'otherSummonersWr':
            matchList.winrateByOtherSummoners(),

            #Dragons:
            'pctElementals':
            matchList.pctAllElemental(),
            'pctElders':
            matchList.pctAllElders(),
            'pctOfEachType':
            matchList.pctDragonsKilledByType(),
            'pctOfTotalByType':
            matchList.pctEachDragonType(),
            'pctEleByOrder':
            matchList.pctElementalKilledByOrder(),
            'pctElderByOrder':
            matchList.pctElderKilledByOrder(),

            #Dragon time info:
            'firstDragTime':
            matchList.firstElementalDragonTime(),
            'enemyContDragInfo':
            matchList.timePercentEnemyContestedElementalDragons(),
            'enemyB2BDragInfo':
            matchList.timePercentEnemyBackToBackElementalDragons(),
            'contDragInfo':
            matchList.timePercentContestedElementalDragons(),
            'b2BDragInfo':
            matchList.timePercentBackToBackElementalDragons(),

            #Barons:
            'avgBarons':
            matchList.avgBarons(),

            #Towers:
            'winTowersKilled':
            matchList.towersKilledInWins(),
            'lossTowersKilled':
            matchList.towersKilledInLosses(),
            'winTowersLost':
            matchList.towersLostInWins(),
            'lossTowersLost':
            matchList.towersLostInLosses(),

            #kills:
            'championKills':
            matchList.compileKillLists()
        })