From 4085a270dea85f115cb1c2cd8b35ada36a7e4195 Mon Sep 17 00:00:00 2001 From: Steffen Buchholz Date: Sat, 26 Oct 2024 09:36:19 +0200 Subject: [PATCH 1/4] Move time not shown in score graph Fixes #187 --- tcl/tools/graphs.tcl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tcl/tools/graphs.tcl b/tcl/tools/graphs.tcl index 0cb6f7870..bcc90f936 100644 --- a/tcl/tools/graphs.tcl +++ b/tcl/tools/graphs.tcl @@ -498,6 +498,7 @@ proc MoveTimeList {color add} { set game [sc_base getGame $base $gnum live] set n [llength $game] set movenr 0 + set oldtime 0 for {set i 0} { $i < $n} { incr i } { set RAVd [lindex [lindex $game $i] 0] set RAVn [lindex [lindex $game $i] 1] @@ -533,7 +534,19 @@ proc MoveTimeList {color add} { regexp $clkExp $comment -> clock if { $clock != "" } { if { [scan $clock "%f:%f:%f" ho mi sec ] == 3 } { - lappend movetimes [expr $movenr+$offset] [expr { $ho*60.0 + $mi + $sec/60}] } + if { ! $add } { + set len [llength $movetimes] + set newtime [expr { $ho*60.0 + $mi + $sec/60}] + if { $len == 0 } { + lappend movetimes [expr $movenr+$offset] 0.0 + } else { + lappend movetimes [expr $movenr+$offset] [expr { 60 * ($oldtime - $newtime) } ] + } + set oldtime $newtime + } else { + lappend movetimes [expr $movenr+$offset] [expr { $ho*60.0 + $mi + $sec/60} ] + } + } } else { set emtExp {.*?\[%emt\s*(.*?)\s*\].*} set emt "" From 8d023a9314517d53ecb2499f2b41a7f8cd6911cb Mon Sep 17 00:00:00 2001 From: Steffen Buchholz Date: Sat, 26 Oct 2024 21:28:12 +0200 Subject: [PATCH 2/4] Fixes #187 --- tcl/tools/graphs.tcl | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tcl/tools/graphs.tcl b/tcl/tools/graphs.tcl index bcc90f936..0157b72fa 100644 --- a/tcl/tools/graphs.tcl +++ b/tcl/tools/graphs.tcl @@ -496,9 +496,31 @@ proc MoveTimeList {color add} { set base [sc_base current] set gnum [sc_game number] set game [sc_base getGame $base $gnum live] + set extraTags [sc_game tag get Extra] + set extraTagsList [split $extraTags "\n"] set n [llength $game] set movenr 0 set oldtime 0 + set timecontrolnormaltime 0 + set timecontrolextratime 0 + foreach i $extraTagsList { + if { [string equal -nocase [lindex $i 0] "TimeControl" ] } { + set timecontrol [string range $i [expr [string length "TimeControl"] + 2] end-1] + set entries [split $timecontrol "+"] + set i 0 + foreach e $entries { + if { $i == 0 } { + set timecontrolnormaltime $e + set oldtime [expr $timecontrolnormaltime / 60 ] + } + if { $i == 1} { + set timecontrolextratime $e + } + incr i + } + } + } + for {set i 0} { $i < $n} { incr i } { set RAVd [lindex [lindex $game $i] 0] set RAVn [lindex [lindex $game $i] 1] @@ -537,10 +559,11 @@ proc MoveTimeList {color add} { if { ! $add } { set len [llength $movetimes] set newtime [expr { $ho*60.0 + $mi + $sec/60}] - if { $len == 0 } { + set diff [expr { 60 * ($oldtime - $newtime) + $timecontrolextratime} ] + if { $len == 0 && $oldtime == 0 } { lappend movetimes [expr $movenr+$offset] 0.0 } else { - lappend movetimes [expr $movenr+$offset] [expr { 60 * ($oldtime - $newtime) } ] + lappend movetimes [expr $movenr+$offset] [expr { 60 * ($oldtime - $newtime) + $timecontrolextratime} ] } set oldtime $newtime } else { From ef8a71ae8277dc7a0d26cc9cf70e08f437de77da Mon Sep 17 00:00:00 2001 From: Steffen Buchholz Date: Sat, 26 Oct 2024 22:00:37 +0200 Subject: [PATCH 3/4] Code for movetime is only active if timecontrol tag is found --- tcl/tools/graphs.tcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcl/tools/graphs.tcl b/tcl/tools/graphs.tcl index 0157b72fa..1f6525af2 100644 --- a/tcl/tools/graphs.tcl +++ b/tcl/tools/graphs.tcl @@ -556,7 +556,7 @@ proc MoveTimeList {color add} { regexp $clkExp $comment -> clock if { $clock != "" } { if { [scan $clock "%f:%f:%f" ho mi sec ] == 3 } { - if { ! $add } { + if { ! $add && $timecontrolnormaltime > 0 } { set len [llength $movetimes] set newtime [expr { $ho*60.0 + $mi + $sec/60}] set diff [expr { 60 * ($oldtime - $newtime) + $timecontrolextratime} ] From 983421523bd1c72435a849e5149db2d8b002e955 Mon Sep 17 00:00:00 2001 From: Steffen Buchholz Date: Wed, 30 Oct 2024 07:05:28 +0100 Subject: [PATCH 4/4] consider %clk and pgn Timecontrol tag in move time calculation --- tcl/tools/graphs.tcl | 136 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 108 insertions(+), 28 deletions(-) diff --git a/tcl/tools/graphs.tcl b/tcl/tools/graphs.tcl index 1f6525af2..aa5e7431d 100644 --- a/tcl/tools/graphs.tcl +++ b/tcl/tools/graphs.tcl @@ -478,6 +478,62 @@ set ::tools::graphs::score::Times 1 set ::tools::graphs::score::TimeSum 0 set ::tools::graphs::score::MaxY 6 +########################### +# Game score and time graph + +# TimeControlList +# Returns a Tcl list of the timecontrols, as found +# in the tag TimeControl. +# The parameter timecontrol is a string with the format +# ? unknown +# - no timecontrol +# 46/9000:20/3600:0 +# 40/5400+30:1800+30 +# 40/6000+30:20/3000+30:600+30 +proc TimeControlList {timecontrol} { + set timecontrols {} + set entries [split $timecontrol ":"] + foreach e $entries { + switch $e { + ? { + # unknown, not processed + return {} + } + - { + # no timecontrol + return {} + } + default { + set pattern {^((\d+)/)?(\d+)([+](\d+))?$} + if {[expr [regexp $pattern $e notused notused moves starttime notused increment] > 0]} { + lappend timecontrols [list $moves $starttime $increment] + } else { + # could not parse the timecontrol string + return {} + } + } + } + } + return $timecontrols +} + +# GetTimecontrol +# Returns a list with the matching moves, starttime and increment from the list timecontrols and the matching movenr +proc GetTimecontrol {timecontrols movenr} { + set timecontrol {} + set movecount 0 + foreach e $timecontrols { + lassign $e moves starttime increment + if { $moves eq "" || $movenr < ($movecount + $moves) } { + set timecontrol [list $moves $starttime $increment] + break + } + incr movecount $moves + } + return $timecontrol +} + + ########################### # Game score and time graph @@ -501,25 +557,18 @@ proc MoveTimeList {color add} { set n [llength $game] set movenr 0 set oldtime 0 - set timecontrolnormaltime 0 + set timecontrols {} + set timecontrolmoves 0 + set oldtimecontrolmoves 0 + set timecontrolstarttime 0 set timecontrolextratime 0 - foreach i $extraTagsList { - if { [string equal -nocase [lindex $i 0] "TimeControl" ] } { - set timecontrol [string range $i [expr [string length "TimeControl"] + 2] end-1] - set entries [split $timecontrol "+"] - set i 0 - foreach e $entries { - if { $i == 0 } { - set timecontrolnormaltime $e - set oldtime [expr $timecontrolnormaltime / 60 ] - } - if { $i == 1} { - set timecontrolextratime $e - } - incr i - } + foreach i $extraTagsList { + if { [string equal -nocase [lindex $i 0] "TimeControl" ] } { + set timecontrolstr [string range $i [expr [string length "TimeControl"] + 2] end-1] + set timecontrols [TimeControlList $timecontrolstr] + break } - } + } for {set i 0} { $i < $n} { incr i } { set RAVd [lindex [lindex $game $i] 0] @@ -537,6 +586,8 @@ proc MoveTimeList {color add} { } set movenr 0 set offset 0.0 + set movecount 1 + set colormovenr 0 if { $color == "w" } { set offset 0.5 } set sum 0.0 for {set i 0} { $i < $n} { incr i } { @@ -556,19 +607,48 @@ proc MoveTimeList {color add} { regexp $clkExp $comment -> clock if { $clock != "" } { if { [scan $clock "%f:%f:%f" ho mi sec ] == 3 } { - if { ! $add && $timecontrolnormaltime > 0 } { - set len [llength $movetimes] - set newtime [expr { $ho*60.0 + $mi + $sec/60}] - set diff [expr { 60 * ($oldtime - $newtime) + $timecontrolextratime} ] - if { $len == 0 && $oldtime == 0 } { - lappend movetimes [expr $movenr+$offset] 0.0 - } else { - lappend movetimes [expr $movenr+$offset] [expr { 60 * ($oldtime - $newtime) + $timecontrolextratime} ] - } - set oldtime $newtime + if { !$add && $timecontrols ne "" } { + # enable support for displaying time differences for %clk flag + # this is used e.g. for lichess and chess.com game analysis + set len [llength $movetimes] + set oldtimecontrolmoves $timecontrolmoves + lassign [GetTimecontrol $timecontrols $colormovenr] timecontrolmoves timecontrolstarttime timecontrolextratime + if {$timecontrolmoves eq ""} { + if { $oldtimecontrolmoves ne "" && [expr $oldtimecontrolmoves > 0]} { + # formely interval has limited moves, now has unlimited moves + set movecount 1 + } + set timecontrolmoves 0 + } + if {$timecontrolstarttime eq ""} { + set timecontrolstarttime 0 + } + if {$timecontrolextratime eq ""} { + set timecontrolextratime 0 + } + set newtime [expr { $ho*3600.0 + $mi*60 + $sec}] + if {[expr $timecontrolmoves > 0 && $movecount > $timecontrolmoves] } { + # move counter exceeds the moves of the interval, reset move counter + set movecount 1 + } + if {[expr $movecount == 1] } { + # add additional time for a new interval to oldtime + set oldtime [expr $oldtime + $timecontrolstarttime] + } + set diff [expr { $oldtime - $newtime + $timecontrolextratime} ] + if { $len == 0 && $oldtime == 0 } { + # movetimes is empty and oldtime not initialized, assume 0 as start difference + lappend movetimes [expr $movenr+$offset] 0.0 + } else { + lappend movetimes [expr $movenr+$offset] $diff + } + set oldtime $newtime } else { - lappend movetimes [expr $movenr+$offset] [expr { $ho*60.0 + $mi + $sec/60} ] + lappend movetimes [expr $movenr+$offset] [expr { $ho*60.0 + $mi + $sec/60} ] } + # increment movecount/colormovenr only if valid %clk is found in the mainline + incr movecount + incr colormovenr } } else { set emtExp {.*?\[%emt\s*(.*?)\s*\].*}