;; ;;;;;;;;;;;;;;; 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.
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:
(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
(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))
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
(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)
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)))
(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))