I swear I'm at my limit
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import {scoreStore} from "./scoreStore";
|
||||
import {Score} from "../models/score";
|
||||
|
||||
const api_url = import.meta.env.VITE_API_URL
|
||||
const api_url = import.meta.env.VITE_CLIENT_API_URL
|
||||
|
||||
export async function submitScore(owner: string, value : number){
|
||||
return await handleRequest(`${api_url}/submit/`,
|
||||
|
||||
@@ -1,42 +1,37 @@
|
||||
import {writable} from "svelte/store";
|
||||
import {Score} from "../models/score";
|
||||
import {browser} from "$app/environment"
|
||||
import {getLeaderboard, submitScore} from "./requests";
|
||||
|
||||
let localStorageKey = "scores"
|
||||
let scores : Score[] = []
|
||||
|
||||
function isBrowser() {
|
||||
return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
|
||||
function sort(scores : Score[]){
|
||||
if(scores.length >= 2) scores.sort((s1 : Score, s2 : Score) => s1.value - s2.value)
|
||||
return scores;
|
||||
}
|
||||
|
||||
function createStore(scores : Score[]){
|
||||
//const storedValue = isBrowser() ? localStorage.getItem(localStorageKey) : null;
|
||||
function createStore(){
|
||||
|
||||
const { set, update, subscribe } = writable<Score[]>(scores);
|
||||
|
||||
//if (isBrowser()) subscribe(value => localStorage.setItem(localStorageKey, JSON.stringify(value)));
|
||||
|
||||
return {
|
||||
update,
|
||||
subscribe,
|
||||
set : (value : Score[]) => set(!value ? [] : value),
|
||||
reset : () => set([]),
|
||||
|
||||
add : async (playerName : string, value : number) => {
|
||||
|
||||
await submitScore(playerName, value)
|
||||
|
||||
update(scores => {
|
||||
let s = [...scores, new Score({playerName : playerName, value : value})];
|
||||
if(s.length >= 2) s.sort((s1 : Score, s2 : Score) => s1.value - s2.value)
|
||||
return s;
|
||||
return sort(s);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
loadData: async () => {
|
||||
if(scores.length !== 0) return;
|
||||
let res = sort(await getLeaderboard());
|
||||
set(res);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function createStoreFromAPI(){
|
||||
let scores = await getLeaderboard();
|
||||
return createStore(scores)
|
||||
}
|
||||
|
||||
export const scoreStore= createStoreFromAPI();
|
||||
export const scoreStore = createStore();
|
||||
Reference in New Issue
Block a user