Skip to content

Commit

Permalink
game: move all remaining actions to custom store
Browse files Browse the repository at this point in the history
Nice cleanups in main app.
  • Loading branch information
jnikula committed Mar 1, 2024
1 parent da6b159 commit b1c22ec
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 77 deletions.
92 changes: 16 additions & 76 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@
return `piste-on-piste-save-${slot}`;
}
function undo_stack_push(s: State, autosave: boolean = true): State {
$game.undo_stack.splice(++$game.undo_index, $game.undo_stack.length, s);
if (autosave)
game.save();
return s;
}
type SaveGameId = {
slot: number;
timestamp: number;
Expand Down Expand Up @@ -190,10 +181,7 @@
if (!ui_can_new_game)
return;
$game.state = new State(ui_names);
// Don't autosave before first shot
undo_stack_push($game.state, false);
game.new_game(ui_names);
save_names(ui_names);
Expand All @@ -207,76 +195,32 @@
}
// ui actions, each need to handle undo
function ui_pot_ball(value: number): void {
let s: State = $game.state.deepcopy();
s.pot_ball(value);
$game.state = undo_stack_push(s);
}
function ui_plus_balls(): void {
let s: State = $game.state.deepcopy();
s.plus_balls();
$game.state = undo_stack_push(s);
}
function ui_minus_balls(): void {
let s: State = $game.state.deepcopy();
s.minus_balls();
$game.state = undo_stack_push(s);
}
function ui_commit_foul(value: number): void {
let s: State = $game.state.deepcopy();
s.commit_foul(value);
$game.state = undo_stack_push(s);
}
function ui_click_player(player: Player): void {
let s: State = $game.state.deepcopy();
// FIXME: don't duplicate the conditions here and in html
if ($game.state.is_current_player(player.pid) && $game.state.can_end_turn())
s.end_turn();
game.end_turn();
else if ($game.state.can_foul_retake() && $game.state.is_previous_player(player.pid))
s.foul_retake();
else
return;
$game.state = undo_stack_push(s);
game.foul_retake();
}
function ui_click_player_more(player: Player): void {
let s: State = $game.state.deepcopy();
// FIXME: don't duplicate the conditions here and in html
if ($game.state.can_concede(player.pid))
s.concede(player.pid);
game.concede(player.pid);
else if ($game.state.can_declare_winner(player.pid))
s.declare_winner(player.pid);
else
return;
$game.state = undo_stack_push(s);
game.declare_winner(player.pid);
}
function ui_player_edit_points(pid: number, amount: number): void {
let s: State = $game.state.deepcopy();
if ($game.state.can_player_edit_points(pid, amount))
s.player_edit_points(pid, amount);
else
return;
$game.state = undo_stack_push(s);
game.edit_points(pid, amount);
}
function ui_new_frame(): void {
if (!$game.state.can_new_frame())
return;
let s: State = $game.state.deepcopy();
s.new_frame();
$game.state = undo_stack_push(s);
game.new_frame();
ui_page = UiPage.PLAY;
}
Expand Down Expand Up @@ -324,22 +268,18 @@
}
function ui_key_end_turn(): void {
let s: State = $game.state.deepcopy();
if ($game.state.can_end_turn())
s.end_turn();
$game.state = undo_stack_push(s)
game.end_turn();
}
function ui_key_pot_ball(value: number): void {
if ($game.state.can_pot_ball(value))
ui_pot_ball(value);
game.pot_ball(value);
}
function ui_key_commit_foul(value: number): void {
if ($game.state.can_commit_foul(value))
ui_commit_foul(value);
game.commit_foul(value);
}
function ui_key_undo(): void {
Expand All @@ -354,12 +294,12 @@
function ui_key_plus_balls(): void {
if ($game.state.can_plus_balls())
ui_plus_balls();
game.plus_balls();
}
function ui_key_minus_balls(): void {
if ($game.state.can_minus_balls())
ui_minus_balls();
game.minus_balls();
}
function ui_key_down(event: KeyboardEvent) {
Expand Down Expand Up @@ -500,7 +440,7 @@
{#each [1,2,3,4,5,6,7] as value}
<Ball value={value}
active={$game.state.can_pot_ball(value)}
action={() => ui_pot_ball(value)}>
action={() => game.pot_ball(value)}>
{value}
{#if value === 7 && $game.state.respot_black }
<div class='respot'>re-spot!</div>
Expand All @@ -526,7 +466,7 @@
{#each [4,5,6,7] as value}
<Ball value={0}
active={$game.state.can_commit_foul(value)}
action={() => ui_commit_foul(value)}>
action={() => game.commit_foul(value)}>
{value}
</Ball>
{/each}
Expand Down Expand Up @@ -629,12 +569,12 @@
<div class='label'>Fix</div>
<Ball value={0}
active={$game.state.can_minus_balls()}
action={() => ui_minus_balls()}>
action={() => game.minus_balls()}>
-
</Ball>
<Ball value={0}
active={$game.state.can_plus_balls()}
action={() => ui_plus_balls()}>
action={() => game.plus_balls()}>
+
</Ball>
<div class='label'></div>
Expand Down
88 changes: 87 additions & 1 deletion src/lib/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2024 Jani Nikula <jani@nikula.org>

import { writable } from 'svelte/store';
import type State from './State';
import State from './State';

class Game {
undo_stack: State[] = [];
Expand Down Expand Up @@ -40,6 +40,81 @@ class Game {

this.state = this.undo_stack[++this.undo_index];
}

_push(): void {
let s: State = this.state.deepcopy();
this.undo_stack.splice(++this.undo_index, this.undo_stack.length, s);
this.state = s;
}

_pot_ball(value: number): void {
this._push();
this.state.pot_ball(value);
this._save();
}

_plus_balls(): void {
this._push();
this.state.plus_balls();
this._save();
}

_minus_balls(): void {
this._push();
this.state.minus_balls();
this._save();
}

_commit_foul(value: number): void {
this._push();
this.state.commit_foul(value);
this._save();
}

_end_turn(): void {
this._push();
this.state.end_turn();
this._save();
}

_foul_retake(): void {
this._push();
this.state.foul_retake();
this._save();
}

_concede(pid: number): void {
this._push();
this.state.concede(pid);
this._save();
}

_declare_winner(pid: number): void {
this._push();
this.state.declare_winner(pid);
this._save();
}

_edit_points(pid: number, amount: number): void {
this._push();
this.state.player_edit_points(pid, amount);
this._save();
}

_new_frame(): void {
this._push();
this.state.new_frame();
this._save();
}

// FIXME: use real type
_new_game(names: any[]): void {
let s: State = new State(names);
this.undo_stack.splice(++this.undo_index, this.undo_stack.length, s);
this.state = s;

// Note: Don't autosave before first shot
}
};

function create_game(_game: Game) {
Expand All @@ -52,6 +127,17 @@ function create_game(_game: Game) {
undo: () => update((val) => { val._undo(); return val; }),
redo: () => update((val) => { val._redo(); return val; }),
save: () => update((val) => { val._save(); return val; }),
pot_ball: (value: number) => update((val) => { val._pot_ball(value); return val; }),
plus_balls: () => update((val) => { val._plus_balls(); return val; }),
minus_balls: () => update((val) => { val._minus_balls(); return val; }),
commit_foul: (value: number) => update((val) => { val._commit_foul(value); return val; }),
end_turn: () => update((val) => { val._end_turn(); return val; }),
foul_retake: () => update((val) => { val._foul_retake(); return val; }),
concede: (pid: number) => update((val) => { val._concede(pid); return val; }),
declare_winner: (pid: number) => update((val) => { val._declare_winner(pid); return val; }),
edit_points: (pid: number, amount: number) => update((val) => { val._edit_points(pid, amount); return val; }),
new_frame: () => update((val) => { val._new_frame(); return val; }),
new_game: (names: any[]) => update((val) => { val._new_game(names); return val; }),
};
}

Expand Down

0 comments on commit b1c22ec

Please sign in to comment.