Browse Source

added new page

added with-alist-fields, agetf, and find-or-intern functions
added route for new page
ava 8 months ago
3 changed files with 92 additions and 6 deletions
  1. +34
  2. +25
  3. +33

+ 34
- 6
src/util.lisp View File

@@ -2,7 +2,8 @@
(defpackage universe-time-calc.util
(:use :cl)
(:export :export-all-symbols
(in-package :universe-time-calc.util)

(defun export-all-symbols (&key (package *package*) (keep-privates t))
@@ -23,9 +24,36 @@ if KEEP-PRIVATES it doesn't export symbols with '--'"

uses *-AMT variables to represent how many of a unit smaller it takes to make that amount
e.g., YEAR=1 YEAR-AMT=14: one year = 14 months"
(format t "~a" sec)
(+ sec
(* min min-amt)
(* (* hour hour-amt) min-amt)
(* (* (* day day-amt) hour-amt) min-amt)
(* (* (* (* month month-amt) day-amt) hour-amt) min-amt)
(* (* (* (* (* year year-amt) month-amt) day-amt) hour-amt) min-amt)))
(* (float min) (float min-amt))
(* (* (float hour) (float hour-amt)) (float min-amt))
(* (* (* (float day) (float day-amt)) (float hour-amt)) (float min-amt))
(* (* (* (* (float month) (float month-amt)) (float day-amt)) (float hour-amt)) (float min-amt))
(* (* (* (* (* (float year) (float year-amt)) (float month-amt)) (float day-amt)) (float hour-amt)) (float min-amt))))

(defmacro with-alist-fields (fields alist &body body)
"takes a list of FIELDS and binds the value from ALIST to variable name
fields can contain strings, and performs all assoc lookups with :test #'equalp
strings that contain an underscore will have their variable names replace the underscore with a hyphen
shadows the ALIST variable with an alist lacking the values requested in FIELDS"
(let ((vars (loop for f in fields
collect (list (find-or-intern f)
`(agetf ,alist ,(if (symbolp f) `(quote ,f) f))))))
`(let ,(append vars (list `(,alist
(remove-if (lambda (a) (member (car a) (quote ,fields) :test #'equalp)) ,alist))))

(defun find-or-intern (symbol &key (package *package*))
"finds SYMBOL in PACKAGE or interns SYMBOL in PACKAGE and then returns it"
(declare (ftype (function (string package) symbol) find-or-intern))
(let ((symbol (string-upcase (str:replace-all "_" "-" symbol)))
(package (typecase package
(symbol (find-package package))
(t package))))
(or (find-symbol symbol package)
(intern symbol package))))

(defun agetf (place indicator &optional default)
(or (cdr (assoc indicator place :test #'equal))

+ 25
- 0
src/web.lisp View File

@@ -7,6 +7,9 @@
(:import-from :universe-time-calc.util
(:export :*web*))
(in-package :universe-time-calc.web)

@@ -27,6 +30,28 @@
(render #P"index.html"
(list :systems (get-system-names))))

(defroute "/add" ()
(render #P"add.html"))

(defroute ("/new" :method :post) (&key _parsed)
(with-alist-fields ("name" "shortcode"
"yr-amt" "mnth-amt" "day-amt" "hr-amt" "min-amt"
"yr-gts" "mnth-gts" "day-gts" "hr-gts" "min-gts" "sec-gts"
"year-one") _parsed
(add-time-system name shortcode
(convert-to-seconds 1 0 0 0 0 0
(read-from-string yr-amt) (read-from-string mnth-amt)
(read-from-string day-amt) (read-from-string hr-amt)
(read-from-string min-amt))
(convert-to-seconds (read-from-string yr-gts) (read-from-string mnth-gts)
(read-from-string day-gts) (read-from-string hr-gts)
(read-from-string min-gts) (read-from-string sec-gts)
(read-from-string yr-amt) (read-from-string mnth-amt)
(read-from-string day-amt) (read-from-string hr-amt)
(read-from-string min-amt))
(redirect "/"))

;; Error pages

+ 33
- 0
templates/add.html View File

@@ -0,0 +1,33 @@
{% extends "layouts/default.html" %}
{% block title %}Add New Time System{% endblock %}
{% block content %}
<div id="main">

<form method="POST" action="/new">

<input type="text" name="name" placeholder="Time System Name"> <br/>
<input type="text" name="shortcode" placeholder="Time System Shortcode"> <br/>
<input type="text" name="year-one" placeholder="Time System GTS Start Date"> <br/>

Months in a Year: <input type="number" name="yr-amt" value="0"> <br/>
Days in a Month: <input type="number" name="mnth-amt" value="0"> <br/>
Hours in a Day: <input type="number" name="day-amt" value="0"> <br/>
Minutes in an Hour: <input type="number" name="hr-amt" value="0"> <br/>
Seconds in a Minute: <input type="number" name="min-amt" value="0"> <br/>

<h2>Conversion to GTS</h2>
Year: <input type="number" name="yr-gts" value="0"> <br/>
Month: <input type="number" name="mnth-gts" value="0"> <br/>
Day: <input type="number" name="day-gts" value="0"> <br/>
Hour: <input type="number" name="hr-gts" value="0"> <br/>
Minute: <input type="number" name="min-gts" value="0"> <br/>
Seconds: <input type="number" name="sec-gts" value="0"> <br/>

<input type="submit" value="Add!">
{% endblock %}