mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-01-22 20:58:01 -05:00
* upgraded storybook dependencies * upgraded web dependencies * updated timezone selector * upgrade chakra in metamaps * upgraded react-dnd in metamaps * upgraded framer-motion * fixed types in metamaps * upgraded eslint * upgraded lerna, husky and graphql * upgraded node version * removed metamaps package * fixed all eslint issues * ran yarn format to prettier format all files * updated lint-staged & husky scripts * add executable perms to pre-push scripts * updated yarn.lock * fixed eslint and moved chakra icons to ds * fixed emotion errors * removed extra useContext * update yarn.lock * upgraded more packages * removed unnecessary .huskyrc.json * lint fix
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { numbers } from '@metafam/utils';
|
|
|
|
import {
|
|
QuestRepetition_Enum,
|
|
QuestStatus_Enum,
|
|
QuestWithCompletionFragmentFragment,
|
|
} from '../graphql/autogen/types';
|
|
import { MeType } from '../graphql/types';
|
|
|
|
const { BN, amountToDecimal } = numbers;
|
|
|
|
export const UriRegexp = /\w+:(\/?\/?)[^\s]+/;
|
|
|
|
// Hours to seconds
|
|
export function transformCooldownForBackend(
|
|
cooldown: number | undefined | null,
|
|
repetition: QuestRepetition_Enum | undefined | null,
|
|
): number | null {
|
|
if (!cooldown || !repetition || repetition !== QuestRepetition_Enum.Recurring)
|
|
return null;
|
|
return cooldown * 60 * 60;
|
|
}
|
|
|
|
export function isAllowedToCreateQuest(
|
|
balance: string | undefined | null,
|
|
): boolean {
|
|
if (!balance) return false;
|
|
|
|
const pSEEDDecimals = 18;
|
|
const minimumPooledSeedBalance = new BN(100);
|
|
const pSEEDBalanceInDecimal = amountToDecimal(balance, pSEEDDecimals);
|
|
|
|
const allowed = new BN(pSEEDBalanceInDecimal).gt(minimumPooledSeedBalance);
|
|
|
|
return allowed;
|
|
}
|
|
|
|
// TODO factorize this with backend
|
|
export function canCompleteQuest(
|
|
quest: QuestWithCompletionFragmentFragment | null | undefined,
|
|
user: MeType | null | undefined,
|
|
): boolean {
|
|
if (!user || !quest) return false;
|
|
|
|
if (quest.status !== QuestStatus_Enum.Open) {
|
|
return false;
|
|
}
|
|
// Personal or unique, check if not already done by player
|
|
if (
|
|
quest.repetition === QuestRepetition_Enum.Unique ||
|
|
quest.repetition === QuestRepetition_Enum.Personal
|
|
) {
|
|
return !quest.quest_completions.some((qc) => qc.player.id === user.id);
|
|
}
|
|
if (quest.repetition === QuestRepetition_Enum.Recurring && quest.cooldown) {
|
|
const myLastCompletion = quest.quest_completions.find(
|
|
(qc) => qc.player.id === user.id,
|
|
);
|
|
if (myLastCompletion) {
|
|
const submittedAt = new Date(myLastCompletion.submitted_at);
|
|
const now = new Date();
|
|
const diff = +now - +submittedAt;
|
|
if (diff < quest.cooldown * 1000) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export const QuestRepetitionHint: Record<QuestRepetition_Enum, string> = {
|
|
[QuestRepetition_Enum.Recurring]:
|
|
'Recurring quests can be done multiple time per player after a cooldown.',
|
|
[QuestRepetition_Enum.Personal]:
|
|
'Personal quests can be done once per player',
|
|
[QuestRepetition_Enum.Unique]: 'Unique quests can be done only once',
|
|
};
|