Skip to content

Commit

Permalink
Merge branch 'refs/heads/koni/dev/issue-task-share-leaderboard' into …
Browse files Browse the repository at this point in the history
…telegram-dev
  • Loading branch information
anhnhu committed Jun 11, 2024
2 parents 486a430 + d010c22 commit f267ee8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
19 changes: 14 additions & 5 deletions packages/extension-koni-ui/src/Popup/Home/Mission/TaskItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { SWTransactionResponse } from '@subwallet/extension-base/services/transaction-service/types';
import { GamePoint } from '@subwallet/extension-koni-ui/components';
import { BookaSdk } from '@subwallet/extension-koni-ui/connector/booka/sdk';
import { Task, TaskHistoryStatus } from '@subwallet/extension-koni-ui/connector/booka/types';
import { ShareLeaderboard, Task, TaskHistoryStatus } from '@subwallet/extension-koni-ui/connector/booka/types';
import { TelegramConnector } from '@subwallet/extension-koni-ui/connector/telegram';
import { useNotification, useSetCurrentPage, useTranslation } from '@subwallet/extension-koni-ui/hooks';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
Expand Down Expand Up @@ -112,6 +112,15 @@ const _TaskItem = ({ actionReloadPoint, className, task }: Props): React.ReactEl
extrinsicHash = res.extrinsicHash || '';
}

let shareLeaderboard: ShareLeaderboard | null = null;

if (task.share_leaderboard) {
try {
shareLeaderboard = JSON.parse(task.share_leaderboard) as ShareLeaderboard;
} catch (e) {
console.error('shareLeaderboard', e);
}
}
apiSDK.finishTask(taskId, extrinsicHash, networkKey)
.finally(() => {
setTaskLoading(false);
Expand All @@ -129,11 +138,11 @@ const _TaskItem = ({ actionReloadPoint, className, task }: Props): React.ReactEl
let urlRedirect = task.url;

if (urlRedirect) {
if (urlRedirect === 'share_airdrop') {
const startEnv = process.env.KARURA_PLAYDROP_START_DATE || '2024-06-01 03:00:00' as string;
const endEnv = process.env.KARURA_PLAYDROP_END_DATE || '2024-06-15 00:00:00' as string;
if (shareLeaderboard && shareLeaderboard.content) {
const startEnv = shareLeaderboard.start_time;
const endEnv = shareLeaderboard.end_time;

urlRedirect = await apiSDK.getShareTwitterURL(startEnv, endEnv);
urlRedirect = await apiSDK.getShareTwitterURL(startEnv, endEnv, shareLeaderboard.content, task.gameId ?? 0, shareLeaderboard.url);
}

telegramConnector.openLink(urlRedirect);
Expand Down
22 changes: 12 additions & 10 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { InGameItem } from '@subwallet/extension-koni-ui/Popup/Home/Games/types'
import { calculateStartAndEnd, formatDateFully } from '@subwallet/extension-koni-ui/utils/date';
import fetch from 'cross-fetch';
import { BehaviorSubject } from 'rxjs';
import { populateTemplateString } from '@subwallet/extension-koni-ui/utils';

export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz';
export const TELEGRAM_WEBAPP_LINK = process.env.TELEGRAM_WEBAPP_LINK || 'Playnation_bot/app';
Expand Down Expand Up @@ -336,25 +337,26 @@ export class BookaSdk {
return `http://x.com/share?text=${content}&url=${linkApp}`;
}

async getShareTwitterURL (startDate: string, endDate: string) {
async getShareTwitterURL (startDate: string, endDate: string, content: string, gameId: number, url: string) {
const start = formatDateFully(new Date(startDate));
const end = formatDateFully(new Date(endDate));
const leaderBoard = await this.postRequest<LeaderboardPerson[]>(`${GAME_API_HOST}/api/game/leader-board`, { startDate: start, endDate: end, limit: 1 });
const leaderBoard = await this.postRequest<LeaderboardPerson[]>(`${GAME_API_HOST}/api/game/leader-board`,
{
startDate: start,
endDate: end,
gameId: gameId,
limit: 1 });

const personMine = leaderBoard.find((item) => item.mine);
let content = 'A new exciting game is in town, Karura Token Playdrop! Want some fun and a chance to win Karura airdrop? Join me NOW 👇%0A';
let contentShare = '';

if (personMine) {
const result = `Wooho, I got ${personMine.point} points and ranked ${personMine.rank} on the Karura Token Playdrop leaderboard 🔥`;

content = `${result} Want some fun and a chance to win Karura airdrop? Join me NOW 👇%0A`;
contentShare = `text=${populateTemplateString(content, personMine)}%0A&`;
}

const urlShareImage = 'https://x.playnation.app/playnation-share-karura';

const linkShare = `${urlShareImage}?startApp=${this.account?.info.inviteCode || 'booka'}`;
const linkShare = `${url}?startApp=${this.account?.info.inviteCode || 'booka'}`;

return `http://x.com/share?text=${content}&url=${linkShare}`;
return `http://x.com/share?${contentShare}url=${linkShare}`;
}

async fetchReferalList () {
Expand Down
9 changes: 9 additions & 0 deletions packages/extension-koni-ui/src/connector/booka/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export enum TaskHistoryStatus {
COMPLETED = 'completed',
}

export interface ShareLeaderboard {
content: string;
url: string;
start_time: string;
end_time: string;

}

export interface Task {
id: number; // id on db
contentId: number;
Expand All @@ -109,6 +117,7 @@ export interface Task {
status: TaskHistoryStatus;
completedAt?: string;
taskHistoryId?: number;
share_leaderboard?: string | null;
}

export interface TaskCategory {
Expand Down
1 change: 1 addition & 0 deletions packages/extension-koni-ui/src/utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './function';
export * from './getLanguageOptions';
export * from './i18n';
export * from './number';
export * from './string';
27 changes: 27 additions & 0 deletions packages/extension-koni-ui/src/utils/common/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// [object Object]
// SPDX-License-Identifier: Apache-2.0

// eslint-disable-next-line header/header
export function populateTemplateString (template: string, data: any): string {
if (!data) {
return template;
}

return template.replace(/{(.*?)}/g, (match: string, p1: string): string => {
const keys = p1.split('.');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
let value = data;

for (const key of keys) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
value = value[key];

if (value === undefined) {
return '';
}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return value;
});
}
14 changes: 7 additions & 7 deletions packages/extension-koni-ui/src/utils/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// SPDX-License-Identifier: Apache-2.0
const formatDate = (date: Date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0
const day = String(date.getUTCDate()).padStart(2, '0');

return `${year}-${month}-${day}`;
};

export const formatDateFully = (date: Date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0
const day = String(date.getDate()).padStart(2, '0');
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
const second = String(date.getSeconds()).padStart(2, '0');
const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0
const day = String(date.getUTCDate()).padStart(2, '0');
const hour = String(date.getUTCHours()).padStart(2, '0');
const minute = String(date.getUTCMinutes()).padStart(2, '0');
const second = String(date.getUTCSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};
Expand Down

0 comments on commit f267ee8

Please sign in to comment.