2019-07-10 14:16:13 +02:00
|
|
|
import 'dart:collection';
|
2019-07-06 00:44:20 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2019-07-06 00:44:20 +02:00
|
|
|
import 'package:http/http.dart' as http;
|
2019-07-16 23:37:18 +02:00
|
|
|
import 'package:preferences/preferences.dart';
|
2019-07-25 13:37:31 +02:00
|
|
|
import 'package:localstorage/localstorage.dart';
|
2019-07-06 00:44:20 +02:00
|
|
|
import 'package:yaml/yaml.dart';
|
2019-07-25 13:37:31 +02:00
|
|
|
import 'package:toolheim/data/warband_roster.dart';
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
class GitHubAdapter extends ChangeNotifier {
|
2019-07-25 13:37:31 +02:00
|
|
|
final LocalStorage storage = new LocalStorage('rosters');
|
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
List<String> _syncErrors = new List<String>();
|
|
|
|
DateTime _lastSync;
|
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
List<WarbandRoster> _rosters = [];
|
2019-07-10 22:28:51 +02:00
|
|
|
String _activePlayerName;
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
String get repository => PrefService.getString('repository');
|
|
|
|
String get path => PrefService.getString('path');
|
|
|
|
|
|
|
|
bool _syncinProgress = false;
|
|
|
|
bool get isSyncInProgress => _syncinProgress;
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
DateTime get lastSync => _lastSync;
|
2019-07-16 23:37:18 +02:00
|
|
|
List<String> get syncErrors => _syncErrors;
|
2019-07-10 14:16:13 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
UnmodifiableListView<WarbandRoster> get rosters =>
|
|
|
|
UnmodifiableListView(_rosters);
|
2019-07-12 00:30:57 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
WarbandRoster activeRoster() {
|
|
|
|
if (_activePlayerName == null || _rosters.length == 0) {
|
2019-07-12 00:30:57 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
return _rosters.firstWhere((roster) {
|
|
|
|
return roster.playerName == _activePlayerName;
|
|
|
|
}, orElse: () => null);
|
2019-07-12 00:30:57 +02:00
|
|
|
}
|
2019-07-10 14:16:13 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
void changeActiveRoster(String playerName) {
|
2019-07-10 14:16:13 +02:00
|
|
|
_activePlayerName = playerName;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
// TODO: Add persistence layer here
|
2019-07-06 00:44:20 +02:00
|
|
|
|
|
|
|
/// Search for warband files in the GitHub repository
|
|
|
|
///
|
|
|
|
/// This method will search for matching files and check their content in a
|
|
|
|
/// subfolder (see fields [repository] and [path]). If the file
|
|
|
|
/// contain errors or can't read, a sync error message will be written into
|
|
|
|
/// the [syncErrors] list.
|
2019-07-10 14:16:13 +02:00
|
|
|
void search() async {
|
|
|
|
_syncErrors.clear();
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncinProgress = true;
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
Stream<Map<String, String>> rosterStream() async* {
|
2019-07-06 00:44:20 +02:00
|
|
|
// Get all files which could be potential warband files (end with
|
|
|
|
// mordheim.yml and contain the word "heros").
|
|
|
|
http.Response response = await http.get(
|
|
|
|
"https://api.github.com/search/code?q=heros+repo:" +
|
2019-07-16 23:37:18 +02:00
|
|
|
repository +
|
2019-07-06 00:44:20 +02:00
|
|
|
"+filename:mordheim.yml+path:\"" +
|
2019-07-16 23:37:18 +02:00
|
|
|
path +
|
2019-07-06 00:44:20 +02:00
|
|
|
"\"");
|
|
|
|
|
2019-07-07 22:31:06 +02:00
|
|
|
// GitHub is not reachable
|
|
|
|
if (response.statusCode != 200) {
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncErrors.add('Could not find any warband roster files.');
|
2019-07-07 22:31:06 +02:00
|
|
|
yield {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No valid response from GitHub
|
|
|
|
dynamic searchResults;
|
|
|
|
try {
|
|
|
|
searchResults = jsonDecode(response.body);
|
|
|
|
} on FormatException catch (e) {
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncErrors.add('Could not parse GitHub response. ' + e.toString());
|
2019-07-07 22:31:06 +02:00
|
|
|
yield {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find suitable files for examination
|
|
|
|
RegExp fileRegex = new RegExp(r"[a-zA-Z]+\.mordheim\.ya?ml");
|
|
|
|
for (dynamic searchResult in searchResults['items']) {
|
2019-07-06 00:44:20 +02:00
|
|
|
if (fileRegex.hasMatch(searchResult['name'])) {
|
2019-07-07 22:31:06 +02:00
|
|
|
// We try to get the name of the player from the name of the folder
|
|
|
|
// in which the file resists
|
2019-07-06 00:44:20 +02:00
|
|
|
String completePath = searchResult['path'];
|
2019-07-07 22:31:06 +02:00
|
|
|
List<String> pathParts =
|
2019-07-16 23:37:18 +02:00
|
|
|
completePath.substring(path.length + 1).split('/');
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-07 22:31:06 +02:00
|
|
|
String playerName;
|
|
|
|
if (pathParts.length >= 2) {
|
|
|
|
playerName = pathParts.first;
|
2019-07-06 00:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch last change and some metainformation of the file
|
|
|
|
http.Response response = await http.get(
|
|
|
|
"https://api.github.com/repos/" +
|
2019-07-16 23:37:18 +02:00
|
|
|
repository +
|
2019-07-06 00:44:20 +02:00
|
|
|
"/commits?path=" +
|
|
|
|
completePath);
|
|
|
|
|
2019-07-07 22:31:06 +02:00
|
|
|
if (response.statusCode != 200) {
|
2019-07-10 14:16:13 +02:00
|
|
|
_syncErrors.add('Could not load the warband metadata from GitHub.');
|
2019-07-07 22:31:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-07 22:31:06 +02:00
|
|
|
// No valid response from GitHub
|
|
|
|
dynamic commits;
|
|
|
|
try {
|
|
|
|
commits = jsonDecode(response.body);
|
|
|
|
} on FormatException catch (e) {
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncErrors.add('Could not parse GitHub response. ' + e.toString());
|
2019-07-07 22:31:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No commits available
|
|
|
|
if (commits.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic latestCommit = commits.first;
|
2019-07-06 00:44:20 +02:00
|
|
|
yield {
|
|
|
|
'filePath': completePath.toString(),
|
2019-07-07 22:31:06 +02:00
|
|
|
'shaHash': latestCommit['sha'],
|
2019-07-06 00:44:20 +02:00
|
|
|
'player': playerName.toString(),
|
2019-07-07 22:31:06 +02:00
|
|
|
'author': latestCommit['commit']['author']['name'],
|
|
|
|
'date': latestCommit['commit']['committer']['date'],
|
|
|
|
'message': latestCommit['commit']['message']
|
2019-07-06 00:44:20 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
_rosters.clear();
|
2019-07-10 14:16:13 +02:00
|
|
|
notifyListeners();
|
2019-07-16 23:37:18 +02:00
|
|
|
|
|
|
|
if (_syncErrors.length == 0) {
|
|
|
|
await for (Map<String, String> player in rosterStream()) {
|
|
|
|
http.Response response;
|
|
|
|
try {
|
|
|
|
response = await http.get("https://raw.githubusercontent.com/" +
|
|
|
|
repository +
|
2019-07-06 00:44:20 +02:00
|
|
|
'/master/' +
|
|
|
|
player['filePath']);
|
2019-07-16 23:37:18 +02:00
|
|
|
} catch (e) {
|
|
|
|
// TODO: Ignore this error, we catch it elsewhere.
|
2019-07-07 22:31:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
try {
|
|
|
|
if (response != null) {
|
|
|
|
YamlMap yamlObject = loadYaml(response.body);
|
|
|
|
WarbandRoster roster = WarbandRoster.fromJson(yamlObject);
|
|
|
|
if (player['player'] != '') {
|
|
|
|
roster.playerName = player['player'];
|
|
|
|
}
|
|
|
|
|
|
|
|
roster.currentVersion = new Version(player['shaHash'],
|
|
|
|
player['date'], player['author'], player['message']);
|
2019-07-07 22:31:06 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
// On a search, we drop all previous information about the warbands,
|
|
|
|
// Sp, lastSyncVersion is equal to the currentVersion.
|
|
|
|
roster.lastSyncVersion = roster.currentVersion;
|
2019-07-06 00:44:20 +02:00
|
|
|
|
2019-07-16 23:37:18 +02:00
|
|
|
_rosters.add(roster);
|
2019-07-25 13:37:31 +02:00
|
|
|
//https://github.com/lesnitsky/flutter_localstorage/blob/master/example/lib/main.dart
|
2019-07-25 16:44:51 +02:00
|
|
|
// FIXME: store it correctly
|
|
|
|
//storage.setItem(player['player'], roster);
|
2019-07-16 23:37:18 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_syncErrors.add(e.toString());
|
|
|
|
}
|
2019-07-06 00:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 22:28:51 +02:00
|
|
|
// Sort by CP
|
2019-07-16 23:37:18 +02:00
|
|
|
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
|
2019-07-10 22:28:51 +02:00
|
|
|
|
|
|
|
// Select first as active player if no active player is selected
|
2019-07-16 23:37:18 +02:00
|
|
|
if (_rosters.length > 0) {
|
|
|
|
_activePlayerName = _rosters.first.playerName;
|
|
|
|
}
|
2019-07-10 22:28:51 +02:00
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
_lastSync = DateTime.now();
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncinProgress = false;
|
2019-07-25 16:44:51 +02:00
|
|
|
storage.setItem('lastSync', _lastSync.toIso8601String());
|
2019-07-10 14:16:13 +02:00
|
|
|
notifyListeners();
|
2019-07-06 00:44:20 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
void update() async {
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncinProgress = true;
|
2019-07-06 00:44:20 +02:00
|
|
|
// TODO: Search for warband yml files
|
|
|
|
// TODO: Check if it is in the right format
|
|
|
|
// TODO: Store it into the database if valid
|
|
|
|
|
2019-07-10 14:16:13 +02:00
|
|
|
_lastSync = DateTime.now();
|
2019-07-16 23:37:18 +02:00
|
|
|
_syncinProgress = false;
|
2019-07-10 14:16:13 +02:00
|
|
|
notifyListeners();
|
2019-07-06 00:44:20 +02:00
|
|
|
}
|
|
|
|
}
|