Advertisement
Guest User

Off-Season International Round 1 Matching algorithm

a guest
Nov 20th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.79 KB | None | 0 0
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'package:csv/csv.dart';
  4. import 'dart:convert';
  5.  
  6. class TeamObject {
  7.   TeamObject(
  8.       {this.teamName,
  9.       this.coachName,
  10.       this.race,
  11.       this.redditName,
  12.       this.region,
  13.       this.timezone,
  14.       this.previousDiv});
  15.  
  16.   final String teamName;
  17.   final String coachName;
  18.   final String race;
  19.   final String timezone;
  20.   final String redditName;
  21.   final String region;
  22.   final String previousDiv;
  23.  
  24.   List<String> produceCSVSequence() {
  25.     return [redditName, coachName, teamName, timezone, region];
  26.   }
  27.  
  28.   bool isSingleSeasonTeam() {
  29.     return previousDiv.contains("5") || previousDiv.isEmpty;
  30.   }
  31. }
  32.  
  33. List<List<String>> randomMatchesForTeams(List<TeamObject> teams) {
  34.   var matches = List<List<String>>();
  35.   var rng = Random();
  36.   while (teams.length > 1) {
  37.     var firstTeam = teams.removeAt(rng.nextInt(teams.length));
  38.     var secondTeam = teams.removeAt(rng.nextInt(teams.length));
  39.     print(
  40.         "Match $matchNumber : ${firstTeam.teamName} vs ${secondTeam.teamName} | ${firstTeam.race} vs ${secondTeam.race}"
  41.     );
  42.  
  43.     matches.add(["OSI"] +
  44.         ["${matchNumber++}"] +
  45.         firstTeam.produceCSVSequence() +
  46.         ["VS"] +
  47.         secondTeam.produceCSVSequence().reversed.toList());
  48.     sleep(Duration(seconds: 1));
  49.   }
  50.  
  51.   if (teams.length == 1) {
  52.     print(
  53.         "One team left remaining ${teams[0].teamName} coached by ${teams[0].coachName}");
  54.   }
  55.  
  56.   return matches;
  57. }
  58.  
  59. int matchNumber = 1;
  60.  
  61. main(List<String> arguments) async {
  62.   if (arguments.length != 2) {
  63.     print("Usage: osi_matchmaker coachlist.csv matchups.csv");
  64.     return;
  65.   }
  66.  
  67.   var fileName = arguments[0];
  68.   final input = File(fileName).openRead();
  69.  
  70.   var teams = await input
  71.       .transform(utf8.decoder)
  72.       .transform(CsvToListConverter(eol: "\n"))
  73.       .where((value) => value[0] != "team name")
  74.       .toList()
  75.       .then((values) {
  76.     return values.map((value) {
  77.       return TeamObject(
  78.         teamName: value[0],
  79.         coachName: value[5],
  80.         race: value[1],
  81.         timezone: value[2],
  82.         redditName: value[3],
  83.         region: value[7],
  84.         previousDiv: value[8],
  85.       );
  86.     }).toList();
  87.   });
  88.  
  89.   var singleSeasonTeams =
  90.       teams.where((team) => team.isSingleSeasonTeam()).toList();
  91.  
  92.   var multiSeasonTeams =
  93.       teams.where((team) => !team.isSingleSeasonTeam()).toList();
  94.  
  95.   print("Single Season Team Match ups");
  96.   var allMatches = randomMatchesForTeams(singleSeasonTeams);
  97.  
  98.   print("Multiseason team match ups");
  99.   allMatches += randomMatchesForTeams(multiSeasonTeams);
  100.  
  101.   var matchesCsv = ListToCsvConverter().convert(allMatches);
  102.   var newFileName = arguments[1];
  103.   var output = File(newFileName).openWrite();
  104.   output.write(matchesCsv);
  105.   await output.close();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement