Skip to content

Commit

Permalink
refactor(constants): append uuid to app name for game id
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 29, 2024
1 parent 7fe78c4 commit 7590324
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<main class="flex justify-center xl:center">
<div class="flex max-xl:hidden">
<h1 class="basis-1/2">Code Arcade</h1>
<h1 class="basis-1/2 self-end">Code Arcade</h1>
<button>► Run</button>
</div>

Expand Down
1 change: 1 addition & 0 deletions src/constants/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GAME_ID = `${import.meta.env.VITE_APP_NAME}-${typeof crypto?.randomUUID == 'function' ? crypto.randomUUID() : Date.now()}`
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './game'
9 changes: 4 additions & 5 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GAME_ID } from '../constants'
import { setData } from '../helpers'
import {
addEventListeners,
Expand Down Expand Up @@ -51,12 +52,10 @@ export async function go(levelNumber: number) {
/**
* Listen to iframe postMessage on level clear.
*/
addEventListener('message', (event) => {
const { origin, data } = event

window.addEventListener('message', (event) => {
if (
!origin.includes(location.origin) ||
data?.source !== import.meta.env.VITE_APP_NAME
!event.origin.includes(location.origin) ||
event.data?.source !== GAME_ID
) {
return
}
Expand Down
4 changes: 3 additions & 1 deletion src/templates/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GAME_ID } from '../constants'

/**
* Creates cursor keys for player.
*
Expand Down Expand Up @@ -39,7 +41,7 @@ onKeyDown((key) => {
export const registerWinCondition = (level: number) => `
onCollide('player', 'exit', () => {
parent.postMessage({
source: ${JSON.stringify(import.meta.env.VITE_APP_NAME)},
source: '${GAME_ID}',
level: ${level + 1},
})
})
Expand Down

0 comments on commit 7590324

Please sign in to comment.