]> git.sesse.net Git - remoteglot/commitdiff
Add an undo button to the admin interface.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 30 Dec 2023 09:56:33 +0000 (10:56 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 30 Dec 2023 09:56:33 +0000 (10:56 +0100)
www/js/remoteglot.js
www/manual-override.pl

index ceeff7b3c94de6c10ff6f013d837b414c500227f..d36bf9e5117bed15911ea36263ed5084fc6f0c48 100644 (file)
@@ -1319,7 +1319,7 @@ function update_board() {
                document.getElementById("searchstats").textContent = "";
        }
        if (admin_password !== null) {
-               document.getElementById("searchstats").innerHTML += " | <span style=\"color: red;\">ADMIN MODE (if password is right)</span>";
+               document.getElementById("searchstats").innerHTML += " | <span style=\"color: red;\">ADMIN MODE (if password is right) | <a href=\"javascript:undo_move()\">Undo move</a></span>";
        }
 
        // Update the board itself.
@@ -2258,6 +2258,29 @@ function send_chosen_move(fen, move) {
        }
 }
 
+function undo_move() {
+       if (admin_password !== null) {
+               let history = current_analysis_data['position']['history'];
+               history = history.slice(0, history.length - 1);
+
+               let position = current_analysis_data['position']['start_fen'];
+               let hiddenboard = chess_from(position, history, history.length);
+               let fen = hiddenboard.fen();
+
+               let url = '/manual-override.pl';
+               url += '?fen=' + encodeURIComponent(fen);
+               url += '&history=' + encodeURIComponent(JSON.stringify(history));
+               url += '&move=null';
+               url += '&player_w=' + encodeURIComponent(current_analysis_data['position']['player_w']);
+               url += '&player_b=' + encodeURIComponent(current_analysis_data['position']['player_b']);
+               url += '&password=' + encodeURIComponent(admin_password);
+
+               console.log(fen, history);
+               fetch(url);  // Ignore the result.
+       }
+}
+window['undo_move'] = undo_move;
+
 function onSnapEnd(source, target) {
        if (source === target && recommended_move !== null) {
                source = recommended_move.from;
index 13333524a91b7a78eea44e067e17a015bd5d0a7e..23f571f4c84105ef4dbd6a215ac296f2c60cf89c 100755 (executable)
@@ -20,14 +20,23 @@ if (!defined($password) || $password ne $remoteglotconf::adminpass) {
        exit;
 }
 
-$dbh->do('INSERT INTO game_extensions ( fen, history, player_w, player_b, ts, next_move ) VALUES ( ?, ?, ?, ?, CURRENT_TIMESTAMP, ? )',
-       undef,
-       Encode::decode_utf8($cgi->param('fen')),
-       Encode::decode_utf8($cgi->param('history')),
-       Encode::decode_utf8($cgi->param('player_w')),
-       Encode::decode_utf8($cgi->param('player_b')),
-       Encode::decode_utf8($cgi->param('move')));
-system("sudo", "touch", $remoteglotconf::target);
+if ($cgi->param('move') eq 'null') {
+       $dbh->do('DELETE FROM game_extensions WHERE fen=? AND history=? AND player_w=? AND player_b=?',
+               undef,
+               Encode::decode_utf8($cgi->param('fen')),
+               Encode::decode_utf8($cgi->param('history')),
+               Encode::decode_utf8($cgi->param('player_w')),
+               Encode::decode_utf8($cgi->param('player_b')));
+} else {
+       $dbh->do('INSERT INTO game_extensions ( fen, history, player_w, player_b, ts, next_move ) VALUES ( ?, ?, ?, ?, CURRENT_TIMESTAMP, ? )',
+               undef,
+               Encode::decode_utf8($cgi->param('fen')),
+               Encode::decode_utf8($cgi->param('history')),
+               Encode::decode_utf8($cgi->param('player_w')),
+               Encode::decode_utf8($cgi->param('player_b')),
+               Encode::decode_utf8($cgi->param('move')));
+}
+system("sudo", "/usr/bin/touch", $remoteglotconf::target);
 
 print CGI->header(-type=>'text/plain; charset=utf-8');
 print "OK\n";