Add multiple message types

This commit is contained in:
Tobias Berger 2021-09-27 12:28:17 +02:00
parent 66ea2388e9
commit 4884d13300
2 changed files with 35 additions and 6 deletions

View file

@ -1,13 +1,16 @@
<script lang="ts"> <script lang="ts">
import { Message } from "../shark/Message"; import { Message, MessageType } from "../shark/Message";
import { SharkGame } from "../shark/SharkGame"; import { SharkGame } from "../shark/SharkGame";
export function addMessage(message: string): void { export function addMessage(
message: string,
type: MessageType = MessageType.message
): void {
SharkGame.messages = [ SharkGame.messages = [
...SharkGame.messages.slice( ...SharkGame.messages.slice(
-Math.max(...SharkGame.Settings.logLength.options) -Math.max(...SharkGame.Settings.logLength.options)
), ),
new Message(message), new Message(message, type),
]; ];
} }
@ -18,9 +21,19 @@
<div id="log"> <div id="log">
<button <button
on:click={() => { on:click={() => {
addMessage("test" + SharkGame.messages.length); addMessage("message" + SharkGame.messages.length);
}}>Add Test Message</button }}>Add Test Message</button
> >
<button
on:click={() => {
addMessage("warn" + SharkGame.messages.length, MessageType.warning);
}}>Add Test Warning</button
>
<button
on:click={() => {
addMessage("error" + SharkGame.messages.length, MessageType.error);
}}>Add Test Error</button
>
<button <button
on:click={() => { on:click={() => {
Message.even = true; Message.even = true;
@ -30,7 +43,9 @@
<ol> <ol>
{#each displaymessages as message, i} {#each displaymessages as message, i}
{#if i < logLength} {#if i < logLength}
<li class={message.isEven ? "even" : "odd"}>{message}</li> <li class={(message.isEven ? "even" : "odd") + " " + message.type}>
{message}
</li>
{/if} {/if}
{/each} {/each}
</ol> </ol>
@ -58,6 +73,12 @@
&.even { &.even {
background-color: var(--color-dark); background-color: var(--color-dark);
} }
&.warning {
color: darkorange;
}
&.error {
color: red;
}
} }
} }
} }

View file

@ -1,11 +1,19 @@
export enum MessageType {
message = "message",
warning = "warning",
error = "error",
}
export class Message { export class Message {
static even = true; static even = true;
isEven: boolean; isEven: boolean;
message: string; message: string;
type: MessageType;
constructor(message: string) { constructor(message: string, type: MessageType = MessageType.message) {
this.message = message; this.message = message;
this.isEven = Message.even; this.isEven = Message.even;
this.type = type;
Message.even = !Message.even; Message.even = !Message.even;
} }