fixes
This commit is contained in:
@@ -149,8 +149,6 @@ function App() {
|
||||
);
|
||||
}
|
||||
|
||||
// Only calculate userClicks if userId is defined (i.e., user is signed in)
|
||||
const userClicks = isSignedIn && userId ? gameState.users[userId]?.clicks || 0 : 0;
|
||||
const userBonusMultiplier = isSignedIn && userId ? gameState.users[userId]?.bonusMultiplier || 1 : 1;
|
||||
const effectiveClickMultiplier = gameState.clickMultiplier * userBonusMultiplier;
|
||||
|
||||
@@ -211,7 +209,7 @@ function App() {
|
||||
<div className="lg:col-span-1">
|
||||
<UpgradeShop
|
||||
gameState={gameState}
|
||||
userClicks={userClicks}
|
||||
totalClicks={gameState.totalClicks}
|
||||
onPurchase={purchaseUpgrade}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,7 @@ import { GameState, MascotTier } from '../types'; // Import MascotTier
|
||||
|
||||
interface UpgradeShopProps {
|
||||
gameState: GameState;
|
||||
userClicks: number;
|
||||
totalClicks: number; // Changed from userClicks
|
||||
onPurchase: (upgradeId: string) => void;
|
||||
}
|
||||
|
||||
@@ -12,13 +12,17 @@ interface UpgradeShopProps {
|
||||
const getMascotName = (imageSrc: string): string => {
|
||||
const fileName = imageSrc.split('/').pop() || '';
|
||||
const nameWithoutExtension = fileName.split('.')[0];
|
||||
// remove the word neurosama if it exists in the name
|
||||
if (nameWithoutExtension.includes('neurosama')) {
|
||||
return nameWithoutExtension.replace('neurosama', '').trim();
|
||||
}
|
||||
return nameWithoutExtension
|
||||
.split('-')
|
||||
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
||||
.join(' ');
|
||||
};
|
||||
|
||||
export function UpgradeShop({ gameState, userClicks, onPurchase }: UpgradeShopProps) {
|
||||
export function UpgradeShop({ gameState, totalClicks, onPurchase }: UpgradeShopProps) { // Changed from userClicks
|
||||
return (
|
||||
<div className="bg-gradient-to-b from-purple-800 to-pink-600 p-6 rounded-xl border-4 border-cyan-400 shadow-2xl">
|
||||
<h2 className="text-3xl font-bold text-yellow-300 mb-6 text-center" style={{ fontFamily: 'Comic Sans MS, cursive' }}>
|
||||
@@ -29,7 +33,7 @@ export function UpgradeShop({ gameState, userClicks, onPurchase }: UpgradeShopPr
|
||||
{UPGRADES.map((upgrade) => {
|
||||
const owned = gameState.upgrades[upgrade.id]?.owned || 0;
|
||||
const cost = gameState.upgrades[upgrade.id]?.cost || upgrade.baseCost;
|
||||
const canAfford = userClicks >= cost;
|
||||
const canAfford = totalClicks >= cost; // Changed from userClicks
|
||||
|
||||
let description = upgrade.description;
|
||||
|
||||
@@ -84,7 +88,7 @@ export function UpgradeShop({ gameState, userClicks, onPurchase }: UpgradeShopPr
|
||||
|
||||
<div className="mt-6 text-center">
|
||||
<div className="text-2xl font-bold text-yellow-300">
|
||||
Your Clicks: {userClicks.toLocaleString()}
|
||||
Total Clicks: {totalClicks.toLocaleString()}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -10,23 +10,48 @@ export function usePartyKit() {
|
||||
const { user } = useUser();
|
||||
const [gameState, setGameState] = useState<GameState | null>(null);
|
||||
const [socket, setSocket] = useState<PartySocket | null>(null);
|
||||
// Generate a persistent guest ID if the user is not signed in
|
||||
const [guestId] = useState(() => {
|
||||
let storedGuestId = localStorage.getItem('bozo_guest_id');
|
||||
if (!storedGuestId) {
|
||||
storedGuestId = `guest_${Math.random().toString(36).substring(2, 15)}`;
|
||||
localStorage.setItem('bozo_guest_id', storedGuestId);
|
||||
}
|
||||
return storedGuestId;
|
||||
});
|
||||
const [guestName] = useState(() => {
|
||||
let storedGuestName = localStorage.getItem('bozo_guest_name');
|
||||
if (!storedGuestName) {
|
||||
storedGuestName = `Guest${Math.floor(Math.random() * 10000)}`;
|
||||
localStorage.setItem('bozo_guest_name', storedGuestName);
|
||||
}
|
||||
return storedGuestName;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoaded || !isSignedIn || !user) return;
|
||||
|
||||
// Always attempt to connect the socket
|
||||
const ws = new PartySocket({
|
||||
host: PARTY_HOST,
|
||||
room: 'bozo-clicker'
|
||||
});
|
||||
|
||||
ws.onopen = async () => {
|
||||
const token = await getToken();
|
||||
ws.send(JSON.stringify({
|
||||
type: 'user-join',
|
||||
userId: user.id,
|
||||
userName: user.username || user.fullName || user.emailAddresses[0].emailAddress,
|
||||
token
|
||||
}));
|
||||
if (isLoaded && isSignedIn && user) {
|
||||
const token = await getToken();
|
||||
ws.send(JSON.stringify({
|
||||
type: 'user-join',
|
||||
userId: user.id,
|
||||
userName: user.username || user.fullName || user.emailAddresses[0].emailAddress,
|
||||
token
|
||||
}));
|
||||
} else {
|
||||
// If not signed in, send a generic user-join message for guest
|
||||
ws.send(JSON.stringify({
|
||||
type: 'user-join',
|
||||
userId: guestId,
|
||||
userName: guestName
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
@@ -41,7 +66,7 @@ export function usePartyKit() {
|
||||
return () => {
|
||||
ws.close();
|
||||
};
|
||||
}, [isLoaded, isSignedIn, user, getToken]);
|
||||
}, [isLoaded, guestId, guestName]); // Removed isSignedIn, user, getToken from dependencies
|
||||
|
||||
const sendClick = useCallback(async () => {
|
||||
if (socket && isSignedIn && user) {
|
||||
@@ -67,7 +92,7 @@ export function usePartyKit() {
|
||||
}
|
||||
}, [socket, isSignedIn, user, getToken]);
|
||||
|
||||
const sendMascotClickBonus = useCallback(async (multiplierBonus: number) => { // Renamed function
|
||||
const sendMascotClickBonus = useCallback(async (multiplierBonus: number) => {
|
||||
if (socket && isSignedIn && user) {
|
||||
const token = await getToken();
|
||||
socket.send(JSON.stringify({
|
||||
@@ -80,12 +105,17 @@ export function usePartyKit() {
|
||||
}
|
||||
}, [socket, isSignedIn, user, getToken]);
|
||||
|
||||
// Determine the userId and userName to expose based on sign-in status
|
||||
// Only expose Clerk user ID/name if signed in, otherwise undefined
|
||||
const currentUserId = isSignedIn && user ? user.id : undefined;
|
||||
const currentUserName = isSignedIn && user ? (user.username || user.fullName || user.emailAddresses[0].emailAddress) : undefined;
|
||||
|
||||
return {
|
||||
gameState,
|
||||
sendClick,
|
||||
purchaseUpgrade,
|
||||
sendMascotClickBonus, // Export the renamed function
|
||||
userId: user?.id,
|
||||
userName: user?.username || user?.fullName || user?.emailAddresses[0]?.emailAddress
|
||||
sendMascotClickBonus,
|
||||
userId: currentUserId,
|
||||
userName: currentUserName
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user