From c6ddeab3f26a7db60afe806cc49a828ac0582d1c Mon Sep 17 00:00:00 2001 From: rexim Date: Mon, 19 Oct 2015 23:36:51 +0600 Subject: [PATCH 1/5] Roughly implement it replayer (#122) --- it-cases/it-case01.el | 10 +++ it-cases/it-case02.el | 139 ++++++++++++++++++++++++++++++++++++++++++ tools/it-recorder.el | 34 +++++++++-- 3 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 it-cases/it-case01.el create mode 100644 it-cases/it-case02.el diff --git a/it-cases/it-case01.el b/it-cases/it-case01.el new file mode 100644 index 0000000..551e6d3 --- /dev/null +++ b/it-cases/it-case01.el @@ -0,0 +1,10 @@ +((pacmacs-down . 7) + (pacmacs-right . 10) + (pacmacs-down . 14) + (pacmacs-right . 17) + (pacmacs-down . 21) + (pacmacs-right . 26) + (pacmacs-up . 30) + (pacmacs-left . 32) + (pacmacs-up . 34) + (pacmacs-left . 37)) diff --git a/it-cases/it-case02.el b/it-cases/it-case02.el new file mode 100644 index 0000000..25b1520 --- /dev/null +++ b/it-cases/it-case02.el @@ -0,0 +1,139 @@ +((pacmacs-down . 8) + (pacmacs-right . 11) + (pacmacs-down . 15) + (pacmacs-right . 17) + (pacmacs-down . 21) + (pacmacs-right . 26) + (pacmacs-up . 31) + (pacmacs-left . 33) + (pacmacs-up . 36) + (pacmacs-left . 38) + (pacmacs-right . 41) + (pacmacs-down . 42) + (pacmacs-left . 46) + (pacmacs-up . 50) + (pacmacs-left . 52) + (pacmacs-down . 56) + (pacmacs-left . 59) + (pacmacs-right . 62) + (pacmacs-up . 64) + (pacmacs-left . 68) + (pacmacs-up . 71) + (pacmacs-left . 73) + (pacmacs-up . 91) + (pacmacs-up . 95) + (pacmacs-up . 98) + (pacmacs-up . 100) + (pacmacs-right . 102) + (pacmacs-down . 105) + (pacmacs-right . 110) + (pacmacs-up . 113) + (pacmacs-right . 118) + (pacmacs-down . 120) + (pacmacs-right . 125) + (pacmacs-up . 128) + (pacmacs-left . 133) + (pacmacs-down . 136) + (pacmacs-up . 140) + (pacmacs-right . 144) + (pacmacs-down . 147) + (pacmacs-right . 171) + (pacmacs-down . 182) + (pacmacs-left . 184) + (pacmacs-up . 190) + (pacmacs-right . 192) + (pacmacs-up . 206) + (pacmacs-left . 208) + (pacmacs-down . 215) + (pacmacs-up . 221) + (pacmacs-right . 224) + (pacmacs-up . 235) + (pacmacs-left . 240) + (pacmacs-right . 254) + (pacmacs-left . 272) + (pacmacs-down . 279) + (pacmacs-up . 288) + (pacmacs-right . 290) + (pacmacs-up . 297) + (pacmacs-left . 304) + (pacmacs-right . 322) + (pacmacs-down . 330) + (pacmacs-left . 335) + (pacmacs-up . 342) + (pacmacs-right . 344) + (pacmacs-up . 352) + (pacmacs-left . 356) + (pacmacs-right . 367) + (pacmacs-left . 379) + (pacmacs-right . 398) + (pacmacs-down . 407) + (pacmacs-left . 410) + (pacmacs-down . 443) + (pacmacs-left . 458) + (pacmacs-up . 466) + (pacmacs-right . 472) + (pacmacs-down . 482) + (pacmacs-left . 483) + (pacmacs-down . 491) + (pacmacs-right . 493) + (pacmacs-down . 503) + (pacmacs-left . 506) + (pacmacs-up . 517) + (pacmacs-right . 519) + (pacmacs-up . 527) + (pacmacs-left . 528) + (pacmacs-right . 536) + (pacmacs-left . 546) + (pacmacs-up . 564) + (pacmacs-right . 571) + (pacmacs-down . 583) + (pacmacs-down . 620) + (pacmacs-left . 625) + (pacmacs-down . 630) + (pacmacs-right . 631) + (pacmacs-left . 664) + (pacmacs-up . 669) + (pacmacs-right . 675) + (pacmacs-down . 683) + (pacmacs-left . 686) + (pacmacs-down . 691) + (pacmacs-left . 696) + (pacmacs-down . 703) + (pacmacs-up . 708) + (pacmacs-right . 714) + (pacmacs-up . 718) + (pacmacs-right . 720) + (pacmacs-right . 724) + (pacmacs-right . 726) + (pacmacs-left . 729) + (pacmacs-down . 729) + (pacmacs-up . 731) + (pacmacs-left . 751) + (pacmacs-up . 758) + (pacmacs-right . 766) + (pacmacs-down . 772) + (pacmacs-left . 774) + (pacmacs-down . 782) + (pacmacs-right . 789) + (pacmacs-left . 807) + (pacmacs-up . 814) + (pacmacs-right . 819) + (pacmacs-left . 849) + (pacmacs-up . 855) + (pacmacs-left . 863) + (pacmacs-right . 870) + (pacmacs-down . 879) + (pacmacs-left . 886) + (pacmacs-right . 890) + (pacmacs-left . 893) + (pacmacs-right . 899) + (pacmacs-left . 902) + (pacmacs-right . 905) + (pacmacs-up . 911) + (pacmacs-right . 915) + (pacmacs-up . 917) + (pacmacs-right . 920) + (pacmacs-left . 926) + (pacmacs-right . 928) + (pacmacs-right . 937) + (pacmacs-left . 941)) diff --git a/tools/it-recorder.el b/tools/it-recorder.el index e314241..f96611e 100644 --- a/tools/it-recorder.el +++ b/tools/it-recorder.el @@ -1,5 +1,7 @@ (require 'pacmacs) +(require 'dash) +(require 'f) (require 'cl-lib) (defvar pacmacs--tick-counter 0) @@ -22,24 +24,29 @@ (insert)) (write-file filename))) +(defun pacmacs--load-test-case (filename) + (-> (f-read-text filename) + (read-from-string) + (car))) + (defun pacmacs-record-up () (interactive) - (pacmacs--record-action 'up) + (pacmacs--record-action 'pacmacs-up) (pacmacs-up)) (defun pacmacs-record-down () (interactive) - (pacmacs--record-action 'down) + (pacmacs--record-action 'pacmacs-down) (pacmacs-down)) (defun pacmacs-record-left () (interactive) - (pacmacs--record-action 'left) + (pacmacs--record-action 'pacmacs-left) (pacmacs-left)) (defun pacmacs-record-right () (interactive) - (pacmacs--record-action 'right) + (pacmacs--record-action 'pacmacs-right) (pacmacs-right)) (defun pacmacs-record-tick () @@ -47,6 +54,17 @@ (cl-incf pacmacs--tick-counter) (pacmacs-tick)) +(defun pacmacs-replay-tick () + (cl-incf pacmacs--tick-counter) + (pacmacs-tick) + + (if (not pacmacs--recorded-actions) + (pacmacs-quit) + (-let ((((action . tick-number) . _) pacmacs--recorded-actions)) + (when (= tick-number pacmacs--tick-counter) + (funcall action) + (setq pacmacs--recorded-actions (cdr pacmacs--recorded-actions)))))) + (define-derived-mode pacmacs-it-recorder-mode pacmacs-mode "pacmacs-it-recorder-mode" (define-key pacmacs-it-recorder-mode-map (kbd "") 'pacmacs-record-up) (define-key pacmacs-it-recorder-mode-map (kbd "") 'pacmacs-record-down) @@ -58,3 +76,11 @@ (pacmacs--initialize-game 'pacmacs-record-tick) (pacmacs-it-recorder-mode) (pacmacs--reset-recorder)) + +(defun pacmacs--start-it-replayer (filename) + (interactive "fLoad test case: ") + (pacmacs--initialize-game 'pacmacs-replay-tick) + (pacmacs-mode) + + (setq pacmacs--recorded-actions (pacmacs--load-test-case filename)) + (setq pacmacs--tick-counter 0)) From a3b2a5ce449ecd5fdf6f4fab10a02e89a4cc3dad Mon Sep 17 00:00:00 2001 From: rexim Date: Mon, 19 Oct 2015 23:53:45 +0600 Subject: [PATCH 2/5] Replace pacmacs--file-content with f-read-text (#122) --- pacmacs-score.el | 3 ++- pacmacs-utils.el | 5 ----- pacmacs.el | 3 ++- test/pacmacs-utils-test.el | 5 ----- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/pacmacs-score.el b/pacmacs-score.el index 64c6e25..75256cf 100644 --- a/pacmacs-score.el +++ b/pacmacs-score.el @@ -32,6 +32,7 @@ ;;; Code: +(require 'f) (require 'dash) (require 'dash-functional) @@ -53,7 +54,7 @@ (defun pacmacs--read-score-table () (when (file-exists-p pacmacs--score-file-name) (-> pacmacs--score-file-name - (pacmacs--file-content) + (f-read-text) (read-from-string) (car)))) diff --git a/pacmacs-utils.el b/pacmacs-utils.el index f4f603d..eccf325 100644 --- a/pacmacs-utils.el +++ b/pacmacs-utils.el @@ -72,11 +72,6 @@ side-effects." (cdr (assoc direction-vector direction-table)))) -(defun pacmacs--file-content (filename) - (with-temp-buffer - (insert-file-contents filename) - (buffer-string))) - (defun pacmacs--levelname-from-filename (filename) (when (string-match "\\(map[0-9]+\\)\\.txt" filename) (match-string 1 filename))) diff --git a/pacmacs.el b/pacmacs.el index 701ded9..9d6b3f4 100644 --- a/pacmacs.el +++ b/pacmacs.el @@ -38,6 +38,7 @@ (require 'cl-lib) (require 'dash) +(require 'f) (require 'pacmacs-anim) (require 'pacmacs-board) @@ -508,7 +509,7 @@ (let* ((lines (split-string (->> map-name (format "./maps/%s.txt") (pacmacs--find-resource-file) - (pacmacs--file-content)) + (f-read-text)) "\n" t)) (board-width (apply 'max (mapcar #'length lines))) (board-height (length lines))) diff --git a/test/pacmacs-utils-test.el b/test/pacmacs-utils-test.el index d323b4f..168dfba 100644 --- a/test/pacmacs-utils-test.el +++ b/test/pacmacs-utils-test.el @@ -10,8 +10,3 @@ (should (equal "map06" (pacmacs--levelname-from-filename "map06.txt"))) (should (not (pacmacs--levelname-from-filename ".")))) -(ert-deftest pacmacs--file-content-test () - (with-mock - (should (equal "hello\n" - (pacmacs--file-content - "test-data/file-with-hello-string.txt"))))) From 09c0a15f40280f67e166f3a83bb7735ffc8fb9b8 Mon Sep 17 00:00:00 2001 From: rexim Date: Sat, 24 Oct 2015 00:05:10 +0600 Subject: [PATCH 3/5] Get pacmacs--object-at-p back (#122) --- pacmacs-board.el | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pacmacs-board.el b/pacmacs-board.el index 837dfa9..68e5714 100644 --- a/pacmacs-board.el +++ b/pacmacs-board.el @@ -69,6 +69,20 @@ (equal object-type type))) cell))) +(defun pacmacs--object-at-p (board row column objects) + (plist-bind ((width :width) + (height :height)) + board + (let ((wrapped-row (mod row height)) + (wrapped-column (mod column width))) + (-find (-lambda (object) + (plist-bind ((object-row :row) + (object-column :column)) + object + (and (= object-row wrapped-row) + (= object-column wrapped-column)))) + objects)))) + (defun pacmacs--step-point (board row column direction) (plist-bind ((width :width) (height :height)) From b0bc866a7bb05bd40d53cac7419db02ef1f765be Mon Sep 17 00:00:00 2001 From: rexim Date: Sat, 24 Oct 2015 00:09:01 +0600 Subject: [PATCH 4/5] Measure tick time during replay (#122) --- pacmacs-utils.el | 6 ++++++ tools/it-recorder.el | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pacmacs-utils.el b/pacmacs-utils.el index eccf325..2c14345 100644 --- a/pacmacs-utils.el +++ b/pacmacs-utils.el @@ -76,6 +76,12 @@ side-effects." (when (string-match "\\(map[0-9]+\\)\\.txt" filename) (match-string 1 filename))) +(defmacro pacmacs--measure-time (&rest body) + "Measure the time it takes to evaluate BODY." + `(let ((time (current-time))) + ,@body + (float-time (time-since time)))) + (provide 'pacmacs-utils) ;;; pacmacs.el ends here diff --git a/tools/it-recorder.el b/tools/it-recorder.el index f96611e..8570a50 100644 --- a/tools/it-recorder.el +++ b/tools/it-recorder.el @@ -6,6 +6,7 @@ (defvar pacmacs--tick-counter 0) (defvar pacmacs--recorded-actions nil) +(defvar pacmacs--tick-times nil) (defun pacmacs--record-action (action-name) (add-to-list 'pacmacs--recorded-actions @@ -56,7 +57,10 @@ (defun pacmacs-replay-tick () (cl-incf pacmacs--tick-counter) - (pacmacs-tick) + + (add-to-list 'pacmacs--tick-times + (pacmacs--measure-time + (pacmacs-tick))) (if (not pacmacs--recorded-actions) (pacmacs-quit) @@ -83,4 +87,5 @@ (pacmacs-mode) (setq pacmacs--recorded-actions (pacmacs--load-test-case filename)) - (setq pacmacs--tick-counter 0)) + (setq pacmacs--tick-counter 0) + (setq pacmacs--tick-times nil)) From daca08b8b152004f89d634f8c0261b8ad3e1e5ed Mon Sep 17 00:00:00 2001 From: rexim Date: Sat, 24 Oct 2015 00:09:36 +0600 Subject: [PATCH 5/5] Add another it case (#122) --- it-cases/it-case03.el | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 it-cases/it-case03.el diff --git a/it-cases/it-case03.el b/it-cases/it-case03.el new file mode 100644 index 0000000..9d38fc4 --- /dev/null +++ b/it-cases/it-case03.el @@ -0,0 +1,63 @@ +((pacmacs-down . 7) + (pacmacs-right . 10) + (pacmacs-down . 14) + (pacmacs-right . 17) + (pacmacs-right . 21) + (pacmacs-left . 25) + (pacmacs-right . 29) + (pacmacs-down . 31) + (pacmacs-right . 35) + (pacmacs-down . 36) + (pacmacs-right . 41) + (pacmacs-up . 45) + (pacmacs-left . 47) + (pacmacs-up . 49) + (pacmacs-left . 52) + (pacmacs-right . 54) + (pacmacs-down . 56) + (pacmacs-left . 61) + (pacmacs-up . 66) + (pacmacs-left . 68) + (pacmacs-down . 73) + (pacmacs-left . 75) + (pacmacs-right . 78) + (pacmacs-up . 81) + (pacmacs-left . 85) + (pacmacs-up . 89) + (pacmacs-left . 92) + (pacmacs-up . 108) + (pacmacs-up . 111) + (pacmacs-up . 114) + (pacmacs-up . 116) + (pacmacs-right . 121) + (pacmacs-down . 124) + (pacmacs-right . 130) + (pacmacs-up . 133) + (pacmacs-right . 139) + (pacmacs-down . 141) + (pacmacs-right . 147) + (pacmacs-up . 149) + (pacmacs-right . 154) + (pacmacs-down . 156) + (pacmacs-left . 159) + (pacmacs-right . 161) + (pacmacs-down . 163) + (pacmacs-left . 166) + (pacmacs-right . 169) + (pacmacs-down . 171) + (pacmacs-right . 175) + (pacmacs-down . 178) + (pacmacs-up . 192) + (pacmacs-up . 214) + (pacmacs-right . 217) + (pacmacs-down . 220) + (pacmacs-right . 225) + (pacmacs-up . 228) + (pacmacs-right . 233) + (pacmacs-down . 236) + (pacmacs-right . 241) + (pacmacs-up . 246) + (pacmacs-down . 281) + (pacmacs-left . 282) + (pacmacs-down . 289) + (pacmacs-right . 291))