std::vector<int> Schedule::create_pooled_eqdistrib_sched(std::vector<Entity> plars, int minimum_turns){ std::vector<int> agilities; for (Entity x : plars){ agilities.insert(agilities.begin(), x.agility.get()); } int period = std::accumulate(agilities.begin(), agilities.end(), 1/*initial value*/, nf::least_common_multiple); int multiplier = 1; while (period < minimum_turns) period *= ++multiplier; //BOOM ONE-LINER AREN'T I COOL!!! //The period length is now set. //We need to sort plars before we go any further. //LAMBDAS! AREN'T I COOL? should sort in ascending order.... std::sort(plars.begin(), plars.end(), [](Entity a, Entity b){ return b.agility.get() < a.agility.get(); }); std::cout << "Sorted plarlist: " << std::endl; for (Entity x : plars) std::cout << x.getName(); std::cout << std::endl; std::vector<int> turns_to_assign(period); //Period-length std::vector for the schedule... we'll assign from here. for (int i = 0; i < period; ++i) //Isn't there an easier way to do this? turns_to_assign[i] = i; schedule_t newSched(period); //Blank schedule to fill. int i = 0; while (!turns_to_assign.empty()){ int nextID = i; int nextAgi = plars[i].agility.get(); int delta = (int)(pow((((float)nextAgi / (float)period)), -1)); unsigned int nextAssignedTurn = 0; //Index of turns_to_assign while (nextAssignedTurn < turns_to_assign.size()){ newSched[turns_to_assign[nextAssignedTurn]] = nextID; turns_to_assign.erase(turns_to_assign.begin() + nextAssignedTurn); nextAssignedTurn += delta - 1; } i = (i + 1) % plars.size(); } return newSched; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTextStream out(stdout); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("thunderbowl.sqlite"); if (!db.open()) { QStringList r = QSqlDatabase::drivers(); QString drivers = r.join(", "); return 1; } out << "ThunderSchedule version 0.3" << endl; // v0.1 inital design with hand coded teams // v0.2 accepts files, and arguments in the form of [season] [games] [league name] // v0.3 creates schedule from an html dump fron the obblm database // -- go to team standings, change tournament to be the selected one, right click save as website, html only // -- pass that file name as the first argument, second argument is number of games QStringList arguments = a.arguments(); if (arguments.count() == 3){ // this section reads an html file and extracts all the teams QFile file(QString(arguments.at(1))); if (!file.open(QIODevice::ReadOnly)) { //("Unable to open file %s, aborting\n", file.fileName().toStdString().c_str()); return 1; } if (!file.isReadable()) { //("Unable to read file %s, aborting\n", file.fileName().toStdString().c_str()); return false; } // B-League Schedule int games = arguments.at(2).toInt(); // QString baseFileName = "D://project//thunder_noui//output//season19//1//"; // QString filename = baseFileName + "thundersched_19_B.txt"; QString filename = arguments.at(1) + ".out"; schedule newSched(filename, games); newSched = schedule(filename, games); QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); // QRegularExpression team_html(".*<td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=2\\&obj_id=(\\d+)'>([\\w-'\\s]+)</a></td><td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=\\d\\&obj_id=(\\d+)'>([\\w-'\\s]+)</a></td>.*<td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=\\d\\&obj_id=(\\d+)'>([\\w-'\\s]+)</a></td>.*$"); QRegularExpression team_html(".*<td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=2\\&obj_id=(\\d+)'>([\\x20-\\xFF]+)</a></td><td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=\\d\\&obj_id=(\\d+)'>([\\x20-\\xFF]+)</a></td>.*<td><a\\shref='index\\.php\\?section=objhandler\\&type=1\\&obj=\\d\\&obj_id=(\\d+)'>([\\x20-\\xFF]+)</a></td>.*$"); QRegularExpressionMatch match = team_html.match(line); if(match.hasMatch()){ QString team_id = match.captured(1); QString name = match.captured(2); QString coach_id = match.captured(3); QString coach = match.captured(4); QString race = match.captured(6); int race_id = getMyTeam(race); if (race_id < 0) { out << QString("Race not found:") << race << " for " << name << endl; } team* add_team = new team( race_id , name , coach , coach_id , team_id ); newSched.addTeam( add_team ); } } int num_teams = newSched.numberTeams(); out << QString("*************") << endl; out << QString("Schedule for ")+QString::number(games)+" games, "+QString::number(num_teams)+" teams." << endl; newSched.generate(); } if (arguments.count() < 4){ return 1; } // import teams int games = arguments.at(2).toInt(); QString file_base = "D:\\project\\thunder_noui\\files\\"; QString in_file = file_base+arguments.at(3)+"teams.txt"; QString out_file = file_base+arguments.at(3)+ arguments.at(1) + ".txt"; schedule newSched(out_file, games); int num_teams = 0; QFile file(in_file); if (file.open(QIODevice::ReadOnly)){ QTextStream in(&file); while (!in.atEnd()){ QString line = in.readLine(); QRegExp comment_RexExp("^\\s*//.*"); QRegExp team_RexExp("^\\s*([\\w]+)\\s*,\\s*(.*)\\s*,\\s*(.*)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*$"); if (comment_RexExp.exactMatch(line)){ continue;} if (team_RexExp.exactMatch(line)){ QStringList team_values = team_RexExp.capturedTexts(); team* add_team = new team( getMyTeam(team_values.at(1)) , team_values.at(2) , team_values.at(3) , team_values.at(4) , team_values.at(5) ); if (getMyTeam(team_values.at(1)) < 0) { out << QString("Race not found:") << team_values.at(1) << " for " << team_values.at(2) << endl; } newSched.addTeam( add_team ); } } file.close(); } num_teams = newSched.numberTeams(); out << QString("*************") << endl; out << QString("Schedule for ")+QString::number(games)+" games, "+QString::number(num_teams)+" teams." << endl; newSched.generate(); return 0; }