| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | from __future__ import annotations | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | import functools | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | from .models import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | from MultiServer import Context, server, auto_shutdown, ServerCommandProcessor, ClientMessageProcessor | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | from Utils import get_public_ipv4, get_public_ipv6 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 13:52:03 +02:00
										 |  |  | class CustomClientMessageProcessor(ClientMessageProcessor): | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |     ctx: WebHostContext | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-07-25 22:40:24 +02:00
										 |  |  |         elif platform.lower().startswith("y"): # youtube | 
					
						
							|  |  |  |             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 | 
					
						
							|  |  |  | MultiServer.client_message_processor = CustomClientMessageProcessor | 
					
						
							|  |  |  | del (MultiServer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | class DBCommandProcessor(ServerCommandProcessor): | 
					
						
							|  |  |  |     def output(self, text: str): | 
					
						
							|  |  |  |         logging.info(text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WebHostContext(Context): | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							| 
									
										
										
										
											2020-07-16 16:57:38 +02:00
										 |  |  |         super(WebHostContext, self).__init__("", 0, "", 1, 40, True, "enabled", "enabled", 0, 2) | 
					
						
							| 
									
										
										
										
											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-07-11 17:44:52 +02:00
										 |  |  |         self.tags = ["Berserker", "WebHost"] | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |         while self.running: | 
					
						
							|  |  |  |             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() | 
					
						
							|  |  |  |         return self._load(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: | 
					
						
							| 
									
										
										
										
											2020-07-16 02:29:36 +02:00
										 |  |  |             existing_savegame = Room.get(id=self.room_id).multisave | 
					
						
							|  |  |  |             if existing_savegame: | 
					
						
							|  |  |  |                 self.set_save(existing_savegame) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-07-20 14:17:05 +02:00
										 |  |  |     def _save(self, exit_save:bool = False) -> bool: | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |         room = Room.get(id=self.room_id) | 
					
						
							|  |  |  |         room.multisave = self.get_save() | 
					
						
							|  |  |  |         # saving only occurs on activity, so we can "abuse" this information to mark this as last_activity | 
					
						
							| 
									
										
										
										
											2020-07-20 14:17:05 +02:00
										 |  |  |         if not exit_save: # we don't want to count a shutdown as activity, which would restart the server again | 
					
						
							|  |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | def get_random_port(): | 
					
						
							|  |  |  |     return random.randint(49152, 65535) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | def run_server_process(room_id, ponyconfig: dict): | 
					
						
							|  |  |  |     # 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(): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  |         logging.basicConfig(format='[%(asctime)s] %(message)s', | 
					
						
							|  |  |  |                             level=logging.INFO, | 
					
						
							| 
									
										
										
										
											2020-06-22 03:53:00 +02:00
										 |  |  |                             handlers=[ | 
					
						
							|  |  |  |                                 logging.FileHandler(os.path.join(LOGS_FOLDER, f"{room_id}.txt"), 'a', 'utf-8-sig')]) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |         ctx = WebHostContext() | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											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]}') | 
					
						
							| 
									
										
										
										
											2020-06-21 17:04:25 +02:00
										 |  |  |                 with db_session: | 
					
						
							|  |  |  |                     room = Room.get(id=ctx.room_id) | 
					
						
							|  |  |  |                     room.last_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]}') | 
					
						
							| 
									
										
										
										
											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") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     asyncio.run(main()) | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 16:59:37 +02:00
										 |  |  | from WebHostLib import LOGS_FOLDER |