-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathext.py
More file actions
299 lines (244 loc) · 8.1 KB
/
ext.py
File metadata and controls
299 lines (244 loc) · 8.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
"""
MIT License
Copyright (c) 2018-2019 Evan Liapakis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import random
import discord
def get(file, col, cond=''):
"""Gets a column from the specified csv file.
Data is stored as player_id,nickname,tribe,vote
If the player has not voted, vote equals "nobody"
If cond is set, it will only return the specified column if cond is in
any column."""
with open(file) as f:
col -= 1
if cond:
for line in f:
data = line.strip().split(',')
if cond in data:
return data[col]
else:
data = [line.strip().split(',')[col] for line in f]
return data
def write(file, data, delete=False):
"""Writes a row to the specified csv file.
Data is stored as player_id,nickname,tribe,vote
Line to be written is passed as a list ([player_id, nickname, vote])
If the player has not voted, vote equals "nobody"
If delete is True, it will instead delete the row with the passed data"""
new = ''
with open(file) as f:
for line in f:
if data[0] not in line:
new += line
if not delete:
new += ','.join(data) + '\n'
with open(file, 'w') as f:
f.write(new)
def toggle():
"""Toggles vote time"""
file = "vote_time"
content = ''
with open(file) as f:
content = f.read().strip()
with open(file, 'w') as f:
if content == '1':
f.write('0')
elif content == '0':
f.write('1')
def exists(file, item):
"""Returns true if an item is in a file"""
exist = get(file, 1, item)
if exist:
return True
return False
def is_vote_time():
"""Returns true if it voting has been allowed"""
time = get("vote_time", 1)
if time[0] == '1':
return True
return False
def voted(voter):
"""Checks if player has already voted
voter is the player's Discord id"""
vote = get("players.csv", 4, voter)
if vote != 'nobody':
return True
return False
def same(player, vote):
"""Checks to see if the vote is the same
player is the player's Discord id
vote is the nickname of the person they have voted"""
who = get("players.csv", 4, player)
if vote == who:
return True
return False
def get_tribal():
"""Returns the tribe at tribal council"""
tribe = get("tribes.csv", 2, 'voting')
return tribe
def set_tribal(tribe):
"""Sets tribal council to a tribe
tribe is the tribe to set tribal council to"""
write("tribes.csv", ['voting', tribe])
class Player:
"""Class for a player"""
file = "players.csv"
def __init__(self, user_id):
self.user_id = user_id
self.nick = get(self.file, 2, user_id)
self.tribe = get(self.file, 3, user_id)
self.vote = get(self.file, 4, user_id)
if exists("players.csv", user_id):
self.strikes = int(get(self.file, 5, user_id))
else:
self.strikes = 0
def write(self, nick='', tribe='', vote='nobody', strike=False):
"""Write data for a player"""
if nick:
self.nick = nick
if tribe:
self.tribe = tribe
if strike:
self.strikes += 1
self.vote = vote
write(self.file, [self.user_id, self.nick, self.tribe, self.vote, str(self.strikes)])
def destroy(self):
"""Delete a player"""
write(self.file, [self.user_id], True)
def get_players():
"""Return a list of all players"""
ids = get("players.csv", 1)
players = [Player(id) for id in ids]
return players
def get_idols():
players = get_players()
idols = []
for player in players:
if get("idols.csv", 2, player.nick) == "yes":
idols.append(player.nick)
return idols
def sort_votes(votes):
"""Return a list which gives a more 'dramatic' vote order"""
# Grab a tally of the votes
tally = {}
for item in votes:
if item in tally:
tally[item] += 1
else:
tally[item] = 1
# Check if a player is using an idol and handle accordingly
has_idol = []
idols = get_idols()
for player in tally:
if player in idols:
has_idol.append(player)
check = {}
for item in votes:
if item not in has_idol:
if item in check:
check[item] += 1
else:
check[item] = 1
# Get who had the most votes
highest = max(check.values())
most = [a for a, b in check.items() if b == highest]
# If more than one person has the most votes, shuffle the vote order
# and return
if len(most) != 1:
random.shuffle(votes)
return votes, None
most = most[0]
majority = len(votes) // 2
if tally[most] > majority:
# If a player has more than majority, get how many more they have
extra = tally[most] - majority
tally[most] -= extra
else:
tally[most] -= 1
new = []
# Add the rest to new
for item in tally:
for n in range(tally[item]):
new.append(item)
# Shuffle
random.shuffle(new)
# Add the final vote back to the end of new
new.append(most)
return new, most
def get_player_object(ctx, player):
"""Returns the object for a player"""
if isinstance(player, Player):
user_id = player.user_id
elif '#' in player:
user_id = player[:-5]
else:
user_id = player
obj = discord.utils.get(ctx.message.server.members, id=user_id)
return obj
def get_role_object(ctx, role):
"""Returns the object for a role"""
return discord.utils.get(ctx.message.server.roles, name=role)
async def remove_player(client, ctx, nick, role):
"""Removes a player and replaces their roles"""
player = Player(get("players.csv", 1, nick))
# Delete player from players.csv
player.destroy()
# Replace roles with role
user = get_player_object(ctx, player)
spec = get_role_object(ctx, role)
try:
await client.replace_roles(user, spec)
except discord.errors.Forbidden:
await client.say("Unable to replace role.")
except AttributeError:
await client.say("Unable to replace role.")
def host(ctx):
"""Returns true if player has host role"""
if "Host" in [role.name for role in ctx.message.author.roles]:
return True
return False
def get_channel(ctx, name):
return discord.utils.get(ctx.message.server.channels, name=name)
def player_num():
with open("playernum") as f:
return f.read().strip()
def get_placing():
player_num = len(get("players.csv", 1))
total_num = int(get("playernum", 1)[0])
place = str(total_num - player_num + 1)
if place[-1] == '1':
place += "st"
elif place[-1] == '2':
place += "nd"
elif place[-1] == '3':
place += "rd"
else:
place += "th"
return place
def get_final_place():
place = str(len(get("players.csv", 1)))
if place[-1] == '1':
place += "st"
elif place[-1] == '2':
place += "nd"
elif place[-1] == '3':
place += "rd"
else:
place += "th"
return place