chat commands added
This commit is contained in:
parent
cf3815446a
commit
331e8a7da5
@ -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;
|
||||
|
||||
|
19
src/timer.js
19
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) {
|
||||
|
@ -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: `<em>${game.user.name} used "/timers"!</em><br>
|
||||
|
||||
Usage: /timers <command><br/>
|
||||
|
||||
Commands:
|
||||
|
||||
<li><strong>show</strong> - shows timers</li>
|
||||
<li><strong>hide</strong> - hide timers</li>
|
||||
<li><strong>pause</strong> - pause the timer runners</li>
|
||||
<li><strong>resume</strong> - resume timer runners</li>
|
||||
<li><strong>add</strong> - add a timer</li>
|
||||
`,
|
||||
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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user