From 59d50a694b5b5442fcaaf5e2787ca58e4572a114 Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Mon, 5 Jan 2026 14:11:56 -0500 Subject: [PATCH] start making file source configurable --- drops.lisp | 2 +- main.lisp | 36 +++++++++++++++++++++++++++--------- mobs.lisp | 2 +- utils.lisp | 13 +++++++++++-- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/drops.lisp b/drops.lisp index 036d907..b8cd076 100644 --- a/drops.lisp +++ b/drops.lisp @@ -32,6 +32,6 @@ :if-does-not-exist :create))) (comment - (defvar drops (make-drop-data-source)) + (defvar drops (make-drop-data-source "foo")) (refresh drops) (reload drops)) diff --git a/main.lisp b/main.lisp index 7f68d75..24ed019 100644 --- a/main.lisp +++ b/main.lisp @@ -4,19 +4,37 @@ (defparameter *chronostory-gacha-search-format* "https://chronostory.onrender.com/api/gacha-search?itemId=~a") (defparameter *chronostory-gacha-url-format "https://chronostory.onrender.com/api/gacha-items?gachaId=~a") -(defun main () - (sb-ext:disable-debugger) - (let ((program-name (car sb-ext:*posix-argv*)) - (args (rest sb-ext:*posix-argv*))) - (when (member "--fail" args :test #'string=) - (error "Simulated error!")) - (when (member "--version" args :test #'string=) - (let ((write-time (file-write-date program-name))) +(defun print-version-information () + (let* ((program-name (car sb-ext:*posix-argv*)) + (write-time (file-write-date program-name))) (multiple-value-bind (second minute hour date month year) (decode-universal-time write-time) (let ((dt (format nil "~4,'0d-~2,'0d-~2,'0d ~2,'0d:~2,'0d:~2,'0d" year month date hour minute second))) - (format t "lispostory, created ~a~%" dt))))))) + (format t "lispostory, created ~a~%" dt))))) + +(defun get-arg (opt args) + "Get's the argument for a given command line option. Expects " + (loop :for (current next) :on args + :while next + :when (string-equal current opt) + :return next)) + +(defun refresh-data (data-type) + (string-case data-type + ("drops" (refresh (make-drop-data-source))) + ("items" (refresh (make-item-data-source))) + ("mobs" (refresh (make-mob-data-source))) + ("spawns" (refresh (make-spawn-data-source))))) + +(defun main () + (sb-ext:disable-debugger) + (let ((args (rest sb-ext:*posix-argv*))) + (when (find-string "--fail" args) + (error "Simulated error!")) + (when (find-string "--version" args) + (print-version-information)) + (when-let ((arg (get-arg "refresh")))))) (defun create-exe-and-die () (sb-ext:save-lisp-and-die diff --git a/mobs.lisp b/mobs.lisp index f2e9264..86a4e00 100644 --- a/mobs.lisp +++ b/mobs.lisp @@ -41,5 +41,5 @@ (uiop:println "Done!"))) (comment - (defvar mobs (make-mob-data-source)) + (defvar mobs (make-mob-data-source nil)) (reload mobs)) diff --git a/utils.lisp b/utils.lisp index 5c7f2bb..06a0b2e 100644 --- a/utils.lisp +++ b/utils.lisp @@ -27,10 +27,19 @@ (defun string->bool (s) (if (string-equal (string-downcase s) "true") t nil)) +(defun find-string (str seq) + (sequence:find str seq :test #'string-equal)) + (defclass data-source () ((cache :initform nil :accessor cache) (file-path :initarg :file-path :accessor file-path))) +(defun maybe-pass (func arg) + "Pass ARG to FUNC if arg is not nil." + (if (fnil)arg + (funcall func arg) + (funcall func))) + (defmacro define-data-source (singular-name) (let* ((class-name (symbolicate singular-name '-data-source)) (constructor-name (symbolicate 'make- singular-name '-data-source)) @@ -39,8 +48,8 @@ `(progn (defclass ,class-name (data-source) ()) - (defun ,constructor-name () - (make-instance ',class-name :file-path ,file-name))))) + (defun ,constructor-name (&optional (file-path ,file-name)) + (make-instance ',class-name :file-path file-path))))) (defgeneric data (data-source)) (defmethod data ((ds data-source))