Skip to content

Commit

Permalink
!!online command on mcdr client
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Oct 29, 2023
1 parent 5e6e55f commit 01d5b37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
18 changes: 15 additions & 3 deletions chatbridge/impl/mcdr/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from chatbridge.core.client import ChatBridgeClient
from chatbridge.core.network.protocol import ChatPayload, CommandPayload
from chatbridge.impl.mcdr.config import MCDRClientConfig
from chatbridge.impl.tis.protocol import StatsQueryResult
from chatbridge.impl.tis.protocol import StatsQueryResult, OnlineQueryResult


class ChatBridgeMCDRClient(ChatBridgeClient):
Expand Down Expand Up @@ -38,9 +38,10 @@ def on_chat(self, sender: str, payload: ChatPayload):
self.server.say(RText('[{}] {}'.format(sender, payload.formatted_str()), RColor.gray))

def on_command(self, sender: str, payload: CommandPayload):
is_ask = not payload.responded
command = payload.command
result: Optional[Serializable] = None
if command.startswith('!!stats '):
if command.startswith('!!stats '): # !!stats request
try:
import stats_helper
except (ImportError, ModuleNotFoundError):
Expand Down Expand Up @@ -69,6 +70,17 @@ def on_command(self, sender: str, payload: CommandPayload):
result = StatsQueryResult.create(stats_name, lines[1:-1], total)
else:
result = StatsQueryResult.unknown_stat()
elif command == '!!online': # !!online response
player = payload.params.get('player')
if player is None:
self.logger.warning('No player in params, params {}'.format(payload.params))
else:
result: OnlineQueryResult = OnlineQueryResult.deserialize(payload.result)
for line in result.data:
self.server.tell(player, line)

if result is not None:
if is_ask and result is not None:
self.reply_command(sender, payload, result)

def query_online(self, client_to_query_online: str, player: str):
self.send_command(client_to_query_online, '!!online', params={'player': player})
3 changes: 3 additions & 0 deletions chatbridge/impl/mcdr/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Optional

from chatbridge.core.config import ClientConfig


class MCDRClientConfig(ClientConfig):
enable: bool = True
debug: bool = False
client_to_query_online: Optional[str] = None
12 changes: 12 additions & 0 deletions chatbridge/impl/mcdr/mcdr_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def display_status(source: CommandSource):
source.reply(tr('status.info', client.is_online(), client.get_ping_text()))


def query_online(source: CommandSource):
if config.client_to_query_online is None:
source.reply('client_to_query_online unset')
return

if client is not None:
client.query_online(config.client_to_query_online, source.player)
else:
source.reply(tr('status.not_init'))


@new_thread('ChatBridge-restart')
def restart_client(source: CommandSource):
with cb_lock:
Expand Down Expand Up @@ -98,6 +109,7 @@ def on_load(server: PluginServerInterface, old_module):
then(Literal('status').runs(display_status)).
then(Literal('restart').runs(restart_client))
)
server.register_command(Literal('!!online').runs(query_online))

@new_thread('ChatBridge-start')
def start():
Expand Down

0 comments on commit 01d5b37

Please sign in to comment.