-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_controller.py
78 lines (65 loc) · 2.79 KB
/
channel_controller.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
import asyncio
import itertools
import discord
from .controller import GameCubeController, ACTIONS
class ChannelController(GameCubeController):
def __init__(self, channel:discord.TextChannel, clone_parent:str,
controller_name:str):
super().__init__(clone_parent, controller_name)
self.paused = False
self.channel = channel
self.members = set()
self.members_who_pushed = set()
async def ready_message(self):
await self.channel.send(f"{self.ui.name}: READY")
async def close(self):
super().close()
await self.channel.send(f"{self.ui.name}: CLOSED")
def channel_and_member_check(self, channel:discord.TextChannel,
member: discord.Member):
if self.channel.id != channel.id:
return False
return member in self.members
async def member_perform_action(self, member:discord.Member, actions:str,
max_button_presses: int, min_participation: float,
override_pause:bool=False):
# If paused, do nothing!
if self.paused and not override_pause:
return
# Prevent division by 0 when overriding a controller with no members.
# Usually occurs with random input controllers
if len(self.members) > 0:
# Check if at least the required percentage of the team
# participated
if len(self.members_who_pushed)/len(self.members) >=\
min_participation:
# All have pushed, reset list
self.members_who_pushed = set()
# Restrict max button presses down proportionally to the team size
mbp = max(2, int(max_button_presses / len(self.members)))
# Check if member has already pressed a button since reset
if member in self.members_who_pushed:
return
else:
mbp = max_button_presses
# Collect valid actions
validated_actions = []
# Look at all button press, to a limit
for action in itertools.islice(actions.split(" "), mbp):
if ACTIONS.get(action) is not None:
act = ACTIONS.get(action)
placed = False
for i in range(len(validated_actions)):
if not act in validated_actions[i]:
placed = True
validated_actions[i].append(act)
break
if not placed:
validated_actions.append([act])
# Push button
if len(validated_actions) > 0:
# Add member to pressed list
self.members_who_pushed.add(member)
for action_set in validated_actions:
await self.perform_actions(action_set)
await asyncio.sleep(1/8)