From b24a94352b80c42f3417c790671954a1f1410e49 Mon Sep 17 00:00:00 2001 From: Roy Date: Thu, 4 May 2023 21:24:32 +0200 Subject: [PATCH] Correct JS function order. --- chess_gui/lib/chess_gui.pm | 11 ++++------- chess_gui/public/js/dand.js | 24 ++++++++---------------- chess_gui/public/js/head.js | 10 ++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/chess_gui/lib/chess_gui.pm b/chess_gui/lib/chess_gui.pm index 8761410..46de444 100644 --- a/chess_gui/lib/chess_gui.pm +++ b/chess_gui/lib/chess_gui.pm @@ -715,7 +715,6 @@ my ($pval, $called_player, $reason) = @_; # Arguments: (Value of all pieces of $ } sub PROGRAM { -my $sessid = $_[2]{'id'}; $user_mode = 0; # if (!defined($user_mode)) { # print 'Please input your side:', "\n"; @@ -757,12 +756,12 @@ $user_mode = 0; ($xrc, $yrc) = split //, $_[1]; $_[1] = join '.', $pxr{$xrc}, ($yrc-1); print $_[0], ' ', $_[1], "\n"; - ($prio, @id[0, 1], $selection, $ice, $n_o_selections) = &MOVEMENT_TREE($player, $depth, undef, undef, undef, undef, undef, $_[0], $_[1], $sessid); + ($prio, @id[0, 1], $selection, $ice, $n_o_selections) = &MOVEMENT_TREE($player, $depth, undef, undef, undef, undef, undef, $_[0], $_[1], $_[2]); print 'w ', $id[0], ' ', $id[1], "\n"; } else { $depth = 2; # Usually about 20^$depth possibilities (but ten times as much iterates for including no movement). - ($prio, @id[0, 1], $selection, $ice, $n_o_selections) = &MOVEMENT_TREE($player, $depth, undef, undef, undef, undef, undef, undef, undef, $sessid); - $sth10->execute($sessid, @id[0, 1]); + ($prio, @id[0, 1], $selection, $ice, $n_o_selections) = &MOVEMENT_TREE($player, $depth, undef, undef, undef, undef, undef, undef, undef, $_[2]); + $sth10->execute($_[2], @id[0, 1]); print 'b ', $id[0], ' ', $id[1], "\n"; } # print 'keys: ', scalar keys %key_list_by_value, "\n"; @@ -934,7 +933,7 @@ get '/board/init' => sub { get '/move/:src/:dst' => sub { #if (session('init')) { - my %diff = &PROGRAM(route_parameters->get('src'), route_parameters->get('dst'), session); # Includes player input. + my %diff = &PROGRAM(route_parameters->get('src'), route_parameters->get('dst'), session->id); # Includes player input. encode_json(\%diff); #} }; @@ -945,8 +944,6 @@ get '/' => sub { #any ['get', 'post'] => '/' => sub { -# my $session = session; # TODO: I only want %{session}{'id'}. -# $session = %{$session}{'id'}; # $sth11->execute($session); #print $session, "\n"; # my @sth11 = $sth11->fetchrow_array(); diff --git a/chess_gui/public/js/dand.js b/chess_gui/public/js/dand.js index b556c4d..c8a6653 100644 --- a/chess_gui/public/js/dand.js +++ b/chess_gui/public/js/dand.js @@ -1,6 +1,3 @@ -var d; -var drag; -var drop; var xhr = new XMLHttpRequest(); //var e = document.createElement('li'); //var f = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']; @@ -44,17 +41,7 @@ i++; } } -onload = function () { -for (var b=0; C('coord').length > b; b++) {C('coord')[b].onmousedown = function () { -event.preventDefault(); -drag = this.id; -}} -} - -for (var c=0; C('coord').length > c; c++) {C('coord')[c].onmouseup = function () { -drop = this.id; -//console.log(this.id); -if (drag.length === 2 && drop.length === 2) { // Shouldn't rely on legal input here. +function dragAndDrop(drag, drop) { xhr.open('GET', 'move/' + drag + '/' + drop , true); xhr.send(null); xhr.onload = function () { @@ -171,11 +158,16 @@ d.appendChild(li); //I('description').innerHTML = responseObject.Black.status[1]; } } -}; -} } //for (b=0; C('coord').length > b; b++) {C('coord')[b].onmousemove = function () { //if (drag !== null && drag !== undefined) {console.log(this.x + '.' + this.y)} //if (drag !== null && drag !== undefined) {I(this.id).style.position = 'absolute'; I(this.id).style.left = this.x + 'px'; I(this.id).style.up = this.y + 'px'} //}} + +//window.onload = function () { +//for (var b=0; C('coord').length > b; b++) {C('coord')[b].onmousedown = function () { +//event.preventDefault(); +//drag = this.id; +//}} +//} diff --git a/chess_gui/public/js/head.js b/chess_gui/public/js/head.js index 58d9812..9410b68 100644 --- a/chess_gui/public/js/head.js +++ b/chess_gui/public/js/head.js @@ -52,4 +52,14 @@ li.innerHTML = srci; // Add. } } } +postG(); +} + +function postG () { +var drag; +var drop; +for (var b=0; C('coord').length > b; b++) { +C('coord')[b].onmousedown = function () {event.preventDefault(); drag = this.id} +C('coord')[b].onmouseup = function () {drop = this.id; if (drag.length === 2 && drop.length === 2) {dragAndDrop(drag, drop)}} // Shouldn't rely on legal input here. +} }