-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathogstosgf.py
More file actions
343 lines (305 loc) · 11.7 KB
/
Copy pathogstosgf.py
File metadata and controls
343 lines (305 loc) · 11.7 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import argparse
import datetime
import json
import logging
import math
import os
import re
import sys
class SgfException(Exception):
pass
failure=False
def warn(message):
global failure
failure = True
#logging.warning(message)
raise SgfException(message)
def get(ogsdata, field, default=None, logifnone=False, game_id=None):
if field in ogsdata:
return ogsdata[field]
if logifnone:
game_id = game_id or ogsdata.get("game_id") or "Unknown"
warn(f"JSON field not found for game {game_id}: {field}")
return default
def get_sgf(game_id, sgf, field, default=None, logifnone=False):
m = re.search(field + r'''\\[([^][]+)\\]''', sgf)
if m:
assert m.group(1)
return m.group(1)
if logifnone:
warn(f"SGF field not found for game {game_id or 'Unknown'}: {field}")
return default
def rankstr(rank):
# glicko -> kyudan
# if rank <= 0.0:
# rank = 1e-30
# rank = math.log(rank/850.0)/0.032
if rank >= 30.0:
danrank = min(9,int(math.floor(rank) - 29))
return f"{danrank}d"
else:
kyurank = min(30,int(30-math.floor(rank)))
return f"{kyurank}k"
sgfescapetable = str.maketrans({"]":"\\]", "\\": "\\\\", "\0": "", "[": "\\["})
def sgfescape(s):
return s.translate(sgfescapetable)
sgfletters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def param(key, contents):
contents = sgfescape(str(contents))
return f"{key}[{contents}]"
def construct_sgf(ogsdata):
global failure
failure = False
out = ""
out += "(;FF[4]CA[UTF-8]GM[1]US[lightvector/ogstosgf.py]"
extra_info = []
game_id = get(ogsdata,"game_id",logifnone=True)
if game_id is not None:
out += param("PC",f"OGS: https://online-go.com/game/{game_id}")
if game_id is None:
game_id = "Unknown"
time = get(ogsdata,"start_time")
if time is not None:
date = datetime.datetime.utcfromtimestamp(time)
strdate = date.strftime('%Y-%m-%d')
out += param("DT",strdate)
players = get(ogsdata,"players",logifnone=True)
black_username = None
white_username = None
if players is not None:
black = get(players,"black",logifnone=True)
white = get(players,"white",logifnone=True)
if black is not None and len(black) > 0:
black_username = get(black,"username",logifnone=True,game_id=game_id)
if black_username is not None:
out += param("PB",black_username)
if white is not None and len(white) > 0:
white_username = get(white,"username",logifnone=True,game_id=game_id)
if white_username is not None:
out += param("PW",white_username)
if black is not None and len(black) > 0:
black_rank = get(black,"rank",logifnone=False,game_id=game_id)
if black_rank is not None:
out += param("BR",rankstr(black_rank))
if white is not None and len(white) > 0:
white_rank = get(white,"rank",logifnone=False,game_id=game_id)
if white_rank is not None:
out += param("WR",rankstr(white_rank))
original_sgf = get(ogsdata,"original_sgf")
if original_sgf is not None:
if original_sgf == 0:
warn("Invalid uploaded SGF")
strdate = get_sgf(game_id, original_sgf, "DT")
if strdate is not None:
try:
date = datetime.datetime.strptime(strdate, "%Y-%M-%d")
except ValueError:
pass
black_username = get_sgf(game_id, original_sgf, "PB") or black_username
white_username = get_sgf(game_id, original_sgf, "PW") or white_username
white_player_id = get(ogsdata,"white_player_id")
black_player_id = get(ogsdata,"black_player_id")
return True, ([black_username, black_player_id], [white_username, white_player_id], date, game_id), original_sgf
game_name = get(ogsdata,"game_name")
if game_name is not None:
out += param("GN",game_name)
time_control = get(ogsdata,"time_control")
if time_control is not None:
system = get(time_control,"time_control")
if system is None:
system = get(time_control,"system")
if system is None:
system = get(time_control,"time_control",logifnone=True)
if system == "byoyomi":
main_time = get(time_control,"main_time",logifnone=True)
period_time = get(time_control,"period_time",logifnone=True)
periods = get(time_control,"periods",logifnone=True)
if main_time is not None and period_time is not None and periods is not None:
out += param("TM",main_time)
out += param("OT",f"{periods}x{period_time} byo-yomi")
elif system == "fischer":
initial_time = get(time_control,"initial_time",logifnone=True)
time_increment = get(time_control,"time_increment",logifnone=True)
if initial_time is not None and time_increment is not None:
out += param("TM",initial_time)
out += param("OT",f"{time_increment} fischer")
elif system == "simple":
per_move = get(time_control,"per_move",logifnone=True)
if per_move is not None:
out += param("TM",0)
out += param("OT",f"{per_move} simple")
elif system == "canadian":
main_time = get(time_control,"main_time",logifnone=True)
period_time = get(time_control,"period_time",logifnone=True)
stones_per_period = get(time_control,"stones_per_period",logifnone=True)
if main_time is not None and period_time is not None and stones_per_period is not None:
out += param("TM",main_time)
out += param("OT",f"{stones_per_period}/{period_time} canadian")
elif system == "absolute":
total_time = get(time_control,"total_time",logifnone=True)
if total_time is not None:
out += param("TM",total_time)
elif system == "none":
pass
else:
warn(f"Unknown time control for game {game_id}: {system}")
speed = get(time_control,"speed")
if speed is not None:
extra_info.append(speed)
elif system == "none":
extra_info.append("none")
else:
extra_info.append("unknown")
winner = get(ogsdata,"winner")
outcome = get(ogsdata,"outcome")
white_player_id = get(ogsdata,"white_player_id",logifnone=True)
black_player_id = get(ogsdata,"black_player_id",logifnone=True)
if winner is not None or outcome is not None:
if outcome == "0 points":
out += param("RE","Draw")
elif outcome and not winner:
out += param("RE","Void")
else:
assert outcome
assert winner
if winner == white_player_id:
winner = "W"
elif winner == black_player_id:
winner = "B"
else:
warn(f"Unknown winner '{winner}' for game {game_id}")
winner = None
if winner is not None:
if outcome.endswith(" points"):
out += param("RE",f"{winner}+{outcome[:-7]}")
elif outcome == "1 point":
out += param("RE",f"{winner}+1")
elif outcome == "Resignation":
out += param("RE",f"{winner}+R")
elif outcome == "Timeout":
out += param("RE",f"{winner}+T")
elif outcome in ("Cancellation", "Disconnection", "Game not started"):
out += param("RE",f"{winner}+F")
elif outcome in ("Moderator Decision", "Decision", "Disqualification","Ladder Withdrawn", "TD Decision", "Player's account was removed", "Opponent's account was removed", "Banned user", "Server Decision"):
out += param("RE",f"{winner}+F")
elif "Server Decision" in outcome:
if m := re.match(r"Server Decision \(\d+\.\d+% ([WB]+.*)\)", outcome):
out += param("RE",m.group(1))
else:
warn(f"Unknown outcome '{outcome}' (with known winner) for game {game_id}")
out += param("RE",f"{winner}+F")
else:
warn(f"Unknown outcome '{outcome}' (with known winner) for game {game_id}")
out += param("RE","?")
else:
warn(f"Unknown outcome '{outcome}' (with unknown winner) for game {game_id}")
out += param("RE","?")
else:
out += param("RE","?") # Unfinished
width = get(ogsdata,"width",default=19,logifnone=True)
height = get(ogsdata,"height",default=19,logifnone=True)
if width != height:
out += param("SZ",f"{width}:{height}")
else:
out += param("SZ",width)
komi = get(ogsdata,"komi",logifnone=True)
if komi is not None:
out += param("KM",komi)
rules = get(ogsdata,"rules",logifnone=True)
if rules is not None:
if rules.lower() == "chinese":
out += param("RU","Chinese")
elif rules.lower() == "japanese":
out += param("RU","Japanese")
elif rules.lower() == "korean":
out += param("RU","Korean")
elif rules.lower() == "nz":
out += param("RU","NZ")
elif rules.lower() == "aga":
out += param("RU","AGA")
elif rules.lower() == "ing":
out += param("RU","Ing")
else:
out += param("RU",rules)
handicap = get(ogsdata,"handicap",default=0)
if handicap > 0:
out += param("HA",handicap)
ranked = get(ogsdata,"ranked",default=False,logifnone=True)
if ranked:
extra_info.append("ranked")
else:
extra_info.append("unranked")
out += param("GC",",".join(extra_info))
initial_player = get(ogsdata,"initial_player")
if initial_player is not None:
if initial_player == "black":
pass
elif initial_player == "white":
out += param("PL","W")
else:
warn(f"Unknown initial player for game {game_id}")
initial_player = "black"
else:
initial_player = "black"
initial_state = get(ogsdata,"initial_state")
if initial_state is not None:
bstate = get(initial_state,"black",logifnone=True)
wstate = get(initial_state,"white",logifnone=True)
if bstate is not None and len(bstate) > 0:
out += "AB"
for i in range(0, len(bstate), 2):
out += "[" + bstate[i:i+2] + "]"
if wstate is not None and len(wstate) > 0:
out += "AW"
for i in range(0, len(wstate), 2):
out += "[" + wstate[i:i+2] + "]"
moves = get(ogsdata,"moves",logifnone=True)
if moves is not None:
blacknext = True if initial_player == "black" else False
for idx,data in enumerate(moves):
if idx < handicap:
if idx == 0:
out += "AB"
out += "[" + sgfletters[data[0]] + sgfletters[data[1]] + "]"
if idx == handicap-1:
blacknext = False
else:
if blacknext:
out += ";B["
else:
out += ";W["
if data[0] < 0 or data[1] < 0: # pass
pass
else:
out += sgfletters[data[0]] + sgfletters[data[1]]
out += "]"
blacknext = not blacknext
out += ")"
out += "\n"
#assert not failure, "Game {} failed to parse".format(game_id)
if failure: return None
return False, ([black_username, black_player_id], [white_username, white_player_id], date, game_id), out
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', stream = sys.stdout, level=logging.INFO)
parser = argparse.ArgumentParser(description='Convert ogs jsons to sgfs')
parser.add_argument('dirs', metavar='DIR', nargs='+', help='Directories of ogs json files')
parser.add_argument('-verbose', required=False, action="store_true", help='Text file with npzs to ignore, one per line')
args = parser.parse_args()
num_processed = 0
for arg in args.dirs:
for (path,dirnames,filenames) in os.walk(arg, followlinks=True):
filenames = [os.path.join(path,filename) for filename in filenames if filename.endswith('.json')]
for filename in filenames:
outfile = filename[:-5]+".sgf"
if args.verbose:
logging.info(f"{filename} -> {outfile}")
with open(filename) as f:
ogsdata = json.load(f)
sgf = construct_sgf(ogsdata)
with open(outfile,"w") as f:
f.write(sgf)
num_processed += 1
if num_processed % 10000 == 0:
logging.info(f"Processed {num_processed} files")
logging.info(f"Processed {num_processed} files")
logging.info("Done")