Skip to content

Commit c03b879

Browse files
committed
refactor: move host logic from constant to stores
1 parent a9795a5 commit c03b879

File tree

6 files changed

+31
-24
lines changed

6 files changed

+31
-24
lines changed

src/__tests__/js/networking.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ describe('networking as a node', () => {
101101

102102
jest.doMock('@/js/constants.js', () => ({
103103
...Constants,
104-
hivemindBrain: testId2,
105-
isHivemindBrain: false,
106104
external: { Peer },
107105
}));
108106

107+
stores.hivemindBrain.set(testId2);
108+
109109
networking = getNetworking();
110110
givePeerConnectionId(testId1);
111111
openConnection();
@@ -206,11 +206,11 @@ describe('networking as a hivemind', () => {
206206

207207
jest.doMock('@/js/constants.js', () => ({
208208
...Constants,
209-
hivemindBrain: null,
210-
isHivemindBrain: true,
211209
external: { Peer },
212210
}));
213211

212+
stores.hivemindBrain.set(null);
213+
214214
networking = getNetworking();
215215
givePeerConnectionId(testId2);
216216
openConnection();

src/components/Game.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
toAristocratCipher,
1414
toPatristocratCipher,
1515
} from '@/js/quotes.js';
16-
import { isHivemindBrain, hivemindBrain } from '@/js/constants.js';
1716
import {
1817
hintEnabled,
1918
patristocratEnabled,
2019
progress,
2120
gameProblem,
2221
solved,
2322
users,
23+
isHivemindBrain,
24+
hivemindBrain,
2425
hivemindConnection,
2526
} from '@/js/store.js';
2627
@@ -38,7 +39,7 @@
3839
getNewQuote().then((quote) => gameProblem.set(createProblem(quote)));
3940
};
4041
41-
$: connectingToHivemind = !isHivemindBrain && $hivemindConnection === null;
42+
$: connectingToHivemind = !$isHivemindBrain && $hivemindConnection === null;
4243
</script>
4344

4445
<div class="game">
@@ -69,7 +70,7 @@
6970
</Panel>
7071
{#if connectingToHivemind}
7172
<Panel title="Connecting...">
72-
Connecting to {hivemindBrain ?? ''}
73+
Connecting to {$hivemindBrain ?? ''}
7374
</Panel>
7475
{:else if $users.length > 0}
7576
<Panel title="Lobby">
@@ -81,7 +82,7 @@
8182
<OpponentProgress />
8283
</Panel>
8384
{/if}
84-
{#if $gameProblem || isHivemindBrain}
85+
{#if $gameProblem || $isHivemindBrain}
8586
<Panel title="Game">
8687
{#if $gameProblem}
8788
<CryptogramSolver
@@ -91,7 +92,7 @@
9192
on:error
9293
/>
9394
{/if}
94-
{#if isHivemindBrain}
95+
{#if $isHivemindBrain}
9596
<button on:click={newProblem}>New Problem</button>
9697
{/if}
9798
</Panel>

src/components/JoinLink.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
2-
import { id as id_ } from '@/js/store.js';
2+
import { id as id_, isHivemindBrain as isHivemindBrain_ } from '@/js/store.js';
33
import { getJoinLink } from '@/js/cryptoduelutils.js';
44
55
export let id = id_;
6+
export let isHivemindBrain = isHivemindBrain_;
67
78
let clicked = false;
89
// whether we can use the navigator.share api
@@ -22,7 +23,7 @@
2223
clicked = true;
2324
};
2425
25-
$: joinLink = getJoinLink($id);
26+
$: joinLink = getJoinLink($id, $isHivemindBrain);
2627
</script>
2728

2829
<div class="join-links">

src/js/cryptoduelutils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { choose, fmtTime } from './utils.js';
6-
import { congratulationTitles, isHivemindBrain } from './constants.js';
6+
import { congratulationTitles } from './constants.js';
77

88
/** @typedef {import('./quotes.js').EncryptedQuote} EncryptedQuote */
99

@@ -19,8 +19,8 @@ export const getCongratulationsMessage = (timeTaken) => {
1919
/** @type {() => string} */
2020
export const getCongratulationsTitle = () => choose(congratulationTitles);
2121

22-
/** @type {(id: string) => string} */
23-
export const getJoinLink = (id) =>
22+
/** @type {(id: string, isHivemindBrain: boolean) => string} */
23+
export const getJoinLink = (id, isHivemindBrain) =>
2424
isHivemindBrain
2525
? `${window.location.href}?game=${encodeURIComponent(id)}`
2626
: window.location.href;

src/js/networking.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
connections,
55
gameProblem,
66
id,
7+
hivemindBrain,
8+
isHivemindBrain,
79
hivemindConnection,
810
name,
911
self,
@@ -13,8 +15,6 @@ import {
1315
} from './store.js';
1416
import {
1517
external,
16-
hivemindBrain,
17-
isHivemindBrain,
1818
Messages,
1919
} from './constants.js';
2020
import { log } from './utils.js';
@@ -72,7 +72,7 @@ const onNewProblem = (_, data) => {
7272

7373
/** @type {DataResponder<UPDATE_S_MSG>} */
7474
const updateFromClient = (id, data) => {
75-
if (!isHivemindBrain) return;
75+
if (!get(isHivemindBrain)) return;
7676
users.update(($users) =>
7777
$users.map((u) =>
7878
u.id !== id
@@ -89,7 +89,7 @@ const updateFromClient = (id, data) => {
8989

9090
/** @type {DataResponder<UPDATE_C_MSG>} */
9191
const updateFromServer = (_, data) => {
92-
if (isHivemindBrain) return;
92+
if (get(isHivemindBrain)) return;
9393
const ourId = get(id);
9494
users.set(data.users.filter((u) => u.id !== ourId));
9595
};
@@ -116,7 +116,7 @@ const sendInitialState = (/** @type {Connection} */ conn) =>
116116
const openConnection = (/** @type {Connection} */ conn) =>
117117
new Promise((res) => {
118118
conn.on('open', () => {
119-
if (conn.peer === hivemindBrain) hivemindConnection.set(conn);
119+
if (conn.peer === get(hivemindBrain)) hivemindConnection.set(conn);
120120
connections.set(conn.peer, conn);
121121
conn.on('data', onData(conn.peer));
122122
conn.on('close', removePlayer(conn.peer));
@@ -142,9 +142,11 @@ export const connectTo = (otherId) =>
142142
});
143143

144144
peer.on('open', (/** @type {string} */ $id) => {
145+
const $isbrain = get(isHivemindBrain);
146+
const $brainid = get(hivemindBrain);
145147
id.set($id);
146-
if (!isHivemindBrain && hivemindBrain !== null) {
147-
connectTo(hivemindBrain);
148+
if (!$isbrain && $brainid !== null) {
149+
connectTo($brainid);
148150
}
149151
});
150152
peer.on('connection', openConnection);
@@ -159,15 +161,15 @@ peer.on('error', (/** @type {any} */ e) => {
159161

160162
const subscriptions = [
161163
users.subscribe(($users) => {
162-
if (isHivemindBrain)
164+
if (get(isHivemindBrain))
163165
emit({
164166
type: Messages.UPDATE_CLIENT_STATE,
165167
users: [...$users.map((u) => ({ ...u, conn: null })), get(self)],
166168
});
167169
}),
168170
self.subscribe(($self) => {
169171
log('own state changed', $self);
170-
if (isHivemindBrain)
172+
if (get(isHivemindBrain))
171173
emit({
172174
type: Messages.UPDATE_CLIENT_STATE,
173175
users: [...get(users).map((u) => ({ ...u, conn: null })), $self],
@@ -186,7 +188,7 @@ const subscriptions = [
186188
progress.set(null);
187189
solved.set(false);
188190

189-
if (isHivemindBrain) {
191+
if (get(isHivemindBrain)) {
190192
users.update((us) =>
191193
us.map((u) => ({ ...u, progress: null, solved: false }))
192194
);

src/js/store.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const defaultUsers = [
1414
},
1515
].slice(1);
1616

17+
export const hivemindBrain = writable(new URLSearchParams(location.search).get('game'));
18+
export const isHivemindBrain = derived(hivemindBrain, $brainid => $brainid === null);
19+
1720
/** @typedef {typeof defaultUsers[0]} User */
1821
/** @typedef {import('peerjs').DataConnection} Connection */
1922

0 commit comments

Comments
 (0)