Made Enten permanENTEn

This commit is contained in:
BaerbelBox
2022-06-17 10:06:34 +02:00
parent c22b1a10cb
commit 6621b5faf7
2 changed files with 77 additions and 9 deletions
+36
View File
@@ -0,0 +1,36 @@
import sqlite3
class DucksProvider(object):
_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS ducks (id INTEGER PRIMARY KEY, \
user TEXT, friends INTEGER, dead INTEGER)'
_GET_DUCKS = 'SELECT id, friends, dead FROM ducks WHERE user = ?'
_SAVE_OR_OVERWRITE = 'REPLACE INTO ducks (id, user, friends, dead) VALUES (?, ?, ?,?)'
_DELETE_DUCKS = 'DELETE FROM ducks WHERE user = ?'
def __init__(self):
self._database_connection = sqlite3.connect('faust_bot.db')
cursor = self._database_connection.cursor()
cursor.execute(DucksProvider._CREATE_TABLE)
self._database_connection.commit()
def get_ducks(self, user: str):
cursor = self._database_connection.cursor()
cursor.execute(DucksProvider._GET_DUCKS, (user.lower(),))
return cursor.fetchone()
def save_or_replace(self, user: str, friends: int, dead: int):
existing = self.get_ducks(user)
_id = existing[0] if existing is not None else None
data = (_id, user.lower(), friends, dead)
cursor = self._database_connection.cursor()
cursor.execute(DucksProvider._SAVE_OR_OVERWRITE, data)
self._database_connection.commit()
def delete_score(self, user: str):
cursor = self._database_connection.cursor()
cursor.execute(DucksProvider._DELETE_DUCKS, (user.lower(),))
self._database_connection.commit()
def __exit__(self):
self._database_connection.close()
+41 -9
View File
@@ -3,7 +3,7 @@ from FaustBot.Communication.Connection import Connection
from FaustBot.Modules.PrivMsgObserverPrototype import PrivMsgObserverPrototype from FaustBot.Modules.PrivMsgObserverPrototype import PrivMsgObserverPrototype
from FaustBot.Modules.PingObserverPrototype import PingObserverPrototype from FaustBot.Modules.PingObserverPrototype import PingObserverPrototype
from random import randint from random import randint
from collections import defaultdict from FaustBot.Model.DuckProvider import DucksProvider
class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype): class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
@staticmethod @staticmethod
@@ -22,8 +22,6 @@ class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
super().__init__() super().__init__()
self.active = 0 self.active = 0
self.duck_alive = 0 self.duck_alive = 0
self.ducks_hunt = defaultdict(int)
self.ducks_befriend = defaultdict(int)
def update_on_priv_msg(self, data, connection: Connection): def update_on_priv_msg(self, data, connection: Connection):
if data['message'].find('.starthunt') != -1: if data['message'].find('.starthunt') != -1:
@@ -43,7 +41,7 @@ class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
connection.send_channel("Jagd beendet") connection.send_channel("Jagd beendet")
return return
if data['message'].find('.ducks') != -1: if data['message'].find('.ducks') != -1:
connection.send_channel(data['nick'] + " hat schon " + str(self.ducks_befriend[data['nick']]) + " befreundete Enten und " + str(self.ducks_hunt[data['nick']]) + " getötete Enten.") connection.send_channel(self.build_duck_string(data['nick']))
if data['message'].find('.freunde') != -1: if data['message'].find('.freunde') != -1:
self.befriend(data, connection) self.befriend(data, connection)
if data['message'].find('.schiessen') != -1: if data['message'].find('.schiessen') != -1:
@@ -55,8 +53,8 @@ class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
connection.send_channel(data['nick'] + " probiert eine Ente zu befreunden aber sie will nicht.") connection.send_channel(data['nick'] + " probiert eine Ente zu befreunden aber sie will nicht.")
else: else:
self.duck_alive = 0 self.duck_alive = 0
self.ducks_befriend[data['nick']] += 1 self.addLivingDuck(data['nick'])
connection.send_channel(data['nick'] + " hat schon " + str(self.ducks_befriend[data['nick']]) + " befreundete Enten und " + str(self.ducks_hunt[data['nick']]) + " getötete Enten.") connection.send_channel(self.build_duck_string(data['nick']))
return return
if (self.duck_alive == 0 and self.active == 1): if (self.duck_alive == 0 and self.active == 1):
connection.send_channel(data['nick']+ " probiert eine nicht existente Ente zu befreunden.") connection.send_channel(data['nick']+ " probiert eine nicht existente Ente zu befreunden.")
@@ -68,8 +66,8 @@ class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
connection.send_channel(data['nick'] + " trifft daneben.") connection.send_channel(data['nick'] + " trifft daneben.")
else: else:
self.duck_alive = 0 self.duck_alive = 0
self.ducks_hunt[data['nick']] += 1 self.addDeadDuck(data['nick'])
connection.send_channel(data['nick'] + " hat schon " + str(self.ducks_befriend[data['nick']]) + " befreundete Enten und " + str(self.ducks_hunt[data['nick']]) + " getötete Enten.") connection.send_channel(self.build_duck_string(data['nick']))
return return
if (self.duck_alive == 0 and self.active == 1): if (self.duck_alive == 0 and self.active == 1):
connection.send_channel(data['nick']+ " schießt ins Nichts.") connection.send_channel(data['nick']+ " schießt ins Nichts.")
@@ -79,10 +77,44 @@ class DuckObserver(PrivMsgObserverPrototype, PingObserverPrototype):
def update_on_ping(self, data, connection: Connection): def update_on_ping(self, data, connection: Connection):
if self.active == 0: if self.active == 0:
return return
if 1 == randint(1,11): if 1 == randint(1,1):
if self.duck_alive == 0: if self.duck_alive == 0:
connection.send_channel("*. *. *. * <<w°)> *. *. * Quack!") connection.send_channel("*. *. *. * <<w°)> *. *. * Quack!")
self.duck_alive = 1 self.duck_alive = 1
def _is_idented_mod(self, data: dict, connection: Connection): def _is_idented_mod(self, data: dict, connection: Connection):
return data['nick'] in self._config.mods and connection.is_idented(data['nick']) return data['nick'] in self._config.mods and connection.is_idented(data['nick'])
def getLiving(self, nick: str):
duck_provider = DucksProvider()
duck = duck_provider.get_ducks(nick)
if duck is not None:
return duck[1]
else:
return 0
def getDead(self, nick: str):
duck_provider = DucksProvider()
duck = duck_provider.get_ducks(nick)
if duck is not None:
return duck[2]
else:
return 0
def addDeadDuck(self, nick:str):
self.writeDucks(nick, self.getLiving(nick), self.getDead(nick)+1)
def addLivingDuck(self,nick:str):
self.writeDucks(nick, self.getLiving(nick)+1, self.getDead(nick))
def writeDucks(self, nick: str, living: int, dead: int):
ducks_provider = DucksProvider()
ducks_provider.save_or_replace(nick, living, dead)
def build_duck_string(self, nick: str):
return nick + " hat schon " +str(self.getLiving(nick))+ " befreundete "+self.pluralEnte(self.getLiving(nick))+" und " + str(self.getDead(nick)) + " getötete "+self.pluralEnte(self.getDead(nick))
def pluralEnte(self, enten:int):
if enten == 1:
return "Ente"
return "Enten"