@@ -2,9 +2,9 @@ import {Scene} from 'phaser';
22import { displayPlayer , globalConsts } from '../main.ts' ;
33import { formatTime } from '../thatFolder/ThatPlayer.ts' ;
44import { Button } from '../custom_classes/Button.ts' ;
5+ import { fetchLeaderboard , removeEntry , sortedLeaderboard , sortLeaderboard } from './Leaderboard.ts' ;
56import Text = Phaser . GameObjects . Text ;
67import Rectangle = Phaser . GameObjects . Rectangle ;
7- import { fetchLeaderboard , removeEntry , sortedLeaderboard , sortLeaderboard } from './Leaderboard.ts' ;
88
99// config
1010const range : number = 2 ;
@@ -64,7 +64,6 @@ export class GameOver extends Scene {
6464
6565 // Save score button
6666 saveButton = new Button ( 700 , 700 , 7 , "button_save" , scene , ( ) => prompt ( ) ) ;
67- // TODO | add "to full leaderboard" button
6867
6968 // Clicker
7069 const blocker : Rectangle = this . add . rectangle ( 0 , 0 , globalConsts . gameWidth , globalConsts . gameHeight , 0x000000 , 0.001 )
@@ -91,7 +90,7 @@ function prompt(): void {
9190 // Cancel
9291 if ( prompt == null ) return ;
9392
94- // YOU cannot be used
93+ // " YOU" cannot be used
9594 if ( prompt . toUpperCase ( ) == "YOU" ) {
9695 alert ( "This cannot be used as name" ) ;
9796 return ;
@@ -133,7 +132,7 @@ function prompt(): void {
133132
134133 // Removes and rerenders leaderboard
135134 clearsLeaderboardLine ( ) ;
136- renderLeaderboard ( ) ;
135+ renderLeaderboard ( ) . then ( ) ;
137136
138137 // Resets button
139138 saveButton . setImage ( "button_saved" ) ;
@@ -229,10 +228,12 @@ export async function generateCode(): Promise<string | undefined> {
229228async function saveLeaderboard ( name : string , key : string | null , value : number ) : Promise < Response | undefined > {
230229 // Local-storage
231230 if ( globalConsts . apiURL == undefined ) {
231+ const oldEntry : leaderboardEntry | undefined = getEntryByName ( name ) ;
232+ if ( oldEntry && oldEntry . score > value ) return new Response ( JSON . stringify ( { success : false } ) , { status : 208 } ) ; // Better score exists
232233 sortedLeaderboard ?. push ( { name : name , score : value } ) ; // Adds entry
233234 sortedLeaderboard ?. filter ( entry => entry . name !== "YOU" ) ; // Removes YOU
234235 localStorage . setItem ( "leaderboard" , JSON . stringify ( sortedLeaderboard , null , 0 ) )
235- return new Response ( JSON . stringify ( { success : true } ) , { status : 200 } ) ;
236+ return new Response ( JSON . stringify ( { success : true } ) , { status : 200 } ) ;
236237 }
237238
238239 // Request info
@@ -254,3 +255,8 @@ async function saveLeaderboard(name: string, key: string | null, value: number):
254255 return undefined ;
255256 }
256257}
258+
259+ // Gets leaderboard entry by name
260+ function getEntryByName ( name : string ) : leaderboardEntry | undefined {
261+ return sortedLeaderboard ?. find ( entry => entry . name === name ) ;
262+ }
0 commit comments