diff options
| author | tslil <tslil@posteo.de> | 2021-01-01 16:09:45 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | 45d2aa065c7ac8143110873c069cd0de471739a7 (patch) | |
| tree | edd17d79d4da7cf975cf9593a034663df9621458 /data/extract.sh | |
| parent | 7f95bc20fcfb1b6ae10ad8c6add5cce7ad3b0963 (diff) | |
extract games from playtak.com/games database
Diffstat (limited to 'data/extract.sh')
| -rwxr-xr-x | data/extract.sh | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/data/extract.sh b/data/extract.sh new file mode 100755 index 0000000..7acb1e2 --- /dev/null +++ b/data/extract.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +query() { + echo "SELECT $1 FROM games WHERE (size == $2) \ +and (result != '1-0') and (result != '0-1') and (result != '0-0');" +} + +extract() { + in_file=games_anon.db + for size in 5 6; do + sqlite3 "$in_file" "$(query $1 $size)" > "playtak-$size" + done +} + +# Correct move notation, assuming only valid stuff +fix_move() { + args=($1) + if [ "${args[0]}" = "M" ]; then + result="" + # Annoying to compute direction + base_col=${args[1]:0:1} + base_row=${args[1]:1:1} + step_col=${args[2]:0:1} + step_row=${args[2]:1:1} + if [[ "$base_col" < "$step_col" ]]; then + result=">" + elif [[ "$base_col" > "$step_col" ]]; then + result="<" + elif [[ "$base_row" > "$step_row" ]]; then + result="-" + elif [[ "$base_row" < "$step_row" ]]; then + result="+" + fi + total=0 + steps=${#args[@]} + for (( i = 3 ; i < steps ; i++ )); do + total=$(( total + ${args[i]} )) + result="$result${args[i]}" + done + echo "$total${args[1],}$result" + else + echo "$1" + fi; +} + +# Use GNU sed to fix placement notation +fix_place="\ +s/P \(..\) C,/C\L\1,/g;\ +s/P \(..\) W,/S\L\1,/g;\ +s/P \(..\),/\L\1,/g" + +# Thiss is way too slow +extract_ptn() { + extract "notation, result" + + size="$1" + dir="ptn-$size" + if [ ! -d "$dir" ]; then + mkdir "$dir"; + fi; + + # Separate each line into a file + count=0 + while read game; do + file="$dir/$count.ptn" + echo "$game" | tr ',' '\n' | sed "$fix_place" > "$file" + # Prepare to reformat log as PTN + ply=1 + prepend="" + new_contents="" + while read line; do + # PTN calls for numbers before every new turn in the game + if [ $(( $ply % 2 )) -eq 1 ]; then + prepend="$(( $ply / 2 + 1 )). " + if [ $ply -gt 1 ]; then + prepend="\n$prepend" + fi + else + prepend=" " + fi + # We have to fix the move notation + line=$(fix_move "$line") + new_contents="$new_contents$prepend$line" + ply=$(( $ply + 1 )) + done < "$file" + echo -e "$new_contents" | tr '|' '\n' > "$file" + count=$(( $count + 1 )) + done < "playtak-$size" +} + +extract notation |
