147 lines
3.4 KiB
JavaScript
147 lines
3.4 KiB
JavaScript
/**
|
|
* FLOCKRADAR - USER AUTHENTICATION & COMMUNITY STATE MANAGER
|
|
* Waze-style driver profile, preferences, and crowdsourced reporting store.
|
|
*/
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
const STORAGE_KEY_USER = 'flock_radar_user';
|
|
const STORAGE_KEY_REPORTS = 'flock_community_reports';
|
|
|
|
// Default Guest Profile
|
|
const defaultUser = {
|
|
isLoggedIn: false,
|
|
username: 'Guest Driver',
|
|
callsign: 'SCOUT-104',
|
|
level: 1,
|
|
points: 120,
|
|
vehicle: 'Sedan (Silver)',
|
|
soundEnabled: true,
|
|
alertsMuted: false,
|
|
reportsCount: 0
|
|
};
|
|
|
|
// State
|
|
let currentUser = null;
|
|
let communityReports = [];
|
|
|
|
function init() {
|
|
loadUser();
|
|
loadReports();
|
|
}
|
|
|
|
function loadUser() {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY_USER);
|
|
if (stored) {
|
|
currentUser = JSON.parse(stored);
|
|
} else {
|
|
currentUser = { ...defaultUser };
|
|
}
|
|
} catch (e) {
|
|
currentUser = { ...defaultUser };
|
|
}
|
|
}
|
|
|
|
function saveUser() {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY_USER, JSON.stringify(currentUser));
|
|
window.dispatchEvent(new CustomEvent('flock_user_updated', { detail: currentUser }));
|
|
} catch (e) {
|
|
console.error('Failed to save user session', e);
|
|
}
|
|
}
|
|
|
|
function login(username, vehicle, soundEnabled) {
|
|
currentUser = {
|
|
isLoggedIn: true,
|
|
username: username || 'Citizen Scout',
|
|
callsign: 'RADAR-' + Math.floor(100 + Math.random() * 900),
|
|
level: 2,
|
|
points: 250,
|
|
vehicle: vehicle || 'Standard Vehicle',
|
|
soundEnabled: soundEnabled !== false,
|
|
alertsMuted: false,
|
|
reportsCount: communityReports.length
|
|
};
|
|
saveUser();
|
|
return currentUser;
|
|
}
|
|
|
|
function logout() {
|
|
currentUser = { ...defaultUser };
|
|
saveUser();
|
|
}
|
|
|
|
function loadReports() {
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY_REPORTS);
|
|
if (stored) {
|
|
communityReports = JSON.parse(stored);
|
|
} else {
|
|
communityReports = [];
|
|
}
|
|
} catch (e) {
|
|
communityReports = [];
|
|
}
|
|
}
|
|
|
|
function saveReports() {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY_REPORTS, JSON.stringify(communityReports));
|
|
window.dispatchEvent(new CustomEvent('flock_reports_updated', { detail: communityReports }));
|
|
} catch (e) {
|
|
console.error('Failed to save community reports', e);
|
|
}
|
|
}
|
|
|
|
function addReport(report) {
|
|
const newReport = {
|
|
id: 'rpt-' + Date.now(),
|
|
lat: report.lat,
|
|
lng: report.lng,
|
|
type: report.type || 'Solar Pole Mount',
|
|
intersection: report.intersection || 'Reported Location',
|
|
notes: report.notes || 'Spotted by community driver',
|
|
reportedBy: currentUser ? currentUser.username : 'Anonymous Scout',
|
|
timestamp: new Date().toISOString(),
|
|
confirmations: 1
|
|
};
|
|
|
|
communityReports.push(newReport);
|
|
saveReports();
|
|
|
|
if (currentUser) {
|
|
currentUser.points = (currentUser.points || 0) + 50;
|
|
currentUser.reportsCount = (currentUser.reportsCount || 0) + 1;
|
|
saveUser();
|
|
}
|
|
|
|
return newReport;
|
|
}
|
|
|
|
function getUser() {
|
|
if (!currentUser) loadUser();
|
|
return currentUser;
|
|
}
|
|
|
|
function getReports() {
|
|
if (!communityReports) loadReports();
|
|
return communityReports;
|
|
}
|
|
|
|
// Export API to global scope
|
|
window.FlockAuth = {
|
|
init,
|
|
getUser,
|
|
login,
|
|
logout,
|
|
saveUser,
|
|
getReports,
|
|
addReport
|
|
};
|
|
|
|
init();
|
|
})();
|