slay.py
1import time 2from typing import Callable, Collection 3 4from websocket import WebSocketException 5 6import slay.data.info as Info 7from slay.server.socket import Socket 8from slay.server.connection import Connection 9from slay.server.event import CallbackRegistrar, CallbackDict 10 11from slay.utils import export 12 13@export 14class Connections: 15 def __setattr__(self, name: str, value): 16 if isinstance(value, CallbackRegistrar): 17 value.name = name 18 value.connection_obj = self 19 20 if name.startswith("c_"): 21 for connection in self.list: 22 connection.__setattr__(name, value) 23 24 return 25 26 if name.startswith("on_"): 27 try: 28 old_value: CallbackRegistrar | any = self.__getattribute__(name) 29 except: 30 super().__setattr__(name, value) 31 return 32 33 if ( 34 isinstance(value, Callable) 35 and old_value and isinstance(old_value, CallbackRegistrar) 36 ): 37 old_value(cover=True)(value) 38 return 39 40 super().__setattr__(name, value) 41 42 def __init__( 43 self, sockets: Collection[Socket], 44 category: str = "Connections", 45 event_callback_dict: CallbackDict = None, 46 enable_replay_cache: bool = False 47 ): 48 self.sockets = sockets 49 self.category = category 50 51 self.list: list[Connection] = [] 52 53 if len(sockets) == 0: 54 raise ValueError("Length of sockets arguement cannot be zero.") 55 56 for socket in sockets: 57 self.list.append(Connection(socket, category, enable_replay_cache=enable_replay_cache)) 58 59 if event_callback_dict: 60 self.set_event_callback_dict(event_callback_dict) 61 62 self.keep_loop_alive: bool = False 63 64 # Callback Registrars 65 66 self.on_open = CallbackRegistrar() 67 self.on_message = CallbackRegistrar[str]() 68 self.on_error = CallbackRegistrar[WebSocketException]() 69 self.on_close = CallbackRegistrar[int, str]() 70 """ args: code, message. """ 71 self.event = CallbackRegistrar() 72 73 if Socket.SOCIAL in sockets: 74 self.on_global_chat_history = CallbackRegistrar[ 75 Info.GlobalChatHistoryDict 76 ]() 77 78 self.on_id = CallbackRegistrar[int]() 79 self.on_game_list = CallbackRegistrar[list[Info.GameProfile]]() 80 self.on_game_init = CallbackRegistrar[Info.GameInitial]() 81 self.on_player_join = CallbackRegistrar[Info.NewPlayer]() 82 self.on_player_leave = CallbackRegistrar[Info.InGameId]() 83 self.on_game_stats = CallbackRegistrar[Info.GameStats]() 84 self.on_ranked_search_count = CallbackRegistrar[Info.RankedSearchCount]() 85 self.on_account_logging = CallbackRegistrar[Info.AccountLogging]() 86 self.on_me_join = CallbackRegistrar[Info.InGameId]() 87 self.on_hp_update = CallbackRegistrar[Info.HP]() 88 """ This is still in experimental phase. """ 89 self.on_player_respawn = CallbackRegistrar[Info.PlayerRespawn]() 90 self.on_ability_cancel = CallbackRegistrar[Info.AbilityCancel]() 91 self.on_in_game_chat = CallbackRegistrar[Info.InGameChat]() 92 self.on_server_message = CallbackRegistrar[Info.ServerMessage]() 93 self.on_clan_info = CallbackRegistrar[Info.Clan]() 94 self.on_clan_member_list = CallbackRegistrar[list[Info.ClanMember]]() 95 96 def set_event_callback_dict(self, callback_dict: CallbackDict): 97 """ Each event callback can be a single callable object 98 or a list of callable object 99 """ 100 101 if isinstance(callback_dict, dict): 102 for connection in self.list: 103 connection.event_callback_dict = callback_dict 104 105 def start( 106 self, 107 non_blocking: bool = False, 108 loop_function: Callable = None, 109 end_function: Callable = None, 110 sleep_time: int = 10, 111 reopen_attempts: int = 0, 112 reopen_interval: int = 60 113 ): 114 for connection in self.list: 115 connection.start(True, reopen_attempts, reopen_interval) 116 117 if non_blocking: 118 return 119 120 self.keep_loop_alive = True 121 122 try: 123 while self.keep_loop_alive: 124 if loop_function: 125 loop_function() 126 127 time.sleep(sleep_time) 128 129 except (KeyboardInterrupt, SystemExit): 130 self.keep_loop_alive = False 131 132 finally: 133 for connection in self.list: 134 connection.close() 135 136 if end_function: 137 end_function() 138 139 def close(self): 140 if not self.keep_loop_alive: 141 for connection in self.list: 142 connection.close() 143 144 return 145 146 self.keep_loop_alive = False
@export
class
Connections:
14@export 15class Connections: 16 def __setattr__(self, name: str, value): 17 if isinstance(value, CallbackRegistrar): 18 value.name = name 19 value.connection_obj = self 20 21 if name.startswith("c_"): 22 for connection in self.list: 23 connection.__setattr__(name, value) 24 25 return 26 27 if name.startswith("on_"): 28 try: 29 old_value: CallbackRegistrar | any = self.__getattribute__(name) 30 except: 31 super().__setattr__(name, value) 32 return 33 34 if ( 35 isinstance(value, Callable) 36 and old_value and isinstance(old_value, CallbackRegistrar) 37 ): 38 old_value(cover=True)(value) 39 return 40 41 super().__setattr__(name, value) 42 43 def __init__( 44 self, sockets: Collection[Socket], 45 category: str = "Connections", 46 event_callback_dict: CallbackDict = None, 47 enable_replay_cache: bool = False 48 ): 49 self.sockets = sockets 50 self.category = category 51 52 self.list: list[Connection] = [] 53 54 if len(sockets) == 0: 55 raise ValueError("Length of sockets arguement cannot be zero.") 56 57 for socket in sockets: 58 self.list.append(Connection(socket, category, enable_replay_cache=enable_replay_cache)) 59 60 if event_callback_dict: 61 self.set_event_callback_dict(event_callback_dict) 62 63 self.keep_loop_alive: bool = False 64 65 # Callback Registrars 66 67 self.on_open = CallbackRegistrar() 68 self.on_message = CallbackRegistrar[str]() 69 self.on_error = CallbackRegistrar[WebSocketException]() 70 self.on_close = CallbackRegistrar[int, str]() 71 """ args: code, message. """ 72 self.event = CallbackRegistrar() 73 74 if Socket.SOCIAL in sockets: 75 self.on_global_chat_history = CallbackRegistrar[ 76 Info.GlobalChatHistoryDict 77 ]() 78 79 self.on_id = CallbackRegistrar[int]() 80 self.on_game_list = CallbackRegistrar[list[Info.GameProfile]]() 81 self.on_game_init = CallbackRegistrar[Info.GameInitial]() 82 self.on_player_join = CallbackRegistrar[Info.NewPlayer]() 83 self.on_player_leave = CallbackRegistrar[Info.InGameId]() 84 self.on_game_stats = CallbackRegistrar[Info.GameStats]() 85 self.on_ranked_search_count = CallbackRegistrar[Info.RankedSearchCount]() 86 self.on_account_logging = CallbackRegistrar[Info.AccountLogging]() 87 self.on_me_join = CallbackRegistrar[Info.InGameId]() 88 self.on_hp_update = CallbackRegistrar[Info.HP]() 89 """ This is still in experimental phase. """ 90 self.on_player_respawn = CallbackRegistrar[Info.PlayerRespawn]() 91 self.on_ability_cancel = CallbackRegistrar[Info.AbilityCancel]() 92 self.on_in_game_chat = CallbackRegistrar[Info.InGameChat]() 93 self.on_server_message = CallbackRegistrar[Info.ServerMessage]() 94 self.on_clan_info = CallbackRegistrar[Info.Clan]() 95 self.on_clan_member_list = CallbackRegistrar[list[Info.ClanMember]]() 96 97 def set_event_callback_dict(self, callback_dict: CallbackDict): 98 """ Each event callback can be a single callable object 99 or a list of callable object 100 """ 101 102 if isinstance(callback_dict, dict): 103 for connection in self.list: 104 connection.event_callback_dict = callback_dict 105 106 def start( 107 self, 108 non_blocking: bool = False, 109 loop_function: Callable = None, 110 end_function: Callable = None, 111 sleep_time: int = 10, 112 reopen_attempts: int = 0, 113 reopen_interval: int = 60 114 ): 115 for connection in self.list: 116 connection.start(True, reopen_attempts, reopen_interval) 117 118 if non_blocking: 119 return 120 121 self.keep_loop_alive = True 122 123 try: 124 while self.keep_loop_alive: 125 if loop_function: 126 loop_function() 127 128 time.sleep(sleep_time) 129 130 except (KeyboardInterrupt, SystemExit): 131 self.keep_loop_alive = False 132 133 finally: 134 for connection in self.list: 135 connection.close() 136 137 if end_function: 138 end_function() 139 140 def close(self): 141 if not self.keep_loop_alive: 142 for connection in self.list: 143 connection.close() 144 145 return 146 147 self.keep_loop_alive = False
Connections( sockets: Collection[slay.server.socket.Socket], category: str = 'Connections', event_callback_dict: slay.server.event.CallbackDict = None, enable_replay_cache: bool = False)
43 def __init__( 44 self, sockets: Collection[Socket], 45 category: str = "Connections", 46 event_callback_dict: CallbackDict = None, 47 enable_replay_cache: bool = False 48 ): 49 self.sockets = sockets 50 self.category = category 51 52 self.list: list[Connection] = [] 53 54 if len(sockets) == 0: 55 raise ValueError("Length of sockets arguement cannot be zero.") 56 57 for socket in sockets: 58 self.list.append(Connection(socket, category, enable_replay_cache=enable_replay_cache)) 59 60 if event_callback_dict: 61 self.set_event_callback_dict(event_callback_dict) 62 63 self.keep_loop_alive: bool = False 64 65 # Callback Registrars 66 67 self.on_open = CallbackRegistrar() 68 self.on_message = CallbackRegistrar[str]() 69 self.on_error = CallbackRegistrar[WebSocketException]() 70 self.on_close = CallbackRegistrar[int, str]() 71 """ args: code, message. """ 72 self.event = CallbackRegistrar() 73 74 if Socket.SOCIAL in sockets: 75 self.on_global_chat_history = CallbackRegistrar[ 76 Info.GlobalChatHistoryDict 77 ]() 78 79 self.on_id = CallbackRegistrar[int]() 80 self.on_game_list = CallbackRegistrar[list[Info.GameProfile]]() 81 self.on_game_init = CallbackRegistrar[Info.GameInitial]() 82 self.on_player_join = CallbackRegistrar[Info.NewPlayer]() 83 self.on_player_leave = CallbackRegistrar[Info.InGameId]() 84 self.on_game_stats = CallbackRegistrar[Info.GameStats]() 85 self.on_ranked_search_count = CallbackRegistrar[Info.RankedSearchCount]() 86 self.on_account_logging = CallbackRegistrar[Info.AccountLogging]() 87 self.on_me_join = CallbackRegistrar[Info.InGameId]() 88 self.on_hp_update = CallbackRegistrar[Info.HP]() 89 """ This is still in experimental phase. """ 90 self.on_player_respawn = CallbackRegistrar[Info.PlayerRespawn]() 91 self.on_ability_cancel = CallbackRegistrar[Info.AbilityCancel]() 92 self.on_in_game_chat = CallbackRegistrar[Info.InGameChat]() 93 self.on_server_message = CallbackRegistrar[Info.ServerMessage]() 94 self.on_clan_info = CallbackRegistrar[Info.Clan]() 95 self.on_clan_member_list = CallbackRegistrar[list[Info.ClanMember]]()
list: list[slay.server.connection.Connection]
def
set_event_callback_dict(self, callback_dict: slay.server.event.CallbackDict):
97 def set_event_callback_dict(self, callback_dict: CallbackDict): 98 """ Each event callback can be a single callable object 99 or a list of callable object 100 """ 101 102 if isinstance(callback_dict, dict): 103 for connection in self.list: 104 connection.event_callback_dict = callback_dict
Each event callback can be a single callable object or a list of callable object
def
start( self, non_blocking: bool = False, loop_function: Callable = None, end_function: Callable = None, sleep_time: int = 10, reopen_attempts: int = 0, reopen_interval: int = 60):
106 def start( 107 self, 108 non_blocking: bool = False, 109 loop_function: Callable = None, 110 end_function: Callable = None, 111 sleep_time: int = 10, 112 reopen_attempts: int = 0, 113 reopen_interval: int = 60 114 ): 115 for connection in self.list: 116 connection.start(True, reopen_attempts, reopen_interval) 117 118 if non_blocking: 119 return 120 121 self.keep_loop_alive = True 122 123 try: 124 while self.keep_loop_alive: 125 if loop_function: 126 loop_function() 127 128 time.sleep(sleep_time) 129 130 except (KeyboardInterrupt, SystemExit): 131 self.keep_loop_alive = False 132 133 finally: 134 for connection in self.list: 135 connection.close() 136 137 if end_function: 138 end_function()