Skip to content

Commit

Permalink
Resolve SabakiHQ#11: Fixing fox's Komi values
Browse files Browse the repository at this point in the history
This is just the functional part and two test files. Test cases coming
next as well as documentation.

Adjusting Komi on fox is added as an option you can pass to ParseToken.

This handles fox's komi in 3 cases by following kaTrain's fixup:
1. Handicap games it adjusts to 0.5 Komi
2. Even games using chinese rules it adjusts to 7.5 Komi (from 375)
3. From other rulesets it adjusts to (6.5 from 325)

Note: probably a bug here as some games have 0 as Komi and we shouldn't
add Komi in those cases.
  • Loading branch information
loarabia committed Aug 7, 2023
1 parent 606f059 commit 75343af
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ exports.parseTokens = function(
getId = (id => () => id++)(0),
dictionary = null,
onProgress = () => {},
onNodeCreated = () => {}
onNodeCreated = () => {},
shouldAdjustFoxKomi = true
} = {}
) {
let node = _parseTokens(new Peekable(tokens), null, {
Expand All @@ -126,7 +127,33 @@ exports.parseTokens = function(
onNodeCreated
})

return node.id == null ? node.children : [node]
let rootNodes = node.id == null ? node.children : [node]

// Fox Go Server uses different Komi and in particular for even games will set it to 375.
// Here we're following KaTrain's method to fixup Komi
// https://github.com/sanderland/katrain/blob/485b55c42df4dd1c77abf21eefc23c9a17d6a512/katrain/core/sgf_parser.py#L422-L430
let correctedKomi = 0
let gameData = rootNodes[0].data
if (
shouldAdjustFoxKomi &&
'AP' in gameData &&
gameData['AP'].includes('foxwq') &&
'KM' in gameData
) {
if ('HA' in gameData && parseInt(gameData['HA']) >= 1) {
correctedKomi = 0.5
} else if (
'RU' in gameData &&
['chinese', 'cn'].includes(gameData['RU'][0].toLowerCase())
) {
correctedKomi = 7.5
} else {
correctedKomi = 6.5
}

gameData['KM'] = correctedKomi.toString()
}
return rootNodes
}

exports.parse = function(contents, options = {}) {
Expand Down
10 changes: 10 additions & 0 deletions tests/sgf/fox_go_server_komi.sgf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(;GM[1]FF[4]
SZ[19]
GN[]
DT[2023-07-22]
PB[东升712]
PW[atypclhmn]
BR[5级]
WR[5级]
KM[375]HA[0]RU[Chinese]AP[GNU Go:3.8]RN[1]RE[W+12.75]TM[300]TC[3]TT[30]AP[foxwq]RL[0]
;B[pd];W[dp];B[dd];W[qp];B[jp];W[nq];B[lq];W[gp];B[hq];W[gq];B[bp];W[cp];B[bq];W[bo];B[dr];W[cn];B[fr];W[gr];B[hr];W[gs];B[fs];W[cf];B[ce];W[df];B[fc];W[cj];B[id];W[jr];B[jq];W[lr];B[kr];W[hp];B[ir];W[mr];B[mq];W[nr];B[np];W[op];B[mp];W[qc];B[qd];W[pc];B[oc];W[ob];B[nb];W[nc];B[od];W[mb];B[pb];W[na];B[qb];W[nd];B[ph];W[rc];B[rb];W[no];B[mo];W[nn];B[pl];W[pj];B[qj];W[qi];B[pi];W[qk];B[oj];W[pk];B[ok];W[qh];B[ql];W[rk];B[rd];W[qg];B[nl];W[mn];B[ln];W[mm];B[ip];W[pg];B[oi];W[js];B[ks];W[ko];B[kp];W[kn];B[lo];W[in];B[og];W[of];B[nf];W[ng];B[oh];W[ne];B[pf];W[mf];B[qf];W[rf];B[rl];W[rj];B[re];W[rn];B[rg];W[rh];B[sf];W[si];B[qn];W[ro];B[pn];W[om];B[ol];W[rm];B[li];W[mi];B[mj];W[lj];B[mh];W[ki];B[lh];W[kk];B[kh];W[ji];B[jh];W[ii];B[lk];W[kl];B[ll];W[ih];B[lm];W[oo];B[ml];W[gf];B[hg];W[fh];B[gg];W[fg];B[ig];W[jc];B[ic];W[jb];B[jf];W[hb];B[ib];W[ia];B[gb];W[hc];B[hd];W[de];B[cd];W[ed];B[ec];W[be];B[bd];W[bf];B[le];W[ld];B[kd];W[lc];B[kc];W[kb];B[jd];W[me];B[lf];W[cq];B[cr];W[ap];B[ar];W[gh];B[km];W[jm];B[io];W[ho];B[jn];W[im];B[kj];W[jj];B[ee];W[fd];B[fe];W[gd];B[ge];W[ff];B[gc];W[hf];B[ha];W[ja];B[if];W[he];B[hh];W[hi];B[ae];W[af];B[ad];W[nh];B[ni];W[mg];B[oe];W[nf];B[sm];W[sk];B[sl];W[sn];B[qo];W[rp];B[on];W[pp];B[nm];W[ef];B[ep];W[eo];B[fp];W[fo];B[fq];W[hn];B[dq];W[ao];B[aq];W[hs];B[is];W[ls];B[lj];W[qm];B[pm];W[ie];B[je];W[oa];B[pa];W[lg];B[kg];W[sh];B[sg];W[jk];B[sc])
10 changes: 10 additions & 0 deletions tests/sgf/fox_go_server_nokomi.sgf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(;GM[1]FF[4]
SZ[19]
GN[]
DT[2023-06-19]
PB[atypclhmn]
PW[V911029035]
BR[6级]
WR[4级]
KM[0]HA[1]RU[Chinese]AP[GNU Go:3.8]RN[3]RE[B+R]TM[300]TC[3]TT[30]AP[foxwq]RL[0]
;B[pq];W[pd];B[cp];W[dc];B[de];W[qo];B[qp];W[po];B[nq];W[eq];B[dn];W[hq];B[pl];W[cg];B[qf];W[nc];B[qd];W[qc];B[rc];W[qb];B[pe];W[qi];B[od];W[pc])

0 comments on commit 75343af

Please sign in to comment.