pesterchum/randomer.py

83 lines
2.4 KiB
Python
Raw Normal View History

import logging
2022-08-19 07:12:58 -04:00
try:
from PyQt6 import QtCore, QtWidgets
except ImportError:
print("PyQt5 fallback (randomer.py)")
from PyQt5 import QtCore, QtWidgets
PchumLog = logging.getLogger("pchumLogger")
2014-01-12 02:33:33 -05:00
RANDNICK = "randomEncounter"
class RandomHandler(QtCore.QObject):
def __init__(self, parent):
QtCore.QObject.__init__(self, parent)
2014-01-12 02:33:33 -05:00
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, r):
if r != self.mainwindow.userprofile.getRandom():
if r:
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":
2021-03-23 17:36:43 -04:00
msgbox = QtWidgets.QMessageBox()
msgbox.setText("Unable to fetch you a random encounter!")
msgbox.setInformativeText("Try again later :(")
2022-06-26 22:18:37 -04:00
msgbox.exec()
return
2021-03-23 17:36:43 -04:00
name = str(l[1])
2023-02-13 14:26:05 -05:00
PchumLog.info("Random Encounter name is: %s", name)
self.mainwindow.newConversation(name)