-
Notifications
You must be signed in to change notification settings - Fork 20
/
randomer.py
85 lines (73 loc) · 2.66 KB
/
randomer.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
import logging
try:
from PyQt6 import QtCore, QtWidgets
except ImportError:
print("PyQt5 fallback (randomer.py)")
from PyQt5 import QtCore, QtWidgets
PchumLog = logging.getLogger("pchumLogger")
RANDNICK = "randomEncounter"
class RandomHandler(QtCore.QObject):
def __init__(self, parent):
QtCore.QObject.__init__(self, parent)
self.randNick = RANDNICK
self.mainwindow = parent
self.queue = []
self.running = False
def setRunning(self, on):
self.running = on
self.mainwindow.rand.setEnabled(on)
def getRandomer(self):
self.queue.append("?")
self.mainwindow.sendNotice.emit("?", self.randNick)
def setRandomer(self, value, force=False):
# Notifies randomEncounter of our preferences
# Only does so if the new value differs from what is stored in userprofile
# Can be forced to notify anyways by providing `force=True`
if value != self.mainwindow.userprofile.getRandom() or force:
if value:
code = "+"
else:
code = "-"
self.queue.append(code)
self.mainwindow.sendNotice.emit(code, self.randNick)
def setIdle(self, i):
if i:
code = "~"
else:
code = "*"
self.queue.append(code)
self.mainwindow.sendNotice.emit(code, self.randNick)
@QtCore.pyqtSlot()
def getEncounter(self):
self.queue.append("!")
self.mainwindow.sendNotice.emit("!", self.randNick)
def incoming(self, msg):
l = msg.split("=")
code = l[0][0]
if code not in self.queue:
return # Ignore if we didn't request this
self.queue.remove(code)
if code == "?":
if l[1][0] == "y":
self.mainwindow.userprofile.setRandom(True)
elif l[1][0] == "n":
self.mainwindow.userprofile.setRandom(False)
elif code in ["+", "-"]:
if l[1][0] == "k":
if code == "+":
self.mainwindow.userprofile.setRandom(True)
else:
self.mainwindow.userprofile.setRandom(False)
elif code in ["~", "*"]:
if l[1][0] == "k":
pass
elif code == "!":
if l[1] == "x":
msgbox = QtWidgets.QMessageBox()
msgbox.setText("Unable to fetch you a random encounter!")
msgbox.setInformativeText("Try again later :(")
msgbox.exec()
return
name = l[1]
PchumLog.info("Random Encounter name is: %s", name)
self.mainwindow.newConversation(name)