7
7
from .. import utils
8
8
from ..omq import send_mule
9
9
from .user import User
10
+ from .bot import Bot
10
11
from .room import Room
11
12
from .message import Message
12
13
from .filter import SimpleFilter
13
14
from .exc import InvalidData
15
+ import heapq
14
16
15
- from typing import Optional , List , Union
17
+ from dataclasses import dataclass , field
18
+ from typing import Optional , List , Union , Any
16
19
import time
17
20
18
21
"""
23
26
- what does the bot do with the message they tried to send?
24
27
- can store locally
25
28
- user sends reply
26
- - bot inserts it into room
27
- """
29
+ - bot inserts it into room (?)
28
30
29
- """
30
31
Control Flow:
31
32
1) message comes in HTTP request
32
33
2) unpacked/parsed/verified/permissions checked
38
39
"""
39
40
40
41
41
- class BotHandler :
42
+ @dataclass (order = True )
43
+ class PriorityTuple (tuple ):
44
+ priority : int
45
+ item : Any = field (compare = False )
46
+
47
+
48
+ # Simple "priority queue" of bots implemented using a dict with heap
49
+ # invariance maintained by qheap algorithm
50
+ # TODO: when bots are designed basically, add methods for polling them
51
+ # and receiving their judgments
52
+ class BotQueue :
53
+ def __init__ (self ) -> None :
54
+ self .queue = {}
55
+
56
+ def _qsize (self ) -> int :
57
+ return len (self .queue .keys ())
58
+
59
+ def _empty (self ) -> bool :
60
+ return not self ._qsize ()
61
+
62
+ def _peek (self , priority : int ):
63
+ return self .queue .get (priority )
64
+
65
+ def _put (self , item : PriorityTuple ):
66
+ temp = list (self .queue .items ())
67
+ heapq .heappush (temp , item )
68
+ self .queue = dict (temp )
69
+
70
+ def _get (self ):
71
+ return heapq .heappop (self .queue )
72
+
73
+
74
+ class Manager :
42
75
"""
43
76
Class representing an interface that manages active bots
44
77
45
78
Object Properties:
46
- bots - list of bots attached to room
79
+ queue - BotQueue object
47
80
"""
48
81
49
82
def __init__ (
@@ -54,8 +87,27 @@ def __init__(
54
87
id : Optional [int ] = None ,
55
88
session_id : Optional [int ] = None ,
56
89
) -> None :
57
- # immutable attributes
58
90
self .id = id
91
+ self .queue = BotQueue ()
92
+
93
+ def qempty (self ):
94
+ return not self .queue ._empty ()
95
+
96
+ def add_bot (self , bot : Bot , priority : int = None ):
97
+ if not priority :
98
+ # if no priority is given, lowest priority is assigned
99
+ priority = self .qsize ()
100
+ else :
101
+ # if priority is already taken, find next lowest
102
+ while self .queue .get (priority ):
103
+ priority += 1
104
+ self .queue ._put (PriorityTuple (priority , bot ))
105
+
106
+ def remove_bot (self ):
107
+ do_something = 3
108
+
109
+ def peek (self , priority : int ):
110
+ return self .queue ._peek (priority )
59
111
60
112
def check_permission_for (
61
113
self ,
@@ -236,5 +288,13 @@ def receive_message(
236
288
if whisper_to :
237
289
msg ['whisper_to' ] = whisper_to .session_id
238
290
291
+ if room ._bot_status ():
292
+ add_bot_logic = 3
293
+ """
294
+ TODO: add logic for bots receiving message and doing
295
+ bot things. The bots should be queried in terms of
296
+ priority,
297
+ """
298
+
239
299
send_mule ("message_posted" , msg ["id" ])
240
300
return msg
0 commit comments