-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.rkt
71 lines (55 loc) · 1.82 KB
/
items.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#lang racket
;;;
;;; Define all items
;;;
(provide (struct-out item$)
get-item
get-item-by-save-id
get-sorted-item-names
item-name->label
item->label
label->item-name
label->item
add-item)
(define logging #t)
(struct item$ (name
id
base-value
flags
label) #:prefab)
(define (item-name->label sym)
(string-replace (string-replace (symbol->string sym) "-" " ") "=" "-"))
(define (item->label item)
(item-name->label (item$-name item)))
(define (label->item-name label)
(string->symbol (string-replace (string-replace label "-" "=") " " "-")))
(define (label->item label)
(get-item (label->item-name label)))
(define items (make-hasheq))
(define (get-item name [default (void)])
(define result (hash-ref items name default))
(when (void? result)
(raise-argument-error 'get-item "defined item name symbol" name))
result)
(define items-by-save-id
(make-hash
(for/list ([i (hash-values items)]
#:unless (void? (item$-id i)))
(cons (item$-id i) i))))
(define (get-item-by-save-id id [default (void)])
(define match (regexp-match "^\\^?([^#]+)(#[0-9]+)?$" id))
(unless match (raise-argument-error 'get-item-by-save-id "item save identifier in form ^?id(#num)?" id))
(define key (second match))
(define result (hash-ref items-by-save-id key default))
(unless result
(when logging
(printf "Item save identifier not found: ~a~n" key)))
result)
(define (get-sorted-item-names)
(sort (hash-keys items) symbol<?))
(define (add-item id item)
(define name (item$-name item))
(define existing-item (hash-ref items name #f))
(unless existing-item
(hash-set! items (item$-name item) item))
(hash-set! items-by-save-id id (or existing-item item)))