Created
September 6, 2023 17:48
-
-
Save doceazedo/d10cea9074a6f86f9e8f9c181a1c4de0 to your computer and use it in GitHub Desktop.
willotree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { get, writable } from 'svelte/store'; | |
type Profile = { | |
name: string; | |
pictureUrl: string; | |
id: string; | |
}; | |
type GameState = { | |
state: 'IDLE' | 'PLAYING' | 'END'; | |
options: Profile[]; | |
answer: string; | |
roundStartedAt: Date; | |
guesses: { | |
guess: string; | |
answer: string; | |
timeToGuess: number; | |
}[]; | |
}; | |
type Person = { | |
firstName: string; | |
headshot: { | |
alt: string; | |
height: number; | |
id: string; | |
mimeType: string; | |
type: string; | |
url: string; | |
width: number; | |
}; | |
id: string; | |
jobTitle: string; | |
lastName: string; | |
slug: string; | |
socialLinks: { | |
callToAction: string; | |
type: string; | |
url: string; | |
}[]; | |
type: string; | |
}; | |
const initialState: GameState = { | |
state: 'IDLE', | |
options: [], | |
answer: '', | |
roundStartedAt: new Date(), | |
guesses: [] | |
}; | |
export const GAME_STATE = writable<GameState>(initialState); | |
export const ROUNDS = 5; | |
const OPTIONS_COUNT = 6; | |
let people: Person[] = []; | |
const fetchPeople = async () => { | |
try { | |
const resp = await fetch('https://namegame.willowtreeapps.com/api/v1.0/profiles'); | |
const data = await resp.json(); | |
people = data; | |
} catch (e: any) { | |
// Handle errors | |
} | |
}; | |
export const startGame = async () => { | |
await fetchPeople(); | |
newRound(); | |
GAME_STATE.set({ | |
...get(GAME_STATE), | |
state: 'PLAYING' | |
}); | |
}; | |
export const newRound = () => { | |
const { options, answer } = getOptions(); | |
GAME_STATE.set({ | |
...get(GAME_STATE), | |
roundStartedAt: new Date(), | |
options, | |
answer | |
}); | |
}; | |
const getOptions = () => { | |
let answer = ''; | |
const poolOfOptions = people.slice(); | |
const options = Array(OPTIONS_COUNT) | |
.fill(null) | |
.map((_, i) => { | |
const randomIdx = Math.floor(Math.random() * poolOfOptions.length); | |
const person = poolOfOptions[randomIdx]; | |
// remove the current option from the current | |
// pool of options so we don't get duplicates | |
poolOfOptions.splice(randomIdx, 1); | |
// if this is the first option we are making, | |
// consider it the right answer and remove it | |
// from the people array. | |
if (i == 0) { | |
people.splice(randomIdx, 1); | |
answer = person.id; | |
} | |
return { | |
name: `${person.firstName} ${person.lastName}`, | |
id: person.id, | |
pictureUrl: person.headshot.url | |
}; | |
}) | |
.sort(() => Math.floor(Math.random() * 2) - 1); | |
return { options, answer }; | |
}; | |
export const makeGuess = (id: string) => { | |
const state = get(GAME_STATE); | |
GAME_STATE.set({ | |
...state, | |
guesses: [ | |
...state.guesses, | |
{ | |
guess: id, | |
answer: state.answer, | |
timeToGuess: new Date().getTime() - state.roundStartedAt.getTime() | |
} | |
] | |
}); | |
}; | |
export const nextRound = () => { | |
const state = get(GAME_STATE); | |
if (state.guesses.length >= ROUNDS) return endGame(); | |
newRound(); | |
}; | |
const endGame = () => { | |
GAME_STATE.set({ | |
...get(GAME_STATE), | |
state: 'END' | |
}); | |
}; | |
export const restartGame = () => { | |
GAME_STATE.set(initialState); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment