-
Notifications
You must be signed in to change notification settings - Fork 1
/
plushiecore.py
88 lines (77 loc) · 2.89 KB
/
plushiecore.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
86
87
88
import sqlite3
import time
import queue
from message import Message
from pluginmanager import PluginManager
def run_plushie(config, outputs, inputs):
try:
blacklist = config['plugin_blacklist']
except:
blacklist = []
# TODO: Make PluginManager use a different approach for sending messages
pm = PluginManager(config, outputs)
pm.load_plugins(blacklist)
db = sqlite3.connect("chat.db")
db.execute("""
CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
speaker TEXT NOT NULL,
whisper INTEGER NOT NULL, -- -1 = whisper from plushie, 0 = whisper to plushie, 1 = normal message
message TEXT NOT NULL,
time TEXT NOT NULL
)""")
db.execute("""
CREATE TABLE IF NOT EXISTS flood (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player TEXT NOT NULL,
threatIndex INTEGER NOT NULL,
time TEXT NOT NULL
)""")
db.execute("""
CREATE VIEW IF NOT EXISTS "speakers" AS
SELECT speaker, COUNT(LOWER(speaker))
AS lines FROM history WHERE whisper = 1
GROUP BY LOWER(speaker)
""")
db.commit()
current_time = time.time()
previous_time = current_time
tick_time = 4 # seconds between 'ticks'
while True:
current_time = time.time()
timediff = current_time - previous_time
if timediff >= tick_time:
previous_time = current_time
pm.signal_tick()
try:
# Wait for a message for tick_time - time elapsed (which *should* always be positive, but just in case
# clamp to 0)
message = inputs.get(True, max(tick_time - timediff, 0))
except queue.Empty:
# If the queue is empty, that means it's time to recalcualte the timediff so loop again
continue
if message[0] == "system":
if message[1] == "stop":
break
else:
print("Unknown message: " + message[1])
continue
chat_message = Message(message[1])
# If the message is an actual message and not an action/system message
if chat_message.type:
print("Player: {:s}, ({:d}) Message: {:s}".format(chat_message.player,
chat_message.whisper,
chat_message.msg))
# Store the line in the chat history
db.execute("""
INSERT INTO history
(speaker, whisper, message, time)
VALUES
(?, ?, ?, datetime('now'))
""", (chat_message.player,
chat_message.whisper,
chat_message.msg))
db.commit()
pm.signal_message(chat_message)
else:
print(chat_message.raw)