-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyse_games.jl
267 lines (207 loc) · 6.61 KB
/
analyse_games.jl
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
include("chess/chess.jl")
const Analysis = Dict{String, Dict{Move,Vector{String}}}
function create_analysis(path::String)
white_analysis = Analysis()
black_analysis = Analysis()
for (root, dirs, files) in walkdir(path)
println(root)
println(last(split(root, "\\")))
for file in joinpath.(root, files)
!(occursin("black",file) || occursin("white",file)) && continue
white = occursin("white",file)
file_string = read(file, String)
println(file)
game_moves = extract_moves(file_string)
add!(white_analysis, black_analysis, game_moves, white, file)
end
end
return white_analysis, black_analysis
end
function extract_moves(s::String)::Vector{String}
moves = []
move_nr = 1
for line in split(s, '\n')
line = rstrip(line, '\r')
line_splitted = split(line, ' ')
first_group = line_splitted[1]
if occursin(string(move_nr), first_group)
popfirst!(line_splitted)
move_nr += 1
append!(moves, line_splitted)
end
end
moves
end
function add!(white_analysis::Analysis, black_analysis::Analysis, game_moves::Vector{String}, white::Bool, file_string::String)
board = StartPosition()
_white = true
for move_str in game_moves
move = short_to_long(board, _white, move_str)
fen = FEN(board, _white)
analysis = white ? white_analysis : black_analysis
if !haskey(analysis, fen)
analysis[fen] = Dict{Move, Vector{String}}()
end
board_dict = analysis[fen]
encounters = get(board_dict, move, String[])
push!(encounters, file_string)
board_dict[move] = encounters
make_move!(board, _white, move)
_white = !_white
end
end
function get_played_moves_message(fen::String, ana::Analysis, my_move::Bool)
move_dict = ana[fen]
move_vec = collect(move_dict)
sort!(move_vec, lt=(x,y) -> length(x[2]) < length(y[2]), rev=true)
out = ""
if my_move
out *= "I played:\n"
else
out *= "Opponent played:\n"
end
for (move, games) in move_vec
wins = 0
losses = 0
draws = 0
for game in games
file_string = read(game, String)
if occursin("Result: won", file_string)
wins += 1
elseif occursin("Result: lost", file_string)
losses += 1
elseif occursin("Result: draw", file_string)
draws += 1
end
end
out *= "$move, games played: $(length(games)), wins: $wins, draws: $draws, losses: $losses\n"
if length(games) < 3
for game in games
res = ""
file_string = read(game, String)
if occursin("Result: won", file_string)
res = "won"
elseif occursin("Result: lost", file_string)
res = "lost"
elseif occursin("Result: draw", file_string)
res = "draw"
end
out *= "\t$game $res\n"
end
end
end
return out
end
using Genie
import Genie.Router: route
import Genie.Renderer: respond
import JSON: json
using Printf
mutable struct AnalysisController
white_analysis::Analysis
black_analysis::Analysis
perspective::Bool
board::Board
white::Bool
fen_history::Vector{String}
end
function AnalysisController(path)
white_analysis, black_analysis = create_analysis(path)
board = StartPosition()
perspective = true
white = true
return AnalysisController(white_analysis, black_analysis, perspective, board, white, [FEN(board, white)])
end
function get_played_moves_message(ac::AnalysisController)
my_move = ac.white == ac.perspective
ana = ac.perspective ? ac.white_analysis : ac.black_analysis
try
return get_played_moves_message(FEN(ac.board, ac.white), ana, my_move)
catch e
if e isa KeyError
return "No games found!"
else
rethrow(e)
end
end
end
ac = AnalysisController("backlog/chess.com_engines")
route("/") do
@info "Reset"
global ac
# game = Game()
ac.board = StartPosition()
ac.white = true
ac.perspective = true
ac.fen_history = [FEN(ac.board, ac.white)]
serve_static_file("analysis.html")
end
route("/move") do
@info "make move"
global ac
from_str = @params(:from)
to_str = @params(:to)
promote = parse(Bool, @params(:promotion))
from = Field(from_str)
to = Field(to_str)
if from == to
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=> message)))
end
moves = get_moves(ac.board, ac.white)
filtered_moves = []
if promote
filtered_moves = filter(m -> tofield(m.from) == from && tofield(m.to) == to && m.to_piece == QUEEN, moves)
else
filtered_moves = filter(m -> tofield(m.from) == from && tofield(m.to) == to, moves)
end
if length(filtered_moves) == 1
# player move
move = filtered_moves[1]
make_move!(ac.board, ac.white, move)
ac.white = !ac.white
push!(ac.fen_history, FEN(ac.board, ac.white))
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=> message)))
else
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=>message)))
end
end
route("/flip") do
@info "flip"
global ac
white = parse(Bool, @params(:white))
ac.perspective = white
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=> message)))
end
route("/load") do
@info "load"
fen = @params(:fen)
if fen == ""
ac.board = StartPosition()
ac.white = true
else
ac.board = Board(fen)
groups = split(fen, " ")
white_to_move = groups[2] == "w"
ac.white = white_to_move
end
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=> message)))
end
route("/undo") do
@info "undo"
global ac
display(ac.fen_history)
if length(ac.fen_history) > 1
pop!(ac.fen_history)
end
ac.board = Board(ac.fen_history[end])
ac.white = !ac.white
message = get_played_moves_message(ac)
return respond(json(Dict("fen"=>FEN(ac.board, ac.white), "message"=> message)))
end
@info "Start listening at localhost:8000"
Genie.AppServer.startup(async=false)