summaryrefslogtreecommitdiff
path: root/backend-go/internal/core/loader.go
blob: 8e451ef13181fab7f1f757ca1895a680ec889443 (plain)
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
package core

import (
	"encoding/csv"
	"os"
	"path/filepath"
	"strconv"
)

const (
	TypeIDSolarSystem = 5
	GroupIDStation    = 15
	GroupIDStargate   = 10
)

var AllowedRegionIDs = map[int]bool{
	10000002: true, // The Forge
	10000016: true, // Lonetrek
	10000030: true, // Heimatar
	10000033: true, // The Citadel
	10000032: true, // Sinq Laison
	10000042: true, // Metropolis
	10000064: true, // Essence
	10000037: true, // Everyshore
	10000043: true, // Domain
	10000036: true, // Devoid
	10000052: true, // Kador
	10000067: true, // Genesis
	10000068: true, // Verge Vendor
	10000001: true, // Derelik
	10000020: true, // Tash-Murkon
}

type Position struct {
	X float64 `json:"x"`
	Y float64 `json:"y"`
	Z float64 `json:"z"`
}

type Station struct {
	IsStation           bool       `json:"is_station"`
	ItemName            string     `json:"item_name"`
	Security            float64    `json:"security"`
	Position            Position   `json:"position"`
	SolarSystemID       int        `json:"solar_system_id"`
	RegionID            int        `json:"region_id"`
	RegionName          string     `json:"region_name"`
	SolarSystemName     string     `json:"solar_system_name"`
	SolarSystemPosition Position   `json:"solar_system_position"`
	Jumps               []int      `json:"jumps"`
}

type SolarSystem struct {
	ItemName   string   `json:"item_name"`
	Position   Position `json:"position"`
	RegionID   int      `json:"region_id"`
	RegionName string   `json:"region_name"`
}

type Item struct {
	TypeID      int      `json:"type_id"`
	GroupID     int      `json:"group_id"`
	TypeName    string   `json:"type_name"`
	Description string   `json:"description"`
	Mass        *float64 `json:"mass"`
	Volume      *float64 `json:"volume"`
	Capacity    *float64 `json:"capacity"`
}

// DataLoader handles loading data from CSV files
type DataLoader struct {
	BaseDir string
}

func NewDataLoader(baseDir string) *DataLoader {
	return &DataLoader{BaseDir: baseDir}
}

func (dl *DataLoader) ReadRegions() (map[int]string, error) {
	file, err := os.Open(filepath.Join(dl.BaseDir, "mapRegions.csv"))
	if err != nil {
		return nil, err
	}
	defer file.Close()

	reader := csv.NewReader(file)
	records, err := reader.ReadAll()
	if err != nil {
		return nil, err
	}

	regions := make(map[int]string)
	if len(records) > 0 {
		records = records[1:] // Skip header
	}

	for _, row := range records {
		regionID, err := strconv.Atoi(row[0])
		if err != nil {
			continue
		}
		regions[regionID] = row[1]
	}
	return regions, nil
}

func (dl *DataLoader) LoadMapData() (map[int]*Station, map[int]*SolarSystem, error) {
	regions, err := dl.ReadRegions()
	if err != nil {
		return nil, nil, err
	}

	file, err := os.Open(filepath.Join(dl.BaseDir, "mapDenormalize.csv"))
	if err != nil {
		return nil, nil, err
	}
	defer file.Close()

	reader := csv.NewReader(file)
	// Some fields might be mismatched or have commas in strings, standard csv covers it
	records, err := reader.ReadAll()
	if err != nil {
		return nil, nil, err
	}

	stations := make(map[int]*Station)
	solarSystems := make(map[int]*SolarSystem)

	if len(records) > 0 {
		records = records[1:]
	}

	for _, row := range records {
		if len(row) < 15 {
			continue
		}

		regionIDStr := row[5]
		if regionIDStr == "None" || regionIDStr == "" {
			continue
		}
		regionID, err := strconv.Atoi(regionIDStr)
		if err != nil || !AllowedRegionIDs[regionID] {
			continue
		}

		itemID, _ := strconv.Atoi(row[0])
		typeID, _ := strconv.Atoi(row[1])
		groupID, _ := strconv.Atoi(row[2])

		solarSystemID := 0
		if row[3] != "None" && row[3] != "" {
			solarSystemID, _ = strconv.Atoi(row[3])
		}

		x, _ := strconv.ParseFloat(row[7], 64)
		y, _ := strconv.ParseFloat(row[8], 64)
		z, _ := strconv.ParseFloat(row[9], 64)

		itemName := row[11]
		security, _ := strconv.ParseFloat(row[12], 64)

		regionName := regions[regionID]
		if regionName == "" {
			regionName = "Unknown"
		}

		if typeID == TypeIDSolarSystem {
			solarSystems[itemID] = &SolarSystem{
				ItemName:   itemName,
				Position:   Position{X: x, Y: y, Z: z},
				RegionID:   regionID,
				RegionName: regionName,
			}
		} else if groupID == GroupIDStation || groupID == GroupIDStargate {
			stations[itemID] = &Station{
				IsStation:     groupID == GroupIDStation,
				ItemName:      itemName,
				Security:      security,
				Position:      Position{X: x, Y: y, Z: z},
				SolarSystemID: solarSystemID,
				RegionID:      regionID,
				Jumps:         []int{},
			}
		}
	}

	// Update station data
	for _, station := range stations {
		if ss, ok := solarSystems[station.SolarSystemID]; ok {
			station.SolarSystemName = ss.ItemName
			station.SolarSystemPosition = ss.Position
			station.RegionID = ss.RegionID
			if name, exists := regions[ss.RegionID]; exists {
				station.RegionName = name
			} else {
				station.RegionName = "Unknown"
			}
		}
	}

	return stations, solarSystems, nil
}

func parseOptionalFloat(val string) *float64 {
	if val == "None" || val == "" {
		return nil
	}
	f, err := strconv.ParseFloat(val, 64)
	if err != nil {
		return nil
	}
	return &f
}

func (dl *DataLoader) LoadItems() (map[int]*Item, error) {
	file, err := os.Open(filepath.Join(dl.BaseDir, "invTypes.csv"))
	if err != nil {
		return nil, err
	}
	defer file.Close()

	reader := csv.NewReader(file)
	records, err := reader.ReadAll()
	if err != nil {
		return nil, err
	}

	items := make(map[int]*Item)
	if len(records) > 0 {
		records = records[1:]
	}

	for _, row := range records {
		if len(row) < 7 {
			continue
		}
		typeID, err := strconv.Atoi(row[0])
		if err != nil {
			continue
		}
		groupID, _ := strconv.Atoi(row[1])

		items[typeID] = &Item{
			TypeID:      typeID,
			GroupID:     groupID,
			TypeName:    row[2],
			Description: row[3],
			Mass:        parseOptionalFloat(row[4]),
			Volume:      parseOptionalFloat(row[5]),
			Capacity:    parseOptionalFloat(row[6]),
		}
	}
	return items, nil
}

func (dl *DataLoader) LoadStarGateConnections() ([][2]int, error) {
	file, err := os.Open(filepath.Join(dl.BaseDir, "mapJumps.csv"))
	if err != nil {
		return nil, err
	}
	defer file.Close()

	reader := csv.NewReader(file)
	records, err := reader.ReadAll()
	if err != nil {
		return nil, err
	}

	var connections [][2]int
	if len(records) > 0 {
		records = records[1:]
	}

	for _, row := range records {
		if len(row) < 2 {
			continue
		}
		from, err1 := strconv.Atoi(row[0])
		to, err2 := strconv.Atoi(row[1])
		if err1 == nil && err2 == nil {
			connections = append(connections, [2]int{from, to})
		}
	}
	return connections, nil
}