Merge pull request #4

Updates for help, hangman, mathrun and wordrun
This commit is contained in:
BaerbelBox
2022-05-02 10:43:59 +02:00
committed by GitHub
5 changed files with 118 additions and 55 deletions

View File

@@ -10,11 +10,11 @@ from FaustBot.Model.BlockedUsers import BlockProvider
class BlockObserver(PrivMsgObserverPrototype):
@staticmethod
def cmd():
return [""]
return None
@staticmethod
def help():
return ""
return None
def update_on_priv_msg(self, data, connection: Connection):
if not self._is_idented_mod(data, connection):

View File

@@ -3,6 +3,7 @@ from FaustBot.Communication.Connection import Connection
from FaustBot.Modules.PrivMsgObserverPrototype import PrivMsgObserverPrototype
from FaustBot.Model.ScoreProvider import ScoreProvider
from FaustBot.Model.HanDatabaseProvider import HanDatabaseProvider
from FaustBot.Modules.HelpObserver import HelpObserver
from collections import defaultdict
from threading import Lock
import csv
@@ -13,11 +14,11 @@ import datetime
class HangmanObserver(PrivMsgObserverPrototype):
@staticmethod
def cmd():
return ['.guess', '.word', '.stop', '.hint', '.score', '.spielregeln']
return ['.word', '.han', '.guess', '.hint', '.look' '.stop', '.score', '.resetscore', '.spielregeln']
@staticmethod
def help():
return 'hangman game'
return 'Hangman Spiel. Details bitte per PM an den Bot mit .spielregeln abfragen.'
def __init__(self):
super().__init__()
@@ -29,20 +30,21 @@ class HangmanObserver(PrivMsgObserverPrototype):
self.worder = ''
self.wrongly_guessedWords = []
self.time = time.time()
self.commands = []
def update_on_priv_msg(self, data, connection: Connection):
if data['message'].find('.guess ') != -1:
if data['message'].startswith('.guess '):
self.guess(data, connection)
return
if data['message'].find('.word ') != -1:
if data['message'].startswith('.word '):
self.take_word(data, connection)
if data['message'].find('.han') != -1 and not data['message'].find('.handelete')!= -1 and not data['message'].find('hanadd'
if data['message'].startswith('.han') and not data['message'].find('.handelete')!= -1 and not data['message'].find('hanadd'
) != -1:
self.start_solo_game(data, connection)
if data['message'].find('hanadd') != -1:
if data['message'].startswith('.hanadd'):
self.han_user_add(data, connection)
if data['message'].find('.stop') != -1 and not data['message'].find('.stophunt') != -1 \
and not data['message'].find('.stopMath') != -1:
if data['message'].startswith('.stop') and not data['message'].find('.stophunt') != -1 \
and not data['message'].find('.stopmath') != -1 and not data['message'].find('.stopslf') != -1:
connection.send_channel("Spiel gestoppt. Das Wort war: " + self.word + " in "+self.timeRelapsedString())
self.word = ''
self.guesses = []
@@ -51,17 +53,19 @@ class HangmanObserver(PrivMsgObserverPrototype):
self.worder = ''
self.wrongly_guessedWords = []
self.worder = ''
if data['message'].find('.hint') != -1:
if data['message'].startswith('.hint'):
self.hint(data, connection)
if data['message'].find('.score') != -1:
if data['message'].startswith('.score'):
self.print_score(data, connection)
if data['message'].find('.spielregeln') != -1:
if data['message'].startswith('.spielregeln'):
self.rules(data, connection)
if data['message'].find('.look') != -1:
if data['message'].startswith('.look'):
self.look(data, connection)
if data['message'].find('.resetscore') != -1:
if data['message'].startswith('.resetscore') and len(data['message'].split(' ')) < 2:
self.confirm_reset(data, connection)
if data['message'] == '.resetscore ' + data['nick'] + ' JA':
self.reset(data, connection)
if data['message'].find('.handelete') != -1:
if data['message'].startswith('.handelete '):
self.delete_HanWord(data, connection)
def delete_HanWord(self,data,connection):
@@ -76,7 +80,12 @@ class HangmanObserver(PrivMsgObserverPrototype):
def reset(self, data, connection):
score_provider = ScoreProvider()
score_provider.delete_score(data['nick'])
connection.send_back("Dein Score wurde gelöscht "+data['nick'], data)
connection.send_back("Dein Score wurde gelöscht, "+data['nick'], data)
def confirm_reset(self, data, connection):
connection.send_back('Möchtest du deinen Hangman Punktestand wirklich löschen, ' + data['nick'] + '? ' +
'Wenn ja, antworte bitte mit ".resetscore deinnick JA". Wenn nein, musst du nichts tun.', data)
print(data['message'])
def look(self,data, connection):
if self.worder != '':
@@ -171,9 +180,10 @@ class HangmanObserver(PrivMsgObserverPrototype):
connection.send_channel(self.prepare_word(data))
def take_word(self, data, connection):
self.commands = HelpObserver.collect_commands(self, connection)
if self.word == '':
self.time =time.time()
if data['message'].split(' ')[1] is not None:
if data['message'].split(' ')[1] is not None and data['message'].split(' ')[1] not in self.commands:
self.addHanWord(data['message'].split(' ')[1].upper())
log = open('HangmanLog', 'a')
log.write(data['nick'] + ' ; ' + data['message'].split(' ')[1].upper() + '\n')
@@ -189,10 +199,12 @@ class HangmanObserver(PrivMsgObserverPrototype):
connection.send_channel(self.prepare_word(data))
else:
connection.send_back("Sorry es läuft bereits ein Wort", data)
def han_user_add(self, data, connection):
if data['message'].split(' ')[1] is not None:
self.addHanWord(data['message'].split(' ')[1].upper())
connection.send_channel("Das Wort "+data['message'].split(' ')[1].upper() +" wurde von "+ data['nick']+ " hinzugefügt")
def prepare_word(self, data):
outWord = ""
failedChars = 0
@@ -237,6 +249,7 @@ class HangmanObserver(PrivMsgObserverPrototype):
connection.send_back("Spielregeln bitte im Query abfragen",data)
return
connection.send_back("""Wort starten mit ".word Wort" im Query (Privatchat) mit dem Bot""", data)
connection.send_back("""Solospiel starten mit ".han" - Botty sucht dann das Wort aus""", data)
connection.send_back("""Raten mit ".guess Buchstabe" im Channel""", data)
connection.send_back("""Geraten werden können einzelne Buchstaben oder das ganze Wort.""", data)
connection.send_back("""Alle dürfen durcheinander raten. Es gibt keine Reihenfolge.""", data)

View File

@@ -1,3 +1,4 @@
from types import NoneType
from FaustBot.Communication import Connection
from FaustBot.Modules.PrivMsgObserverPrototype import PrivMsgObserverPrototype
@@ -9,24 +10,52 @@ class HelpObserver(PrivMsgObserverPrototype):
@staticmethod
def help():
return ".help - zeigt Hilftexte aller Module an"
return ".help <Befehl> - zeigt Hilftexte zu <Befehl> an. Für eine Liste aller verfügbaren Befehle: .help all"
def update_on_priv_msg(self, data, connection: Connection):
msg = data["message"]
command = ""
if not msg.startswith(".help"):
return
if len(msg.split(' ')) > 1:
command = msg.split(' ')[1]
if command == 'all':
self.show_available_commands(data, connection)
else:
if not command.startswith("."):
command = '.' + command
self.show_help_for_command(command, data, connection)
else:
connection.send_back(self.help(), data)
if data["channel"] == connection.details.get_channel():
def collect_commands(self, connection):
all_cmd = []
for observer in connection.priv_msg_observable.get_observer():
cmds = observer.cmd()
if cmds is not None:
all_cmd.extend(cmds)
print(all_cmd)
return all_cmd
def show_available_commands(self, data, connection):
if data["channel"] == connection.details.get_channel():
all_cmd = []
all_cmd = self.collect_commands(connection)
msg = ", ".join(all_cmd)
msg = "Bekannte Befehle: " + msg + ". Für Details per Query .help ."
msg = "Bekannte Befehle: " + msg
connection.send_back(msg, data)
else:
all_help = [m.help() for m in connection.priv_msg_observable.get_observer()]
for help_msg in all_help:
if help_msg is not None:
connection.send_back(help_msg, data)
def show_help_for_command(self, command, data, connection):
for observer in connection.priv_msg_observable.get_observer():
if observer.cmd() is not None:
if command in observer.cmd():
print(observer.help())
connection.send_back(observer.help(), data)

View File

@@ -6,11 +6,11 @@ from time import sleep
class MathRunObserver(PrivMsgObserverPrototype):
@staticmethod
def cmd():
return ['.s', '.startMath', '.stopMath']
return ['.s', '.startmath', '.stopmath']
@staticmethod
def help():
return 'startMath startet eine Reihe von Aufgaben. StopMath beendet sie.'
return '".startmath" startet eine Reihe von Aufgaben. ".stopmath" beendet sie. Lösungen mit ".s Lösung" eingeben.'
def __init__(self):
super().__init__()
@@ -21,16 +21,18 @@ class MathRunObserver(PrivMsgObserverPrototype):
self.oldSolution = 0
def update_on_priv_msg(self, data, connection: Connection):
if data['message'].find('.s ') != -1 :
if data['message'].startswith('.s ') and not data['message'].startswith('.startmath') and not data['message'].startswith('.stopmath'):
self.solution(data, connection)
if data['message'].find('.startMath') != -1:
if data['message'].startswith('.startmath'):
self.start_math(data, connection)
if data['message'].startswith('.stopmath'):
self.stop_math(data, connection)
def solution(self, data, connection):
nick = data["nick"]
solutionByPlayer = data['message'].split()[1]
if solutionByPlayer is None:
connection.send_back("Sorry du hast keine Lösung angegeben " + nick, data)
connection.send_back("Du hast keine Lösung angegeben " + nick, data)
return
if solutionByPlayer == str(self.solutionForGame):
connection.send_channel("Korrekte Lösung " + nick)
@@ -63,9 +65,13 @@ class MathRunObserver(PrivMsgObserverPrototype):
self.stop_Timer(data, connection)
def stop_math(self, data, connection):
for player in self.players.keys():
if self.players[player] == 1:
connection.send_channel(player + " hat\t" + str(self.players[player]) + "\tPunkt")
else:
connection.send_channel(player + " hat\t" + str(self.players[player]) + "\tPunkte")
connection.send_channel("Spiel beendet")
connection.send_channel("Mathrun beendet")
self.players = {}
self.solutionForGame = 0
self.type = 0
@@ -73,4 +79,5 @@ class MathRunObserver(PrivMsgObserverPrototype):
def stop_Timer(self, data, connection):
sleep(120)
if self.running:
self.stop_math(data, connection)

View File

@@ -8,11 +8,11 @@ from time import sleep
class WordRunObserver(PrivMsgObserverPrototype):
@staticmethod
def cmd():
return ['.a', '.add', '.begin', '.end']
return ['.a', '.add', '.begin', '.end', '.wordrun']
@staticmethod
def help():
return 'wordrun game'
return 'Wordrun Spiel. Starten mit ".begin <Silbe>" oder ".end <Silbe>". Antwort hinzufügen mit ".a <Antwort>" oder ".add <Antwort>" Mehr Details mit ".wordrun" abfragen.'
def __init__(self):
super().__init__()
@@ -26,12 +26,14 @@ class WordRunObserver(PrivMsgObserverPrototype):
self.syllable = ""
def update_on_priv_msg(self, data, connection: Connection):
if data['message'].find('.a ') != -1 or data['message'].find('.add ') != -1:
if data['message'].startswith('.a ') or data['message'].startswith('.add '):
self.add(data, connection)
if data['message'].find('.begin ') != -1:
if data['message'].startswith('.begin '):
self.begin_word(data, connection)
if data['message'].find('.end ') != -1:
if data['message'].startswith('.end '):
self.end_word(data, connection)
if data['message'].startswith('.wordrun'):
self.rules(data, connection)
def add(self, data, connection):
if self.gamestatus == 0:
@@ -95,3 +97,15 @@ class WordRunObserver(PrivMsgObserverPrototype):
connection.send_channel(s)
self.gamestatus = 0
self.player = {}
def rules(self, data, connection):
if data['channel'] == connection.details.get_channel():
connection.send_back("Spielregeln bitte im Query (Privatchat) mit dem Bot abfragen", data)
return
connection.send_back('Wordrun Spiel: So viele Wörter wie möglich finden, die mit der vorgegebenen Silbe anfangen oder aufhören.', data)
connection.send_back('Spiel starten mit ".begin <Silbe>", um ein Spiel zu starten, bei dem die Antworten mit <Silbe> anfangen müssen.', data)
connection.send_back('Spiel starten mit ".end <Silbe>", um ein Spiel zu starten, bei dem die Antworten mit <Silbe> enden müssen.', data)
connection.send_back('Antwort hinzufügen mit ".a <Antwort>" oder ".add <Antwort>"', data)
connection.send_back('Es können auch mehrere Antworten in einer Zeile angegeben werden', data)
connection.send_back('Das Spiel dauert 3 Minuten. Für jede gültige Antwort gibt es 1 Punkt.', data)