aboutsummaryrefslogtreecommitdiff
path: root/data/extract.sh
blob: 37ce12c4acb9b4f59a540ee9e88a97c85908992c (plain)
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
#!/bin/bash

db_file=games_anon.db

query() {
    echo "SELECT $1 FROM games WHERE (size == $2) \
and (result != '1-0') and (result != '0-1') and (result != '0-0');"
}

extract() {
    for size in 5 6; do
        sqlite3 "$db_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"
}

if [ ! -f "$db_file" ]; then
    wget "https://www.playtak.com/games_anon.db"
fi

extract notation