Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/classic-shared/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type Game = {
username: string;
};
challengeProgress: PlayerProgress;
hardcoreModeAccess: HardcoreAccessStatus;
};

export type GameResponse = Game;
Expand All @@ -80,6 +81,11 @@ export type ChallengeLeaderboardResponse = {
leaderboardByFastest: { member: string; score: number }[];
};

export type HardcoreAccessStatus =
| { status: 'lifetime' }
| { status: 'active'; expires: number }
| { status: 'inactive' };

export type WebviewToBlocksMessage =
| { type: 'GAME_INIT' }
| {
Expand Down
6 changes: 5 additions & 1 deletion packages/classic-webview/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const getPage = (page: Page) => {

export const App = () => {
const page = usePage();
const { mode } = useGame();
const { mode, hardcoreModeAccess } = useGame();

if (mode === 'hardcore' && hardcoreModeAccess?.status === 'inactive') {
return <div>UNLOCK HARDCORE</div>;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: If this is a stub of the "default, not unlocked" page, this logic should likely be added to the userPage hook.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this is a rudimentary placeholder. I'm adjusting pattern in the next PR

}

return (
<div
Expand Down
1 change: 1 addition & 0 deletions packages/classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type-check": "tsc --noEmit"
},
"dependencies": {
"@devvit/payments": "0.11.12",
"@devvit/public-api": "0.11.12",
"@hotandcold/classic-shared": "*",
"@hotandcold/shared": "*",
Expand Down
21 changes: 8 additions & 13 deletions packages/classic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './menu-actions/totalReminders.js';
import { Devvit, useInterval, useState } from '@devvit/public-api';
import { DEVVIT_SETTINGS_KEYS } from './constants.js';
import { isServerCall, omit } from '@hotandcold/shared/utils';
import { WebviewToBlocksMessage } from '@hotandcold/classic-shared';
import { HardcoreAccessStatus, WebviewToBlocksMessage } from '@hotandcold/classic-shared';
import { Guess } from './core/guess.js';
import { ChallengeToPost } from './core/challengeToPost.js';
import { Preview } from './components/Preview.js';
Expand All @@ -19,7 +19,7 @@ import { ChallengeLeaderboard } from './core/challengeLeaderboard.js';
import { Reminders } from './core/reminders.js';
import { RedditApiCache } from './core/redditApiCache.js';
import { sendMessageToWebview } from './utils/index.js';
import { initPayments } from './payments.js';
import { initPayments, PaymentsRepo } from './payments.js';

initPayments();

Expand Down Expand Up @@ -56,6 +56,7 @@ type InitialState =
challengeInfo: Awaited<ReturnType<ChallengeService['getChallenge']>>;
challengeUserInfo: Awaited<ReturnType<(typeof Guess)['getChallengeUserInfo']>>;
challengeProgress: Awaited<ReturnType<(typeof ChallengeProgress)['getPlayerProgress']>>;
hardcoreModeAccess: HardcoreAccessStatus;
};

// Add a post type definition
Expand All @@ -64,13 +65,15 @@ Devvit.addCustomPostType({
height: 'tall',
render: (context) => {
const challengeService = new ChallengeService(context.redis);
const paymentsRepo = new PaymentsRepo(context.redis);
const [initialState] = useState<InitialState>(async () => {
const [user, challenge] = await Promise.all([
const [user, challenge, hardcoreModeAccess] = await Promise.all([
context.reddit.getCurrentUser(),
ChallengeToPost.getChallengeNumberForPost({
redis: context.redis,
postId: context.postId!,
}),
paymentsRepo.getHardcoreAccessStatus(context.userId!),
]);
if (!user) {
return {
Expand Down Expand Up @@ -104,23 +107,14 @@ Devvit.addCustomPostType({
}),
]);

// sendMessageToWebview(context, {
// type: 'INIT',
// payload: {
// challengeInfo: omit(challengeInfo, ['word']),
// challengeUserInfo,
// number: challenge,
// challengeProgress: challengeProgress,
// },
// });

return {
type: 'AUTHED' as const,
user: { username: user.username, avatar },
challenge,
challengeInfo,
challengeUserInfo,
challengeProgress,
hardcoreModeAccess,
};
});

Expand Down Expand Up @@ -170,6 +164,7 @@ Devvit.addCustomPostType({
challengeUserInfo,
number: challenge,
challengeProgress: challengeProgress,
hardcoreModeAccess: initialState.hardcoreModeAccess,
},
});

Expand Down
23 changes: 22 additions & 1 deletion packages/classic/src/payments.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { addPaymentHandler } from '@devvit/payments';
import { type RedisClient } from '@devvit/public-api';
import { HardcoreAccessStatus } from '@hotandcold/classic-shared';
import { DateTime } from 'luxon';

class PaymentsRepo {
export class PaymentsRepo {
static hardcoreModeAccessKey(userId: string) {
return `hardcore-mode-access:${userId}`;
}
Expand Down Expand Up @@ -42,6 +43,26 @@ class PaymentsRepo {
newExpiry.valueOf().toString()
);
}

async getHardcoreAccessStatus(userId: string): Promise<HardcoreAccessStatus> {
const key = PaymentsRepo.hardcoreModeAccessKey(userId);
const currentAccess = await this.#redis.get(key);

if (currentAccess === '-1') {
return { status: 'lifetime' };
}

if (!currentAccess) {
return { status: 'inactive' };
}

const expiryMillis = Number(currentAccess);
const expiryDate = DateTime.fromMillis(expiryMillis);
if (expiryDate <= DateTime.now()) {
return { status: 'inactive' };
}
return { status: 'active', expires: expiryDate.valueOf() };
}
}

export function initPayments() {
Expand Down