overfast_api/lib/maps/maps.dart
2024-05-07 00:14:45 +02:00

93 lines
2.1 KiB
Dart

import 'dart:convert';
import 'package:overfast_api/client.dart';
import 'package:overfast_api/utils/utils.dart';
enum Gamemode {
assault,
captureTheFlag,
control,
deathmatch,
elimination,
escort,
hybrid,
push,
teamDeathmatch,
flashpoint,
clash;
@override
String toString() => toKebabCase(name);
static Gamemode fromString(String value) {
return Gamemode.values.firstWhere((e) => e.toString() == value);
}
}
final class GameMap {
final String name;
final List<Gamemode> gamemodes;
final String screenshot;
final String location;
final String? countryCode;
const GameMap({
required this.name,
required this.gamemodes,
required this.screenshot,
required this.location,
this.countryCode,
});
// Stub empty constructor
GameMap.empty() : this(name: '', gamemodes: [], screenshot: '', location: '');
factory GameMap.fromJson(Map<String, dynamic> json) {
return GameMap(
name: json['name'],
gamemodes: (json['gamemodes'] as List)
.map((e) => Gamemode.fromString(e))
.cast<Gamemode>()
.toList(),
screenshot: json['screenshot'],
location: json['location'],
countryCode: json['countryCode'],
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
'gamemode': gamemodes.map((e) => e.toString()).toList(),
'screenshot': screenshot,
'location': location,
'countryCode': countryCode,
};
}
}
final class Maps {
final Overfast _client;
Maps(this._client);
Future<List<GameMap>> maps({Gamemode? gameMode}) => _client.getList<GameMap>(
'/maps',
gameMode != null ? {'gamemode': gameMode.toString()} : {},
);
Future<List<Gamemode>> gamemodes() async {
// Special case, manual request
final response = await _client.client.get(
_client.baseUri.replace(path: '/maps/gamemodes'),
);
throwIfNecessary(response.statusCode, response.body);
return (json.decode(response.body) as List)
.map((e) => Gamemode.fromString(e))
.cast<Gamemode>()
.toList();
}
}