summaryrefslogtreecommitdiff
path: root/backend-go/internal/core/graph_route.go
blob: c779c40ba5bb1e55bc7acac066b758e7ab331b38 (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
package core

import "fmt"

func (g *Graph) ShortestPath(source, target int) []int {
	if source == target {
		return []int{source}
	}
	res := g.SingleSourceDijkstra(source)
	if pr, ok := res[target]; ok {
		return pr.Path
	}
	return nil
}

func (g *Graph) FormattedRouteWithOffsets(path []int) ([]RouteStep, []int) {
	if len(path) == 0 {
		return nil, nil
	}

	var combined []int
	var rawOffsets []int

	for i := 0; i < len(path)-1; i++ {
		currID := path[i]
		nextID := path[i+1]

		currStation, currOk := g.Stations[currID]
		nextStation, nextOk := g.Stations[nextID]

		if currOk && currStation.IsStation {
			combined = append(combined, currID)
			rawOffsets = append(rawOffsets, i)
		} else if currOk && nextOk && !currStation.IsStation && !nextStation.IsStation && currStation.SolarSystemID == nextStation.SolarSystemID {
			combined = append(combined, currStation.SolarSystemID)
			rawOffsets = append(rawOffsets, i)
		}
	}

	lastID := path[len(path)-1]
	lastStation, lastOk := g.Stations[lastID]
	if lastOk && lastStation.IsStation {
		combined = append(combined, lastID)
		rawOffsets = append(rawOffsets, len(path)-1)
	}

	var steps []RouteStep
	var filteredOffsets []int

	for i, locID := range combined {
		if st, ok := g.Stations[locID]; ok {
			steps = append(steps, RouteStep{
					ID:           i,
					LocationID:   locID,
					Location:     fmt.Sprintf("%s - %s - %s", st.RegionName, st.SolarSystemName, st.ItemName),
					LocationType: "station",
				SystemID:    st.SolarSystemID,
				SystemName:  st.SolarSystemName,
				StationName: st.ItemName,
				Security:    st.Security,
				RegionID:    st.RegionID,
				Actions:     make([]Action, 0), // initialize empty for JSON formatting
			})
			filteredOffsets = append(filteredOffsets, rawOffsets[i])
		} else if sys, ok := g.SolarSystems[locID]; ok {
			steps = append(steps, RouteStep{
					ID:           i,
					LocationID:   locID,
					Location:     fmt.Sprintf("%s - %s", sys.RegionName, sys.ItemName),
					LocationType: "system",
				SystemID:    locID,
				SystemName:  sys.ItemName,
				StationName: fmt.Sprintf("%s - %s", sys.RegionName, sys.ItemName), // Using Region/System name like Python code formatting does
				RegionID:    sys.RegionID,
				Actions:     make([]Action, 0),
			})
			filteredOffsets = append(filteredOffsets, rawOffsets[i])
		}
	}

	return steps, filteredOffsets
}