From 331e8a7da5b8cddd044bfa58f2a4294a5b58e6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zlatko=20=C4=90uri=C4=87?= Date: Mon, 14 Apr 2025 00:01:43 +0200 Subject: [PATCH] chat commands added --- src/add-timer-screen.js | 2 +- src/timer.js | 19 ++++++----- src/timer.loop.js | 70 +++++++++++++++++++++++++++++++++++++++++ styles/yat-style.css | 6 ++++ 4 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/add-timer-screen.js b/src/add-timer-screen.js index 9ca9e1f..e021c77 100644 --- a/src/add-timer-screen.js +++ b/src/add-timer-screen.js @@ -3,7 +3,7 @@ import { getNextName, createTimer } from './timer.repository.js'; const templatePath = `modules/yet-another-timer/templates/add-timer.hbs`; -export class AddTimerScreen extends FormApplication {a +export class AddTimerScreen extends FormApplication { static get defaultOptions() { const defaults = super.defaultOptions; diff --git a/src/timer.js b/src/timer.js index 82e4735..19e9a1d 100644 --- a/src/timer.js +++ b/src/timer.js @@ -1,5 +1,5 @@ import { logger } from './logger.js'; -import { startTimers, stopTimers, resumeTimer } from './timer.loop.js'; +import { setScreen, startTimers, stopTimers, resumeTimer, triggerChatCommand, chatCommandName } from './timer.loop.js'; import { createTimer } from './timer.repository.js'; import { TimerScreen } from "./timer-screen.js"; @@ -7,8 +7,17 @@ let isPaused = true; console.debug('Timer module booted!'); -Hooks.on('init', function() { +Hooks.once('init', function() { logger.log('Timer module init'); + const screen = new TimerScreen(); + setScreen(screen); +}); + +Hooks.on('chatMessage', (chatLog, messageText, chatData) => { + if (messageText.trim().toLowerCase().startsWith(chatCommandName)) { + return triggerChatCommand(chatLog, messageText, chatData); + } + return true; }); Hooks.on('ready', function(details) { @@ -18,12 +27,6 @@ Hooks.on('ready', function(details) { isPaused = false; addSomeTimers(); - - const screen = new TimerScreen(); - logger.log(screen); - const result = screen.render(true); - logger.log(result); - }); Hooks.on('error', function(location, error, payload) { diff --git a/src/timer.loop.js b/src/timer.loop.js index 5b3a9a5..0b2847e 100644 --- a/src/timer.loop.js +++ b/src/timer.loop.js @@ -3,18 +3,88 @@ import { TimerError } from "./error.js"; import { getActiveTimers, getTimer, deleteTimer as repoDeleteTimer } from "./timer.repository.js"; import { logTimer } from "./timer-screen.js"; import { TimerCompleteScreen } from "./timer-complete-screen.js"; +import { showAddTimer } from './add-timer-screen.js'; const INTERVAL_MS = 1000; // main loop interval const state = { timerInterval: null, subscribers: [], + screen: null, }; +export const chatCommandName = '/timers'; + export function getStatus() { return state.timerInterval === null; } +export function setScreen(screen) { + state.screen = screen; +} + +function showChatHelpMessage() { + ChatMessage.create({ + user: game.user.id, + speaker: ChatMessage.getSpeaker(), + content: `${game.user.name} used "/timers"!
+ +Usage: /timers
+ +Commands: + +
  • show - shows timers
  • +
  • hide - hide timers
  • +
  • pause - pause the timer runners
  • +
  • resume - resume timer runners
  • +
  • add - add a timer
  • +`, + type: CONST.CHAT_MESSAGE_STYLES.OOC, + }); +} + +export function triggerChatCommand(log, msg, data) { + logger.debug('Chat message:', log, msg, data); + try { + const parts = msg.split(' ') + .map(part => part.trim()) + .filter(Boolean); + const op = parts[1]; + if (!op) { + logger.error('No chat command given.'); + showChatHelpMessage(); + return false; + } + logger.debug('Parts:', parts); + switch (op) { + case 'show': + state.screen?.render(true); + break; + case 'hide': + state.screen?.close(); + break; + case 'pause': + stopTimers(); + break; + case 'stop': + stopTimers(); + break; + case 'resume': + startTimers(); + break; + case 'add': + showAddTimer(); + break; + default: + showChatHelpMessage(); + logger.error(`Invalid chat command "${op}".`); + } + } catch(error) { + logger.error(`Error processing errors:`, error); + } + return false; +} + export function flipTimers() { if (getStatus()) { startTimers(); diff --git a/styles/yat-style.css b/styles/yat-style.css index bf5d823..4fc4c44 100644 --- a/styles/yat-style.css +++ b/styles/yat-style.css @@ -27,6 +27,7 @@ #yet-another-timer .timer-card .content p { margin: 5px 0; font-size: 0.9em; + max-width: 33em; } #yet-another-timer .timer-card .actions { @@ -62,3 +63,8 @@ flex-flow: column; gap: 1rem; } + +img { + height: 40px; + width: 40px; +}