109 lines
2.8 KiB
TypeScript
109 lines
2.8 KiB
TypeScript
export const addActivity = (game: any, session: any, message: string): void => {
|
|
let date = Date.now();
|
|
if (game.activities.length && game.activities[game.activities.length - 1].date === date) {
|
|
date++;
|
|
}
|
|
game.activities.push({ color: session ? session.color : "", message, date });
|
|
if (game.activities.length > 30) {
|
|
game.activities.splice(0, game.activities.length - 30);
|
|
}
|
|
};
|
|
|
|
export const addChatMessage = (game: any, session: any, message: string, isNormalChat?: boolean) => {
|
|
let now = Date.now();
|
|
let lastTime = 0;
|
|
if (game.chat.length) {
|
|
lastTime = game.chat[game.chat.length - 1].date;
|
|
}
|
|
if (now <= lastTime) {
|
|
now = lastTime + 1;
|
|
}
|
|
|
|
const entry: any = {
|
|
date: now,
|
|
message: message,
|
|
};
|
|
if (isNormalChat) {
|
|
entry.normalChat = true;
|
|
}
|
|
if (session && session.name) {
|
|
entry.from = session.name;
|
|
}
|
|
if (session && session.color) {
|
|
entry.color = session.color;
|
|
}
|
|
game.chat.push(entry);
|
|
if (game.chat.length > 50) {
|
|
game.chat.splice(0, game.chat.length - 50);
|
|
}
|
|
};
|
|
|
|
export const getColorFromName = (game: any, name: string): string => {
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].name === name) {
|
|
return game.sessions[id].color;
|
|
}
|
|
}
|
|
return "";
|
|
};
|
|
|
|
export const getLastPlayerName = (game: any): string => {
|
|
let index = game.playerOrder.length - 1;
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].color === game.playerOrder[index]) {
|
|
return game.sessions[id].name;
|
|
}
|
|
}
|
|
return "";
|
|
};
|
|
|
|
export const getFirstPlayerName = (game: any): string => {
|
|
let index = 0;
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].color === game.playerOrder[index]) {
|
|
return game.sessions[id].name;
|
|
}
|
|
}
|
|
return "";
|
|
};
|
|
|
|
export const getNextPlayerSession = (game: any, name: string): any => {
|
|
let color;
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].name === name) {
|
|
color = game.sessions[id].color;
|
|
break;
|
|
}
|
|
}
|
|
|
|
let index = game.playerOrder.indexOf(color);
|
|
index = (index + 1) % game.playerOrder.length;
|
|
color = game.playerOrder[index];
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].color === color) {
|
|
return game.sessions[id];
|
|
}
|
|
}
|
|
console.error(`getNextPlayerSession -- no player found!`);
|
|
console.log(game.players);
|
|
};
|
|
|
|
export const getPrevPlayerSession = (game: any, name: string): any => {
|
|
let color;
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].name === name) {
|
|
color = game.sessions[id].color;
|
|
break;
|
|
}
|
|
}
|
|
let index = game.playerOrder.indexOf(color);
|
|
index = (index - 1) % game.playerOrder.length;
|
|
for (let id in game.sessions) {
|
|
if (game.sessions[id].color === game.playerOrder[index]) {
|
|
return game.sessions[id];
|
|
}
|
|
}
|
|
console.error(`getNextPlayerSession -- no player found!`);
|
|
console.log(game.players);
|
|
};
|