forked from D-E-F-E-A-T-2/Modified-elo-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModified_elo_algorithm_implementaion_in_py.py
More file actions
127 lines (105 loc) · 3.48 KB
/
Copy pathModified_elo_algorithm_implementaion_in_py.py
File metadata and controls
127 lines (105 loc) · 3.48 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import math
import random
# Function to calculate the Probability
def Probability(rating1, rating2):
return 1.0 * 1.0 / (1 + 1.0 * math.pow(10, 1.0 * (rating1 - rating2) / 400))
#Function to claculate the new player's rating
def EloRating(Ra, Rb, K, Sa, Sb, name1, name2):
# To calculate the Winning Probability of Player B
Pb = Probability(Ra, Rb)
# To calculate the Winning Probability of Player A
Pa = Probability(Rb, Ra)
# To calculate the Performance Multiplier
P1 = Sa / ( Sa + Sb )
P2 = Sb / ( Sa + Sb )
oRa = Ra
oRb = Rb
# Case -1 When Player A wins
# Updating the Player's Ratings
if (Sa>Sb) :
Ra = Ra + K * P1*(1 - Pa)
Rb = Rb + K * P2*(0 - Pb)
# Case -2 When Player B wins
# Updating the Elo Ratings
else :
Ra = Ra + K * P1*(0 - Pa)
Rb = Rb + K * P2*(1 - Pb)
# Assigning Leagues to the players
# To player A
if Ra<1200:
league_a = 'Unranked'
elif 1200<Ra<2400:
league_a = 'Gold'
elif 2400<Ra<3500:
league_a = 'Diamond'
elif Ra>3500:
league_a = 'Master'
#To Player B
if Rb<1200:
league_b = 'Unranked'
elif 1200<Rb<2400:
league_b = 'Gold'
elif 2400<Rb<3500:
league_b = 'Diamond'
elif Rb>3500:
league_b = 'Master'
Ra = str(round(Ra, 2))
Rb = str(round(Rb, 2))
oRa = str(round(oRa, 2))
oRb = str(round(oRb, 2))
Sa = str(Sa)
Sb = str(Sb)
print("Player\tScore\tOld Rating\tNew Rating\tNew League")
print(name1+"\t"+Sa+"\t"+oRa+" "+Ra+" "+league_a)
print(name2+"\t"+Sb+"\t"+oRb+" "+Rb+" "+league_b)
if Sa>Sb:
print("Winner: "+name1)
elif Sb>Sa:
print("Winner: "+name2)
elif Sa==Sb:
print("Draw")
array=[] #Data-set storage
for x in range(100): #Makes a data-set
array.append(['p'+str(x+1), random.uniform(800, 5000)])
for x in range(len(array)): # Giving players their leagues
if array[x][1]>3500:
array[x].append('Master')
elif 2400<array[x][1]<3500:
array[x].append('Diamond')
elif 1200<array[x][1]<2400:
array[x].append('Gold')
elif 800<array[x][1]<1200:
array[x].append('unranked')
n = int(input("Number of online players: "))
online = [x for x in input('Give space seprated names of players: ').split()]
Master = []
Diamond = []
Gold = []
unranked = []
dict_ = {x[0]:[x[1], x[2]] for x in array}
for x in online:
if dict_.get(x)[1]=='Master':
Master.append([x, *dict_.get(x)])
elif dict_.get(x)[1]=='Gold':
Gold.append([x, *dict_.get(x)])
elif dict_.get(x)[1]=='Diamond':
Diamond.append([x, *dict_.get(x)])
elif dict_.get(x)[1]=='unranked':
unranked.append([x, *dict_.get(x)])
leagues = [Master, Diamond, Gold, unranked]
for x in leagues:
if len(x)==1:
print("Waiting: "+str(x[0][0]))
elif len(x)==0:
pass
else:
if len(x)&1:
print("Waiting: "+str(x[len(x)-1][0]))
x.pop(len(x)-1)
for y in range(0, len(x), 2):
score_a = int(input("Give score of Player "+str(x[y][0])+" for this Match: "))
score_b = int(input("Give score of Player "+str(x[y+1][0])+" for this Match: "))
EloRating(x[y][1], x[y+1][1], 30, score_a, score_b,x[y][0], x[y+1][0])
input("Hit any key to exit.")