2021-12-23 18:07:51 +01:00
|
|
|
import { derived, writable } from "svelte/store";
|
|
|
|
import { getColorRgbFromHue } from "../WebRtc/ColorGenerator";
|
|
|
|
import { gameManager } from "../Phaser/Game/GameManager";
|
2021-12-12 16:56:26 +01:00
|
|
|
|
2021-12-24 15:49:25 +01:00
|
|
|
type FollowState = "off" | "requesting" | "active" | "ending";
|
|
|
|
type FollowRole = "leader" | "follower";
|
2021-12-12 16:56:26 +01:00
|
|
|
|
2021-12-24 15:49:25 +01:00
|
|
|
export const followStateStore = writable<FollowState>("off");
|
|
|
|
export const followRoleStore = writable<FollowRole>("leader");
|
2021-12-23 18:07:51 +01:00
|
|
|
|
|
|
|
function createFollowUsersStore() {
|
|
|
|
const { subscribe, update, set } = writable<number[]>([]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
addFollowRequest(leader: number): void {
|
2021-12-24 15:49:25 +01:00
|
|
|
followStateStore.set("requesting");
|
|
|
|
followRoleStore.set("follower");
|
2021-12-23 18:07:51 +01:00
|
|
|
set([leader]);
|
|
|
|
},
|
|
|
|
addFollower(user: number): void {
|
|
|
|
update((followers) => {
|
|
|
|
followers.push(user);
|
|
|
|
return followers;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes the follower from the store.
|
|
|
|
* Will update followStateStore and followRoleStore if nobody is following anymore.
|
|
|
|
* @param user
|
|
|
|
*/
|
|
|
|
removeFollower(user: number): void {
|
|
|
|
update((followers) => {
|
|
|
|
const oldFollowerCount = followers.length;
|
|
|
|
followers = followers.filter((id) => id !== user);
|
|
|
|
|
|
|
|
if (followers.length === 0 && oldFollowerCount > 0) {
|
2021-12-24 15:49:25 +01:00
|
|
|
followStateStore.set("off");
|
|
|
|
followRoleStore.set("leader");
|
2021-12-23 18:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return followers;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
stopFollowing(): void {
|
|
|
|
set([]);
|
2021-12-24 15:49:25 +01:00
|
|
|
followStateStore.set("off");
|
|
|
|
followRoleStore.set("leader");
|
2021-12-23 18:07:51 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const followUsersStore = createFollowUsersStore();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This store contains the color of the follow group. It is derived from the ID of the leader.
|
|
|
|
*/
|
|
|
|
export const followUsersColorStore = derived(
|
|
|
|
[followStateStore, followRoleStore, followUsersStore],
|
|
|
|
([$followStateStore, $followRoleStore, $followUsersStore]) => {
|
2021-12-24 15:49:25 +01:00
|
|
|
if ($followStateStore !== "active") {
|
2021-12-23 18:07:51 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($followUsersStore.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let leaderId: number;
|
2021-12-24 15:49:25 +01:00
|
|
|
if ($followRoleStore === "leader") {
|
2021-12-23 18:07:51 +01:00
|
|
|
// Let's get my ID by a quite complicated way....
|
|
|
|
leaderId = gameManager.getCurrentGameScene().connection?.getUserId() ?? 0;
|
|
|
|
} else {
|
|
|
|
leaderId = $followUsersStore[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's compute a random hue between 0 and 1 that varies enough to be interesting
|
|
|
|
const hue = ((leaderId * 197) % 255) / 255;
|
|
|
|
|
|
|
|
let { r, g, b } = getColorRgbFromHue(hue);
|
2021-12-24 15:49:25 +01:00
|
|
|
if ($followRoleStore === "follower") {
|
2021-12-23 18:07:51 +01:00
|
|
|
// Let's make the followers very slightly darker
|
|
|
|
r *= 0.9;
|
|
|
|
g *= 0.9;
|
|
|
|
b *= 0.9;
|
|
|
|
}
|
|
|
|
return (Math.round(r * 255) << 16) | (Math.round(g * 255) << 8) | Math.round(b * 255);
|
|
|
|
}
|
|
|
|
);
|