11import enum
22import json
33import asyncio
4+ import logging
45import websockets
56from typing import Union , Any , Dict , Callable , Coroutine
67from typing import TYPE_CHECKING
910if TYPE_CHECKING :
1011 from .atserver import AternosServer
1112
12- class Streams (enum .IntEnum ):
13- status = 0
14- queue = 1
15- console = 2
16- ram = 3
17- tps = 4
13+ class Streams (enum .Enum ):
14+
15+ status = (0 ,None )
16+ queue = (1 ,None )
17+ console = (2 ,'console' )
18+ ram = (3 ,'heap' )
19+ tps = (4 ,'tick' )
20+
21+ def __init__ (self , num :int , stream :str ):
22+ self .num = num
23+ self .stream = stream
1824
1925class AternosWss :
2026
@@ -23,7 +29,7 @@ def __init__(self, atserv:'AternosServer', autoconfirm:bool=False) -> None:
2329 self .atserv = atserv
2430 self .cookies = atserv .atconn .session .cookies
2531 self .session = self .cookies ['ATERNOS_SESSION' ]
26- self .servid = self . cookies [ 'ATERNOS_SERVER' ]
32+ self .servid = atserv . servid
2733 self .recv = {}
2834 self .autoconfirm = autoconfirm
2935 self .confirmed = False
@@ -32,7 +38,7 @@ async def confirm(self) -> None:
3238
3339 self .atserv .confirm ()
3440
35- def wssreceiver (self , stream :int ) -> Callable [[Callable [[Any ],Coroutine [Any ,Any ,None ]]],Any ]:
41+ def wssreceiver (self , stream :Streams ) -> Callable [[Callable [[Any ],Coroutine [Any ,Any ,None ]]],Any ]:
3642 def decorator (func :Callable [[Any ],Coroutine [Any ,Any ,None ]]) -> None :
3743 self .recv [stream ] = func
3844 return decorator
@@ -53,6 +59,30 @@ async def connect(self) -> None:
5359 origin = 'https://aternos.org' ,
5460 extra_headers = headers
5561 )
62+
63+ @self .wssreceiver (Streams .status )
64+ async def confirmfunc (msg ):
65+ # Autoconfirm
66+ if not self .autoconfirm :
67+ return
68+ if msg ['class' ] == 'queueing' \
69+ and msg ['queue' ]['pending' ] == 'pending' \
70+ and not self .confirmed :
71+ self .confirm ()
72+
73+ @self .wssreceiver (Streams .status )
74+ async def streamsfunc (msg ):
75+ if msg ['status' ] == 2 :
76+ # Automatically start streams
77+ for strm in self .recv :
78+ if not isinstance (strm ,Streams ):
79+ continue
80+ if strm .stream :
81+ logging .debug (f'Enabling { strm .stream } stream' )
82+ await self .send ({
83+ 'stream' : strm .stream ,
84+ 'type' : 'start'
85+ })
5686
5787 await self .wssworker ()
5888
@@ -66,7 +96,7 @@ async def send(self, obj:Union[Dict[str, Any],str]) -> None:
6696 if isinstance (obj , dict ):
6797 obj = json .dumps (obj )
6898
69- self .socket .send (obj )
99+ await self .socket .send (obj )
70100
71101 async def wssworker (self ) -> None :
72102
@@ -86,6 +116,7 @@ async def receiver(self) -> None:
86116 while True :
87117 data = await self .socket .recv ()
88118 obj = json .loads (data )
119+ msgtype = - 1
89120
90121 if obj ['type' ] == 'line' :
91122 msgtype = Streams .console
@@ -104,16 +135,6 @@ async def receiver(self) -> None:
104135 msgtype = Streams .status
105136 msg = json .loads (obj ['message' ])
106137
107- if not self .autoconfirm :
108- continue
109- if msg ['class' ] == 'queueing' \
110- and msg ['queue' ]['pending' ] == 'pending' \
111- and not self .confirmed :
112- t = asyncio .create_task (
113- self .confirm ()
114- )
115- await t
116-
117138 if msgtype in self .recv :
118139 t = asyncio .create_task (
119140 self .recv [msgtype ](msg )
0 commit comments