aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFederico Tedin2019-09-04 00:18:11 +0200
committerEli Zaretskii2019-09-14 11:20:32 +0300
commit36bf5534bf9034860ee6ffda94fa71d4eec8a671 (patch)
tree7bd68551016f11614877ec22c0b3ef2d79f86768
parentbac66302e92bdd3a353102d2076548e7e83d92e5 (diff)
downloademacs-36bf5534bf9034860ee6ffda94fa71d4eec8a671.tar.gz
emacs-36bf5534bf9034860ee6ffda94fa71d4eec8a671.zip
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. (Bug#36867) * etc/NEWS: Announce the change.
-rw-r--r--etc/NEWS3
-rw-r--r--lisp/play/gamegrid.el49
2 files changed, 30 insertions, 22 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 1bde9c442b7..d3338bf57d4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1375,6 +1375,9 @@ the Elisp manual for documentation of the new mode and its commands.
1375dimensions, instead of always using 16 pixels. As a result, Tetris, 1375dimensions, instead of always using 16 pixels. As a result, Tetris,
1376Snake and Pong are more playable on HiDPI displays. 1376Snake and Pong are more playable on HiDPI displays.
1377 1377
1378*** 'gamegrid-add-score' can now sort scores from lower to higher.
1379This is useful for games where lower scores are better, like time-based games.
1380
1378** Filecache 1381** Filecache
1379 1382
1380--- 1383---
diff --git a/lisp/play/gamegrid.el b/lisp/play/gamegrid.el
index be09a73a1f1..df9b1352480 100644
--- a/lisp/play/gamegrid.el
+++ b/lisp/play/gamegrid.el
@@ -505,9 +505,12 @@ format."
505 505
506;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 506;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507 507
508(defun gamegrid-add-score (file score) 508(defun gamegrid-add-score (file score &optional reverse)
509 "Add the current score to the high score file. 509 "Add the current score to the high score file.
510 510
511If REVERSE is non-nil, treat lower scores as better than higher
512scores. This is useful for games where lower scores are better.
513
511On POSIX systems there may be a shared game directory for all users in 514On POSIX systems there may be a shared game directory for all users in
512which the scorefiles are kept. On such systems Emacs doesn't create 515which the scorefiles are kept. On such systems Emacs doesn't create
513the score file FILE in this directory, if it doesn't already exist. 516the score file FILE in this directory, if it doesn't already exist.
@@ -525,9 +528,9 @@ specified by the variable `temporary-file-directory'. If necessary,
525FILE is created there." 528FILE is created there."
526 (pcase system-type 529 (pcase system-type
527 ((or 'ms-dos 'windows-nt) 530 ((or 'ms-dos 'windows-nt)
528 (gamegrid-add-score-insecure file score)) 531 (gamegrid-add-score-insecure file score reverse))
529 (_ 532 (_
530 (gamegrid-add-score-with-update-game-score file score)))) 533 (gamegrid-add-score-with-update-game-score file score reverse))))
531 534
532 535
533;; On POSIX systems there are four cases to distinguish: 536;; On POSIX systems there are four cases to distinguish:
@@ -556,20 +559,21 @@ FILE is created there."
556 559
557(defvar gamegrid-shared-game-dir) 560(defvar gamegrid-shared-game-dir)
558 561
559(defun gamegrid-add-score-with-update-game-score (file score) 562(defun gamegrid-add-score-with-update-game-score (file score &optional reverse)
560 (let* ((update-game-score-modes 563 (let* ((update-game-score-modes
561 (file-modes (expand-file-name "update-game-score" exec-directory))) 564 (file-modes (expand-file-name "update-game-score" exec-directory)))
562 (gamegrid-shared-game-dir 565 (gamegrid-shared-game-dir
563 (not (zerop (logand #o6000 (or update-game-score-modes 0)))))) 566 (not (zerop (logand #o6000 (or update-game-score-modes 0))))))
564 (cond ((or (not update-game-score-modes) (file-name-absolute-p file)) 567 (cond ((or (not update-game-score-modes) (file-name-absolute-p file))
565 (gamegrid-add-score-insecure file score 568 (gamegrid-add-score-insecure file score
566 gamegrid-user-score-file-directory)) 569 gamegrid-user-score-file-directory
570 reverse))
567 ((and gamegrid-shared-game-dir 571 ((and gamegrid-shared-game-dir
568 (file-exists-p (expand-file-name file shared-game-score-directory))) 572 (file-exists-p (expand-file-name file shared-game-score-directory)))
569 ;; Use the setgid (or setuid) "update-game-score" program 573 ;; Use the setgid (or setuid) "update-game-score" program
570 ;; to update a system-wide score file. 574 ;; to update a system-wide score file.
571 (gamegrid-add-score-with-update-game-score-1 file 575 (gamegrid-add-score-with-update-game-score-1 file
572 (expand-file-name file shared-game-score-directory) score)) 576 (expand-file-name file shared-game-score-directory) score reverse))
573 ;; Else: Add the score to a score file in the user's home 577 ;; Else: Add the score to a score file in the user's home
574 ;; directory. 578 ;; directory.
575 (gamegrid-shared-game-dir 579 (gamegrid-shared-game-dir
@@ -579,7 +583,8 @@ FILE is created there."
579 (directory-file-name gamegrid-user-score-file-directory)) 583 (directory-file-name gamegrid-user-score-file-directory))
580 (make-directory gamegrid-user-score-file-directory t)) 584 (make-directory gamegrid-user-score-file-directory t))
581 (gamegrid-add-score-insecure file score 585 (gamegrid-add-score-insecure file score
582 gamegrid-user-score-file-directory)) 586 gamegrid-user-score-file-directory
587 reverse))
583 (t 588 (t
584 (unless (file-exists-p 589 (unless (file-exists-p
585 (directory-file-name gamegrid-user-score-file-directory)) 590 (directory-file-name gamegrid-user-score-file-directory))
@@ -588,9 +593,9 @@ FILE is created there."
588 gamegrid-user-score-file-directory))) 593 gamegrid-user-score-file-directory)))
589 (unless (file-exists-p f) 594 (unless (file-exists-p f)
590 (write-region "" nil f nil 'silent nil 'excl)) 595 (write-region "" nil f nil 'silent nil 'excl))
591 (gamegrid-add-score-with-update-game-score-1 file f score)))))) 596 (gamegrid-add-score-with-update-game-score-1 file f score reverse))))))
592 597
593(defun gamegrid-add-score-with-update-game-score-1 (file target score) 598(defun gamegrid-add-score-with-update-game-score-1 (file target score &optional reverse)
594 (let ((default-directory "/") 599 (let ((default-directory "/")
595 (errbuf (generate-new-buffer " *update-game-score loss*")) 600 (errbuf (generate-new-buffer " *update-game-score loss*"))
596 (marker-string (concat 601 (marker-string (concat
@@ -601,17 +606,16 @@ FILE is created there."
601 (with-local-quit 606 (with-local-quit
602 (apply 607 (apply
603 'call-process 608 'call-process
604 (append 609 `(,(expand-file-name "update-game-score" exec-directory)
605 (list 610 nil ,errbuf nil
606 (expand-file-name "update-game-score" exec-directory) 611 "-m" ,(int-to-string gamegrid-score-file-length)
607 nil errbuf nil 612 "-d" ,(if gamegrid-shared-game-dir
608 "-m" (int-to-string gamegrid-score-file-length) 613 (expand-file-name shared-game-score-directory)
609 "-d" (if gamegrid-shared-game-dir 614 (file-name-directory target))
610 (expand-file-name shared-game-score-directory) 615 ,@(if reverse '("-r"))
611 (file-name-directory target)) 616 ,file
612 file 617 ,(int-to-string score)
613 (int-to-string score) 618 ,marker-string)))
614 marker-string))))
615 (if (buffer-modified-p errbuf) 619 (if (buffer-modified-p errbuf)
616 (progn 620 (progn
617 (display-buffer errbuf) 621 (display-buffer errbuf)
@@ -632,7 +636,7 @@ FILE is created there."
632 marker-string) nil t) 636 marker-string) nil t)
633 (beginning-of-line))))) 637 (beginning-of-line)))))
634 638
635(defun gamegrid-add-score-insecure (file score &optional directory) 639(defun gamegrid-add-score-insecure (file score &optional directory reverse)
636 (save-excursion 640 (save-excursion
637 (setq file (expand-file-name file (or directory 641 (setq file (expand-file-name file (or directory
638 temporary-file-directory))) 642 temporary-file-directory)))
@@ -645,7 +649,8 @@ FILE is created there."
645 (user-full-name) 649 (user-full-name)
646 user-mail-address)) 650 user-mail-address))
647 (sort-fields 1 (point-min) (point-max)) 651 (sort-fields 1 (point-min) (point-max))
648 (reverse-region (point-min) (point-max)) 652 (unless reverse
653 (reverse-region (point-min) (point-max)))
649 (goto-char (point-min)) 654 (goto-char (point-min))
650 (forward-line gamegrid-score-file-length) 655 (forward-line gamegrid-score-file-length)
651 (delete-region (point) (point-max)) 656 (delete-region (point) (point-max))