2021-07-07 18:07:58 +02:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
import { playersStore } from "./PlayersStore";
|
|
|
|
import type { PlayerInterface } from "../Phaser/Game/PlayerInterface";
|
2021-10-11 18:39:34 +02:00
|
|
|
import { iframeListener } from "../Api/IframeListener";
|
2022-01-14 17:04:31 +01:00
|
|
|
import { Subject } from "rxjs";
|
2021-07-07 18:07:58 +02:00
|
|
|
|
|
|
|
export const chatVisibilityStore = writable(false);
|
|
|
|
export const chatInputFocusStore = writable(false);
|
|
|
|
|
2022-01-14 17:04:31 +01:00
|
|
|
const _newChatMessageSubject = new Subject<string>();
|
|
|
|
export const newChatMessageSubject = _newChatMessageSubject.asObservable();
|
2021-07-07 18:07:58 +02:00
|
|
|
|
|
|
|
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) {
|
2022-01-06 10:49:44 +01:00
|
|
|
throw new Error("Could not find data for author " + authorId);
|
2021-07-07 18:07:58 +02:00
|
|
|
}
|
|
|
|
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) {
|
2021-10-27 14:29:39 +02:00
|
|
|
iframeListener.sendUserInputChat(text);
|
|
|
|
|
2022-01-14 17:04:31 +01:00
|
|
|
_newChatMessageSubject.next(text);
|
2021-07-07 18:07:58 +02:00
|
|
|
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(),
|
|
|
|
});
|
|
|
|
}
|
2021-10-11 18:39:34 +02:00
|
|
|
|
2021-07-07 18:07:58 +02:00
|
|
|
return list;
|
|
|
|
});
|
|
|
|
},
|
2022-04-26 14:40:54 +02:00
|
|
|
/**
|
|
|
|
* @param origin The iframe that originated this message (if triggered from the Scripting API), or undefined otherwise.
|
|
|
|
*/
|
|
|
|
addExternalMessage(authorId: number, text: string, origin?: Window) {
|
2021-07-07 18:07:58 +02:00
|
|
|
update((list) => {
|
|
|
|
const lastMessage = list[list.length - 1];
|
2021-10-27 10:02:47 +02:00
|
|
|
if (
|
|
|
|
lastMessage &&
|
|
|
|
lastMessage.type === ChatMessageTypes.text &&
|
|
|
|
lastMessage.text &&
|
|
|
|
lastMessage?.author?.userId === authorId
|
|
|
|
) {
|
2021-07-07 18:07:58 +02:00
|
|
|
lastMessage.text.push(text);
|
|
|
|
} else {
|
|
|
|
list.push({
|
|
|
|
type: ChatMessageTypes.text,
|
|
|
|
text: [text],
|
|
|
|
author: getAuthor(authorId),
|
|
|
|
date: new Date(),
|
|
|
|
});
|
|
|
|
}
|
2021-10-11 18:39:34 +02:00
|
|
|
|
2022-04-26 14:40:54 +02:00
|
|
|
iframeListener.sendUserInputChat(text, origin);
|
2021-07-07 18:07:58 +02:00
|
|
|
return list;
|
|
|
|
});
|
2021-07-15 19:12:19 +02:00
|
|
|
chatVisibilityStore.set(true);
|
2021-07-07 18:07:58 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export const chatMessagesStore = createChatMessagesStore();
|
2021-07-13 11:00:32 +02:00
|
|
|
|
|
|
|
function createChatSubMenuVisibilityStore() {
|
|
|
|
const { subscribe, update } = writable<string>("");
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
openSubMenu(playerName: string, index: number) {
|
|
|
|
const id = playerName + index;
|
|
|
|
update((oldValue) => {
|
|
|
|
return oldValue === id ? "" : id;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:02:47 +02:00
|
|
|
export const chatSubMenuVisibilityStore = createChatSubMenuVisibilityStore();
|