73 lines
2.7 KiB
Dart
73 lines
2.7 KiB
Dart
/// Which cameras exist: shooting positions (lanes 1–6) × locations (strelišča).
|
||
/// Each position can have a camera at several locations, e.g. "Kovinar" and "Doma".
|
||
class CameraSetup {
|
||
static const int maxPositions = 6;
|
||
static const int firstPort = 9081;
|
||
|
||
final List<String> locations;
|
||
final int positions;
|
||
final Map<int, Map<String, String>> ips; // position (1-based) → location → ip:port
|
||
final String? activeLocation; // last chosen, used by screens that show one location
|
||
final String? clubLocation; // added by the club code, removed when access is removed
|
||
|
||
const CameraSetup({
|
||
this.locations = const [],
|
||
this.positions = maxPositions,
|
||
this.ips = const {},
|
||
this.activeLocation,
|
||
this.clubLocation,
|
||
});
|
||
|
||
String ipFor(int position, String location) => ips[position]?[location] ?? '';
|
||
|
||
/// Locations with a camera at [position], in the user's location order.
|
||
Map<String, String> camerasAt(int position) => {
|
||
for (final l in locations)
|
||
if (ipFor(position, l).isNotEmpty) l: ipFor(position, l),
|
||
};
|
||
|
||
int camerasIn(String location) => [
|
||
for (int p = 1; p <= positions; p++)
|
||
if (ipFor(p, location).isNotEmpty) p,
|
||
].length;
|
||
|
||
CameraSetup copyWith({
|
||
List<String>? locations,
|
||
int? positions,
|
||
Map<int, Map<String, String>>? ips,
|
||
String? activeLocation,
|
||
String? clubLocation,
|
||
bool clearClubLocation = false,
|
||
}) {
|
||
return CameraSetup(
|
||
locations: locations ?? this.locations,
|
||
positions: positions ?? this.positions,
|
||
ips: ips ?? this.ips,
|
||
activeLocation: activeLocation ?? this.activeLocation,
|
||
clubLocation: clearClubLocation ? null : (clubLocation ?? this.clubLocation),
|
||
);
|
||
}
|
||
|
||
/// Deep copy of [ips] for editing.
|
||
Map<int, Map<String, String>> ipsCopy() => {for (final e in ips.entries) e.key: {...e.value}};
|
||
|
||
Map<String, dynamic> toJson() => {
|
||
'locations': locations,
|
||
'positions': positions,
|
||
'ips': {for (final e in ips.entries) '${e.key}': e.value},
|
||
'activeLocation': activeLocation,
|
||
'clubLocation': clubLocation,
|
||
};
|
||
|
||
factory CameraSetup.fromJson(Map<String, dynamic> json) => CameraSetup(
|
||
locations: [for (final l in json['locations'] as List? ?? []) l as String],
|
||
positions: (json['positions'] as int? ?? maxPositions).clamp(1, maxPositions),
|
||
ips: {
|
||
for (final e in (json['ips'] as Map<String, dynamic>? ?? {}).entries)
|
||
int.parse(e.key): {for (final i in (e.value as Map).entries) i.key as String: i.value as String},
|
||
},
|
||
activeLocation: json['activeLocation'] as String?,
|
||
clubLocation: json['clubLocation'] as String?,
|
||
);
|
||
}
|