summaryrefslogtreecommitdiff
path: root/scheduling/sql-import.cc
blob: 6f58b2f9210af46b9019be02894bcc7a4254021f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

using std::string;
using std::vector;
using std::map;
using std::cerr;
using std::cout;

vector<string> Split(const string& message, char deliminator) {
  std::istringstream stream(message);
  vector<string> result;
  string tmp;
  while (std::getline(stream, tmp, deliminator)) {
    result.emplace_back(tmp);
  }
  return result;
}

struct Player {
  Player(const string id, const int rank, const int desired_games)
      : id_(id), rank_(rank), desired_games_(desired_games) {}
  Player() = default;
  Player(const Player& other) = default;
  Player& operator=(const Player& other) = default;

  string id_;
  int rank_;
  int desired_games_;

  string DebugString() const {
    std::ostringstream output;
    output << "Player(id=" << id_ << ", rank=" << rank_
           << ", desired_games=" << desired_games_ << ")";
    return output.str();
  }

  string PrintRank() const {
    std::ostringstream output;
    output << std::abs(rank_) << (rank_ > 0 ? 'd' : 'k');
    return output.str();
  }

  string ToString() const {
    std::ostringstream output;
    output << id_ << " (" << std::abs(rank_) << (rank_ > 0 ? 'd' : 'k')
           << ")";
    return output.str();
  }
};

std::unique_ptr<Player> ParsePlayer(const vector<string>& data, int round) {
  const int kPlayerTokens = 7;
  if (data.size() < kPlayerTokens) {
    return nullptr;
  }
  string id = data[0];
  std::istringstream rank_stream(data[2]);
  int rank;
  rank_stream >> rank;
  char sign;
  rank_stream >> sign;
  if (sign != 'k' && sign != 'd') {
    return nullptr;
  }
  if (sign == 'k') {
    rank = -rank;
  }
  std::istringstream desired_games_stream(data[3 + round]);
  int desired_games;
  desired_games_stream >> desired_games;
  return std::unique_ptr<Player>(new Player(id, rank, desired_games));
}


int main() {
  const string kDate = "2015-09-01";
  map<string, Player> players;
  map<string, int> player_ids;
  int current_id = 1;
  string line;
  cerr << "Input players from sheet. End with an empty line.\n";
  while (std::getline(std::cin, line) && !line.empty()) {
    std::unique_ptr<Player> player = ParsePlayer(Split(line, '\t'), 1);
    if (player) {
      cerr << player->DebugString() << "\n";
      players[player->id_] = *player;
      player_ids[player->id_] = current_id++;
    } else {
      cerr << "Failed to parse player: " << line;
    }
  }

  cout << "insert into players (Pin_Player,Name,Rating) values ";
  bool first = true;
  for (const auto& e : players) {
    const Player& player = e.second;
    int id = player_ids[player.id_];
    if (first) {
      first = false;
    } else {
      cout << ",";
    }
    cout << "(" << id << ",\"" << player.id_ << "\"," << player.rank_ << ")";
  }
  cout << ";\n";

  cerr << "Input tournament code:\n";
  std::getline(std::cin, line);
  string tournament_code = line;
  cerr << "Tournament " << tournament_code << "\n";

  cerr << "Input results from sheet. End with an empty line.\n";
  cout << "insert into games (Tournament_Code,Game_Date,Pin_Player_1,"
       << "Color_1,Rank_1,Pin_Player_2,Color_2,Rank_2,Handicap,Komi,Result) values";
  first = true;
  while (std::getline(std::cin, line) && !line.empty()) {
    vector<string> data = Split(line, '\t');
    if (data.size() < 5) {
      cerr << "Error parsing result: " << line << "\n";
      continue;
    }
    string p1 = data[0];
    string p2 = data[1];
    std::istringstream handicap_stream(data[2]);
    char dropper;
    handicap_stream >> dropper;
    int handicap;
    handicap_stream >> handicap;
    int komi;
    std::istringstream komi_stream(data[3]);
    komi_stream >> komi;
    char result = data[4][0];
    if (result != 'W' && result != 'B') {
      cerr << "Error parsing result: " << line << "\n";
      continue;
    }
    if (first) {
      first = false;
    } else {
      cout << ",";
    }
    cout << "(";
    cout << "\"" << tournament_code << "\","
         << "\"" << kDate << "\","
         << player_ids[p1] << ","
         << "'W',"
         << "\"" << players[p1].PrintRank() << "\","
         << player_ids[p2] << ","
         << "'B',"
         << "\"" << players[p2].PrintRank() << "\","
         << handicap << ","
         << komi << ","
         << "\"" << result << "\"";
    cout << ")";
  }
  cout << ";";
}