Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit ad71afc

Browse files
committed
Fix stuff?
1 parent dc25fb6 commit ad71afc

2 files changed

Lines changed: 41 additions & 37 deletions

File tree

src/cogs/util/results.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,48 @@ def f(v):
77
perc = (v['borda'] / v['votes']) / (len(round['votes'][0]['vote']) - 1) * 100
88
stdv = (sum((i - perc) ** 2 for i in v['raw_borda']) / len(v['raw_borda']))**0.5
99
return (perc, -stdv)
10+
1011
totals = {}
1112
for r in round['responses']:
1213
totals[r] = {'borda': 0, 'votes': 0, 'raw_borda': []}
13-
14+
1415
vote_weights = {}
15-
16+
1617
for vote in round['votes']:
1718
if vote['voter'] not in vote_weights:
1819
vote_weights[vote['voter']] = 1
1920
else:
2021
vote_weights[vote['voter']] += 1
2122
for i in vote_weights:
2223
vote_weights[i] = 1 / vote_weights[i]
23-
24+
2425
for vote in round['votes']:
2526
c = len(vote['vote'])
2627
for n, v in enumerate(vote['vote']):
2728
bc = c - n - 1
2829
totals[v]['borda'] += bc * vote_weights[vote['voter']]
2930
totals[v]['votes'] += vote_weights[vote['voter']]
30-
totals[v]['raw_borda'].append(bc / (c - 1) * 100)
31-
31+
totals[v]['raw_borda'].append((bc * vote_weights[vote['voter']]) / (c - 1) * 100)
32+
3233
totals = [{'name': i[0], **i[1]} for i in totals.items()]
33-
34+
3435
totals.sort(key=f, reverse=True)
3536
for twower in alive:
3637
if twower not in round['responses']:
3738
round['responses'][twower] = '*DNP*'.encode('UTF-8')
3839
totals.append({'name': twower, 'borda': 0, 'votes': 1, 'raw_borda': [0]})
3940
return totals
40-
41+
4142
def get_results(totals, elim, round):
4243
def f(v):
43-
return (v['borda'] / v['votes']) / (len(round['votes'][0]['vote']) - 1) * 100
44+
perc = (v['borda'] / v['votes']) / (len(round['votes'][0]['vote']) - 1) * 100
45+
stdv = (sum((i - perc) ** 2 for i in v['raw_borda']) / len(v['raw_borda']))**0.5
46+
return (perc, stdv)
47+
4448
for n, v in list(enumerate(totals))[::-1]:
45-
score = f(v)
49+
score, stdev = f(v)
4650

47-
mean = sum(v['raw_borda']) / len(v['raw_borda'])
48-
variance = sum((i - mean) ** 2 for i in v['raw_borda']) / len(v['raw_borda'])
49-
stdev = variance ** 0.5
50-
51+
# Formatting
5152
if str(n + 1)[-1] == '1':
5253
if n + 1 == 11:
5354
symbol = SUPERSCRIPT[3]
@@ -65,16 +66,16 @@ def f(v):
6566
symbol = SUPERSCRIPT[2]
6667
else:
6768
symbol = SUPERSCRIPT[3]
68-
69+
6970
dead = n >= elim
70-
71-
71+
72+
# :blobeyes:
7273
msg = '\n{}\n{} **{}{} place**: *{}*\n**{}** ({}% σ={})'.format(
73-
'=' * 50,
74-
':skull_crossbones:' if (dead and n!=0) else ':white_check_mark:',
75-
n + 1, symbol, round['responses'][v['name']].decode('utf-8'),
74+
'=' * 50,
75+
':skull_crossbones:' if (dead and n!=0) else ':white_check_mark:',
76+
n + 1, symbol, round['responses'][v['name']].decode('utf-8'),
7677
'{}',
77-
builtins.round(score, 2),
78+
builtins.round(score, 2),
7879
builtins.round(stdev, 2)
7980
)
8081
yield (msg, dead, v['name'], n)

src/cogs/util/timed_funcs.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
async def start_voting(bot, channel):
77
sd = bot.server_data[channel.id]
8-
8+
99
if sd['voting']:
1010
await bot.send_message(channel, 'Voting is already active.')
1111
return
12-
12+
1313
round = sd['seasons']['season-{}'.format(sd['season'])]['rounds']['round-{}'.format(sd['round'])]
1414
if len(round['responses']) < 2:
1515
await bot.send_message(channel, 'There aren\'t enough responses to start voting. You need at least 2.')
@@ -26,23 +26,23 @@ async def start_voting(bot, channel):
2626
round['restimer'] = datetime.datetime.utcnow()+sd['queuetimer']['results']
2727
bot.save_data()
2828
await bot.send_message(channel, 'Voting has been activated.')
29-
29+
3030
async def do_results(bot, channel, guild, nums='', message=None):
3131
sd = bot.server_data[channel.id]
3232
round = sd['seasons']['season-{}'.format(sd['season'])]['rounds']['round-{}'.format(sd['round'])]
33-
33+
3434
if not sd['voting']:
3535
await bot.send_message(channel, 'Voting hasn\'t even started yet...')
3636
if round['restimer'] != None:
3737
round['restimer'] = None
3838
bot.save_data()
3939
return
40-
40+
4141
if 'season-{}'.format(sd['season']) not in sd['seasons']:
4242
sd['seasons']['season-{}'.format(sd['season'])] = {}
4343
if 'round-{}'.format(sd['round']) not in sd['seasons']['season-{}'.format(sd['season'])]['rounds']:
4444
sd['seasons']['season-{}'.format(sd['season'])]['rounds']['round-{}'.format(sd['round'])] = dict(templates.round())
45-
45+
4646
voted_ons = set()
4747
for vote in round['votes']: voted_ons |= set(vote['vote'])
4848
if set(round['responses']) != voted_ons:
@@ -56,7 +56,7 @@ async def do_results(bot, channel, guild, nums='', message=None):
5656
totals = results.count_votes(round, round['alive'])
5757
msg = '**Results for round {}, season {}:**'.format(sd['round'], sd['season'])
5858
if message != None:
59-
await message.delete()
59+
await message.delete()
6060
eliminated = []
6161
living = []
6262
elim = int(0.8 * len(totals))
@@ -79,25 +79,27 @@ async def do_results(bot, channel, guild, nums='', message=None):
7979
if votec != 1: votest += 's'
8080
voterst = '{} voter'.format(voterc)
8181
if voterc != 1: voterst += 's'
82-
82+
8383
await asyncio.sleep(1)
8484
await bot.send_message(channel, '{} submitted {}.'.format(voterst, votest))
85-
85+
8686
for msg, dead, uid, n in results.get_results(totals, elim, round):
8787
user = guild.get_member(uid)
8888
name = ''
8989
if user is not None:
9090
name = user.mention
9191
else:
9292
name = str(uid)
93-
93+
9494
if dead:
9595
eliminated.append((name, user))
9696
else:
9797
living.append((name, user))
98-
98+
9999
await asyncio.sleep(len(totals) - n / 2)
100-
await bot.send_message(channel,msg.format(name))
100+
await bot.send_message(channel, msg.format(name))
101+
102+
# Winner/looser stuff
101103
user = guild.get_member(totals[0]['name'])
102104
if user is not None:
103105
name = user.mention
@@ -106,13 +108,14 @@ async def do_results(bot, channel, guild, nums='', message=None):
106108
msg = '{}\nThe winner was {}! Well done!'.format('=' * 50, name)
107109
await bot.send_message(channel,msg)
108110
round['winner'] = user.id
109-
111+
110112
if eliminated:
111113
await bot.send_message(channel,
112114
'Sadly though, we have to say goodbye to {}.'.format(', '.join([i[0] for i in eliminated])))
113115
else:
114116
await bot.send_message(channel, 'You all lived on. I would say well done, but The elimination threshold was probably at 0.')
115117
advance = False
118+
116119
# Do all the round incrementing and stuff.
117120
if len(totals) - len(eliminated) <= 1:
118121
await bot.send_message(channel,'**This season has ended! The winner was {}!**'.format(name))
@@ -128,17 +131,17 @@ async def do_results(bot, channel, guild, nums='', message=None):
128131
if sd['canqueue'] and len(sd['queue']) > 0:
129132
if sd['queuetimer']['prompt'] != None:
130133
sd['hosttimer'] = datetime.datetime.utcnow()+sd['queuetimer']['prompt']
131-
134+
132135
if 'season-{}'.format(sd['season']) not in sd['seasons']:#new season
133136
sd['seasons']['season-{}'.format(sd['season'])] = {'rounds':{}}
134137
living = []
135138
if 'round-{}'.format(sd['round']) not in sd['seasons']['season-{}'.format(sd['season'])]['rounds']:#new round
136139
sd['seasons']['season-{}'.format(sd['season'])]['rounds']['round-{}'.format(sd['round'])] = dict(templates.round())
137140
sd['seasons']['season-{}'.format(sd['season'])]['rounds']['round-{}'.format(sd['round'])]['alive']=[t[1].id for t in living]
138-
141+
139142
if advance:
140143
await twow_helper.next_host(bot, channel, sd)
141-
144+
142145
sd['voting'] = False
143-
146+
144147
bot.save_data()

0 commit comments

Comments
 (0)