-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessData.java
More file actions
153 lines (131 loc) · 5.24 KB
/
ChessData.java
File metadata and controls
153 lines (131 loc) · 5.24 KB
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
package chess;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import ChessLib.*;
import ChessLib.game.*;
import ChessLib.move.*;
import ChessLib.pgn.*;
import ChessLib.util.*;
public class ChessData {
private static PgnHolder pgn;
private static int i = 0, prog = 0;
private static Date t_proc;
private static Connection c;
private static Statement s;
private static String q;
private static String game_batch, fen_batch;
private static int fen_batch_size;
public static void main(String[] args) throws Exception {
// Open file for read
Date t_start = new Date();
System.out.println(new SimpleDateFormat("MMM dd hh:mm:ssa").format(t_start) + " | Reading data into array...");
pgn = new PgnHolder("C:\\Users\\adamc\\eclipse-workspace\\ChessDataMigration\\data\\lichess_db_standard_rated_2014-01.pgn");
pgn.loadPgn();
t_proc = new Date();
// Store unique games and unique game states
System.out.println(new SimpleDateFormat("MMM dd hh:mm:ssa").format(new Date()) + " | Storing games and unique FEN states...");
c = new ConnectToDB().EstablishConnection(); //Establish connection to the DB
//s = c.createStatement();
game_batch = "";
fen_batch = "";
fen_batch_size = 0;
for (Game game: pgn.getGames()) {
i++;
InsertGameAndFen(game);
}
// pgn.getGames().stream().forEach(sellerNames -> {
// Stream<Game> stream = StreamSupport.stream(pgn.getGames().spliterator(), true); // true means use parallel stream
// stream.forEach(game -> {
// i++;
// InsertGameAndFen(game);
// });
// });
// Clean up
System.out.println(new SimpleDateFormat("MMM dd hh:mm:ssa").format(new Date()) + " | Done!");
s.close();
c.close();
pgn.cleanUp();
// Calculate time elapsed
long diff = new Date().getTime() - t_start.getTime();
long diffSeconds = diff / 1000 % 60, diffMinutes = diff / (60 * 1000) % 60, diffHours = diff / (60 * 60 * 1000) % 24;
String strSec = String.valueOf(diffSeconds), strMin = String.valueOf(diffMinutes), strHr = String.valueOf(diffHours);
if (diffSeconds < 10) strSec = "0" + diffSeconds;
if (diffMinutes < 10) strMin = "0" + diffMinutes;
if (diffHours < 10) strHr = "0" + diffHours;
System.out.println(new SimpleDateFormat("MMM dd hh:mm:ssa").format(new Date()) + " | Time elapsed: " + strHr + ":" + strMin + ":" + strSec);
}
private static String sqlQuote(String str) {
return "'" + str + "'";
}
private static void InsertGameAndFen(Game game) throws Exception {
MoveList moves; //List of moves from game
Board board; //Game board for move iteration
if ((i * 100) / pgn.getGames().size() < 74) //Skip first given %
return;
// Check progress, get percentage processed
if ((i * 100) / pgn.getGames().size() > prog) {
prog = (i * 100) / pgn.getGames().size();
System.out.println(new SimpleDateFormat("MMM dd hh:mm:ssa").format(new Date()) + " | " + prog + "%");
}
InsertGameBatch(game); //Store game
// Store unique fen states
game.loadMoveText();
moves = game.getHalfMoves();
board = new Board();
for (Move move: moves) {
board.doMove(move);
InsertFenBatch(board);
}
}
private static void InsertGameBatch(Game game) throws SQLException {
int outcome; //0 = stalemate, 1 = white wins, 2 = black wins
// Determine outcome from PGN result string
outcome = -1;
if (game.getResult().getDescription() == "1/2-1/2") { //Stalemate
outcome = 0;
} else if (game.getResult().getDescription() == "1-0") { //White wins
outcome = 1;
} else if (game.getResult().getDescription() == "0-1") { //Black wins
outcome = 2;
}
// Add game to SQL batch string
if (game_batch != "") game_batch += ",";
game_batch += "(" + sqlQuote(game.getRound().getEvent().getSite()) + "," + outcome + "," + game.getWhitePlayer().getElo() + "," + game.getBlackPlayer().getElo() + ")";
// Insert game if batch has 10000 games, or we are at the end of processing
if (i % 10000 == 0 || i == pgn.getGames().size()) {
q = " INSERT INTO Games (site, outcome, elo_white, elo_black)";
q += " VALUES " + game_batch;
q += " ON DUPLICATE KEY UPDATE site=site";
//s.execute(q);
game_batch = "";
InsertRunnable r = new InsertRunnable(q);
Thread t = new Thread(r);
t.start();
}
}
private static void InsertFenBatch(Board board) throws SQLException {
// Get fen string, cut off everything past castling
String[] fenSplit = board.getFen().split(" ");
String fen = fenSplit[0] + " " + fenSplit[1] + " " + fenSplit[2];
// Add fen to SQL batch string
if (fen_batch != "") fen_batch += ",";
fen_batch += "(" + sqlQuote(fen) + ")";
fen_batch_size += 1;
// Insert fen if batch has 10000 fens, or we are at the end of processing
if (fen_batch_size >= 20000 || i == pgn.getGames().size()) {
q = " INSERT INTO Fen_States (fen)";
q += " VALUES " + fen_batch;
q += " ON DUPLICATE KEY UPDATE fen=fen";
//s.execute(q);
fen_batch = "";
fen_batch_size = 0;
InsertRunnable r = new InsertRunnable(q);
Thread t = new Thread(r);
t.start();
}
}