-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcheck-aux.rkt
175 lines (150 loc) · 6.52 KB
/
check-aux.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#lang racket/base
(require racket/class
racket/list
racket/bool
racket/match
htdp/error)
(provide (all-defined-out))
(define INSET 5) ;; the space around the image in the canvas
(define RATE 1/30) ;; the clock tick rate
(define TRIES 3) ;; how many times should register try to connect to the server
(define PAUSE 1/2) ;; # secs to wait between attempts to connect to server
(define SQPORT 4567) ;; the port on which universe traffic flows
;
;
;
; ;;; ;;;
; ; ; ; ;
; ; ; ; ;
; ; ;;; ;;;;; ;;;;; ;;; ;;;; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ;;
; ;;; ;;; ; ; ; ; ; ; ;;; ; ; ; ; ;; ; ; ; ;;
;
;
;
;; ---------------------------------------------------------------------------------------------------
;; Any -> Boolean
(define (nat? x)
(and (number? x) (integer? x) (>= x 0)))
;; Number Symbol Symbol -> Integer
(define (number->integer x [t ""] [p ""])
(check-arg t (and (number? x) (real? x)) "real number" p x)
(inexact->exact (floor x)))
;; ---------------------------------------------------------------------------------------------------
;; Nat Nat ->String
;; converts i to a string, adding leading zeros, make it at least as long as L
(define (zero-fill i L)
(let ([n (number->string i)])
(string-append (make-string (max (- L (string-length n)) 0) #\0) n)))
;; ---------------------------------------------------------------------------------------------------
;; MouseEvent% -> [List Nat Nat MouseEventType]
;; turn a mouse event into its pieces
(define (mouse-event->parts e)
(define x (- (send e get-x) INSET))
(define y (- (send e get-y) INSET))
(values x y
(cond [(send e button-down?) "button-down"]
[(send e button-up?) "button-up"]
[(send e dragging?) "drag"]
[(send e moving?) "move"]
[(send e entering?) "enter"]
[(send e leaving?) "leave"]
[else ; (send e get-event-type)
(let ([m (send e get-event-type)])
(error 'on-mouse (format "Unknown event: ~a" m)))])))
;; KeyEvent% -> String
(define (key-event->parts e)
(define x (send e get-key-code))
(cond
[(char? x) (string x)]
[(symbol? x) (symbol->string x)]
[else (error 'on-key (format "Unknown event: ~a" x))]))
;; KeyEvent% -> String
(define (key-release->parts e)
(define x (send e get-key-release-code))
(cond
[(char? x) (string x)]
[(symbol? x) (symbol->string x)]
[else (error 'on-key (format "Unknown event: ~a" x))]))
;; ---------------------------------------------------------------------------------------------------
;; Any -> Symbol
(define (name-of draw tag)
(define fname (object-name draw))
(if fname fname tag))
;; ---------------------------------------------------------------------------------------------------
;; Any -> Boolean
(define (sexp? x)
(cond
[(empty? x) true]
[(string? x) true]
[(bytes? x) true]
[(symbol? x) true]
[(number? x) true]
[(boolean? x) true]
[(char? x) true]
[(pair? x) (and (list? x) (andmap sexp? x))]
[(and (struct? x) (prefab-struct-key x)) (for/and ((i (struct->vector x))) (sexp? i))]
[else false]))
; tests:
;(struct s (t) #:prefab)
;(unless (sexp? (list (s (list 'a))))
; (error 'prefab "structs should be sexp?"))
(define (no-newline? x)
(not (member #\newline (string->list x))))
;; ---------------------------------------------------------------------------------------------------
;; exchange one-line messages between worlds and the server
(define tcp-eof (gensym 'tcp-eof))
;; Any -> Boolean
(define (tcp-eof? a) (eq? tcp-eof a))
;; OutPort Sexp -> Void
(define (tcp-send out msg)
(write msg out)
(newline out)
(flush-output out))
;; InPort -> Sexp
(define (tcp-receive in)
(with-handlers ((exn? (lambda (x) (raise tcp-eof))))
(define x (read in))
(if (eof-object? x)
(raise tcp-eof)
(begin
(read-line in) ;; read the newline
x))))
;; InPort OutPort (X -> Y) -> (U Y Void)
;; process a registration from a potential client, invoke k on name if it is okay
(define (tcp-process-registration in out k)
(define next (tcp-receive in))
(match next
[`(REGISTER ((name ,name)))
(tcp-send out '(OKAY))
(k name)]))
;; InPort OutPort (U #f String) -> Void
;; register with the server, send the given name or make up a symbol
(define (tcp-register in out name)
(define msg `(REGISTER ((name ,(if name name (gensym 'world))))))
(tcp-send out msg)
(define ackn (tcp-receive in))
(unless (equal? ackn '(OKAY))
(raise tcp-eof)))
;
;
;
; ;;; ;;; ; ;
; ; ; ; ; ; ;
; ; ; ; ; ; ;
; ; ; ; ;; ;;;; ; ;;;; ; ;
; ;;;;; ;; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ;;
; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ;
; ; ; ; ;;;; ;;; ; ; ; ;
; ;
; ; ;
; ;;;
;; Symbol Any String -> Void
(define (check-pos t c r)
(check-arg
t (and (real? c) (>= (number->integer c t r) 0)) "positive integer" r c))