11from construct import Container
2+ from twisted .internet .task import LoopingCall
23from twisted .words .ewords import AlreadyLoggedIn
34from base_plugin import BasePlugin
4- from manager import PlayerManager , Banned
5+ from manager import PlayerManager , Banned , Player
56from packets import client_connect , connect_response
67import packets
78from utility_functions import build_packet , Planet
@@ -13,6 +14,18 @@ class PlayerManagerPlugin(BasePlugin):
1314 def activate (self ):
1415 super (PlayerManagerPlugin , self ).activate ()
1516 self .player_manager = PlayerManager (self .config )
17+ self .l_call = LoopingCall (self .check_logged_in )
18+ self .factory = None
19+
20+ def check_logged_in (self ):
21+ for player in self .player_manager .session .query (Player ).filter_by (logged_in = True ).all ():
22+ if player .protocol not in self .factory .protocols .keys ():
23+ player .logged_in = False
24+ def on_protocol_version (self , data ):
25+ if not self .l_call .running :
26+ self .factory = self .protocol .factory
27+ self .l_call .start (.25 )
28+ return True
1629
1730 def on_client_connect (self , data ):
1831 client_data = client_connect ().parse (data .data )
@@ -23,9 +36,23 @@ def on_client_connect(self, data):
2336 ip = self .protocol .transport .getHost ().host ,
2437 protocol = self .protocol .id )
2538 return True
26- except (AlreadyLoggedIn , Banned ):
39+ except AlreadyLoggedIn :
40+ dc_packet = build_packet (
41+ packets .Packets .CONNECT_RESPONSE ,
42+ packets .connect_response ().build (
43+ Container (
44+ success = False ,
45+ client_id = 0 ,
46+ reject_reason = "You are already connected!"
47+ )
48+ )
49+ )
50+ self .protocol .transport .write (dc_packet )
51+ self .protocol .transport .loseConnection ()
52+ self .logger .info ("Already logged in user tried to log in." )
53+ except Banned :
2754 ban_packet = build_packet (
28- packets .Packets .CLIENT_DISCONNECT ,
55+ packets .Packets .CONNECT_RESPONSE ,
2956 packets .connect_response ().build (
3057 Container (
3158 success = False ,
0 commit comments