From 79edd639c7dac2a3b3221217bf3bac9deb1cf6b5 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Mon, 27 Sep 2021 13:25:44 +0200 Subject: [PATCH] Move messages to Log class, use binding --- src/components/Log.svelte | 32 +++++++++++++++----------------- src/components/Wrapper.svelte | 3 ++- src/shark/Log.ts | 15 +++++++++++++++ src/shark/SharkGame.ts | 4 ++-- 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 src/shark/Log.ts diff --git a/src/components/Log.svelte b/src/components/Log.svelte index 5aa3809..9d35236 100644 --- a/src/components/Log.svelte +++ b/src/components/Log.svelte @@ -2,42 +2,40 @@ import { Message, MessageType } from "../shark/Message"; import { SharkGame } from "../shark/SharkGame"; - export function addMessage( + export let messages: Message[]; + + function addMessage( message: string, - type: MessageType = MessageType.message - ): void { - SharkGame.messages = [ - ...SharkGame.messages.slice( - -Math.max(...SharkGame.Settings.logLength.options) - ), - new Message(message, type), - ]; + messageType: MessageType = MessageType.message + ) { + messages = [...messages, new Message(message, messageType)]; } $: logLength = SharkGame.Settings.logLength.current; - $: displaymessages = [...SharkGame.messages].slice(-logLength).reverse(); + $: displaymessages = messages.slice(-logLength).reverse();
Add Message Add Warning Add Error
    diff --git a/src/components/Wrapper.svelte b/src/components/Wrapper.svelte index e099ba2..811048f 100644 --- a/src/components/Wrapper.svelte +++ b/src/components/Wrapper.svelte @@ -1,4 +1,5 @@ @@ -6,7 +7,7 @@
    - +
    diff --git a/src/shark/Log.ts b/src/shark/Log.ts new file mode 100644 index 0000000..6c7892c --- /dev/null +++ b/src/shark/Log.ts @@ -0,0 +1,15 @@ +import type { Message } from "./Message"; +import { SharkGame } from "./SharkGame"; +import { StaticClass } from "./StaticClass"; + +export class Log extends StaticClass { + static #messages: Message[] = []; + static get messages(): Message[] { + return this.#messages; + } + static set messages(messages: Message[]) { + this.#messages = messages.slice( + -Math.max(...SharkGame.Settings.logLength.options) + ); + } +} diff --git a/src/shark/SharkGame.ts b/src/shark/SharkGame.ts index a9de521..f728b71 100644 --- a/src/shark/SharkGame.ts +++ b/src/shark/SharkGame.ts @@ -1,4 +1,4 @@ -import { Message } from "./Message"; +import { Log } from "./Log"; import { Settings } from "./Settings"; import { StaticClass } from "./StaticClass"; @@ -54,7 +54,7 @@ export class SharkGame extends StaticClass { static title: string; static readonly Settings = Settings; - static messages: Message[] = [new Message("Welcome to Sharg!")]; + static readonly Log = Log; static init(): void { for (const setting of Object.values(Settings)) {