feat: add structure widget with timer and associated api

This commit is contained in:
Gustav
2025-01-13 11:41:17 -05:00
parent 1620e1fd21
commit 6714eb5d9b
27 changed files with 1590 additions and 42 deletions

View File

@@ -0,0 +1,4 @@
export * from './parserHelper';
export * from './pasteParser';
export * from './structureTypes';
export * from './structureUtils';

View File

@@ -0,0 +1,92 @@
import { StructureStatus, StructureItem, STRUCTURE_TYPE_MAP } from './structureTypes';
import { formatToISO } from './structureUtils';
// Up to you if you'd like to keep a separate constant here or not
export const statusesRequiringTimer: StructureStatus[] = ['Anchoring', 'Reinforced'];
/**
* parseFormatOneLine(line):
* - Splits by tabs
* - First col => structureTypeId
* - Second col => rawName
* - Third col => structureTypeName
*/
export function parseFormatOneLine(line: string): StructureItem | null {
const columns = line
.split('\t')
.map(c => c.trim())
.filter(Boolean);
// Expecting e.g. "35832 J214811 - SomeName Astrahus"
if (columns.length < 3) {
return null;
}
const [rawTypeId, rawName, rawTypeName] = columns;
if (columns.length != 4) {
return null;
}
if (!STRUCTURE_TYPE_MAP[rawTypeId]) {
return null;
}
if (rawTypeName != STRUCTURE_TYPE_MAP[rawTypeId]) {
return null;
}
const name = rawName.replace(/^J\d{6}\s*-\s*/, '').trim();
return {
id: crypto.randomUUID(),
structureTypeId: rawTypeId,
structureType: rawTypeName,
name,
ownerName: '',
notes: '',
status: 'Powered', // Default
endTime: '', // No timer by default
};
}
export function matchesThreeLineSnippet(lines: string[]): boolean {
if (lines.length < 3) return false;
return /until\s+\d{4}\.\d{2}\.\d{2}/i.test(lines[2]);
}
/**
* parseThreeLineSnippet:
* - Example lines:
* line1: "J214811 - Folgers"
* line2: "1,475 km"
* line3: "Reinforced until 2025.01.13 23:51"
*/
export function parseThreeLineSnippet(lines: string[]): StructureItem {
const [line1, , line3] = lines;
let status: StructureStatus = 'Reinforced';
let endTime: string | undefined;
// e.g. "Reinforced until 2025.01.13 23:27"
const match = line3.match(/^(?<stat>\w+)\s+until\s+(?<dateTime>[\d.]+\s+[\d:]+)/i);
if (match?.groups?.stat) {
const candidateStatus = match.groups.stat as StructureStatus;
if (statusesRequiringTimer.includes(candidateStatus)) {
status = candidateStatus;
}
}
if (match?.groups?.dateTime) {
let dt = match.groups.dateTime.trim().replace(/\./g, '-'); // "2025-01-13 23:27"
dt = dt.replace(' ', 'T'); // "2025-01-13T23:27"
endTime = formatToISO(dt); // => "2025-01-13T23:27:00Z"
}
return {
id: crypto.randomUUID(),
name: line1.replace(/^J\d{6}\s*-\s*/, '').trim(),
status,
endTime,
};
}

View File

@@ -0,0 +1,56 @@
import { StructureItem } from './structureTypes';
import { parseThreeLineSnippet, parseFormatOneLine, matchesThreeLineSnippet } from './parserHelper';
export function processSnippetText(rawText: string, existingStructures: StructureItem[]): StructureItem[] {
if (!rawText) {
return existingStructures.slice();
}
const lines = rawText
.split(/\r?\n/)
.map(line => line.trim())
.filter(Boolean);
if (lines.length === 3 && matchesThreeLineSnippet(lines)) {
return applyThreeLineSnippet(lines, existingStructures);
} else {
return applySingleLineParse(lines, existingStructures);
}
}
function applyThreeLineSnippet(snippetLines: string[], existingStructures: StructureItem[]): StructureItem[] {
const updatedList = [...existingStructures];
const snippetItem = parseThreeLineSnippet(snippetLines);
const existingIndex = updatedList.findIndex(s => s.name.trim() === snippetItem.name.trim());
if (existingIndex !== -1) {
const existing = updatedList[existingIndex];
updatedList[existingIndex] = {
...existing,
status: snippetItem.status,
endTime: snippetItem.endTime,
};
}
return updatedList;
}
function applySingleLineParse(lines: string[], existingStructures: StructureItem[]): StructureItem[] {
const updatedList = [...existingStructures];
const newItems: StructureItem[] = [];
for (const line of lines) {
const item = parseFormatOneLine(line);
if (!item) continue;
const isDuplicate = updatedList.some(
s => s.structureTypeId === item.structureTypeId && s.name.trim() === item.name.trim(),
);
if (!isDuplicate) {
newItems.push(item);
}
}
return [...updatedList, ...newItems];
}

View File

@@ -0,0 +1,32 @@
export type StructureStatus = 'Powered' | 'Anchoring' | 'Unanchoring' | 'Low Power' | 'Abandoned' | 'Reinforced';
export interface StructureItem {
id: string;
systemId?: string;
structureTypeId?: string;
structureType?: string;
name: string;
ownerName?: string;
ownerId?: string;
ownerTicker?: string;
notes?: string;
status: StructureStatus;
endTime?: string;
}
export const STRUCTURE_TYPE_MAP: Record<string, string> = {
'35825': 'Raitaru',
'35826': 'Azbel',
'35827': 'Sotiyo',
'35832': 'Astrahus',
'35833': 'Fortizar',
'35834': 'Keepstar',
'35835': 'Athanor',
'35836': 'Tatara',
'40340': 'Upwell Palatine Keepstar',
'47512': "'Moreau' Fortizar",
'47513': "'Draccous' Fortizar",
'47514': "'Horizon' Fortizar",
'47515': "'Marginis' Fortizar",
'47516': "'Prometheus' Fortizar",
};

View File

@@ -0,0 +1,59 @@
import { StructureItem } from './structureTypes';
export function getActualStructures(oldList: StructureItem[], newList: StructureItem[]) {
const oldMap = new Map(oldList.map(s => [s.id, s]));
const newMap = new Map(newList.map(s => [s.id, s]));
const added: StructureItem[] = [];
const updated: StructureItem[] = [];
const removed: StructureItem[] = [];
for (const newItem of newList) {
const oldItem = oldMap.get(newItem.id);
if (!oldItem) {
added.push(newItem);
} else if (JSON.stringify(oldItem) !== JSON.stringify(newItem)) {
updated.push(newItem);
}
}
for (const oldItem of oldList) {
if (!newMap.has(oldItem.id)) {
removed.push(oldItem);
}
}
return { added, updated, removed };
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function mapServerStructure(serverData: any): StructureItem {
const { owner_id, owner_ticker, structure_type_id, structure_type, owner_name, end_time, system_id, ...rest } =
serverData;
return {
...rest,
ownerId: owner_id,
ownerTicker: owner_ticker,
ownerName: owner_name,
structureType: structure_type,
structureTypeId: structure_type_id,
endTime: end_time ?? '',
systemId: system_id,
};
}
export function formatToISO(datetimeLocal: string): string {
if (!datetimeLocal) return '';
// If missing seconds, add :00
let iso = datetimeLocal;
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(iso)) {
iso += ':00';
}
// Ensure trailing 'Z'
if (!iso.endsWith('Z')) {
iso += 'Z';
}
return iso;
}