Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'dart:math';
- import 'package:csv/csv.dart';
- import 'dart:convert';
- class TeamObject {
- TeamObject(
- {this.teamName,
- this.coachName,
- this.race,
- this.redditName,
- this.region,
- this.timezone,
- this.previousDiv});
- final String teamName;
- final String coachName;
- final String race;
- final String timezone;
- final String redditName;
- final String region;
- final String previousDiv;
- List<String> produceCSVSequence() {
- return [redditName, coachName, teamName, timezone, region];
- }
- bool isSingleSeasonTeam() {
- return previousDiv.contains("5") || previousDiv.isEmpty;
- }
- }
- List<List<String>> randomMatchesForTeams(List<TeamObject> teams) {
- var matches = List<List<String>>();
- var rng = Random();
- while (teams.length > 1) {
- var firstTeam = teams.removeAt(rng.nextInt(teams.length));
- var secondTeam = teams.removeAt(rng.nextInt(teams.length));
- print(
- "Match $matchNumber : ${firstTeam.teamName} vs ${secondTeam.teamName} | ${firstTeam.race} vs ${secondTeam.race}"
- );
- matches.add(["OSI"] +
- ["${matchNumber++}"] +
- firstTeam.produceCSVSequence() +
- ["VS"] +
- secondTeam.produceCSVSequence().reversed.toList());
- sleep(Duration(seconds: 1));
- }
- if (teams.length == 1) {
- print(
- "One team left remaining ${teams[0].teamName} coached by ${teams[0].coachName}");
- }
- return matches;
- }
- int matchNumber = 1;
- main(List<String> arguments) async {
- if (arguments.length != 2) {
- print("Usage: osi_matchmaker coachlist.csv matchups.csv");
- return;
- }
- var fileName = arguments[0];
- final input = File(fileName).openRead();
- var teams = await input
- .transform(utf8.decoder)
- .transform(CsvToListConverter(eol: "\n"))
- .where((value) => value[0] != "team name")
- .toList()
- .then((values) {
- return values.map((value) {
- return TeamObject(
- teamName: value[0],
- coachName: value[5],
- race: value[1],
- timezone: value[2],
- redditName: value[3],
- region: value[7],
- previousDiv: value[8],
- );
- }).toList();
- });
- var singleSeasonTeams =
- teams.where((team) => team.isSingleSeasonTeam()).toList();
- var multiSeasonTeams =
- teams.where((team) => !team.isSingleSeasonTeam()).toList();
- print("Single Season Team Match ups");
- var allMatches = randomMatchesForTeams(singleSeasonTeams);
- print("Multiseason team match ups");
- allMatches += randomMatchesForTeams(multiSeasonTeams);
- var matchesCsv = ListToCsvConverter().convert(allMatches);
- var newFileName = arguments[1];
- var output = File(newFileName).openWrite();
- output.write(matchesCsv);
- await output.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement