mirror of
https://github.com/mudabbir-ahmad/MAD-Treasure-Hunt.git
synced 2026-10-08 03:00:20 +00:00
Refactor API endpoints: update subgroup membership references to subgroup members and streamline cache visibility logic based on selected cache ID.
This commit is contained in:
1 parent
4407d05fdb
commit
e3564bea08
7 files changed
+73
-189
No files matched your search
+12
-16
@@ -6,7 +6,7 @@ const useGameHook = () => {
|
||||
const usersEndpoint = `${API_BASE_URL}/users`;
|
||||
const groupsEndpoint = `${API_BASE_URL}/groups`;
|
||||
const subgroupsEndpoint = `${API_BASE_URL}/subgroups`;
|
||||
const subgroupMembershipsEndpoint = `${API_BASE_URL}/subgroup-memberships`;
|
||||
const subgroupMembersEndpoint = `${API_BASE_URL}/subgroup-members`;
|
||||
const teamsEndpoint = `${API_BASE_URL}/teams`;
|
||||
const teamMembersEndpoint = `${API_BASE_URL}/team-members`;
|
||||
const cachesEndpoint = `${API_BASE_URL}/caches`;
|
||||
@@ -67,19 +67,19 @@ const useGameHook = () => {
|
||||
return response.isSuccess;
|
||||
};
|
||||
|
||||
// Subgroup memberships
|
||||
// Subgroup members
|
||||
const joinPrivateGame = async (payload) => {
|
||||
const response = await API.post(subgroupMembershipsEndpoint, payload);
|
||||
const response = await API.post(subgroupMembersEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const getGroupMembers = async (gid) => {
|
||||
const response = await API.get(`${subgroupMembershipsEndpoint}?Gid=${gid}`);
|
||||
const response = await API.get(`${subgroupMembersEndpoint}?Gid=${gid}`);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const removeMember = async (membershipId) => {
|
||||
const response = await API.delete(`${subgroupMembershipsEndpoint}/${membershipId}`);
|
||||
const response = await API.delete(`${subgroupMembersEndpoint}/${membershipId}`);
|
||||
return response.isSuccess;
|
||||
};
|
||||
|
||||
@@ -125,7 +125,6 @@ const useGameHook = () => {
|
||||
return response.isSuccess;
|
||||
};
|
||||
|
||||
// Caches (standalone resource)
|
||||
const getCaches = async (gid, sgid = null) => {
|
||||
let url = `${cachesEndpoint}?Gid=${gid}`;
|
||||
if (sgid !== null) url += `&SGid=${sgid}`;
|
||||
@@ -134,14 +133,7 @@ const useGameHook = () => {
|
||||
};
|
||||
|
||||
const upsertCache = async (payload) => {
|
||||
if (payload.cacheId) {
|
||||
const response = await API.put(
|
||||
`${cachesEndpoint}/${payload.cacheId}`,
|
||||
payload,
|
||||
);
|
||||
return response.isSuccess ? response.result : null;
|
||||
}
|
||||
const response = await API.post(cachesEndpoint, {
|
||||
const data = {
|
||||
Gid: payload.gid,
|
||||
SGid: payload.subgroupId,
|
||||
Title: payload.name,
|
||||
@@ -149,11 +141,15 @@ const useGameHook = () => {
|
||||
Latitude: payload.latitude,
|
||||
Longitude: payload.longitude,
|
||||
TriggerMeters: payload.radius,
|
||||
});
|
||||
};
|
||||
if (payload.cacheId) {
|
||||
const response = await API.put(`${cachesEndpoint}/${payload.cacheId}`, data);
|
||||
return response.isSuccess ? response.result : null;
|
||||
}
|
||||
const response = await API.post(cachesEndpoint, data);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
// Claim a cache — team-aware: prevents same team from claiming again
|
||||
const claimCache = async (payload) => {
|
||||
const response = await API.post(
|
||||
`${cachesEndpoint}/${payload.cacheId}/claim`,
|
||||
|
||||
+15
-32
@@ -2,52 +2,35 @@ import {useEffect, useRef, useState} from 'react';
|
||||
import {isInClaimCone} from '../utils/geoMath';
|
||||
|
||||
const usePlayerGame = (playerLocation, playerHeading, activeCaches, claimDistance, selectedCacheId) => {
|
||||
// State ----------------------
|
||||
|
||||
const [visibleCaches, setVisibleCaches] = useState([]);
|
||||
const [isClaiming, setIsClaiming] = useState(false);
|
||||
const prevIdsRef = useRef('');
|
||||
const prevIdRef = useRef('');
|
||||
|
||||
// Handlers -------------------
|
||||
|
||||
// Determine which caches fall inside the FOV claim cone
|
||||
useEffect(() => {
|
||||
if (!playerLocation || playerHeading === null || playerHeading === undefined) {
|
||||
if (prevIdsRef.current !== '') {
|
||||
prevIdsRef.current = '';
|
||||
if (!playerLocation || playerHeading === null || playerHeading === undefined || !selectedCacheId) {
|
||||
if (prevIdRef.current !== '') {
|
||||
prevIdRef.current = '';
|
||||
setVisibleCaches([]);
|
||||
setIsClaiming(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const inCone = (activeCaches || []).filter((cache) =>
|
||||
isInClaimCone(playerHeading, playerLocation, cache.coordinates, claimDistance),
|
||||
);
|
||||
const selected = (activeCaches || []).find((c) => c.id === selectedCacheId);
|
||||
const visible = selected && isInClaimCone(playerHeading, playerLocation, selected.coordinates, claimDistance);
|
||||
const newId = visible ? String(selected.id) : '';
|
||||
|
||||
// Only update state when the set of visible caches actually changes
|
||||
const newIds = inCone.map((c) => c.id).join(',');
|
||||
if (newIds !== prevIdsRef.current) {
|
||||
prevIdsRef.current = newIds;
|
||||
setVisibleCaches(inCone);
|
||||
if (newId !== prevIdRef.current) {
|
||||
prevIdRef.current = newId;
|
||||
setVisibleCaches(visible ? [selected] : []);
|
||||
setIsClaiming(Boolean(visible));
|
||||
}
|
||||
}, [playerLocation, playerHeading, activeCaches, claimDistance]);
|
||||
|
||||
// Claiming requires ALL four criteria:
|
||||
// 1. Cache not already claimed by player's team (handled by activeCaches filter)
|
||||
// 2. Cache within claim distance (handled by isInClaimCone)
|
||||
// 3. Player looking at the cache (in FOV cone) (handled by isInClaimCone)
|
||||
// 4. Cache is the one currently selected in list (checked here)
|
||||
useEffect(() => {
|
||||
const canClaim = Boolean(
|
||||
selectedCacheId && visibleCaches.some((c) => c.id === selectedCacheId),
|
||||
);
|
||||
setIsClaiming(canClaim);
|
||||
}, [visibleCaches, selectedCacheId]);
|
||||
|
||||
// Return ---------------------
|
||||
}, [playerLocation, playerHeading, activeCaches, claimDistance, selectedCacheId]);
|
||||
|
||||
return {visibleCaches, isClaiming, setIsClaiming};
|
||||
};
|
||||
|
||||
export default usePlayerGame;
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user