##// END OF EJS Templates
Fixed saves...
Fixed saves...

File last commit:

r26:fd5a11c5 default
r26:fd5a11c5 default
Show More
api.ps
405 lines | 11.6 KiB | application/postscript | PostScriptLexer
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (in-package sugar-qsp.api)
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
;;; API deals with DOM manipulation and some bookkeeping for the
;;; intrinsics, namely variables
;;; API is an implementation detail and has no QSP documentation. It
;;; doesn't call intrinsics
Tutorial game works!
r6 ;;; Utils
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun make-act-html (title img)
(+ "<a class='qsp-act' href='" (ps-inline call-act) "(\"" title "\");'>"
Tutorial game works!
r6 title
"</a>"))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun make-menu-item-html (num title img loc)
(+ "<a href='" (ps-inline run-menu) "(" num ", \"" loc "\")();'>"
Menu, game saving
r11 "<img src='" img "'>"
title
"</a>"))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun report-error (text)
Finishing lib
r20 (alert text))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun sleep (msec)
(new (*promise (=> resolve (set-timeout resolve msec)))))
WAIT with async
r24
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun init-dom ()
Some DOM stuff, VIEW
r18 ;; Save/load buttons
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (let ((btn (by-id "qsp-btn-save")))
(setf (@ btn onclick) savegame)
(setf (@ btn href) "#"))
(let ((btn (by-id "qsp-btn-open")))
(setf (@ btn onclick) opengame)
(setf (@ btn href) "#"))
Some DOM stuff, VIEW
r18 ;; Close image on click
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ (by-id "qsp-image-container") onclick)
(show-image nil))
Some DOM stuff, VIEW
r18 ;; Close the dropdown on any click
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ window onclick)
Some DOM stuff, VIEW
r18 (lambda (event)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ (get-frame :dropdown) style display) "none"))))
Some DOM stuff, VIEW
r18
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun call-serv-loc (var-name &rest args)
(let ((loc-name (get-var var-name 0 :str)))
Finishing lib
r20 (when loc-name
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (let ((loc (getprop (root locs) loc-name)))
Finishing lib
r20 (when loc
(funcall loc args))))))
Menu, game saving
r11
A better UI
r9 ;;; Misc
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun newline (key)
(append-id (key-to-id key) "<br>" t))
Bugfixes
r22
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun clear-id (id)
(setf (inner-html (by-id id)) ""))
Bugfixes
r22
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defvar text-escaper (chain document (create-element :textarea)))
Bugfixes
r22
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun prepare-contents (s &optional force-html)
(if (or force-html (get-var "USEHTML" 0 :num))
Bugfixes
r22 s
(progn
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ text-escaper text-content) s)
(inner-html text-escaper))))
A better UI
r9
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-id (id &optional force-html)
(inner-html (by-id id)))
A better UI
r9
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun set-id (id contents &optional force-html)
(setf (inner-html (by-id id)) (prepare-contents contents force-html)))
A better UI
r9
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun append-id (id contents &optional force-html)
Bugfixes
r22 (when contents
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (incf (inner-html (by-id id)) (prepare-contents contents force-html))))
A better UI
r9
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1 ;;; Function calls
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun init-args (args)
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1 (dotimes (i (length args))
Properly handle stringly-indexed arrays
r16 (let ((arg (elt args i)))
(if (numberp arg)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (set-var args i :num arg)
(set-var args i :str arg)))))
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-result ()
Fixed saves...
r26 (if (not (equal "" (get-var "RESULT" 0 :str)))
(get-var "RESULT" 0 :str)
(get-var "RESULT" 0 :num)))
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun call-loc (name args)
Remove cl-uglify-js
r23 (with-frame
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (with-call-args args
(funcall (getprop (root locs) name) args))))
Remove cl-uglify-js
r23
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun call-act (title)
Remove cl-uglify-js
r23 (with-frame
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (funcall (getprop (root acts) title 'act))))
The Box bugfixes
r21
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1 ;;; Text windows
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun key-to-id (key)
Tutorial game works!
r6 (case key
(:main "qsp-main")
(:stat "qsp-stat")
Menu, game saving
r11 (:objs "qsp-objs")
(:acts "qsp-acts")
(:input "qsp-input")
(:dropdown "qsp-dropdown")
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (t (report-error "Internal error!"))))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-frame (key)
(by-id (key-to-id key)))
Menu, game saving
r11
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun add-text (key text)
(append-id (key-to-id key) text))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-text (key)
(get-id (key-to-id key)))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun clear-text (key)
(clear-id (key-to-id key)))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun enable-frame (key enable)
(let ((obj (get-frame key)))
(setf (@ obj style display) (if enable "block" "none"))
Menu, game saving
r11 (values)))
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1 ;;; Actions
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun add-act (title img act)
(setf (getprop (root acts) title)
(create img img act act))
(update-acts))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun del-act (title)
(delete (getprop (root acts) title))
(update-acts))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun clear-act ()
(setf (root acts) (create))
(clear-id "qsp-acts"))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun update-acts ()
(clear-id "qsp-acts")
(let ((elt (by-id "qsp-acts")))
(for-in (title (root acts))
(let ((obj (getprop (root acts) title)))
(incf (inner-html elt) (make-act-html title (getprop obj :img)))))))
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
API call for FOR loop to make the main code less cluttered
r19 ;;; "Syntax"
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun qspfor (name index from to step body)
(for ((i from))
((< i to))
((incf i step))
(set-var name index :num i)
(unless (funcall body)
(return-from qspfor))))
API call for FOR loop to make the main code less cluttered
r19
Properly handle stringly-indexed arrays
r16 ;;; Variable class
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun *var (name)
Properly handle stringly-indexed arrays
r16 ;; From strings to numbers
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ this indexes) (create))
Properly handle stringly-indexed arrays
r16 ;; From numbers to {num: 0, str: ""} objects
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ this values) (list))
Properly handle stringly-indexed arrays
r16 (values))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun new-value ()
(create :num 0 :str ""))
100% parser, 100% macros, 50% intrinsics, 10% api, 0% misc
r1
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ *var prototype index-num)
(lambda (index)
(let ((num-index
(if (stringp index)
(if (in index (@ this indexes))
(getprop (@ this indexes) index)
(let ((n (length (@ this values))))
(setf (getprop (@ this indexes) index) n)
n))
index)))
(unless (in num-index (@ this values))
(setf (elt (@ this values) num-index) (new-value)))
num-index)))
Properly handle stringly-indexed arrays
r16
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ *var prototype get)
(lambda (index slot)
(unless (or index (= 0 index))
(setf index (1- (length (@ this values)))))
(getprop (@ this values) (chain this (index-num index)) slot)))
Properly handle stringly-indexed arrays
r16
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ *var prototype set)
(lambda (index slot value)
(unless (or index (= 0 index))
(setf index (length (@ this values))))
(case slot
(:num (setf value (chain *number (parse-int value))))
(:str (setf value (chain value (to-string)))))
(setf (getprop (@ this values)
(chain this (index-num index))
slot) value)
(values)))
Properly handle stringly-indexed arrays
r16
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ *var prototype kill)
(lambda (index)
(setf (elt (@ this values) (chain this (index-num index)))
(new-value))
(delete (getprop 'this 'indexes index))))
Properly handle stringly-indexed arrays
r16
;;; Variables
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun var-real-name (name)
(if (= (@ name 0) #\$)
(values (chain name (substr 1)) :str)
Properly handle stringly-indexed arrays
r16 (values name :num)))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun ensure-var (name)
(let ((store (var-ref name)))
Locals
r14 (unless store
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf store (new (-var name)))
(setf (getprop (root vars) name) store))
Properly handle stringly-indexed arrays
r16 store))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun var-ref (name)
(let ((local-store (current-local-frame)))
The Box bugfixes
r21 (cond ((and local-store (in name local-store))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (getprop local-store name))
Properly handle stringly-indexed arrays
r16 ((in name (root vars))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (getprop (root vars) name))
Locals
r14 (t nil))))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-var (name index slot)
(chain (ensure-var name) (get index slot)))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun set-var (name index slot value)
(chain (ensure-var name) (set index slot value))
Properly handle stringly-indexed arrays
r16 (values))
Tutorial game works!
r6
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun get-array (name)
(var-ref name))
Menu, game saving
r11
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun set-array (name value)
(let ((store (var-ref name)))
(setf (@ store values) (@ value values))
(setf (@ store indexes) (@ value indexes)))
Properly handle stringly-indexed arrays
r16 (values))
Locals
r14
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun kill-var (name &optional index)
Properly handle stringly-indexed arrays
r16 (if (and index (not (= 0 index)))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (chain (getprop (root vars) name) (kill index))
(delete (getprop (root vars) name)))
Tutorial game works!
r6 (values))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun array-size (name)
(getprop (var-ref name) 'length))
Properly handle stringly-indexed arrays
r16
;;; Locals
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun push-local-frame ()
(chain (root locals) (push (create)))
Properly handle stringly-indexed arrays
r16 (values))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun pop-local-frame ()
(chain (root locals) (pop))
Properly handle stringly-indexed arrays
r16 (values))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun current-local-frame ()
Properly handle stringly-indexed arrays
r16 (elt (root locals) (1- (length (root locals)))))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun new-local (name)
(let ((frame (current-local-frame)))
Properly handle stringly-indexed arrays
r16 (unless (in name frame)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (getprop frame name) (create)))
Properly handle stringly-indexed arrays
r16 (values)))
Menu, game saving
r11
Tutorial game works!
r6 ;;; Objects
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun update-objs ()
(let ((elt (by-id "qsp-objs")))
(setf (inner-html elt) "<ul>")
Tutorial game works!
r6 (loop :for obj :in (root objs)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 :do (incf (inner-html elt) (+ "<li>" obj)))
(incf (inner-html elt) "</ul>")))
Menu, game saving
r11
;;; Menu
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun menu (menu-data)
(let ((elt (by-id "qsp-dropdown"))
Menu, game saving
r11 (i 0))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (inner-html elt) "")
Menu, game saving
r11 (loop :for item :in menu-data
:do (incf i)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 :do (incf (inner-html elt) (make-menu-item-html i
(@ item text)
(@ item icon)
(@ item loc))))
(setf (@ elt style display) "block")))
Sounds, save/load UI buttons
r12
;;; Content
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun clean-audio ()
(loop :for k :in (chain *object (keys (root playing)))
:for v := (getprop (root playing) k)
:do (when (@ v ended)
(delete (@ (root playing) k)))))
Locals
r14
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun show-image (path)
(let ((img (by-id "qsp-image")))
Some DOM stuff, VIEW
r18 (cond (path
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ img src) path)
(setf (@ img style display) "flex"))
Some DOM stuff, VIEW
r18 (t
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (setf (@ img src) "")
(setf (@ img style display) "hidden")))))
Some DOM stuff, VIEW
r18
;;; Saves
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun opengame ()
(let ((element (chain document (create-element :input))))
(chain element (set-attribute :type :file))
(chain element (set-attribute :id :qsp-opengame))
(chain element (set-attribute :tabindex -1))
(chain element (set-attribute "aria-hidden" t))
(setf (@ element style display) :block)
(setf (@ element style visibility) :hidden)
(setf (@ element style position) :fixed)
(setf (@ element onchange)
Some DOM stuff, VIEW
r18 (lambda (event)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (let* ((file (@ event target files 0))
(reader (new (*file-reader))))
(setf (@ reader onload)
Some DOM stuff, VIEW
r18 (lambda (ev)
(block nil
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (let ((target (@ ev current-target)))
(unless (@ target result)
(return))
(base64-to-state (@ target result))
(unstash-state)))))
(chain reader (read-as-text file)))))
(chain document body (append-child element))
(chain element (click))
(chain document body (remove-child element))))
Some DOM stuff, VIEW
r18
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun savegame ()
(let ((element (chain document (create-element :a))))
(chain element (set-attribute :href (+ "data:text/plain;charset=utf-8," (state-to-base64))))
(chain element (set-attribute :download "savegame.sav"))
(setf (@ element style display) :none)
(chain document body (append-child element))
(chain element (click))
(chain document body (remove-child element))))
Finishing lib
r20
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun stash-state (args)
(call-serv-loc "ONGSAVE")
Finishing lib
r20 (setf (root state-stash)
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (chain *j-s-o-n (stringify
(create vars (root vars)
objs (root objs)
loc-args args
msecs (- (chain *date (now)) (root started-at))
main-html (inner-html
(by-id :qsp-main))
stat-html (inner-html
(by-id :qsp-stat))
next-location (root current-location)))))
Finishing lib
r20 (values))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun unstash-state ()
(let ((data (chain *j-s-o-n (parse (root state-stash)))))
(clear-act)
(setf (root vars) (@ data vars))
(loop :for k :in (chain *object (keys (root vars)))
:do (chain *object (set-prototype-of (getprop (root vars) k)
(@ *var prototype))))
(setf (root started-at) (- (chain *date (now)) (@ data msecs)))
(setf (root objs) (@ data objs))
(setf (root current-location) (@ data next-location))
(setf (inner-html (by-id :qsp-main))
(@ data main-html))
(setf (inner-html (by-id :qsp-stat))
(@ data stat-html))
(update-objs)
(call-serv-loc "ONGLOAD")
(call-loc (root current-location) (@ data loc-args))
Finishing lib
r20 (values)))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun state-to-base64 ()
Finishing lib
r20 (btoa (encode-u-r-i-component (root state-stash))))
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun base64-to-state (data)
Finishing lib
r20 (setf (root state-stash) (decode-u-r-i-component (atob data))))
;;; Timers
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (defun set-timer (interval)
Finishing lib
r20 (setf (root timer-interval) interval)
(clear-interval (root timer-obj))
(setf (root timer-obj)
(set-interval
(lambda ()
Use Parenscript's minifier and obfuscator... and namespaces. SAVES CURRENTLY BROKEN
r25 (call-serv-loc "COUNTER"))
Finishing lib
r20 interval)))