-
Notifications
You must be signed in to change notification settings - Fork 8
/
sync.py
163 lines (137 loc) · 4.4 KB
/
sync.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
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
"""
RE-Rework of sync (v4?). Allows users to ready
and sync movies or whatever. By @agricola
"""
import sopel.module
from sopel.tools import Identifier
import time
old_syncs = []
current_syncs = []
sync_id = 0
max_old_syncs = 100
timeout_time = 30
class Sync:
def __init__(self, syncers, channel):
global sync_id
self.syncers = syncers
self.timeout = timeout_time
self.id = sync_id
self.channel = channel
sync_id += 1
def ready(self):
return all([r for s,r in self.syncers.items()])
def names(self):
return ", ".join([str(s) for s,r in self.syncers.items()])
def check_valid(bot, trigger, sync, output=False):
channel_users = [Identifier(n) for n in bot.channels[trigger.sender.lower()].users]
if Identifier(bot.nick) in sync.syncers:
if output:
bot.say("Sorry! I'm shy!!!")
return False
if len(sync.syncers) < 2:
if output:
bot.say("Not enough syncers! Get some friends!")
return False
for nick in sync.syncers:
if nick not in channel_users:
if output:
bot.say("{} is invalid! Get em outta here!".format(str(nick)))
return False
else:
for s in current_syncs:
if s is not sync and nick in s.syncers:
if output:
bot.say("{} is busy with sync {:X}".format(str(nick), s.id))
return False
return True
def find_sync(bot, trigger, nick, sync_list, sid=None):
sync = None
for s in sync_list:
if Identifier(nick) in s.syncers and check_valid(bot, trigger, s):
if sid is None or s.id == sid:
sync = s
break
return sync
def add_old_sync(sync):
global old_syncs
old_syncs.insert(0, sync)
if len(old_syncs) > max_old_syncs:
old_syncs = old_syncs[:len(old_syncs)-max_old_syncs]
@sopel.module.interval(10)
def sync_update(bot):
global current_syncs
for s in current_syncs:
if s.channel not in bot.channels:
continue
s.timeout -= 1
if s.timeout <= 0:
add_old_sync(s)
bot.say("Sync {:X} failed!".format(s.id), s.channel)
current_syncs = [s for s in current_syncs if s.timeout > 0]
@sopel.module.commands("sync")
@sopel.module.commands("s")
@sopel.module.priority("high")
def sync(bot, trigger):
if trigger.group(2) == None:
return bot.say("Please include syncer names!")
syncers = trigger.group(2).split()
syncers.append(trigger.nick)
sync = Sync({Identifier(s): False for s in set(syncers)}, trigger.sender)
if check_valid(bot, trigger, sync, True):
sync.syncers[Identifier(trigger.nick)] = True
current_syncs.append(sync)
bot.say("Buckle up syncers! (ID: {:X})".format(sync.id))
@sopel.module.commands("desync")
@sopel.module.commands("ds")
@sopel.module.priority("high")
def desync(bot, trigger):
global current_syncs
sync = find_sync(bot, trigger, Identifier(trigger.nick), current_syncs)
if sync is None:
bot.say("Sorry! You are not in a sync!")
return
current_syncs = [s for s in current_syncs if s is not sync]
add_old_sync(sync)
bot.say("Desyncing... (ID: {:X})".format(sync.id))
@sopel.module.commands("ready")
@sopel.module.commands("rdy")
@sopel.module.commands("r")
@sopel.module.commands("lady")
@sopel.module.priority("high")
def ready(bot, trigger):
global current_syncs
sync = find_sync(bot, trigger, Identifier(trigger.nick), current_syncs)
# Debug
#bot.say("{}".format([s.id for s in current_syncs]))
#bot.say("{}".format([s.id for s in old_syncs]))
if sync is None:
bot.say("Sorry! Could not find you in a valid sync!")
return
sync.syncers[Identifier(trigger.nick)] = True
if sync.ready():
current_syncs = [s for s in current_syncs if s is not sync]
add_old_sync(sync)
bot.say("Lets go {}!".format(sync.names()))
time.sleep(2)
for i in reversed(range(1,4)):
bot.say("{}".format(i))
time.sleep(2)
bot.say("GO!")
@sopel.module.commands("resync")
@sopel.module.commands("rs")
@sopel.module.priority("high")
def resync(bot, trigger):
global old_syncs
sid = trigger.group(2)
if sid is not None:
sid = int(sid, 16)
sync = find_sync(bot, trigger, Identifier(trigger.nick), old_syncs, sid)
if sync is None:
bot.say("Sorry! You were not in a recent sync!")
return
sync.syncers = {s:False for s in sync.syncers}
sync.syncers[Identifier(trigger.nick)] = True
sync.timeout = timeout_time
old_syncs = [s for s in old_syncs if s is not sync]
current_syncs.append(sync)
bot.say("Buckle up resyncers! {} (ID: {:X})".format(sync.names(), sync.id))