2020-07-10 15:42:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-06-16 09:26:54 +00:00
|
|
|
import functools
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import websockets
|
2020-06-20 18:03:06 +00:00
|
|
|
import asyncio
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import time
|
2020-06-21 13:32:31 +00:00
|
|
|
import random
|
2021-04-04 01:18:19 +00:00
|
|
|
import pickle
|
2020-06-20 18:03:06 +00:00
|
|
|
|
2021-11-10 14:35:43 +00:00
|
|
|
import Utils
|
2020-06-20 18:03:06 +00:00
|
|
|
from .models import *
|
|
|
|
|
2020-06-27 11:52:03 +00:00
|
|
|
from MultiServer import Context, server, auto_shutdown, ServerCommandProcessor, ClientMessageProcessor
|
2021-04-04 01:18:19 +00:00
|
|
|
from Utils import get_public_ipv4, get_public_ipv6, restricted_loads
|
2020-06-20 18:03:06 +00:00
|
|
|
|
|
|
|
|
2020-06-27 11:52:03 +00:00
|
|
|
class CustomClientMessageProcessor(ClientMessageProcessor):
|
2020-07-10 15:42:22 +00:00
|
|
|
ctx: WebHostContext
|
2020-06-27 11:52:03 +00:00
|
|
|
def _cmd_video(self, platform, user):
|
2020-07-11 14:59:37 +00:00
|
|
|
"""Set a link for your name in the WebHostLib tracker pointing to a video stream"""
|
2020-06-27 11:52:03 +00: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 12:16:51 +00:00
|
|
|
return True
|
2021-04-10 16:45:11 +00:00
|
|
|
elif platform.lower().startswith("y"): # youtube
|
2020-07-25 20:40:24 +00: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 12:16:51 +00:00
|
|
|
return False
|
2020-06-27 11:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# inject
|
|
|
|
import MultiServer
|
|
|
|
MultiServer.client_message_processor = CustomClientMessageProcessor
|
|
|
|
del (MultiServer)
|
|
|
|
|
|
|
|
|
2020-06-20 18:03:06 +00:00
|
|
|
class DBCommandProcessor(ServerCommandProcessor):
|
|
|
|
def output(self, text: str):
|
|
|
|
logging.info(text)
|
|
|
|
|
|
|
|
|
|
|
|
class WebHostContext(Context):
|
|
|
|
def __init__(self):
|
2021-10-18 20:58:29 +00:00
|
|
|
super(WebHostContext, self).__init__("", 0, "", "", 1, 40, True, "enabled", "enabled", "enabled", 0, 2)
|
2020-06-21 23:04:12 +00:00
|
|
|
self.main_loop = asyncio.get_running_loop()
|
2020-06-27 11:52:03 +00:00
|
|
|
self.video = {}
|
2020-10-18 21:07:48 +00:00
|
|
|
self.tags = ["AP", "WebHost"]
|
2020-06-16 09:26:54 +00:00
|
|
|
|
2020-06-20 18:03:06 +00:00
|
|
|
def listen_to_db_commands(self):
|
|
|
|
cmdprocessor = DBCommandProcessor(self)
|
2020-06-16 09:26:54 +00:00
|
|
|
|
2020-06-20 18:03:06 +00: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-21 23:04:12 +00:00
|
|
|
self.main_loop.call_soon_threadsafe(cmdprocessor, command.commandtext)
|
2020-06-20 18:03:06 +00:00
|
|
|
command.delete()
|
|
|
|
commit()
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
@db_session
|
|
|
|
def load(self, room_id: int):
|
|
|
|
self.room_id = room_id
|
2020-06-21 13:32:31 +00:00
|
|
|
room = Room.get(id=room_id)
|
|
|
|
if room.last_port:
|
|
|
|
self.port = room.last_port
|
|
|
|
else:
|
|
|
|
self.port = get_random_port()
|
2020-10-25 23:04:58 +00:00
|
|
|
|
2021-01-03 13:32:32 +00:00
|
|
|
return self._load(self._decompress(room.seed.multidata), True)
|
2020-06-20 18:03:06 +00:00
|
|
|
|
|
|
|
@db_session
|
|
|
|
def init_save(self, enabled: bool = True):
|
|
|
|
self.saving = enabled
|
|
|
|
if self.saving:
|
2021-04-10 13:26:30 +00: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 18:03:06 +00:00
|
|
|
self._start_async_saving()
|
|
|
|
threading.Thread(target=self.listen_to_db_commands, daemon=True).start()
|
|
|
|
|
|
|
|
@db_session
|
2020-07-20 12:17:05 +00:00
|
|
|
def _save(self, exit_save:bool = False) -> bool:
|
2020-07-10 15:42:22 +00:00
|
|
|
room = Room.get(id=self.room_id)
|
2021-04-04 01:18:19 +00:00
|
|
|
room.multisave = pickle.dumps(self.get_save())
|
2020-07-10 15:42:22 +00:00
|
|
|
# saving only occurs on activity, so we can "abuse" this information to mark this as last_activity
|
2020-07-20 12:17:05 +00: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 18:03:06 +00:00
|
|
|
return True
|
|
|
|
|
2020-06-27 11:52:03 +00: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 18:03:06 +00:00
|
|
|
|
2020-06-21 13:32:31 +00:00
|
|
|
def get_random_port():
|
|
|
|
return random.randint(49152, 65535)
|
|
|
|
|
2021-06-29 01:11:48 +00:00
|
|
|
|
2020-06-20 18:03:06 +00: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 09:26:54 +00:00
|
|
|
|
|
|
|
async def main():
|
2021-11-10 14:35:43 +00:00
|
|
|
Utils.init_logging(str(room_id), write_mode="a")
|
2020-06-20 18:03:06 +00:00
|
|
|
ctx = WebHostContext()
|
|
|
|
ctx.load(room_id)
|
2020-06-16 09:26:54 +00:00
|
|
|
ctx.init_save()
|
|
|
|
|
2020-06-21 13:32:31 +00: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 09:26:54 +00:00
|
|
|
|
2020-06-21 13:32:31 +00:00
|
|
|
await ctx.server
|
2020-06-16 09:26:54 +00: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 15:04:25 +00:00
|
|
|
with db_session:
|
|
|
|
room = Room.get(id=ctx.room_id)
|
|
|
|
room.last_port = socketname[1]
|
2020-06-16 09:26:54 +00:00
|
|
|
elif wssocket.family == socket.AF_INET:
|
|
|
|
logging.info(f'Hosting game at {get_public_ipv4()}:{socketname[1]}')
|
2020-07-10 15:42:22 +00:00
|
|
|
with db_session:
|
|
|
|
ctx.auto_shutdown = Room.get(id=room_id).timeout
|
2020-06-16 09:26:54 +00:00
|
|
|
ctx.shutdown_task = asyncio.create_task(auto_shutdown(ctx, []))
|
|
|
|
await ctx.shutdown_task
|
|
|
|
logging.info("Shutting down")
|
|
|
|
|
2021-06-29 01:11:48 +00:00
|
|
|
from .autolauncher import Locker
|
|
|
|
with Locker(room_id):
|
|
|
|
asyncio.run(main())
|
2020-07-10 15:42:22 +00:00
|
|
|
|
|
|
|
|
2020-07-11 14:59:37 +00:00
|
|
|
from WebHostLib import LOGS_FOLDER
|