Subject: bug#36867: 26.2; gamegrid-add-scrore miss lower-is-better flag

From: Federico Tedin <[email-masked]>
Date: Wed, 04 Sep 2019 00:29:22 +0200

Rolf Ade <[email-masked]> writes:

> The function gamegrid-add-scrore is handy for games developer; they get
> a high-score management system with one call. That includes a threshold
> for the number of entries in the high-score file.
>
> A consequence of that is, that an entry has be removed from the list if
> a new, better one must be inserted. In this situation,
> gamegrid-add-scrore currently always removes the "lowest" result.
>
> This is good and well for "more is better" games. But is wrong for
> "faster is better" games. An example:
> https://github.com/calancha/Minesweeper
>
> This feature omission is on lisp level. The in the emacs sources
> included tool update-game-score (which is used by gamegrid, if it is
> available) support reverse opperation.

I've added a new 'reverse' parameter to gamegrid-add-score that allows
switching between storing the scores normally, or in reverse order. As
Rolf mentioned, this feature was already implemented in
update-game-score. I'm attaching a patch with my changes.

- Fede

>From d866bf870ad8e9dc6147728f12cfc796d75dfbc0 Mon Sep 17 00:00:00 2001
From: Federico Tedin <[email-masked]>
Date: Wed, 4 Sep 2019 00:18:11 +0200
Subject: [PATCH 1/1] Allow gamegrid-add-score to treat lower scores as better.

* lisp/play/gamegrid.el (gamegrid-add-score): Add 'reverse' parameter.
(gamegrid-add-score-with-update-game-score): Add 'reverse' parameter.
(gamegrid-add-score-with-update-game-score-1): Add 'reverse'
parameter. Pass on "-r" argument to update-game-score.
(gamegrid-add-score-insecure): Add 'reverse' parameter, reverse scores
when it's non-nil.
* etc/NEWS: Announce the change.
---
 etc/NEWS              |  3 +++
 lisp/play/gamegrid.el | 49 ++++++++++++++++++++++++-------------------
 2 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index d5130e9f3c..05b02e8653 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1370,6 +1370,9 @@ the Elisp manual for documentation of the new mode and its commands.
 dimensions, instead of always using 16 pixels.  As a result, Tetris,
 Snake and Pong are more playable on HiDPI displays.
 
+*** 'gamegrid-add-score' can now sort scores from lower to higher.
+This is useful for games where lower scores are better, like time-based games.
+
 ** Filecache
 
 ---
diff --git a/lisp/play/gamegrid.el b/lisp/play/gamegrid.el
index be09a73a1f..df9b135248 100644
--- a/lisp/play/gamegrid.el
+++ b/lisp/play/gamegrid.el
@@ -505,9 +505,12 @@ gamegrid-kill-timer
 
 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-(defun gamegrid-add-score (file score)
+(defun gamegrid-add-score (file score &optional reverse)
   "Add the current score to the high score file.
 
+If REVERSE is non-nil, treat lower scores as better than higher
+scores. This is useful for games where lower scores are better.
+
 On POSIX systems there may be a shared game directory for all users in
 which the scorefiles are kept.  On such systems Emacs doesn't create
 the score file FILE in this directory, if it doesn't already exist.
@@ -525,9 +528,9 @@ gamegrid-add-score
 FILE is created there."
   (pcase system-type
     ((or 'ms-dos 'windows-nt)
-     (gamegrid-add-score-insecure file score))
+     (gamegrid-add-score-insecure file score reverse))
     (_
-     (gamegrid-add-score-with-update-game-score file score))))
+     (gamegrid-add-score-with-update-game-score file score reverse))))

 ;; On POSIX systems there are four cases to distinguish:
@@ -556,20 +559,21 @@ gamegrid-add-score
 
 (defvar gamegrid-shared-game-dir)
 
-(defun gamegrid-add-score-with-update-game-score (file score)
+(defun gamegrid-add-score-with-update-game-score (file score &optional reverse)
   (let* ((update-game-score-modes
 	  (file-modes (expand-file-name "update-game-score" exec-directory)))
 	 (gamegrid-shared-game-dir
 	  (not (zerop (logand #o6000 (or update-game-score-modes 0))))))
     (cond ((or (not update-game-score-modes) (file-name-absolute-p file))
 	   (gamegrid-add-score-insecure file score
-                                        gamegrid-user-score-file-directory))
+                                        gamegrid-user-score-file-directory
+                                        reverse))
 	  ((and gamegrid-shared-game-dir
 		(file-exists-p (expand-file-name file shared-game-score-directory)))
 	   ;; Use the setgid (or setuid) "update-game-score" program
 	   ;; to update a system-wide score file.
 	   (gamegrid-add-score-with-update-game-score-1 file
-	    (expand-file-name file shared-game-score-directory) score))
+	    (expand-file-name file shared-game-score-directory) score reverse))
 	  ;; Else: Add the score to a score file in the user's home
 	  ;; directory.
 	  (gamegrid-shared-game-dir
@@ -579,7 +583,8 @@ gamegrid-add-score-with-update-game-score
 		    (directory-file-name gamegrid-user-score-file-directory))
 	     (make-directory gamegrid-user-score-file-directory t))
 	   (gamegrid-add-score-insecure file score
-					gamegrid-user-score-file-directory))
+					gamegrid-user-score-file-directory
+                                        reverse))
 	  (t
 	   (unless (file-exists-p
 		    (directory-file-name gamegrid-user-score-file-directory))
@@ -588,9 +593,9 @@ gamegrid-add-score-with-update-game-score
 				      gamegrid-user-score-file-directory)))
 	     (unless (file-exists-p f)
 	       (write-region "" nil f nil 'silent nil 'excl))
-	     (gamegrid-add-score-with-update-game-score-1 file f score))))))
+	     (gamegrid-add-score-with-update-game-score-1 file f score reverse))))))
 
-(defun gamegrid-add-score-with-update-game-score-1 (file target score)
+(defun gamegrid-add-score-with-update-game-score-1 (file target score &optional reverse)
   (let ((default-directory "/")
 	(errbuf (generate-new-buffer " *update-game-score loss*"))
         (marker-string (concat
@@ -601,17 +606,16 @@ gamegrid-add-score-with-update-game-score-1
     (with-local-quit
       (apply
        'call-process
-       (append
-	(list
-	 (expand-file-name "update-game-score" exec-directory)
-	 nil errbuf nil
-	 "-m" (int-to-string gamegrid-score-file-length)
-	 "-d" (if gamegrid-shared-game-dir
-		  (expand-file-name shared-game-score-directory)
-		(file-name-directory target))
-	 file
-	 (int-to-string score)
-	 marker-string))))
+       `(,(expand-file-name "update-game-score" exec-directory)
+         nil ,errbuf nil
+         "-m" ,(int-to-string gamegrid-score-file-length)
+         "-d" ,(if gamegrid-shared-game-dir
+                   (expand-file-name shared-game-score-directory)
+                 (file-name-directory target))
+         ,@(if reverse '("-r"))
+         ,file
+         ,(int-to-string score)
+         ,marker-string)))
     (if (buffer-modified-p errbuf)
 	(progn
 	  (display-buffer errbuf)
@@ -632,7 +636,7 @@ gamegrid-add-score-with-update-game-score-1
 				marker-string) nil t)
         (beginning-of-line)))))
 
-(defun gamegrid-add-score-insecure (file score &optional directory)
+(defun gamegrid-add-score-insecure (file score &optional directory reverse)
   (save-excursion
     (setq file (expand-file-name file (or directory
 					  temporary-file-directory)))
@@ -645,7 +649,8 @@ gamegrid-add-score-insecure
 		    (user-full-name)
 		    user-mail-address))
     (sort-fields 1 (point-min) (point-max))
-    (reverse-region (point-min) (point-max))
+    (unless reverse
+      (reverse-region (point-min) (point-max)))
     (goto-char (point-min))
     (forward-line gamegrid-score-file-length)
     (delete-region (point) (point-max))
-- 
2.17.1