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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477namespace Duets.CityExplorer
open Avalonia
open Avalonia.Controls
open Avalonia.Controls.Shapes
open Avalonia.Layout
open Avalonia.Markup.Xaml
open Avalonia.Media
open Duets.Entities
type MainWindow() =
inherit Window()
// Helper to parse string to CityId DU
let tryParseCityId (name: string) =
match name with
| "London" -> Some CityId.London
| "LosAngeles" -> Some CityId.LosAngeles
| "Madrid" -> Some CityId.Madrid
| "NewYork" -> Some CityId.NewYork
| "Prague" -> Some CityId.Prague
| _ -> None
// Show places summary for the city
member private this.ShowPlacesSummary
(city: City, placesSummaryPanel: StackPanel)
=
placesSummaryPanel.Children.Clear()
let placesSummary =
city.Zones
|> Map.values
|> Seq.collect (fun zone -> zone.Streets.Nodes |> Map.values)
|> Seq.collect _.Places
|> Seq.countBy (fun place ->
place.PlaceType |> World.Place.Type.toIndex)
|> Map.ofSeq
let summaryTitle =
TextBlock(
Text = "Places In City",
FontWeight = FontWeight.Bold,
Margin = Thickness(0.0, 0.0, 0.0, 4.0)
)
placesSummaryPanel.Children.Add(summaryTitle)
placesSummary
|> Map.iter (fun placeTypeIndex count ->
let placeTypeText = placeTypeIndex |> string
let summaryText = TextBlock(Text = $"{placeTypeText}: {count}")
placesSummaryPanel.Children.Add(summaryText))
// Show metro lines for the city
member private this.ShowMetroLines
(
city: City,
zonesPanel: StackPanel,
zoneDetailsPanel: StackPanel,
placesSummaryPanel: StackPanel
) =
zonesPanel.Children.Clear()
city.MetroLines
|> Map.toList
|> List.iter (fun (lineId, line) ->
let lineStack =
StackPanel(
Orientation = Orientation.Horizontal,
Margin = Thickness(0.0, 0.0, 0.0, 16.0),
Spacing = 8.0
)
// Choose color based on lineId
let lineColor =
match lineId with
| MetroLineId.Red -> Colors.IndianRed
| MetroLineId.Blue -> Colors.SteelBlue
// Get ordered list of zones for this line
let rec walkLine acc currentZoneId =
match line.Stations |> Map.tryFind currentZoneId with
| Some(MetroStationConnection.OnlyNext next) ->
walkLine (acc @ [ currentZoneId ]) next
| Some(MetroStationConnection.PreviousAndNext(_, next)) ->
walkLine (acc @ [ currentZoneId ]) next
| _ -> acc @ [ currentZoneId ]
// Find the starting zone (no previous)
let startZoneId =
line.Stations
|> Map.tryFindKey (fun _ conn ->
match conn with
| MetroStationConnection.OnlyNext _ -> true
| MetroStationConnection.PreviousAndNext(prev, _) ->
not (line.Stations.ContainsKey prev)
| _ -> false)
|> Option.defaultValue (
line.Stations |> Map.toList |> List.head |> fst
)
let orderedZones = walkLine [] startZoneId
// Draw the line
orderedZones
|> List.iteri (fun i zoneId ->
// Check if this zone is a transfer (connected to 2+ lines)
let isTransfer =
match city.Zones.TryGetValue(zoneId) with
| true, zone -> zone.MetroStations.Count >= 2
| _ -> false
let border = Border()
border.Background <- SolidColorBrush(Colors.White)
border.BorderBrush <-
if isTransfer then
SolidColorBrush(Colors.MediumSeaGreen)
else
SolidColorBrush(lineColor)
border.BorderThickness <- Thickness(2.0)
border.CornerRadius <- CornerRadius(8.0)
border.Padding <- Thickness(12.0)
let text =
TextBlock(
Text = city.Zones[zoneId].Name,
FontSize = 16.0,
Foreground = SolidColorBrush(Colors.Black)
)
border.Child <- text
border.PointerPressed.Add(fun _ ->
this.ShowZoneDetails(
city,
zoneId,
zonesPanel,
zoneDetailsPanel,
placesSummaryPanel
))
lineStack.Children.Add(border)
// Draw a line between nodes except after the last
if i < orderedZones.Length - 1 then
let connector =
Border(
Width = 32.0,
Height = 4.0,
Background = SolidColorBrush(lineColor),
VerticalAlignment = VerticalAlignment.Center
)
lineStack.Children.Add(connector))
// Add a label for the line
let label =
TextBlock(
Text = lineId.ToString(),
Foreground = SolidColorBrush(lineColor),
FontWeight = FontWeight.Bold,
Margin = Thickness(0.0, 0.0, 0.0, 4.0)
)
zonesPanel.Children.Add(label)
zonesPanel.Children.Add(lineStack))
// Show street graph for a zone
member private this.ShowStreetGraph(zone: Zone) =
let streetGraphCanvas =
Canvas(
Width = 900.0,
Height = 200.0,
Background = SolidColorBrush(Colors.LightGray),
Margin = Thickness(0.0, 0.0, 0.0, 16.0)
)
let streets = zone.Streets.Nodes
let connections = zone.Streets.Connections
if not streets.IsEmpty then
// Check if this is a linear graph (each node has at most 2 connections)
let isLinearGraph =
connections
|> Map.forall (fun _ nodeConnections ->
nodeConnections |> Map.count <= 2)
let streetPositions =
if isLinearGraph && streets.Count > 2 then
// Use linear layout for linear graphs
let startingStreet = zone.Streets.StartingNode
let visited = ref Set.empty
let orderedStreets = ref []
// Traverse the graph linearly starting from the starting node
let rec traverse currentStreetId =
if not (visited.Value.Contains(currentStreetId)) then
visited.Value <- visited.Value.Add(currentStreetId)
orderedStreets.Value <-
currentStreetId :: orderedStreets.Value
// Find next unvisited connected street
match connections.TryFind(currentStreetId) with
| Some nodeConnections ->
let nextStreet =
nodeConnections
|> Map.values
|> Seq.tryFind (fun streetId ->
not (visited.Value.Contains(streetId)))
match nextStreet with
| Some nextId -> traverse nextId
| None -> ()
| None -> ()
traverse startingStreet
let orderedStreets = List.rev orderedStreets.Value
// Ensure all streets have positions, even if not visited in traversal
let allStreetIds = streets |> Map.keys |> Set.ofSeq
let visitedStreetIds = orderedStreets |> Set.ofList
let unvisitedStreets =
Set.difference allStreetIds visitedStreetIds
|> Set.toList
let finalOrderedStreets = orderedStreets @ unvisitedStreets
// Position streets in a horizontal line
let spacing = 800.0 / float (finalOrderedStreets.Length - 1)
let startX = 50.0
let y = 100.0
finalOrderedStreets
|> List.mapi (fun i streetId ->
let x = startX + (float i * spacing)
(streetId, Point(x, y)))
|> Map.ofList
else
// Use circular layout for complex graphs or small graphs
let angleStep = 2.0 * System.Math.PI / float streets.Count
let radius = 80.0
let centerX = 300.0 // Adjusted for larger canvas
let centerY = 100.0
streets
|> Map.toList
|> List.mapi (fun i (streetId, _) ->
let angle = float i * angleStep
let x = centerX + radius * System.Math.Cos(angle)
let y = centerY + radius * System.Math.Sin(angle)
(streetId, Point(x, y)))
|> Map.ofList
// Draw edges first, so they appear behind nodes
for startStreetId, endStreetMap in connections |> Map.toList do
let startPos = streetPositions[startStreetId]
for endStreetId in (endStreetMap |> Map.values) do
if startStreetId < endStreetId then
let endPos = streetPositions[endStreetId]
let line =
Line(
StartPoint = startPos,
EndPoint = endPos,
Stroke = SolidColorBrush(Colors.Black),
StrokeThickness = 1.0
)
streetGraphCanvas.Children.Add(line)
// Draw nodes
for streetId, street in streets |> Map.toList do
let pos = streetPositions[streetId]
let hasMetroStation =
street.Places
|> List.exists (fun p ->
p.PlaceType = PlaceType.MetroStation)
let node =
Ellipse(
Width = 20.0,
Height = 20.0,
Fill =
if hasMetroStation then
SolidColorBrush(Colors.MediumSeaGreen)
else
SolidColorBrush(Colors.DarkSlateBlue)
)
Canvas.SetLeft(node, pos.X - 10.0)
Canvas.SetTop(node, pos.Y - 10.0)
streetGraphCanvas.Children.Add(node)
let label = TextBlock(Text = street.Name, FontSize = 12.0)
// Position labels differently based on layout type
if isLinearGraph && streets.Count > 2 then
// For linear layout, alternate labels above and below to prevent overlap
let streetIndex =
streetPositions
|> Map.toList
|> List.findIndex (fun (id, _) -> id = streetId)
let isEven = streetIndex % 2 = 0
Canvas.SetLeft(label, pos.X - 30.0) // Center the label under the node
Canvas.SetTop(
label,
if isEven then pos.Y + 25.0 else pos.Y - 35.0
) // Alternate above/below
else
// For circular layout, use original positioning
Canvas.SetLeft(label, pos.X + 15.0)
Canvas.SetTop(label, pos.Y - 8.0)
streetGraphCanvas.Children.Add(label)
streetGraphCanvas
// Show places by type for a zone
member private this.ShowPlacesByType(zone: Zone, detailsStack: StackPanel) =
for _, street in zone.Streets.Nodes |> Map.toList do
let streetHeader =
TextBlock(
Text = street.Name,
FontSize = 18.0,
FontWeight = FontWeight.Bold,
Margin = Thickness(0.0, 12.0, 0.0, 4.0)
)
detailsStack.Children.Add(streetHeader)
let placesByType = street.Places |> List.groupBy _.PlaceType
for placeType, places in placesByType do
let typeHeaderBorder =
Border(
Background = SolidColorBrush(Colors.DarkSlateGray),
CornerRadius = CornerRadius(6.0),
Padding = Thickness(8.0, 2.0, 8.0, 2.0),
Margin = Thickness(8.0, 8.0, 0.0, 2.0)
)
let placeTypeText =
placeType |> World.Place.Type.toIndex |> string
let typeHeaderText =
TextBlock(
Text = placeTypeText,
FontWeight = FontWeight.Bold
)
typeHeaderBorder.Child <- typeHeaderText
detailsStack.Children.Add(typeHeaderBorder)
for place in places do
let info =
match place.PlaceType with
| PlaceType.ConcertSpace c -> $" ({c})"
| PlaceType.Hotel h -> $" ({h})"
| PlaceType.RadioStudio r -> $" ({r})"
| PlaceType.RehearsalSpace r -> $" ({r})"
| PlaceType.Studio s -> $" ({s})"
| _ -> ""
let placeText =
TextBlock(
Text = $"- {place.Name}{info}",
Margin = Thickness(16.0, 0.0, 0.0, 0.0)
)
detailsStack.Children.Add(placeText)
// Show zone details
member private this.ShowZoneDetails
(
city: City,
zoneId: ZoneId,
zonesPanel: StackPanel,
zoneDetailsPanel: StackPanel,
placesSummaryPanel: StackPanel
) =
// Hide city overview panels and show zone details
zonesPanel.IsVisible <- false
placesSummaryPanel.IsVisible <- false
zoneDetailsPanel.IsVisible <- true
zoneDetailsPanel.Children.Clear()
let detailsStack = StackPanel()
let arrowBtn =
Button(Content = "โ Back", Margin = Thickness(0.0, 0.0, 0.0, 12.0))
arrowBtn.Click.Add(fun _ ->
// Show city overview panels and hide zone details
zoneDetailsPanel.IsVisible <- false
zonesPanel.IsVisible <- true
placesSummaryPanel.IsVisible <- true)
detailsStack.Children.Add(arrowBtn)
let detailsText =
TextBlock(
Text = $"Zone: {city.Zones[zoneId].Name}",
FontSize = 20.0,
FontWeight = FontWeight.Bold,
Margin = Thickness(0.0, 0.0, 0.0, 8.0)
)
detailsStack.Children.Add(detailsText)
let zone = city.Zones[zoneId]
// Add street graph
let streetGraphCanvas = this.ShowStreetGraph(zone)
detailsStack.Children.Add(streetGraphCanvas)
// Add places by type
this.ShowPlacesByType(zone, detailsStack)
zoneDetailsPanel.Children.Add(detailsStack)
override this.OnInitialized() =
base.OnInitialized()
AvaloniaXamlLoader.Load(this)
let world = Duets.Data.World.World.get
let cityMap = world.Cities
let cities =
cityMap |> Map.toList |> List.map (fun (id, _) -> id.ToString())
let listBox = this.FindControl<ListBox>("CitiesListBox")
let zonesPanel = this.FindControl<StackPanel>("ZonesPanel")
let zoneDetailsPanel = this.FindControl<StackPanel>("ZoneDetailsPanel")
let placesSummaryPanel =
this.FindControl<StackPanel>("PlacesSummaryPanel")
listBox.ItemsSource <- cities
listBox.SelectionChanged.Add(fun _ ->
zonesPanel.Children.Clear()
zoneDetailsPanel.Children.Clear()
placesSummaryPanel.Children.Clear()
// Show city overview panels, hide zone details
zonesPanel.IsVisible <- true
placesSummaryPanel.IsVisible <- true
zoneDetailsPanel.IsVisible <- false
match listBox.SelectedItem with
| :? string as cityName ->
match tryParseCityId cityName with
| Some cityId ->
let cityOpt = cityMap |> Map.tryFind cityId
match cityOpt with
| Some city ->
this.ShowPlacesSummary(city, placesSummaryPanel)
this.ShowMetroLines(
city,
zonesPanel,
zoneDetailsPanel,
placesSummaryPanel
)
| None -> ()
| None -> ()
| _ -> ())