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
2 changes: 1 addition & 1 deletion backend/src/auth/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class User {
@Column({ default: true })
isActive: boolean;

@Column({ nullable: true })
@Column({ unique: true, nullable: true })
lastLoginAt: Date;

@CreateDateColumn()
Expand Down
2 changes: 2 additions & 0 deletions backend/src/reward/entities/reward-claim.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Reward } from './reward.entity';
@Entity('reward_claims')
@Index(['userId', 'rewardId'], { unique: true })
export class RewardClaim {
@Column({ unique: true, nullable: true })
claimKey?: string;
@ApiProperty({ description: 'Unique identifier for the reward claim' })
@PrimaryGeneratedColumn('uuid')
id: string;
Expand Down
10 changes: 9 additions & 1 deletion frontend/app/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import AnimatedBlurBackground from "@/components/AnimatedBlurBackground";

export default function Error() {
const reload = () => {
window.location.reload();
if (typeof window !== 'undefined') {
const count = parseInt(sessionStorage.getItem('error_retry_count') || '0', 10);
if (count >= 3) {
alert('Multiple errors occurred. Please try returning home or contacting support.');
return;
}
sessionStorage.setItem('error_retry_count', (count + 1).toString());
window.location.reload();
}
};

return (
Expand Down
10 changes: 9 additions & 1 deletion frontend/app/global-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { Home, RefreshCcw } from "lucide-react";

export default function GlobalError({ error, reset }) {
const reload = () => {
window.location.reload();
if (typeof window !== 'undefined') {
const count = parseInt(sessionStorage.getItem('error_retry_count') || '0', 10);
if (count >= 3) {
alert('Multiple errors occurred. Please try returning home or contacting support.');
return;
}
sessionStorage.setItem('error_retry_count', (count + 1).toString());
window.location.reload();
}
};

return (
Expand Down
288 changes: 156 additions & 132 deletions frontend/hooks/useReferral.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,157 @@
import { useState, useEffect } from "react";
import axios from "axios";

export const useReferral = (userId = null) => {
const [referralStats, setReferralStats] = useState({
totalInvites: 0,
activeUsers: 0,
totalRewards: 0,
totalXPEarned: 0,
nextMilestone: ""
});

const [invitedUsers, setInvitedUsers] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

useEffect(() => {
if (userId) {
fetchReferralData(userId);
}
}, [userId]);

// Generate referral link for current user
const generateReferralLink = (userId) => {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://nft-hunt.com";
return `${baseUrl}/ref/${userId}`;
};

// Fetch referral data
const fetchReferralData = async (userId) => {
if (!userId) return;

setLoading(true);
setError(null);

try {
const response = await axios.get(`/api/referrals/${userId}`, {
withCredentials: true
});

setReferralStats(response.data.stats);
setInvitedUsers(response.data.invitedUsers);
} catch (err) {
console.error("Failed to fetch referral data:", err);
setError("Failed to load referral data");
} finally {
setLoading(false);
}
};

// Track new referral
const trackReferral = async (referrerId, newUserId) => {
try {
await axios.post("/api/referrals/track", {
referrerId,
newUserId
}, {
withCredentials: true
});

// Refresh referral data
await fetchReferralData(referrerId);
} catch (err) {
console.error("Failed to track referral:", err);
}
};

// Get reward tier info
const getRewardTier = (totalInvites) => {
if (totalInvites >= 50) return { tier: "Mythic", reward: "Mythic NFT", color: "pink" };
if (totalInvites >= 25) return { tier: "Legendary", reward: "Legendary NFT", color: "yellow" };
if (totalInvites >= 10) return { tier: "Epic", reward: "Epic NFT", color: "purple" };
if (totalInvites >= 5) return { tier: "Rare", reward: "Rare NFT", color: "green" };
return { tier: "Common", reward: "Common NFT", color: "gray" };
};

// Calculate progress to next milestone
const getProgressToNextMilestone = (currentInvites) => {
const milestones = [5, 10, 25, 50];
const nextMilestone = milestones.find(m => m > currentInvites) || 50;
const progress = (currentInvites / nextMilestone) * 100;

return {
current: currentInvites,
next: nextMilestone,
progress: Math.min(progress, 100),
remaining: nextMilestone - currentInvites
};
};

// Share referral link
const shareReferral = async (referralLink) => {
if (navigator.share) {
try {
await navigator.share({
title: "Join StellarHunts!",
text: "I'm playing this amazing StellarHunts game. Join me and earn exclusive rewards!",
url: referralLink
});
return true;
} catch (err) {
console.error("Error sharing:", err);
return false;
}
}
return false;
};

// Copy referral link to clipboard
const copyReferralLink = async (referralLink) => {
try {
await navigator.clipboard.writeText(referralLink);
return true;
} catch (err) {
console.error("Failed to copy:", err);
return false;
}
};

return {
referralStats,
invitedUsers,
loading,
error,
generateReferralLink,
fetchReferralData,
trackReferral,
getRewardTier,
getProgressToNextMilestone,
shareReferral,
copyReferralLink
};
import { useState, useEffect } from "react";
import axios from "axios";

export const useReferral = (userId = null) => {
const [referralStats, setReferralStats] = useState({
totalInvites: 0,
activeUsers: 0,
totalRewards: 0,
totalXPEarned: 0,
nextMilestone: ""
});

const [invitedUsers, setInvitedUsers] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

useEffect(() => {
const controller = new AbortController();
if (userId) {
const fetchReferralData = async (userId) => {
if (!userId) return;
setLoading(true);
setError(null);
try {
const response = await axios.get(`/api/referrals/${userId}`, {
withCredentials: true,
signal: controller.signal
});
setReferralStats(response.data.stats);
setInvitedUsers(response.data.invitedUsers);
} catch (err) {
if (!axios.isCancel(err)) {
console.error("Failed to fetch referral data:", err);
setError("Failed to load referral data");
}
} finally {
setLoading(false);
}
};
fetchReferralData(userId);
}
return () => {
controller.abort();
};
}, [userId]);

// Generate referral link for current user
const generateReferralLink = (userId) => {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://nft-hunt.com";
return `${baseUrl}/ref/${userId}`;
};

// Fetch referral data
const fetchReferralData = async (userId) => {
if (!userId) return;

setLoading(true);
setError(null);

try {
const response = await axios.get(`/api/referrals/${userId}`, {
withCredentials: true
});

setReferralStats(response.data.stats);
setInvitedUsers(response.data.invitedUsers);
} catch (err) {
console.error("Failed to fetch referral data:", err);
setError("Failed to load referral data");
} finally {
setLoading(false);
}
};

// Track new referral
const trackReferral = async (referrerId, newUserId) => {
try {
await axios.post("/api/referrals/track", {
referrerId,
newUserId
}, {
withCredentials: true
});

// Refresh referral data
await fetchReferralData(referrerId);
} catch (err) {
console.error("Failed to track referral:", err);
}
};

// Get reward tier info
const getRewardTier = (totalInvites) => {
if (totalInvites >= 50) return { tier: "Mythic", reward: "Mythic NFT", color: "pink" };
if (totalInvites >= 25) return { tier: "Legendary", reward: "Legendary NFT", color: "yellow" };
if (totalInvites >= 10) return { tier: "Epic", reward: "Epic NFT", color: "purple" };
if (totalInvites >= 5) return { tier: "Rare", reward: "Rare NFT", color: "green" };
return { tier: "Common", reward: "Common NFT", color: "gray" };
};

// Calculate progress to next milestone
const getProgressToNextMilestone = (currentInvites) => {
const milestones = [5, 10, 25, 50];
const nextMilestone = milestones.find(m => m > currentInvites) || 50;
const progress = (currentInvites / nextMilestone) * 100;

return {
current: currentInvites,
next: nextMilestone,
progress: Math.min(progress, 100),
remaining: nextMilestone - currentInvites
};
};

// Share referral link
const shareReferral = async (referralLink) => {
if (navigator.share) {
try {
await navigator.share({
title: "Join StellarHunts!",
text: "I'm playing this amazing StellarHunts game. Join me and earn exclusive rewards!",
url: referralLink
});
return true;
} catch (err) {
console.error("Error sharing:", err);
return false;
}
}
return false;
};

// Copy referral link to clipboard
const copyReferralLink = async (referralLink) => {
try {
await navigator.clipboard.writeText(referralLink);
return true;
} catch (err) {
console.error("Failed to copy:", err);
return false;
}
};

return {
referralStats,
invitedUsers,
loading,
error,
generateReferralLink,
fetchReferralData,
trackReferral,
getRewardTier,
getProgressToNextMilestone,
shareReferral,
copyReferralLink
};
};
2 changes: 2 additions & 0 deletions onchain/Cargo.lock

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

8 changes: 1 addition & 7 deletions onchain/contracts/stellar_hunts_nft/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ fn test_double_mint_rejected() {
game.mint(&nft_id, &r, &crate::Levels::Easy);
// Second mint must fail (already-has-badge error).
let should_panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.init(
&admin,
&game,
&String::from_str(&env, &long_uri),
&String::from_str(&env, "StellarHuntsBadge"),
&String::from_str(&env, "SHB"),
);
game.mint(&nft_id, &r, &crate::Levels::Easy);
}));
assert!(should_panic.is_err());
}
Expand Down