Rename SharkGame.Log to .MessageHandler

This commit is contained in:
Tobias Berger 2021-10-02 21:20:53 +02:00
parent b90b397cef
commit 83a5db08be
5 changed files with 21 additions and 15 deletions

View file

@ -13,7 +13,7 @@
const mainHeaderButtons = {
save() {
game.SaveHandler.save(game);
return game.SaveHandler.save(game);
},
settings() {
openModal(SettingsModal, { settings: game.Settings.settings }, { replace: true });

View file

@ -13,10 +13,10 @@
sidebarExpanded = to ?? !sidebarExpanded;
}
function handleAddMessage(event: CustomEvent<AddMessageEvent>) {
game.Log.addMessage(event.detail.message, event.detail.messageType);
game.MessageHandler.addMessage(event.detail.message, event.detail.messageType);
}
function handleResetLog() {
game.Log.reset();
game.MessageHandler.reset();
}
let logLength: number;
@ -30,7 +30,12 @@
<main>
<div id="left-column" class:expanded={sidebarExpanded} class:collapsed={!sidebarExpanded}>
<ResourceTable resources={game.Resources.Resources} />
<Log {logLength} messages={game.Log.messages} on:addMessage={handleAddMessage} on:resetLog={handleResetLog} />
<Log
{logLength}
messages={game.MessageHandler.messages}
on:addMessage={handleAddMessage}
on:resetLog={handleResetLog}
/>
</div>
<div id="right-column" on:click={() => toggleSidebar(false)}>
<div id="right-top">

View file

@ -4,19 +4,19 @@ import { Message, MessageType } from "./Message";
import { Settings } from "./Settings";
import { StaticClass } from "./StaticClass";
export class Log extends StaticClass {
export class MessageHandler extends StaticClass {
static messages = writable<Message[]>([]);
static #maxLogLength: number;
static init(): void {
Settings.settings.subscribe((settings) => {
Log.#maxLogLength = Math.max(...settings.layout.logLength.options);
MessageHandler.#maxLogLength = Math.max(...settings.layout.logLength.options);
})();
}
static addMessage(message: string, messageType: MessageType = MessageType.message): void {
this.messages.update((oldMessages) =>
[...oldMessages, new Message(message, messageType)].slice(-Log.#maxLogLength)
[...oldMessages, new Message(message, messageType)].slice(-MessageHandler.#maxLogLength)
);
}

View file

@ -40,7 +40,7 @@ export class SaveHandler extends StaticClass {
console.debug("Saving");
console.time("Done saving");
const messages = await new Promise<Message[]>((resolve) => {
game.Log.messages.subscribe((messages) => {
game.MessageHandler.messages.subscribe((messages) => {
resolve(messages);
})();
});
@ -119,10 +119,10 @@ export class SaveHandler extends StaticClass {
return settings;
});
game.Log.reset();
game.MessageHandler.reset();
fullSave.messages.forEach((message) => {
game.Log.addMessage(message.content, message.type);
game.MessageHandler.addMessage(message.content, message.type);
});
game.TabHandler.currentTab = game.TabHandler.AllTabs[fullSave.selectedTab];
console.timeEnd("Done loading");

View file

@ -1,5 +1,5 @@
import { Resources } from "./data/Resources";
import { Log } from "./Log";
import { MessageHandler } from "./MessageHandler";
import { SaveHandler } from "./SaveHandler";
import { Settings } from "./Settings";
import { StaticClass } from "./StaticClass";
@ -59,7 +59,7 @@ export class SharkGame extends StaticClass {
static title: string;
static readonly Settings = Settings;
static readonly Log = Log;
static readonly MessageHandler = MessageHandler;
static readonly Resources = Resources;
static readonly TabHandler = TabHandler;
static readonly SaveHandler = SaveHandler;
@ -72,11 +72,12 @@ export class SharkGame extends StaticClass {
SharkGame.title = SharkGame.#GAME_NAMES[Math.floor(Math.random() * SharkGame.#GAME_NAMES.length)];
Log.init();
MessageHandler.init();
SaveHandler.init(this);
}
static save(): void {
SaveHandler.save(this);
static save(): Promise<void> {
return SaveHandler.save(this);
}
}