Skip to content

Commit c68d0f1

Browse files
committed
Refactor matchloading to support Catigoal JSON format
1 parent 7d668a0 commit c68d0f1

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

lib/screens/settings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
334334
),
335335
Padding(
336336
padding: EdgeInsets.symmetric(vertical: 4.0),
337-
child: Text('Version: 0.9.5', style: TextStyle(fontSize: 14)),
337+
child: Text('Version: 0.9.6', style: TextStyle(fontSize: 14)),
338338
),
339339
Padding(
340340
padding: EdgeInsets.symmetric(vertical: 4.0),

lib/services/match_data.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ class Match {
2121

2222
factory Match.fromJson(Map<String, dynamic> json) {
2323
return Match(
24-
id: json['#'] as String,
25-
fieldRaw: json['Field'] as String,
26-
team1: json['Teams_1'] as String,
27-
team2: json['Teams_3'] as String,
28-
29-
// Extract only the number from the fieldRaw string
30-
// Example: "L-1" -> "1"
31-
field: RegExp(r'\d+').firstMatch(json['Field'] as String)?.group(0) ?? '',
24+
id: json['number']?.toString() ?? '', // Safely extract match number as the ID
25+
fieldRaw: json['pitch'] as String? ?? '', // Safely extract pitch
26+
team1: json['team1']?['name'] as String? ?? 'Unknown Team 1', // Safely extract team1 name
27+
team2: json['team2']?['name'] as String? ?? 'Unknown Team 2', // Safely extract team2 name
28+
field: RegExp(r'\d+')
29+
.firstMatch(json['pitch'] as String? ?? '')
30+
?.group(0)
31+
?.replaceFirst(RegExp(r'^0+'), '') ??
32+
'', // Safely extract field number
3233
);
3334
}
3435
}
3536

3637
class MatchDataService {
37-
String _url = 'https://raw.githubusercontent.com/robocup-junior/soccer-matches/refs/heads/main/data/RCJI25/matches.json';
38+
String _url = 'https://catigoal.com/rest/v1/RCJI25/matches?format=json';
3839
String _matchId = '';
3940
String _state = '';
4041
List<Match> _matches = [];
@@ -50,28 +51,34 @@ class MatchDataService {
5051
/// Loads MQTT settings from SharedPreferences
5152
Future<void> loadPreferences() async {
5253
prefs = await SharedPreferences.getInstance();
53-
_url = prefs.getString('matches_url') ?? 'https://raw.githubusercontent.com/robocup-junior/soccer-matches/refs/heads/main/data/RCJI25/matches.json';
54+
_url = prefs.getString('matches_url') ?? 'https://catigoal.com/rest/v1/RCJI25/matches?format=json';
5455
}
5556

5657
Future<List<Match>> fetchMatches(String url) async {
5758
final response = await http.get(Uri.parse(url));
5859

59-
//print(response.body);
6060
if (response.statusCode == 200) {
6161
print('Matches loaded successfully');
62-
final List<dynamic> jsonList = json.decode(response.body);
63-
print('Number of matches: ${jsonList.length}');
64-
return jsonList.map((json) => Match.fromJson(json)).toList();
62+
final Map<String, dynamic> jsonResponse = json.decode(response.body);
63+
final List<dynamic> matchesList = jsonResponse['matches'];
64+
print('Number of matches: ${matchesList.length}');
65+
return matchesList.map((matchJson) => Match.fromJson(matchJson)).toList();
6566
} else {
6667
throw Exception('Failed to load matches');
6768
}
6869
}
6970

7071
Match? findMatchById(List<Match> matches, String gameId) {
7172
try {
72-
return matches.firstWhere((match) => match.id == gameId);
73+
// Iterate through matches and find the one with the matching ID
74+
for (var match in matches) {
75+
if (match.id == gameId) {
76+
return match;
77+
}
78+
}
79+
return null; // Return null if no match is found
7380
} catch (e) {
74-
return null;
81+
return null; // Handle errors gracefully
7582
}
7683
}
7784

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
1616
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
1717
# In Windows, build-name is used as the major, minor, and patch parts
1818
# of the product and file versions while build-number is used as the build suffix.
19-
version: 0.9.5+6
19+
version: 0.9.6+8
2020

2121

2222
environment:

0 commit comments

Comments
 (0)