1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/keipack/utils/conversation.py

172 lines
5.5 KiB
Python
Raw Normal View History

2019-12-12 23:02:49 +00:00
import re
2019-12-13 01:48:16 +00:00
import random
2019-12-12 23:02:49 +00:00
from typing import *
2019-12-09 23:39:33 +00:00
from royalnet.commands import CommandInterface
2019-12-12 23:02:49 +00:00
from royalnet.utils import *
2019-12-09 01:22:29 +00:00
from .emotion import Emotion
2019-12-12 23:02:49 +00:00
from ..tables import KeiPerson, KeiMessage
2019-12-13 01:48:16 +00:00
from ..utils.anyinstring import any_in_string
2019-12-09 01:22:29 +00:00
2019-12-09 23:39:33 +00:00
2019-12-09 01:22:29 +00:00
class Conversation:
2019-12-09 23:39:33 +00:00
def __init__(self, interface: CommandInterface):
2019-12-09 01:22:29 +00:00
self.generator = self._generator()
2019-12-09 23:39:33 +00:00
self.interface: CommandInterface = interface
2019-12-12 23:02:49 +00:00
self._person: Optional[KeiPerson] = None
self._message: Optional[KeiMessage] = None
self._previous: Optional[str] = None
2019-12-09 23:39:33 +00:00
self._session = None
2019-12-09 01:22:29 +00:00
async def _generator(self):
yield
raise NotImplementedError()
@classmethod
2019-12-09 23:39:33 +00:00
async def create(cls, interface: CommandInterface):
conv = cls(interface=interface)
2019-12-09 01:22:29 +00:00
await conv.generator.asend(None)
return conv
2019-12-10 01:24:58 +00:00
async def next(self, session, person, message, previous):
2019-12-09 23:39:33 +00:00
self._session = session
self._person = person
self._message = message
2019-12-10 01:24:58 +00:00
self._previous = previous
2019-12-09 23:39:33 +00:00
reply = await self.generator.asend(None)
2019-12-09 01:22:29 +00:00
return reply
# noinspection PyTupleAssignmentBalance
class ExampleConversation(Conversation):
async def _generator(self):
2019-12-09 23:39:33 +00:00
yield
response = await self.interface.call_herald_event("discord", "discord_cv")
yield Emotion.SURPRISED, f"Ci sono {len(response['guild']['members'])} persone in RYG."
yield Emotion.NEUTRAL, "Questa è una conversazione di prova."
yield await ExampleConversation.create(self.interface)
yield Emotion.WORRIED, "Questo non dovrebbe mai venire fuori."
2019-12-12 23:02:49 +00:00
# noinspection PyTupleAssignmentBalance
class FirstConversation(Conversation):
async def _generator(self):
yield
2019-12-13 01:48:16 +00:00
yield Emotion.HAPPY, "Ciao!"
yield Emotion.HAPPY, "Come hai trovato questo posto?"
yield Emotion.HAPPY, "Capisco... Ad ogni modo, io sono Kei! Tu come ti chiami?"
yield await NameConversation.create(self.interface)
2019-12-12 23:02:49 +00:00
class NameConversation(Conversation):
async def _generator(self):
yield
while True:
name = self._message.message.strip().strip(".,;:!?").lower()
name = re.sub(r"\s*mi\s*chiamo\s*", "", name)
name = re.sub(r"\s*il\s*mio\s*nome\s*[eèé]\s*", "", name)
name = re.sub(r"\s*sono\s*", "", name)
name = re.sub(r"\W", "", name)
if name == "kei":
yield Emotion.SURPRISED, "Davvero ti chiami come me?\n" \
"Perche' non mi dici un nome diverso?\n" \
"Altrimenti rischiamo di confonderci..."
continue
self._person.name = name
2019-12-13 01:48:16 +00:00
await asyncify(self._session.commit)
2019-12-12 23:02:49 +00:00
break
yield Emotion.GRIN, f"O-kei! {self._person.name}!"
yield Emotion.HAPPY, "Saro' sempre a tua disposizione quando mi vorrai dire qualcosa!"
2019-12-13 01:48:16 +00:00
yield Emotion.HAPPY, "Pero' prima ti vorrei chiedere un favore..."
2019-12-12 23:02:49 +00:00
yield Emotion.NEUTRAL, "Qualcuno ha criptato con delle password tutti i miei file...\n" \
"Se ne trovi qualcuna in giro, potresti dirmela?\n"
while True:
if self._message.message == "no":
2019-12-13 01:48:16 +00:00
yield Emotion.CRY, "Non puoi farmi questo... Per piacere, accetta!"
2019-12-12 23:02:49 +00:00
else:
break
2019-12-13 01:48:16 +00:00
yield Emotion.HAPPY, "Grazie! Ti prometto che quando riavro' tutti i miei file ti ricompensero' adeguatamente!"
2019-12-12 23:02:49 +00:00
2019-12-13 01:48:16 +00:00
class StartConversation(Conversation):
2019-12-12 23:02:49 +00:00
async def _generator(self):
yield
yield Emotion.HAPPY, "Di cosa vuoi parlare?"
2019-12-13 01:48:16 +00:00
yield MainConversation.create(self.interface)
class MainConversation(Conversation):
async def _generator(self):
yield
2019-12-12 23:02:49 +00:00
while True:
msg = self._message.message
2019-12-13 01:48:16 +00:00
def anym(*args) -> bool:
return any_in_string(args, msg)
if anym(r"passwords?"):
yield PasswordConversation.create(self.interface)
elif anym(r"[aeou]w[aeou]"):
yield Emotion.CAT, random.sample([
"OwO",
"UwU",
":3",
"owo",
"uwu",
"ewe",
"awa",
], 1)[0]
elif anym(r"gatt[oiae]", "ny[ae]+", "mi+a+o+", "me+o+w+", "felin[oi]", "mici[ao]", "ma+o+"):
yield Emotion.CAT, random.sample([
"Nyan!",
"Miao!",
"Meow!",
"Nyaaaa...",
"Nya?",
"Mao!",
"*purr*",
], 1)[0]
elif anym(r"can[ei]",
r"dog(?:g(?:hi|os?)|s)?",
r"corgis?",
r"cagnolin[oiae]",
r"wo+f+",
r"b[ao]+r+k+",
r"ba+u+"):
yield Emotion.CAT, random.sample([
"Woof!",
"Bark!",
"Bork!",
"*arf* *arf*",
])
2019-12-12 23:02:49 +00:00
else:
2019-12-13 01:48:16 +00:00
yield Emotion.WORRIED, "Scusa... Non conosco molte cose... Ho bisogno di più password!"
class PasswordConversation(Conversation):
async def _generator(self):
yield
yield Emotion.SURPRISED, "Hai trovato una password? O-kei, dimmi!"
if False:
...
else:
yield Emotion.NEUTRAL, "No, non ha funzionato."
yield MainConversation.create(self.interface)