radames commited on
Commit
d6bb1e0
β€’
1 Parent(s): 15ade97

try again on fail

Browse files
frontend/src/lib/Message.svelte CHANGED
@@ -1,15 +1,30 @@
1
  <script lang="ts">
2
  import { fade } from 'svelte/transition';
 
 
 
 
 
 
3
  export let message: string | null = null;
4
  </script>
5
 
6
  <div class="message" transition:fade>
7
  {message}
 
 
 
 
 
 
 
 
 
8
  </div>
9
 
10
  <style lang="postcss" scoped>
11
  .message {
12
  @apply absolute left-1/2 top-1/2 text-white bg-black bg-opacity-80 font-semibold
13
- p-5 z-20 rounded-sm -translate-x-1/2 transition-opacity duration-300 ease-in-out;
14
  }
15
  </style>
 
1
  <script lang="ts">
2
  import { fade } from 'svelte/transition';
3
+ import { GameState } from '../types';
4
+ import { createEventDispatcher } from 'svelte';
5
+
6
+ const dispatch = createEventDispatcher();
7
+
8
+ export let gameState: GameState;
9
  export let message: string | null = null;
10
  </script>
11
 
12
  <div class="message" transition:fade>
13
  {message}
14
+ {#if gameState === GameState.FAIL}
15
+ <div class="font-light flex-1 text-xs sm:text-base text-center">
16
+ <button
17
+ on:click={() => dispatch('restart')}
18
+ class="hover:no-underline underline underline-offset-2 hover:scale-105 transition-all duration-200 ease-in-out"
19
+ >Try Again</button
20
+ >
21
+ </div>
22
+ {/if}
23
  </div>
24
 
25
  <style lang="postcss" scoped>
26
  .message {
27
  @apply absolute left-1/2 top-1/2 text-white bg-black bg-opacity-80 font-semibold
28
+ text-center p-5 z-20 rounded-sm -translate-x-1/2 transition-opacity duration-300 ease-in-out;
29
  }
30
  </style>
frontend/src/routes/index.svelte CHANGED
@@ -1,6 +1,6 @@
1
  <script lang="ts">
2
  // original code inspired by Evan You https://github.com/yyx990803/vue-wordle/
3
- import { LetterState } from '../types';
4
  import type { Board, PromptsData, SuccessPrompt } from '../types';
5
  import { clearTile, fillTile } from '$lib/utils';
6
 
@@ -40,18 +40,18 @@
40
  // Feedback state: message and shake
41
  let message = '';
42
  let shakeRowIndex = -1;
43
- let success = false;
44
  // Handle keyboard input.
45
  let allowInput = true;
46
 
47
  function restartBoard() {
48
  //reset all states
49
- success = false;
50
  shakeRowIndex = -1;
51
  message = '';
52
  currentRowIndex = 0;
53
- letterStates = {}
54
- allowInput= true;
55
 
56
  const prompts: string[] = Object.keys(promptsData);
57
  currPromptIndex = ~~(Math.random() * prompts.length);
@@ -133,7 +133,7 @@
133
  if (currentRow.every((tile) => tile.state === LetterState.CORRECT)) {
134
  // yay!
135
  setTimeout(() => {
136
- success = true;
137
  }, totalTime);
138
  } else if (currentRowIndex < board.length - 1) {
139
  // go the next row
@@ -143,6 +143,7 @@
143
  }, totalTime);
144
  } else {
145
  // game over :(
 
146
  setTimeout(() => {
147
  showMessage(answer.toUpperCase(), -1);
148
  }, totalTime);
@@ -175,9 +176,9 @@
175
  {#if board !== undefined}
176
  <div class="max-w-screen-lg mx-auto px-1 relative z-0 mt-3">
177
  {#if message}
178
- <Message {message} />
179
  {/if}
180
- {#if success}
181
  <Result {board} {currentRowIndex} {imagePaths} on:restart={restartBoard} />
182
  {/if}
183
  <!-- <div class="message" transition:fade>
@@ -210,7 +211,7 @@
210
  <div class="board">
211
  {#each board as row, index}
212
  <div
213
- class="row {shakeRowIndex === index && 'shake'} {success &&
214
  currentRowIndex === index &&
215
  'jump'}"
216
  >
 
1
  <script lang="ts">
2
  // original code inspired by Evan You https://github.com/yyx990803/vue-wordle/
3
+ import { LetterState, GameState } from '../types';
4
  import type { Board, PromptsData, SuccessPrompt } from '../types';
5
  import { clearTile, fillTile } from '$lib/utils';
6
 
 
40
  // Feedback state: message and shake
41
  let message = '';
42
  let shakeRowIndex = -1;
43
+ let gameState: GameState = GameState.PLAYING;
44
  // Handle keyboard input.
45
  let allowInput = true;
46
 
47
  function restartBoard() {
48
  //reset all states
49
+ gameState = GameState.PLAYING;
50
  shakeRowIndex = -1;
51
  message = '';
52
  currentRowIndex = 0;
53
+ letterStates = {};
54
+ allowInput = true;
55
 
56
  const prompts: string[] = Object.keys(promptsData);
57
  currPromptIndex = ~~(Math.random() * prompts.length);
 
133
  if (currentRow.every((tile) => tile.state === LetterState.CORRECT)) {
134
  // yay!
135
  setTimeout(() => {
136
+ gameState = GameState.SUCESS;
137
  }, totalTime);
138
  } else if (currentRowIndex < board.length - 1) {
139
  // go the next row
 
143
  }, totalTime);
144
  } else {
145
  // game over :(
146
+ gameState = GameState.FAIL;
147
  setTimeout(() => {
148
  showMessage(answer.toUpperCase(), -1);
149
  }, totalTime);
 
176
  {#if board !== undefined}
177
  <div class="max-w-screen-lg mx-auto px-1 relative z-0 mt-3">
178
  {#if message}
179
+ <Message {message} {gameState} on:restart={restartBoard}/>
180
  {/if}
181
+ {#if gameState === GameState.SUCESS}
182
  <Result {board} {currentRowIndex} {imagePaths} on:restart={restartBoard} />
183
  {/if}
184
  <!-- <div class="message" transition:fade>
 
211
  <div class="board">
212
  {#each board as row, index}
213
  <div
214
+ class="row {shakeRowIndex === index && 'shake'} {gameState == GameState.SUCESS &&
215
  currentRowIndex === index &&
216
  'jump'}"
217
  >
frontend/src/types.ts CHANGED
@@ -5,6 +5,11 @@ export const enum LetterState {
5
  ABSENT = 'absent'
6
  }
7
 
 
 
 
 
 
8
  export interface Tile {
9
  letter: string;
10
  state: LetterState;
@@ -19,4 +24,4 @@ export interface PromptsData {
19
  export interface SuccessPrompt {
20
  prompt: string;
21
  idx: number;
22
- }
 
5
  ABSENT = 'absent'
6
  }
7
 
8
+ export const enum GameState {
9
+ PLAYING,
10
+ SUCESS,
11
+ FAIL
12
+ }
13
  export interface Tile {
14
  letter: string;
15
  state: LetterState;
 
24
  export interface SuccessPrompt {
25
  prompt: string;
26
  idx: number;
27
+ }