1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; A game database that uses SCID for storage/retrieval
5 ;; The advantage is that it's much faster than PGN, and far, far more
9 (defvar chess-scid-process)
11 (make-variable-buffer-local 'chess-scid-process)
13 (defsubst chess-scid-send (string)
14 (process-send-string chess-scid-process (concat string "\n")))
16 (defun chess-scid-get-result (command)
17 (let ((here (point-max)))
18 (chess-scid-send command)
19 (accept-process-output chess-scid-process)
20 (goto-char (point-max))
21 (while (memq (char-before) '(? ?\t ?\n ?\r ?\%))
23 (buffer-substring here (point))))
25 (defun chess-scid-handler (event &rest args)
28 (if (file-readable-p (concat (car args) ".sg3"))
29 (let* ((buffer (generate-new-buffer " *chess-scid*"))
30 (proc (start-process "*chess-scid*" buffer
31 (executable-find "tcscid"))))
32 (if (and proc (eq (process-status proc) 'run))
33 (with-current-buffer buffer
34 (accept-process-output proc)
35 (setq chess-scid-process proc)
36 (if (= 1 (string-to-number
37 (chess-scid-get-result
38 (format "sc_base open %s"
39 (expand-file-name (car args))))))
48 (chess-scid-send "sc_base close\nexit")
49 (while (eq (process-status chess-scid-process) 'run)
52 ((eq event 'read-only-p)
53 (if (zerop (string-to-number (chess-scid-get-result "sc_base isReadOnly")))
58 (chess-scid-get-result "sc_base filename"))
61 (string-to-number (chess-scid-get-result "sc_base numGames")))
64 (let ((here (point-max)) game)
65 (process-send-string chess-scid-process
66 (format "sc_game load %d\nsc_game pgn\n"
68 (accept-process-output chess-scid-process)
70 (when (setq game (chess-pgn-to-game))
71 (chess-game-set-data game 'database (current-buffer))
72 (chess-game-set-data game 'database-index (car args))
73 (chess-game-set-data game 'database-count (chess-scid-handler 'count))
76 ((and (eq event 'query)
77 (eq (car args) 'tree-search))
79 (chess-scid-get-result
80 (format "sc_game import \"%s\""
82 (chess-pgn-insert-plies
83 (cadr args) 1 (chess-game-plies (cadr args)))
84 (insert " " (or (chess-game-tag (cadr args) "Result") "*"))
86 "PGN text imported with no errors or warnings.")
87 (let ((here (point-max)))
88 (chess-scid-send "sc_tree search")
89 (accept-process-output chess-scid-process)
91 (let ((lines (list t)))
94 (concat "\\s-*\\([1-9][0-9]*\\):\\s-+\\([^ ]+\\)\\s-+"
95 "\\([0-9]+\\):\\s-*\\([0-9.]+\\)%\\s-+"
96 "\\([0-9.]+\\)%\\s-+\\([0-9]+\\| \\)\\s-+"
97 "\\([0-9]+\\| \\)\\s-+\\([0-9]+\\| \\)\\s-+"
100 (let ((move (match-string 2))
101 (freq (string-to-number (match-string 3)))
102 (score (string-to-number (match-string 5)))
103 (avgelo (string-to-number (match-string 6)))
104 (perf (string-to-number (match-string 7)))
105 (avgyear (string-to-number (match-string 8)))
106 (draws (string-to-number (match-string 9))))
110 (list move freq score draws)
112 ((and (zerop avgyear) (zerop avgelo) (zerop perf))
114 ((and (zerop avgelo) (zerop perf))
118 (unless (zerop avgyear) avgyear)
119 (unless (zerop avgelo) avgelo)
120 (unless (zerop perf) perf)))))))))
121 (when (re-search-forward
122 (concat "TOTAL:\\s-+\\([0-9]+\\):100.0%\\s-+"
123 "\\([0-9.]+\\)%\\s-+\\([0-9]+\\| \\)\\s-+"
124 "\\([0-9]+\\| \\)\\s-+\\([0-9]+\\| \\)\\s-+"
125 "\\([0-9.]+\\)%") nil t)
126 (goto-char (point-max))
128 (list (string-to-number (match-string 1))
129 (string-to-number (match-string 2))
130 (string-to-number (match-string 6)))
132 (error "Unable to import game")))
135 (chess-scid-handler 'replace (car args) 0))
138 (unless (chess-scid-handler 'read-only-p)
139 (let ((index (or (cadr args)
140 (chess-game-data (car args) 'database-index))))
142 (format "sc_game import \"%s\""
144 (chess-pgn-insert-plies
145 (car args) 1 (chess-game-plies (car args)))
146 (insert (or (chess-game-tag (car args) "Result") "*"))
148 (dolist (tag (chess-game-tags (car args)))
149 ;; jww (2002-05-01): how do I set extra tags?
150 (unless (string= (car tag) "TimeControl")
152 (concat "sc_game tags set "
154 ((string= (car tag) "Event") "-event")
155 ((string= (car tag) "Site") "-site")
156 ((string= (car tag) "Date") "-date")
157 ((string= (car tag) "Round") "-round")
158 ((string= (car tag) "White") "-white")
159 ((string= (car tag) "WhiteElo") "-whiteElo")
160 ((string= (car tag) "Black") "-black")
161 ((string= (car tag) "BlackElo") "-blackElo")
162 ((string= (car tag) "Result") "-result")
163 ((string= (car tag) "ECO") "-eco")
164 ((string= (car tag) "EventDate") "-eventdate")
165 ((string= (car tag) "Extra") "-extra"))
166 " \"" (cdr tag) "\""))))
167 (chess-scid-send (format "sc_game save %d" index)))))))
169 (provide 'chess-scid)
171 ;;; chess-scid.el ends here