mirror of
https://github.com/mudabbir-ahmad/MAD-Treasure-Hunt.git
synced 2026-10-08 03:00:20 +00:00
Add comments for code structure clarity; enhance readability across multiple components
This commit is contained in:
1 parent
d67522a71d
commit
9b9f13eb6a
29 files changed
+1020
-846
No files matched your search
@@ -1,3 +1,5 @@
|
||||
// --- Session Store ---
|
||||
|
||||
const session = {
|
||||
currentUid: null,
|
||||
currentGid: null,
|
||||
@@ -7,6 +9,8 @@ const session = {
|
||||
isAcceptedAdmin: false,
|
||||
};
|
||||
|
||||
// --- Handlers ---
|
||||
|
||||
const setSessionUser = (user) => {
|
||||
session.currentUid = user?.Uid ?? null;
|
||||
session.currentGid = user?.Gid ?? null;
|
||||
|
||||
+2
-21
@@ -1,22 +1,3 @@
|
||||
import Constants from 'expo-constants';
|
||||
const API_BASE_URL = 'http://localhost:3000';
|
||||
|
||||
const hostFromExpo = Constants.expoConfig?.hostUri?.split(':')[0] || null;
|
||||
const apiBaseUrl = 'http://localhost:3000'; // Shirish, i need to deploy the database first so... wait until that... sorry buddy...
|
||||
// if you want you can run your own db locally i can send you the files for it just ask :(((
|
||||
|
||||
const postJson = async (path, payload) => {
|
||||
const response = await fetch(`${apiBaseUrl}${path}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(result.message || 'Request failed');
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const postDb = async (action, payload = {}) => {
|
||||
return postJson('/db', { action, payload });
|
||||
};
|
||||
export default API_BASE_URL;
|
||||
+36
-25
@@ -1,8 +1,16 @@
|
||||
import {useState} from 'react';
|
||||
import {postDb} from './dbLink';
|
||||
import API from '../components/API/API';
|
||||
import API_BASE_URL from './dbLink';
|
||||
import {clearSession, setSessionUser} from './SessionStore';
|
||||
|
||||
const useAuthHook = () => {
|
||||
// Initialisation ------------
|
||||
|
||||
const loginEndpoint = `${API_BASE_URL}/auth/login`;
|
||||
const registerEndpoint = `${API_BASE_URL}/auth/register`;
|
||||
|
||||
// State ----------------------
|
||||
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [username, setUsername] = useState('');
|
||||
@@ -11,40 +19,41 @@ const useAuthHook = () => {
|
||||
const [error, setError] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
// Handlers -------------------
|
||||
|
||||
const login = async () => {
|
||||
setError('');
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const user = await postDb('login', { email: email.trim(), password });
|
||||
setSessionUser(user);
|
||||
setIsLoading(false);
|
||||
return user;
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
setIsLoading(false);
|
||||
return null;
|
||||
const response = await API.post(loginEndpoint, {
|
||||
email: email.trim(),
|
||||
password,
|
||||
});
|
||||
setIsLoading(false);
|
||||
if (response.isSuccess) {
|
||||
setSessionUser(response.result);
|
||||
return response.result;
|
||||
}
|
||||
setError(response.message);
|
||||
return null;
|
||||
};
|
||||
|
||||
const register = async () => {
|
||||
setError('');
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const user = await postDb('register', {
|
||||
username: username.trim(),
|
||||
email: email.trim().toLowerCase(),
|
||||
password,
|
||||
confirmPassword,
|
||||
accountType,
|
||||
});
|
||||
setSessionUser(user);
|
||||
setIsLoading(false);
|
||||
return user;
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
setIsLoading(false);
|
||||
return null;
|
||||
const response = await API.post(registerEndpoint, {
|
||||
username: username.trim(),
|
||||
email: email.trim().toLowerCase(),
|
||||
password,
|
||||
confirmPassword,
|
||||
accountType,
|
||||
});
|
||||
setIsLoading(false);
|
||||
if (response.isSuccess) {
|
||||
setSessionUser(response.result);
|
||||
return response.result;
|
||||
}
|
||||
setError(response.message);
|
||||
return null;
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
@@ -57,6 +66,8 @@ const useAuthHook = () => {
|
||||
setError('');
|
||||
};
|
||||
|
||||
// Return ---------------------
|
||||
|
||||
return {
|
||||
email,
|
||||
password,
|
||||
|
||||
+84
-15
@@ -1,33 +1,102 @@
|
||||
import {postDb} from './dbLink';
|
||||
import API from '../components/API/API';
|
||||
import API_BASE_URL from './dbLink';
|
||||
|
||||
const useGameHook = () => {
|
||||
const request = async (action, payload = {}) => postDb(action, payload);
|
||||
// Initialisation ------------
|
||||
|
||||
const getGameTypes = async () => request('getGameTypes');
|
||||
const gameTypesEndpoint = `${API_BASE_URL}/game-types`;
|
||||
const groupsEndpoint = `${API_BASE_URL}/groups`;
|
||||
const subgroupMembershipsEndpoint = `${API_BASE_URL}/subgroup-memberships`;
|
||||
const teamsEndpoint = `${API_BASE_URL}/teams`;
|
||||
const teamMembersEndpoint = `${API_BASE_URL}/team-members`;
|
||||
const gameDataEndpoint = `${API_BASE_URL}/game-data`;
|
||||
const adminWaitlistEndpoint = `${API_BASE_URL}/admin-waitlist`;
|
||||
|
||||
const getCreatedPrivateGame = async (userId) => request('getCreatedPrivateGameByUser', { userId });
|
||||
// Handlers -------------------
|
||||
|
||||
const createPrivateGame = async (payload) => request('createPrivateGame', payload);
|
||||
const getGameTypes = async () => {
|
||||
const response = await API.get(gameTypesEndpoint);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const joinPrivateGame = async (payload) => request('joinPrivateGame', payload);
|
||||
const getCreatedPrivateGame = async (userId) => {
|
||||
const response = await API.get(`${groupsEndpoint}?CreatedByUid=${userId}`);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const joinAsAdmin = async (payload) => request('joinAsAdmin', payload);
|
||||
const createPrivateGame = async (payload) => {
|
||||
const response = await API.post(groupsEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const getLobby = async (gid) => request('getLobby', { gid });
|
||||
const joinPrivateGame = async (payload) => {
|
||||
const response = await API.post(subgroupMembershipsEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const getTeam = async (tid) => request('getTeam', { tid });
|
||||
const joinAsAdmin = async (payload) => {
|
||||
const response = await API.post(adminWaitlistEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const createTeam = async (payload) => request('createTeam', payload);
|
||||
const getLobby = async (gid) => {
|
||||
const response = await API.get(`${groupsEndpoint}/${gid}`);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const joinTeamByCode = async (payload) => request('joinTeamByCode', payload);
|
||||
const getTeam = async (tid) => {
|
||||
const response = await API.get(`${teamsEndpoint}/${tid}`);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const getMapPoints = async (gid, sgid = null) => request('getMapPoints', { gid, sgid });
|
||||
const createTeam = async (payload) => {
|
||||
const response = await API.post(teamsEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const getCaches = async (gid, sgid = null) => request('getCaches', { gid, sgid });
|
||||
const joinTeamByCode = async (payload) => {
|
||||
const response = await API.post(teamMembersEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const upsertCache = async (payload) => request('upsertCache', payload);
|
||||
const getMapPoints = async (gid, sgid = null) => {
|
||||
let url = `${gameDataEndpoint}/${gid}/caches`;
|
||||
if (sgid !== null) url += `?SGid=${sgid}`;
|
||||
const response = await API.get(url);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const claimCache = async (payload) => request('claimCache', payload);
|
||||
const getCaches = async (gid, sgid = null) => {
|
||||
let url = `${gameDataEndpoint}/${gid}/caches`;
|
||||
if (sgid !== null) url += `?SGid=${sgid}`;
|
||||
const response = await API.get(url);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const upsertCache = async (payload) => {
|
||||
if (payload.cacheId) {
|
||||
const response = await API.put(
|
||||
`${gameDataEndpoint}/${payload.gid}/caches/${payload.cacheId}`,
|
||||
payload,
|
||||
);
|
||||
return response.isSuccess ? response.result : null;
|
||||
}
|
||||
const response = await API.post(
|
||||
`${gameDataEndpoint}/${payload.gid}/caches`,
|
||||
payload,
|
||||
);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
const claimCache = async (payload) => {
|
||||
const response = await API.put(
|
||||
`${gameDataEndpoint}/${payload.gid}/caches/${payload.cacheId}`,
|
||||
{ClaimedByUid: payload.uid, ClaimedByTid: payload.tid},
|
||||
);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
// Return ---------------------
|
||||
|
||||
return {
|
||||
getGameTypes,
|
||||
|
||||
+25
-24
@@ -2,36 +2,37 @@ import {useEffect, useState} from 'react';
|
||||
import {isLookingAtCache, isWithinRadius} from '../utils/geoMath';
|
||||
|
||||
const usePlayerGame = (playerLocation, playerHeading, activeCaches) => {
|
||||
const [visibleCache, setVisibleCache] = useState(null);
|
||||
const [isClaiming, setIsClaiming] = useState(false);
|
||||
// State ----------------------
|
||||
|
||||
useEffect(() => {
|
||||
if (!playerLocation || playerHeading === null || playerHeading === undefined) {
|
||||
setVisibleCache(null);
|
||||
setIsClaiming(false);
|
||||
return;
|
||||
}
|
||||
const [visibleCache, setVisibleCache] = useState(null);
|
||||
const [isClaiming, setIsClaiming] = useState(false);
|
||||
|
||||
const nearbyCache = (activeCaches || []).find((cache) =>
|
||||
isWithinRadius(playerLocation, cache.coordinates, cache.radius));
|
||||
// Handlers -------------------
|
||||
|
||||
if (!nearbyCache) {
|
||||
setVisibleCache(null);
|
||||
setIsClaiming(false);
|
||||
return;
|
||||
}
|
||||
useEffect(() => {
|
||||
if (!playerLocation || playerHeading === null || playerHeading === undefined) {
|
||||
setVisibleCache(null);
|
||||
setIsClaiming(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const isTargeted = isLookingAtCache(
|
||||
playerHeading,
|
||||
playerLocation,
|
||||
nearbyCache.coordinates,
|
||||
);
|
||||
const nearbyCache = (activeCaches || []).find((cache) =>
|
||||
isWithinRadius(playerLocation, cache.coordinates, cache.radius));
|
||||
|
||||
setVisibleCache(nearbyCache);
|
||||
setIsClaiming(isTargeted);
|
||||
}, [playerLocation, playerHeading, activeCaches]);
|
||||
if (!nearbyCache) {
|
||||
setVisibleCache(null);
|
||||
setIsClaiming(false);
|
||||
return;
|
||||
}
|
||||
|
||||
return { visibleCache, isClaiming, setIsClaiming };
|
||||
const isTargeted = isLookingAtCache(playerHeading, playerLocation, nearbyCache.coordinates);
|
||||
setVisibleCache(nearbyCache);
|
||||
setIsClaiming(isTargeted);
|
||||
}, [playerLocation, playerHeading, activeCaches]);
|
||||
|
||||
// Return ---------------------
|
||||
|
||||
return {visibleCache, isClaiming, setIsClaiming};
|
||||
};
|
||||
|
||||
export default usePlayerGame;
|
||||
|
||||
Reference in new issue
Block a user