Skip to content

Commit

Permalink
prettier all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrude committed Aug 15, 2023
1 parent fe7524a commit 81f9a08
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 148 deletions.
4 changes: 2 additions & 2 deletions drumline-client/src/lib/editor/word_grouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ const findWordBounds = (
};

// when in the center row, stop searching when we cross the center square
const is_in_center_row = !use_band && (location[0] === grid_attributes.center);
const is_in_center_row = !use_band && location[0] === grid_attributes.center;
const crossed_center = (idx: number): boolean => {
const orig_col = location[1];
const new_col = locations[idx][1];
return (orig_col < grid_attributes.center) !== (new_col < grid_attributes.center);
return orig_col < grid_attributes.center !== new_col < grid_attributes.center;
};

// look backwards for a square that has a letter in it and is not
Expand Down
48 changes: 28 additions & 20 deletions drumline-client/src/lib/network/networked_game_state.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { storedGameState } from '$lib/stores/game_state_store';
import { GameState, UserId, actionToString, areActionsEqual, joinPuzzle, leavePuzzle, set_from_json_struct, stringToAction, to_json_struct, type GameActions, type SimpleJsonGameState } from '@chrisrude/drumline-lib';
import {
GameState,
UserId,
actionToString,
areActionsEqual,
joinPuzzle,
leavePuzzle,
set_from_json_struct,
stringToAction,
to_json_struct,
type GameActions,
type SimpleJsonGameState
} from '@chrisrude/drumline-lib';
import { get } from 'svelte/store';
import { ReconnectWsClient, type WSClientEvent } from './reconnect_ws_client';

export { NetworkedGameState };
export type { StoredGameState };

type StoredGameState = {
game_state_json: SimpleJsonGameState,
pending_actions: GameActions[],
server_update_count: number,
}
game_state_json: SimpleJsonGameState;
pending_actions: GameActions[];
server_update_count: number;
};

// todo: this class is poorly defined and probably shouldn't exist,
// but it's good enough for now. It should be folded into SolveClient.
class NetworkedGameState extends GameState {

// actions that we want to the server, but haven't gotten
// a response for yet. These are all locally-generated.
_pending_actions: GameActions[];
Expand All @@ -34,12 +45,7 @@ class NetworkedGameState extends GameState {
// our connection to the server
readonly _ws_client: ReconnectWsClient;


constructor(
size: number,
solve_id: string,
user_id: UserId,
) {
constructor(size: number, solve_id: string, user_id: UserId) {
super(size, solve_id);
this._pending_actions = [];
this._server_game_state = new GameState(size, solve_id);
Expand All @@ -66,15 +72,12 @@ class NetworkedGameState extends GameState {
return {
game_state_json: to_json_struct(this._server_game_state),
pending_actions: this._pending_actions,
server_update_count: this._server_update_count,
server_update_count: this._server_update_count
};
};

update_from_json(storedGameState: StoredGameState) {
set_from_json_struct(
storedGameState.game_state_json,
this._server_game_state,
)
set_from_json_struct(storedGameState.game_state_json, this._server_game_state);
this._pending_actions = [...storedGameState.pending_actions];
this._server_update_count = storedGameState.server_update_count;

Expand Down Expand Up @@ -144,8 +147,13 @@ class NetworkedGameState extends GameState {
// cursor updates will have a change_count of -1 and are
// not counted as server updates
if (action.change_count !== -1) {
if ((this._server_update_count + 1) !== action.change_count) {
throw new Error('Server update count mismatch: ' + this._server_update_count + ' ' + action.change_count);
if (this._server_update_count + 1 !== action.change_count) {
throw new Error(
'Server update count mismatch: ' +
this._server_update_count +
' ' +
action.change_count
);
}
this._server_update_count += 1;
}
Expand Down Expand Up @@ -179,7 +187,7 @@ class NetworkedGameState extends GameState {
this.apply(pending_action);
}
storedGameState.set(this);
}
};

// returns true if the action was in _pending_actions and was removed
_maybeCompletePendingAction = (action: GameActions): boolean => {
Expand Down
10 changes: 3 additions & 7 deletions drumline-client/src/lib/network/puzzle_rest_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const HEADERS: HeadersInit = {
'Content-Type': 'application/json'
};


//////

// app.get('/puzzles', this.list_puzzles);
Expand All @@ -20,19 +19,16 @@ type PuzzleListResponse = {
result: 'OK';
puzzle_list: PuzzleListInfo[];
};
const puzzles_list = async (
user_id: UserId,
base_url?: string
): Promise<PuzzleListInfo[]> => {
const puzzles_list = async (user_id: UserId, base_url?: string): Promise<PuzzleListInfo[]> => {
const url = (base_url ?? '') + `/puzzles/list/${user_id.private_uuid}`;
const response = await fetch(url, {
method: 'GET',
headers: HEADERS,
headers: HEADERS
});
if (!response.ok) {
throw new Error(`list: ${response.status} ${response.statusText}`);
}
const json = await response.json() as PuzzleListResponse;
const json = (await response.json()) as PuzzleListResponse;
return json.puzzle_list;
};

Expand Down
2 changes: 1 addition & 1 deletion drumline-client/src/lib/network/reconnect_ws_client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WS_BASE_URL } from "$lib/stores/settings_store";
import { WS_BASE_URL } from '$lib/stores/settings_store';

export { ReconnectWsClient };
export type {
Expand Down
16 changes: 8 additions & 8 deletions drumline-client/src/lib/stores/game_state_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const cleanup_game_state = () => {
lastGameState = null;
};

const init_game_state = (solveParams: SolveParams | null, user_id: UserId): NetworkedGameState | null => {
const init_game_state = (
solveParams: SolveParams | null,
user_id: UserId
): NetworkedGameState | null => {
if (!browser) {
return null;
}
Expand All @@ -62,10 +65,7 @@ const init_game_state = (solveParams: SolveParams | null, user_id: UserId): Netw
if (!solveParams) {
return null;
}
const networked_game_state = new NetworkedGameState(
solveParams.size,
solveParams.id,
user_id);
const networked_game_state = new NetworkedGameState(solveParams.size, solveParams.id, user_id);

const key = fn_solve_key(solveParams.id);
const storedString = window.localStorage.getItem(key) ?? null;
Expand All @@ -78,7 +78,8 @@ const init_game_state = (solveParams: SolveParams | null, user_id: UserId): Netw
};

const storedGameState = writable<NetworkedGameState | null>(
init_game_state(convert_params(get(params)), get(userStore)));
init_game_state(convert_params(get(params)), get(userStore))
);

params.subscribe((newParams) => {
const newSolveParams = convert_params(newParams);
Expand All @@ -104,5 +105,4 @@ storedGameState.subscribe((newGameState: NetworkedGameState | null) => {
const savePuzzleInput = (input_text: string) =>
window.localStorage.setItem('drumline-puzzle-input', input_text);

const getPuzzleInput = (): string | null =>
window.localStorage.getItem('drumline-puzzle-input');
const getPuzzleInput = (): string | null => window.localStorage.getItem('drumline-puzzle-input');
9 changes: 5 additions & 4 deletions drumline-client/src/lib/stores/puzzle_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const RECENT_PUZZLE_LIST = 'drumline-recent-puzzle-list';
const puzzle_store_key = (puzzle_id: string): string => PUZZLE_STORE_PREFIX + puzzle_id;

const getCachedPuzzle = (puzzle_id: string): Puzzle | null => {
const storedString = browser ? window.localStorage.getItem(puzzle_store_key(puzzle_id)) ?? null : null;
const storedString = browser
? window.localStorage.getItem(puzzle_store_key(puzzle_id)) ?? null
: null;
if (!storedString) {
return null;
}
Expand All @@ -21,8 +23,7 @@ const getCachedPuzzle = (puzzle_id: string): Puzzle | null => {
const setCachedPuzzle = (puzzle: Puzzle, puzzle_id: string) => {
window.localStorage.setItem(puzzle_store_key(puzzle_id), JSON.stringify(puzzle.original_text));
addRecentPuzzle(puzzle, puzzle_id);
}

};

const initStoredPuzzleList: StartStopNotifier<PuzzleListInfo[]> = (
set: (value: PuzzleListInfo[]) => void
Expand All @@ -44,7 +45,7 @@ const addRecentPuzzle = (puzzle: Puzzle, puzzle_id: string) => {
const newEntry: PuzzleListInfo = {
puzzle_id,
size: puzzle.size,
your_puzzle: false, // todo: is this important?
your_puzzle: false // todo: is this important?
};
list.unshift(newEntry);
}
Expand Down
12 changes: 5 additions & 7 deletions drumline-client/src/lib/stores/settings_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ const DEV_CONNECTION_INFO: ConnectionInfo = {
};

const fn_base_url = (protocol: string, connection_info: ConnectionInfo): string =>
`${protocol}${connection_info.use_tls ? 's' : ''}://${connection_info.host}:${connection_info.port}`;
`${protocol}${connection_info.use_tls ? 's' : ''}://${connection_info.host}:${
connection_info.port
}`;

const HTTP_BASE_URL = fn_base_url("http",
dev ? DEV_CONNECTION_INFO : PROD_CONNECTION_INFO
);
const HTTP_BASE_URL = fn_base_url('http', dev ? DEV_CONNECTION_INFO : PROD_CONNECTION_INFO);

const WS_BASE_URL = fn_base_url("ws",
dev ? DEV_CONNECTION_INFO : PROD_CONNECTION_INFO
);
const WS_BASE_URL = fn_base_url('ws', dev ? DEV_CONNECTION_INFO : PROD_CONNECTION_INFO);
8 changes: 5 additions & 3 deletions drumline-client/src/routes/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ body {
background-attachment: fixed;
background-color: var(--color-bg-1);
background-size: 100vw 100vh;
background-image: radial-gradient(50% 50% at 25% 50%,
background-image: radial-gradient(
50% 50% at 25% 50%,
rgba(100, 142, 210, 0.75) 0%,
rgba(255, 255, 255, 0) 100%),
rgba(255, 255, 255, 0) 100%
),
linear-gradient(180deg, var(--color-bg-0) 0%, var(--color-bg-1) 15%, var(--color-bg-2) 50%);
/* more color ideas https://colorpalettes.io/80s-synthwave-color-palette/ */
overflow-y: hidden;
Expand Down Expand Up @@ -131,4 +133,4 @@ button.big-button {
button.big-button:hover:enabled {
background-color: var(--color-theme-1);
color: var(--color-bg-0);
}
}
2 changes: 0 additions & 2 deletions drumline-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export * from './lib/cell_attributes';
export * from './lib/game_actions';
export * from './lib/game_state';
Expand All @@ -8,4 +7,3 @@ export * from './lib/puzzle_json';
export * from './lib/solver';
export * from './lib/user_id';
export * from './lib/validation';

2 changes: 1 addition & 1 deletion drumline-lib/lib/clue_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const BANDS_HEADER = 'BANDS';
class PuzzleValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "PuzzleValidationError";
this.name = 'PuzzleValidationError';
}
}

Expand Down
12 changes: 9 additions & 3 deletions drumline-lib/lib/game_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ export {
areActionsEqual,
clear,
clearSegment,
cursor, joinPuzzle,
cursor,
joinPuzzle,
leavePuzzle,
markSegment, removeCursor, set,
markSegment,
removeCursor,
set,
stringToAction
};
export type {
ClearActionType,
ClearSegmentActionType,
ClueListActionType, CreatePuzzleActionType, CursorActionType, GameActionKinds,
ClueListActionType,
CreatePuzzleActionType,
CursorActionType,
GameActionKinds,
GameActionType,
GameActions,
GridActionType,
Expand Down
12 changes: 5 additions & 7 deletions drumline-lib/lib/game_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class GameState implements GameStateType {
this.row_answer_segments[i].segments = other_game_state.row_answer_segments[i].segments;
}
for (let i = 0; i < this.band_answer_segments.length; i++) {
this.band_answer_segments[i].segments = other_game_state.band_answer_segments[i].segments;
this.band_answer_segments[i].segments =
other_game_state.band_answer_segments[i].segments;
}
};

Expand Down Expand Up @@ -212,15 +213,12 @@ class GameState implements GameStateType {
};

applyCursorAction = (cursor_action: CursorActionType): void => {
this.cursors.set(
cursor_action.user_id,
[cursor_action.row, cursor_action.col]
);
}
this.cursors.set(cursor_action.user_id, [cursor_action.row, cursor_action.col]);
};

applyRemoveCursorAction = (cursor_action: CursorActionType): void => {
this.cursors.delete(cursor_action.user_id);
}
};

getAnswerSegments = (kind: ClueListKind, index: number): AnswerSegments => {
const is_row = 'row' === kind;
Expand Down
1 change: 0 additions & 1 deletion drumline-lib/lib/game_state_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function set_clue_lists(answer_segments: AnswerSegments[], segment_values_list:
}

function set_from_json_struct(simple_json: SimpleJsonGameState, gameState: GameState): void {

// verify sizes line up
if (simple_json.grid.length != gameState.size) {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions drumline-lib/lib/puzzle_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export { loadPuzzleFromJson, savePuzzleToJson };
export type { PuzzleListInfo };

type PuzzleListInfo = {
puzzle_id: string,
size: number,
your_puzzle: boolean,
puzzle_id: string;
size: number;
your_puzzle: boolean;
};

const loadPuzzleFromJson = (storedString: string): Puzzle | null => {
Expand Down
8 changes: 6 additions & 2 deletions drumline-server/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const SECRET_HASH_ALGORITHM = 'SHA-512';
const puzzleHmac = async (puzzle: Puzzle, salt: string): Promise<string> => {
const ec = new TextEncoder();
const data = ec.encode(puzzle.original_text + salt);
return await subtle.digest(SECRET_HASH_ALGORITHM, data).then((hash) => to_emojis(new Uint8Array(hash)));
return await subtle
.digest(SECRET_HASH_ALGORITHM, data)
.then((hash) => to_emojis(new Uint8Array(hash)));
};

const solveHmac = async (puzzle: Puzzle, creator: UserId, salt: string): Promise<string> => {
Expand All @@ -22,5 +24,7 @@ const solveHmac = async (puzzle: Puzzle, creator: UserId, salt: string): Promise
combined_data.set(puzzle_data);
combined_data.set(creator_data, puzzle_data.length);

return await subtle.digest(SECRET_HASH_ALGORITHM, combined_data).then((hash) => to_emojis(new Uint8Array(hash)));
return await subtle
.digest(SECRET_HASH_ALGORITHM, combined_data)
.then((hash) => to_emojis(new Uint8Array(hash)));
};
Loading

0 comments on commit 81f9a08

Please sign in to comment.