nms-graph 0.2.0

In-memory galactic model with spatial index and routing for No Man's Sky Copilot
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Community data import from CSV files.
//!
//! Imports coordinate data (e.g., from NMSCE, wiki, or community spreadsheets)
//! into the galaxy model. Each row must include at minimum a system name and
//! portal glyphs; galaxy, biome, and platform columns are optional.

use std::path::Path;

use serde::Deserialize;

use nms_core::address::GalacticAddress;
use nms_core::biome::Biome;
use nms_core::galaxy::Galaxy;
use nms_core::system::{Planet, System};

use crate::model::GalaxyModel;
use crate::spatial::SystemId;

/// Statistics from a CSV import operation.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImportStats {
    /// Number of new systems added to the model.
    pub added: usize,
    /// Number of rows that matched an existing system (skipped).
    pub duplicates: usize,
    /// Number of rows that could not be parsed (skipped).
    pub skipped: usize,
}

/// Errors that can occur during community data import.
#[derive(Debug, thiserror::Error)]
pub enum ImportError {
    /// CSV reading/parsing error.
    #[error("CSV error: {0}")]
    Csv(#[from] csv::Error),
    /// I/O error reading the file.
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

/// A single row from a community coordinate CSV.
#[derive(Debug, Deserialize)]
struct CommunityRecord {
    #[serde(alias = "System Name", alias = "system_name", alias = "Name")]
    system_name: String,

    #[serde(alias = "Galaxy", alias = "galaxy", default = "default_galaxy")]
    galaxy: String,

    #[serde(
        alias = "Portal Glyphs",
        alias = "portal_glyphs",
        alias = "Glyphs",
        alias = "glyphs"
    )]
    portal_glyphs: String,

    #[serde(alias = "Biome", alias = "biome", default)]
    biome: Option<String>,

    #[allow(dead_code)]
    #[serde(alias = "Platform", alias = "platform", default)]
    platform: Option<String>,
}

fn default_galaxy() -> String {
    "Euclid".to_string()
}

/// Resolve a galaxy name to its index (0-255).
///
/// Performs case-insensitive matching against all 256 galaxy names.
/// Falls back to 0 (Euclid) if the name is not recognized.
fn resolve_galaxy_index(name: &str) -> u8 {
    // Try parsing as a numeric index first
    if let Ok(idx) = name.parse::<u8>() {
        return idx;
    }

    let lower = name.to_lowercase();
    for i in 0..=255u8 {
        let galaxy = Galaxy::by_index(i);
        if galaxy.name.to_lowercase() == lower {
            return i;
        }
    }

    log::warn!("Unknown galaxy name '{}', defaulting to Euclid (0)", name);
    0
}

/// Import community coordinate data from a CSV file into the galaxy model.
///
/// Expected CSV columns (flexible naming via aliases):
/// - `System Name` (required)
/// - `Portal Glyphs` (required) -- 12 hex digits
/// - `Galaxy` (optional, defaults to "Euclid")
/// - `Biome` (optional)
/// - `Platform` (optional, currently ignored)
///
/// Returns [`ImportStats`] summarizing what was imported.
pub fn import_csv(
    model: &mut GalaxyModel,
    path: &Path,
    _source_name: &str,
) -> Result<ImportStats, ImportError> {
    let mut stats = ImportStats::default();
    let mut reader = csv::Reader::from_path(path)?;

    for result in reader.deserialize() {
        let record: CommunityRecord = match result {
            Ok(r) => r,
            Err(e) => {
                log::warn!("Skipping malformed CSV row: {}", e);
                stats.skipped += 1;
                continue;
            }
        };

        // Parse portal glyphs (12 hex digits) into a packed u64
        let glyphs = record.portal_glyphs.trim();
        let hex_str = glyphs
            .strip_prefix("0x")
            .or_else(|| glyphs.strip_prefix("0X"))
            .unwrap_or(glyphs);

        if hex_str.len() != 12 {
            log::warn!(
                "Skipping '{}': portal glyphs '{}' must be exactly 12 hex digits",
                record.system_name,
                record.portal_glyphs
            );
            stats.skipped += 1;
            continue;
        }

        let packed = match u64::from_str_radix(hex_str, 16) {
            Ok(v) => v,
            Err(_) => {
                log::warn!(
                    "Skipping '{}': invalid hex in portal glyphs '{}'",
                    record.system_name,
                    record.portal_glyphs
                );
                stats.skipped += 1;
                continue;
            }
        };

        let galaxy_index = resolve_galaxy_index(&record.galaxy);
        let addr = GalacticAddress::from_packed(packed, galaxy_index);
        let sys_id = SystemId::from_address(&addr);

        // Check for duplicate
        if model.systems.contains_key(&sys_id) {
            stats.duplicates += 1;
            continue;
        }

        // Parse optional biome
        let biome = record
            .biome
            .as_deref()
            .and_then(|b| b.parse::<Biome>().ok());

        // Build planet list (a single planet if biome is provided)
        let planets = if let Some(b) = biome {
            vec![Planet::new(0, Some(b), None, false, None, None)]
        } else {
            vec![]
        };

        let system = System::new(addr, Some(record.system_name), None, None, planets);

        model.insert_system(system);
        stats.added += 1;
    }

    Ok(stats)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    /// Create a temporary CSV file with the given content.
    fn csv_file(content: &str) -> NamedTempFile {
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(content.as_bytes()).unwrap();
        file.flush().unwrap();
        file
    }

    #[test]
    fn test_import_csv_basic() {
        let csv = "\
System Name,Galaxy,Portal Glyphs,Biome
Alpha System,Euclid,01717D8A4EA2,Lush
Beta System,Euclid,0A0002001234,Toxic
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 2);
        assert_eq!(stats.duplicates, 0);
        assert_eq!(stats.skipped, 0);
        assert_eq!(model.system_count(), 2);
    }

    #[test]
    fn test_import_csv_duplicate_detection() {
        let csv = "\
System Name,Galaxy,Portal Glyphs,Biome
Alpha System,Euclid,01717D8A4EA2,Lush
Alpha Duplicate,Euclid,01717D8A4EA2,Toxic
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        assert_eq!(stats.duplicates, 1);
        assert_eq!(model.system_count(), 1);
    }

    #[test]
    fn test_import_csv_bad_glyphs_skipped() {
        let csv = "\
System Name,Galaxy,Portal Glyphs,Biome
Good System,Euclid,01717D8A4EA2,Lush
Bad Glyphs,Euclid,ZZZZZZZZZZZZ,Lush
Short Glyphs,Euclid,0171,Lush
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        assert_eq!(stats.skipped, 2);
        assert_eq!(model.system_count(), 1);
    }

    #[test]
    fn test_import_csv_no_biome() {
        let csv = "\
System Name,Galaxy,Portal Glyphs
No Biome System,Euclid,01717D8A4EA2
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        let sys_id = *model.systems.keys().next().unwrap();
        let system = model.system(&sys_id).unwrap();
        assert!(system.planets.is_empty());
    }

    #[test]
    fn test_import_csv_default_galaxy() {
        let csv = "\
System Name,Portal Glyphs
Default Galaxy,01717D8A4EA2
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        let sys_id = *model.systems.keys().next().unwrap();
        let system = model.system(&sys_id).unwrap();
        assert_eq!(system.address.reality_index, 0);
    }

    #[test]
    fn test_import_csv_eissentam_galaxy() {
        let csv = "\
System Name,Galaxy,Portal Glyphs
Eissentam System,Eissentam,01717D8A4EA2
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        let sys_id = *model.systems.keys().next().unwrap();
        let system = model.system(&sys_id).unwrap();
        assert_eq!(system.address.reality_index, 9);
    }

    #[test]
    fn test_import_csv_galaxy_numeric() {
        let csv = "\
System Name,Galaxy,Portal Glyphs
Numeric Galaxy,9,01717D8A4EA2
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        let sys_id = *model.systems.keys().next().unwrap();
        let system = model.system(&sys_id).unwrap();
        assert_eq!(system.address.reality_index, 9);
    }

    #[test]
    fn test_import_csv_into_populated_model() {
        let mut model = GalaxyModel::new();

        // Pre-populate with a system
        let addr = GalacticAddress::new(100, 0, 0, 0x100, 0, 0);
        let existing = System::new(addr, Some("Existing".into()), None, None, vec![]);
        model.insert_system(existing);
        assert_eq!(model.system_count(), 1);

        let csv = "\
System Name,Galaxy,Portal Glyphs
New System,Euclid,01717D8A4EA2
";
        let file = csv_file(csv);
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
        assert_eq!(model.system_count(), 2);
    }

    #[test]
    fn test_import_csv_empty_file() {
        let csv = "System Name,Galaxy,Portal Glyphs\n"; // headers only
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 0);
        assert_eq!(stats.duplicates, 0);
        assert_eq!(stats.skipped, 0);
    }

    #[test]
    fn test_import_csv_multi_galaxy() {
        let csv = "\
System Name,Galaxy,Portal Glyphs
Euclid Sys,Euclid,01717D8A4EA2
Hilbert Sys,Hilbert Dimension,0A0002001234
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 2);

        // Should have separate spatial indexes per galaxy
        let galaxies = model.discovered_galaxies();
        assert_eq!(galaxies.len(), 2);
        assert!(galaxies.contains(&0));
        assert!(galaxies.contains(&1));
    }

    #[test]
    fn test_import_csv_with_biome_creates_planet() {
        let csv = "\
System Name,Galaxy,Portal Glyphs,Biome
Lush World,Euclid,01717D8A4EA2,Lush
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        import_csv(&mut model, file.path(), "test").unwrap();

        let sys_id = *model.systems.keys().next().unwrap();
        let system = model.system(&sys_id).unwrap();
        assert_eq!(system.planets.len(), 1);
        assert_eq!(system.planets[0].biome, Some(Biome::Lush));
    }

    #[test]
    fn test_import_csv_file_not_found() {
        let mut model = GalaxyModel::new();
        let result = import_csv(&mut model, Path::new("/no/such/file.csv"), "test");
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_galaxy_index_euclid() {
        assert_eq!(resolve_galaxy_index("Euclid"), 0);
    }

    #[test]
    fn test_resolve_galaxy_index_case_insensitive() {
        assert_eq!(resolve_galaxy_index("euclid"), 0);
        assert_eq!(resolve_galaxy_index("EUCLID"), 0);
        assert_eq!(resolve_galaxy_index("eissentam"), 9);
    }

    #[test]
    fn test_resolve_galaxy_index_numeric() {
        assert_eq!(resolve_galaxy_index("0"), 0);
        assert_eq!(resolve_galaxy_index("9"), 9);
        assert_eq!(resolve_galaxy_index("255"), 255);
    }

    #[test]
    fn test_resolve_galaxy_index_unknown_defaults_to_zero() {
        assert_eq!(resolve_galaxy_index("NotAGalaxy"), 0);
    }

    #[test]
    fn test_import_csv_glyphs_with_0x_prefix() {
        let csv = "\
System Name,Galaxy,Portal Glyphs
Prefixed,Euclid,0x01717D8A4EA2
";
        let file = csv_file(csv);
        let mut model = GalaxyModel::new();
        let stats = import_csv(&mut model, file.path(), "test").unwrap();

        assert_eq!(stats.added, 1);
    }

    #[test]
    fn test_import_stats_default() {
        let stats = ImportStats::default();
        assert_eq!(stats.added, 0);
        assert_eq!(stats.duplicates, 0);
        assert_eq!(stats.skipped, 0);
    }
}