57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { AxiosInstance } from 'axios';
|
|
import { HttpInstanceFactory } from '@/utils/HttpInstanceFactory';
|
|
import { IPlayer, IUpdatePlayerTask } from '@/interfaces/player.type';
|
|
|
|
export class PlayerApi {
|
|
private static instance: PlayerApi | null = null;
|
|
private httpInstance: AxiosInstance;
|
|
private populateString: string =
|
|
'populate=archive_request.rejected_requests&populate=archive_request.exchange_requests&populate=finished_tasks.task.icon&populate=referral.my_referrals&populate=my_referrals.my_referrals';
|
|
|
|
private constructor(token?: string) {
|
|
this.httpInstance = HttpInstanceFactory.getAuthorizedInstance(token);
|
|
}
|
|
|
|
public static getInstance(token?: string): PlayerApi {
|
|
if (this.instance) return this.instance;
|
|
this.instance = new PlayerApi(token);
|
|
return this.instance;
|
|
}
|
|
|
|
async findPlayerByAuthId(authId: string): Promise<IPlayer | null> {
|
|
const filteredPlayers: IPlayer[] = (
|
|
await this.httpInstance.get(
|
|
`/players/?filters[auth_id]=${authId}&${this.populateString}`,
|
|
)
|
|
).data.data;
|
|
return filteredPlayers.length > 0 ? filteredPlayers[0] : null;
|
|
}
|
|
|
|
async startFarm(startFarm: Date, playerId: number): Promise<IPlayer> {
|
|
return (
|
|
await this.httpInstance.put<{ data: IPlayer }>(
|
|
`/players/${playerId}?${this.populateString}`,
|
|
{
|
|
data: { start_farm: startFarm },
|
|
},
|
|
)
|
|
).data.data;
|
|
}
|
|
|
|
async claimRewards(playerId: number, balance: number): Promise<IPlayer> {
|
|
return (
|
|
await this.httpInstance.put<{ data: IPlayer }>(
|
|
`/players/${playerId}?${this.populateString}`,
|
|
{
|
|
data: { balance: balance, start_farm: null },
|
|
},
|
|
)
|
|
).data.data;
|
|
}
|
|
|
|
async updateTasks(playerId: number, data: IUpdatePlayerTask): Promise<IPlayer> {
|
|
return (await this.httpInstance.put<IPlayer>(`/players/finished-tasks/${playerId}`, data))
|
|
.data;
|
|
}
|
|
}
|