Skip to content

Commit

Permalink
feat: add stats record for slot
Browse files Browse the repository at this point in the history
  • Loading branch information
401U committed Mar 13, 2023
1 parent 9432d49 commit e544775
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
77 changes: 76 additions & 1 deletion mount/MountSlot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
from threading import Lock
from threading import Lock, Thread
import time
from typing import Callable, Optional
from anyio import Event

from jproperties import Properties

Expand All @@ -8,12 +11,39 @@
from .utils import logger, psi, rtr


class StatsChecker(Thread):
def __init__(self, interval: int, cb: Callable[[], None]):
super().__init__()
self.setDaemon(True)
self.setName(self.__class__.__name__)
self._report_time = time.time()
self.stop_event = Event()
self.interval = interval
self._callback = cb

def run(self):
while True: # loop until stop
while True: # # loop for report
if self.stop_event.wait(1):
return
if time.time() - self._report_time > self.interval:
break
self._report_time = time.time()
self._callback()

def stop(self):
self.stop_event.set()

class MountSlot:
def __init__(self, path):
self.path = path
self.load_config()
self.properties = Properties()
self.slot_lock = Lock()
self.__players = []
self.__players_lock = Lock()
self.__stats_lock = Lock()
self.__stats_checker = StatsChecker(60, self.update_stats)

@property
def name(self):
Expand Down Expand Up @@ -88,3 +118,48 @@ def edit_config(self, key: str, value: str):
self._config.__setattr__(key, value)
self.save_config()
return rtr('config.set_value', key=rtr(f'config.slot.{key}'), value=self._config.__getattribute__(key))

def on_player_join(self, player: str):
with self.__players_lock:
self.update_stats()
self._config.stats.total_players = self._config.stats.total_players + 1
self.__players.append(player)
self.update_stats()

def on_player_left(self, player: str):
try:
with self.__players_lock:
self.update_stats()
self.__players.remove(player)
except ValueError:
pass

def on_mount(self):
if not self.__stats_checker.is_alive:
with self.__stats_lock:
current = time.time_ns
self._config.stats.last_mount_ns = current
self.save_config()
self.__stats_checker.start()

def on_unmount(self):
if self.__stats_checker.is_alive:
self.update_stats()
self.__stats_checker.stop()
self.__stats_checker.join()


def update_stats(self):
if not self.__stats_checker.is_alive:
return
with self.__stats_lock:
current = time.time_ns
prev = self._config.stats.last_mount_ns
p = len(self.__players)
t = current - prev
stats = self._config.stats
stats.last_mount_ns = current
stats.total_use_time = stats.total_use_time + t
stats.total_player_time = stats.total_player_time + t * p
self._config.stats = stats
self.save_config()
10 changes: 10 additions & 0 deletions mount/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import List

from mcdreforged.api.rtext import *
Expand Down Expand Up @@ -30,6 +31,12 @@ def load() -> 'MountConfig':
return psi.load_config_simple(file_name=CONFIG_NAME, target_class=MountConfig, in_data_folder=False)


class SlotStats(Serializable):
last_mount_ns: int = time.time_ns
total_use_time: int = 0
total_player_time: int = 0
total_players: int = 0

class SlotConfig(Serializable):
checked: bool = False
desc: str = "Demo server"
Expand All @@ -45,6 +52,9 @@ class SlotConfig(Serializable):
# mcdr plugin path for specific plugin, empty for disable, should be relative to mc server path
plugin_dir: str = ""

# slot stats, used for rank
stats: SlotStats = SlotStats()

def display(self, server_path: str):
conf_list = self.get_annotations_fields()

Expand Down
47 changes: 40 additions & 7 deletions mount/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,49 @@
manager: Optional[MountManager] = None


def welcome(server: PluginServerInterface, player: str, info: Info):
server.tell(player, rtr('help_msg.welcome'))


def on_load(server: PluginServerInterface, prev_module):
global manager
config: MountConfig = server.load_config_simple(
file_name=CONFIG_NAME, in_data_folder=False, target_class=MountConfig)
manager = MountManager(config=config)
register_commands(server, manager)
if config.welcome_player:
server.register_event_listener(
event='mcdr.player_joined', callback=welcome)

if manager.current_slot and server.is_server_running():
manager.current_slot.on_mount()


def on_unload(server: PluginServerInterface):
if not manager:
return
if manager.current_slot and server.is_server_running():
manager.current_slot.on_unmount()


def on_server_startup(server: PluginServerInterface):
if not manager:
return
if manager.current_slot:
manager.current_slot.on_mount()


def on_server_stop(server: PluginServerInterface, code: int):
if not manager:
return
if manager.current_slot:
manager.current_slot.on_unmount()


def on_player_joined(server: PluginServerInterface, player: str, info: Info):
if not manager:
return
if manager._config.welcome_player:
server.tell(player, rtr('help_msg.welcome'))
if manager.current_slot:
manager.current_slot.on_player_join(player)


def on_player_left(server: PluginServerInterface, player: str):
if not manager:
return
if manager.current_slot:
manager.current_slot.on_player_left(player)

0 comments on commit e544775

Please sign in to comment.