-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremote-game.rkt
executable file
·72 lines (63 loc) · 2.18 KB
/
remote-game.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
#! /bin/sh
#|
exec racket -tm "$0" ${1+"$@"}
|#
#lang racket
;; ---------------------------------------------------------------------------------------------------
;; run many tests on remote server; starts remote server and then runs a bunch of plain/bad samples
(provide
;; $ ./remote-game n
;; runs n random games via localhost, if n > 1 it also tests some really bad 'interactors'
(rename-out (run main)))
;; ---------------------------------------------------------------------------------------------------
;; IMPLEMENTATION
(require "server-start.rkt" "player-factory.rkt" "remote-admin.rkt")
(define HOST "127.0.0.1")
(define PORT "51239")
(define PLAYERS# "4")
;; Nat -> Void
;; run n rounds of random players with one ordered one and then some games with ill-behaving players
(define (run n:string)
(define n (string->number n:string))
;; launch server
(define c (make-custodian))
(parameterize ((current-custodian c))
(void (thread (lambda () (main PORT PLAYERS#))))
(sleep 1)
;; launch specified number of games
(for ((i (in-range n)))
(go (first (random-players 1))))
;; launch bad games
(when (> n 1)
(go (merge-bad-player))
(go (keep-bad-player))
(go (end-bad-player))
(go (receive-bad-player))
(go (inform-bad-player))
(go (setup-bad-player))
(go (inf-loop-player 1))))
;; tear down the server
(sleep 1)
(custodian-shutdown-all c))
(define (go extra)
(define c (make-custodian))
(define all-threads
(parameterize ((current-custodian c))
(for/list ((p (cons extra (random-players 3))))
(thread
(lambda ()
(define-values (status score _state) (make-player HOST PORT p))
score)))))
;; wait for a thread to shut down
(let wait ((l all-threads))
(when (boolean? (apply sync/timeout 60 l)) (wait l)))
;; then clean up
(sleep 1)
(custodian-shutdown-all c))
(define (make-player host port player)
(define-values (in out) (tcp-connect host (string->number port)))
(define remote-administrator (new remote-administrator% [in in][out out]))
(send player go remote-administrator)
(send remote-administrator run 0))
(module+ test
(run "1"))