| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | from __future__ import annotations | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | import functools | 
					
						
							|  |  |  | import websockets | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | import asyncio | 
					
						
							|  |  |  | import socket | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | import random | 
					
						
							| 
									
										
										
										
											2021-04-04 03:18:19 +02:00
										 |  |  | import pickle | 
					
						
							| 
									
										
										
										
											2022-06-08 00:35:35 +02:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 15:35:43 +01:00
										 |  |  | import Utils | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  | from .models import db_session, Room, select, commit, Command, db | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | from MultiServer import Context, server, auto_shutdown, ServerCommandProcessor, ClientMessageProcessor | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  | from Utils import get_public_ipv4, get_public_ipv6, restricted_loads, cache_argsless | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | class CustomClientMessageProcessor(ClientMessageProcessor): | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |     ctx: WebHostContext | 
					
						
							| 
									
										
										
										
											2021-12-03 07:01:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  |     def _cmd_video(self, platform, user): | 
					
						
							| 
									
										
										
										
											2020-07-11 16:59:37 +02:00
										 |  |  |         """Set a link for your name in the WebHostLib tracker pointing to a video stream""" | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  |         if platform.lower().startswith("t"):  # twitch | 
					
						
							|  |  |  |             self.ctx.video[self.client.team, self.client.slot] = "Twitch", user | 
					
						
							|  |  |  |             self.ctx.save() | 
					
						
							|  |  |  |             self.output(f"Registered Twitch Stream https://www.twitch.tv/{user}") | 
					
						
							| 
									
										
										
										
											2020-06-27 14:16:51 +02:00
										 |  |  |             return True | 
					
						
							| 
									
										
										
										
											2021-04-10 18:45:11 +02:00
										 |  |  |         elif platform.lower().startswith("y"):  # youtube | 
					
						
							| 
									
										
										
										
											2020-07-25 22:40:24 +02:00
										 |  |  |             self.ctx.video[self.client.team, self.client.slot] = "Youtube", user | 
					
						
							|  |  |  |             self.ctx.save() | 
					
						
							|  |  |  |             self.output(f"Registered Youtube Stream for {user}") | 
					
						
							|  |  |  |             return True | 
					
						
							| 
									
										
										
										
											2020-06-27 14:16:51 +02:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # inject | 
					
						
							|  |  |  | import MultiServer | 
					
						
							| 
									
										
										
										
											2021-12-03 07:01:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | MultiServer.client_message_processor = CustomClientMessageProcessor | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  | del MultiServer | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | class DBCommandProcessor(ServerCommandProcessor): | 
					
						
							|  |  |  |     def output(self, text: str): | 
					
						
							|  |  |  |         logging.info(text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WebHostContext(Context): | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  |     def __init__(self, static_server_data: dict): | 
					
						
							|  |  |  |         # static server data is used during _load_game_data to load required data, | 
					
						
							|  |  |  |         # without needing to import worlds system, which takes quite a bit of memory | 
					
						
							|  |  |  |         self.static_server_data = static_server_data | 
					
						
							| 
									
										
										
										
											2021-10-18 22:58:29 +02:00
										 |  |  |         super(WebHostContext, self).__init__("", 0, "", "", 1, 40, True, "enabled", "enabled", "enabled", 0, 2) | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  |         del self.static_server_data | 
					
						
							| 
									
										
										
										
											2020-06-22 01:04:12 +02:00
										 |  |  |         self.main_loop = asyncio.get_running_loop() | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  |         self.video = {} | 
					
						
							| 
									
										
										
										
											2020-10-18 23:07:48 +02:00
										 |  |  |         self.tags = ["AP", "WebHost"] | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  |     def _load_game_data(self): | 
					
						
							|  |  |  |         for key, value in self.static_server_data.items(): | 
					
						
							|  |  |  |             setattr(self, key, value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     def listen_to_db_commands(self): | 
					
						
							|  |  |  |         cmdprocessor = DBCommandProcessor(self) | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 04:06:30 +01:00
										 |  |  |         while not self.exit_event.is_set(): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |             with db_session: | 
					
						
							|  |  |  |                 commands = select(command for command in Command if command.room.id == self.room_id) | 
					
						
							|  |  |  |                 if commands: | 
					
						
							|  |  |  |                     for command in commands: | 
					
						
							| 
									
										
										
										
											2020-06-22 01:04:12 +02:00
										 |  |  |                         self.main_loop.call_soon_threadsafe(cmdprocessor, command.commandtext) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |                         command.delete() | 
					
						
							|  |  |  |                     commit() | 
					
						
							|  |  |  |             time.sleep(5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @db_session | 
					
						
							|  |  |  |     def load(self, room_id: int): | 
					
						
							|  |  |  |         self.room_id = room_id | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  |         room = Room.get(id=room_id) | 
					
						
							|  |  |  |         if room.last_port: | 
					
						
							|  |  |  |             self.port = room.last_port | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.port = get_random_port() | 
					
						
							| 
									
										
										
										
											2020-10-26 00:04:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 17:18:48 +01:00
										 |  |  |         return self._load(self.decompress(room.seed.multidata), True) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @db_session | 
					
						
							|  |  |  |     def init_save(self, enabled: bool = True): | 
					
						
							|  |  |  |         self.saving = enabled | 
					
						
							|  |  |  |         if self.saving: | 
					
						
							| 
									
										
										
										
											2021-04-10 15:26:30 +02:00
										 |  |  |             savegame_data = Room.get(id=self.room_id).multisave | 
					
						
							|  |  |  |             if savegame_data: | 
					
						
							|  |  |  |                 self.set_save(restricted_loads(Room.get(id=self.room_id).multisave)) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |             self._start_async_saving() | 
					
						
							|  |  |  |         threading.Thread(target=self.listen_to_db_commands, daemon=True).start() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @db_session | 
					
						
							| 
									
										
										
										
											2021-12-03 07:01:21 +01:00
										 |  |  |     def _save(self, exit_save: bool = False) -> bool: | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |         room = Room.get(id=self.room_id) | 
					
						
							| 
									
										
										
										
											2021-04-04 03:18:19 +02:00
										 |  |  |         room.multisave = pickle.dumps(self.get_save()) | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |         # saving only occurs on activity, so we can "abuse" this information to mark this as last_activity | 
					
						
							| 
									
										
										
										
											2021-12-03 07:01:21 +01:00
										 |  |  |         if not exit_save:  # we don't want to count a shutdown as activity, which would restart the server again | 
					
						
							| 
									
										
										
										
											2020-07-20 14:17:05 +02:00
										 |  |  |             room.last_activity = datetime.utcnow() | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  |     def get_save(self) -> dict: | 
					
						
							|  |  |  |         d = super(WebHostContext, self).get_save() | 
					
						
							|  |  |  |         d["video"] = [(tuple(playerslot), videodata) for playerslot, videodata in self.video.items()] | 
					
						
							|  |  |  |         return d | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 07:01:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | def get_random_port(): | 
					
						
							|  |  |  |     return random.randint(49152, 65535) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 03:11:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  | @cache_argsless | 
					
						
							|  |  |  | def get_static_server_data() -> dict: | 
					
						
							|  |  |  |     import worlds | 
					
						
							|  |  |  |     data = { | 
					
						
							|  |  |  |         "forced_auto_forfeits": {}, | 
					
						
							|  |  |  |         "non_hintable_names": {}, | 
					
						
							|  |  |  |         "gamespackage": worlds.network_data_package["games"], | 
					
						
							|  |  |  |         "item_name_groups": {world_name: world.item_name_groups for world_name, world in | 
					
						
							|  |  |  |                              worlds.AutoWorldRegister.world_types.items()}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for world_name, world in worlds.AutoWorldRegister.world_types.items(): | 
					
						
							|  |  |  |         data["forced_auto_forfeits"][world_name] = world.forced_auto_forfeit | 
					
						
							|  |  |  |         data["non_hintable_names"][world_name] = world.hint_blacklist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def run_server_process(room_id, ponyconfig: dict, static_server_data: dict): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     # establish DB connection for multidata and multisave | 
					
						
							|  |  |  |     db.bind(**ponyconfig) | 
					
						
							|  |  |  |     db.generate_mapping(check_tables=False) | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async def main(): | 
					
						
							| 
									
										
										
										
											2021-11-10 15:35:43 +01:00
										 |  |  |         Utils.init_logging(str(room_id), write_mode="a") | 
					
						
							| 
									
										
										
										
											2022-08-07 18:28:50 +02:00
										 |  |  |         ctx = WebHostContext(static_server_data) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |         ctx.load(room_id) | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  |         ctx.init_save() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             ctx.server = websockets.serve(functools.partial(server, ctx=ctx), ctx.host, ctx.port, ping_timeout=None, | 
					
						
							|  |  |  |                                           ping_interval=None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await ctx.server | 
					
						
							|  |  |  |         except Exception:  # likely port in use - in windows this is OSError, but I didn't check the others | 
					
						
							|  |  |  |             ctx.server = websockets.serve(functools.partial(server, ctx=ctx), ctx.host, 0, ping_timeout=None, | 
					
						
							|  |  |  |                                           ping_interval=None) | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  |             await ctx.server | 
					
						
							| 
									
										
										
										
											2022-06-08 00:35:35 +02:00
										 |  |  |         port = 0 | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  |         for wssocket in ctx.server.ws_server.sockets: | 
					
						
							|  |  |  |             socketname = wssocket.getsockname() | 
					
						
							|  |  |  |             if wssocket.family == socket.AF_INET6: | 
					
						
							|  |  |  |                 logging.info(f'Hosting game at [{get_public_ipv6()}]:{socketname[1]}') | 
					
						
							| 
									
										
										
										
											2022-06-08 00:35:35 +02:00
										 |  |  |                 # Prefer IPv4, as most users seem to not have working ipv6 support | 
					
						
							|  |  |  |                 if not port: | 
					
						
							|  |  |  |                     port = socketname[1] | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  |             elif wssocket.family == socket.AF_INET: | 
					
						
							|  |  |  |                 logging.info(f'Hosting game at {get_public_ipv4()}:{socketname[1]}') | 
					
						
							| 
									
										
										
										
											2022-06-08 00:35:35 +02:00
										 |  |  |                 port = socketname[1] | 
					
						
							|  |  |  |         if port: | 
					
						
							|  |  |  |             with db_session: | 
					
						
							|  |  |  |                 room = Room.get(id=ctx.room_id) | 
					
						
							|  |  |  |                 room.last_port = port | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |         with db_session: | 
					
						
							|  |  |  |             ctx.auto_shutdown = Room.get(id=room_id).timeout | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  |         ctx.shutdown_task = asyncio.create_task(auto_shutdown(ctx, [])) | 
					
						
							|  |  |  |         await ctx.shutdown_task | 
					
						
							|  |  |  |         logging.info("Shutting down") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 03:11:48 +02:00
										 |  |  |     from .autolauncher import Locker | 
					
						
							|  |  |  |     with Locker(room_id): | 
					
						
							|  |  |  |         asyncio.run(main()) |