FEATURE: migrated the chat window to svelte
This commit is contained in:
parent
e5f7c62e25
commit
3cfbcc6b02
24 changed files with 470 additions and 305 deletions
102
front/src/Stores/ChatStore.ts
Normal file
102
front/src/Stores/ChatStore.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import { writable } from "svelte/store";
|
||||
import { playersStore } from "./PlayersStore";
|
||||
import type { PlayerInterface } from "../Phaser/Game/PlayerInterface";
|
||||
|
||||
export const chatVisibilityStore = writable(false);
|
||||
export const chatInputFocusStore = writable(false);
|
||||
|
||||
export const newChatMessageStore = writable<string | null>(null);
|
||||
|
||||
export enum ChatMessageTypes {
|
||||
text = 1,
|
||||
me,
|
||||
userIncoming,
|
||||
userOutcoming,
|
||||
}
|
||||
|
||||
export interface ChatMessage {
|
||||
type: ChatMessageTypes;
|
||||
date: Date;
|
||||
author?: PlayerInterface;
|
||||
targets?: PlayerInterface[];
|
||||
text?: string[];
|
||||
}
|
||||
|
||||
function getAuthor(authorId: number): PlayerInterface {
|
||||
const author = playersStore.getPlayerById(authorId);
|
||||
if (!author) {
|
||||
throw "Could not find data for author " + authorId;
|
||||
}
|
||||
return author;
|
||||
}
|
||||
|
||||
function createChatMessagesStore() {
|
||||
const { subscribe, update } = writable<ChatMessage[]>([]);
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
addIncomingUser(authorId: number) {
|
||||
update((list) => {
|
||||
const lastMessage = list[list.length - 1];
|
||||
if (lastMessage && lastMessage.type === ChatMessageTypes.userIncoming && lastMessage.targets) {
|
||||
lastMessage.targets.push(getAuthor(authorId));
|
||||
} else {
|
||||
list.push({
|
||||
type: ChatMessageTypes.userIncoming,
|
||||
targets: [getAuthor(authorId)],
|
||||
date: new Date(),
|
||||
});
|
||||
}
|
||||
return list;
|
||||
});
|
||||
},
|
||||
addOutcomingUser(authorId: number) {
|
||||
update((list) => {
|
||||
const lastMessage = list[list.length - 1];
|
||||
if (lastMessage && lastMessage.type === ChatMessageTypes.userOutcoming && lastMessage.targets) {
|
||||
lastMessage.targets.push(getAuthor(authorId));
|
||||
} else {
|
||||
list.push({
|
||||
type: ChatMessageTypes.userOutcoming,
|
||||
targets: [getAuthor(authorId)],
|
||||
date: new Date(),
|
||||
});
|
||||
}
|
||||
return list;
|
||||
});
|
||||
},
|
||||
addPersonnalMessage(text: string) {
|
||||
newChatMessageStore.set(text);
|
||||
update((list) => {
|
||||
const lastMessage = list[list.length - 1];
|
||||
if (lastMessage && lastMessage.type === ChatMessageTypes.me && lastMessage.text) {
|
||||
lastMessage.text.push(text);
|
||||
} else {
|
||||
list.push({
|
||||
type: ChatMessageTypes.me,
|
||||
text: [text],
|
||||
date: new Date(),
|
||||
});
|
||||
}
|
||||
return list;
|
||||
});
|
||||
},
|
||||
addExternalMessage(authorId: number, text: string) {
|
||||
update((list) => {
|
||||
const lastMessage = list[list.length - 1];
|
||||
if (lastMessage && lastMessage.type === ChatMessageTypes.text && lastMessage.text) {
|
||||
lastMessage.text.push(text);
|
||||
} else {
|
||||
list.push({
|
||||
type: ChatMessageTypes.text,
|
||||
text: [text],
|
||||
author: getAuthor(authorId),
|
||||
date: new Date(),
|
||||
});
|
||||
}
|
||||
return list;
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
export const chatMessagesStore = createChatMessagesStore();
|
Loading…
Add table
Add a link
Reference in a new issue