Skip to content

Commit

Permalink
Merge pull request #169 from Koniverse/koni/dev/issue-46
Browse files Browse the repository at this point in the history
[Issue-46] Mythical_App - rank on new weekly score on Event Result screen
  • Loading branch information
saltict authored Nov 27, 2024
2 parents 6ad60ab + c06a475 commit 3959451
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/extension-koni-ui/src/Popup/Home/Games/gameSDK.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2019-2022 @subwallet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { BuyInGameItemResponse, ErrorCode, GetLeaderboardRequest, GetLeaderboardResponse, HapticFeedbackType, InGameItem, NewGamePlayPayload, Player, PlaynationSDKError, PlayResponse, SDKInitParams, Tournament, UpdateStatePayload, UseInGameItemResponse } from '@playnation/game-sdk';
import { BuyInGameItemResponse, ErrorCode, GetLeaderboardRequest, HapticFeedbackType, InGameItem, NewGamePlayPayload, Player, PlaynationSDKError, PlayResponse, SDKInitParams, Tournament, UpdateStatePayload, UseInGameItemResponse } from '@playnation/game-sdk';
import { GameState } from '@playnation/game-sdk/dist/types';
import { SWStorage } from '@subwallet/extension-base/storage';
import { addLazy, createPromiseHandler, removeLazy } from '@subwallet/extension-base/utils';
import { BookaSdk } from '@subwallet/extension-koni-ui/connector/booka/sdk';
import { Game, GameEvent } from '@subwallet/extension-koni-ui/connector/booka/types';
import { Game, GameEvent, LeaderboardGroups, LeaderboardInfo, LeaderboardPerson } from '@subwallet/extension-koni-ui/connector/booka/types';
import { TelegramConnector } from '@subwallet/extension-koni-ui/connector/telegram';
import { camelCase } from 'lodash';
import z from 'zod';
Expand All @@ -19,6 +19,16 @@ export interface GameAppOptions {
onExit: () => void;
}

export type LeaderboardItem = {
rank: number;
score: number;
}

export interface GetLeaderboardResponse {
players: LeaderboardPerson[];
me?: LeaderboardItem;
}

const cloudStorage = SWStorage.instance;
const telegramConnector = TelegramConnector.instance;

Expand Down Expand Up @@ -71,15 +81,14 @@ export class GameApp {
const playerId = `${account?.info?.telegramId || 'player'}-${account?.info.id || 0}`;
const gameData = (account?.gameData || []).find((item) => item.gameId === this.currentGameInfo.id);
const point = gameData?.point || 0;

const state = await this.gameStateHandler.promise;

const player: Player = {
totalScore: 0,
id: playerId,
balance: point,
name: `${account?.info?.firstName || ''} ${account?.info?.lastName || ''}` || 'Player',
avatar: 'https://thispersondoesnotexist.com/',
avatar: account?.info.photoUrl,
energy: account?.attributes?.energy || 0,
pointConversionRate: this.currentGameInfo.pointConversionRate || 0,
gameEnergy: this.currentGameInfo.energyPerGame,
Expand Down Expand Up @@ -282,14 +291,40 @@ export class GameApp {
}

onShowLeaderboard () {
console.log('show leaderboard');
window.location.href = '/home/leaderboard';
}

onShowShop () {
console.log('open shop');
}

onGetLeaderboard (req: GetLeaderboardRequest): GetLeaderboardResponse {
async onGetLeaderboard (req: GetLeaderboardRequest): Promise<GetLeaderboardResponse> {
const leaderboardGeneral = this.apiSDK.leaderboardConfig.leaderboard_general as unknown as LeaderboardGroups[];
const leaderboards = this.apiSDK.leaderboardConfig.leaderboard_map as unknown as LeaderboardInfo[];

if (leaderboardGeneral && leaderboards) {
const firstLeaderboardGroups = leaderboardGeneral[0];

if (!firstLeaderboardGroups || !firstLeaderboardGroups.leaderboards.length) {
return { players: [] };
}

const weekLeaderBoard = leaderboards.find((l) => l.id === firstLeaderboardGroups.leaderboards[0]?.id);

if (!weekLeaderBoard) {
return { players: [] };
}

const mapLeader = await this.apiSDK.fetchLeaderboard(weekLeaderBoard.id);
const mineRanked = mapLeader.results.find((item) => item.mine);

if (!mineRanked) {
return { players: mapLeader.results };
}

return { players: mapLeader.results, me: { score: mineRanked.point, rank: mineRanked.rank } };
}

return { players: [] };
}

Expand Down

0 comments on commit 3959451

Please sign in to comment.