~shironeko/lotte

ref: 249929dab2b4479a3d295bba1477476d0f90c511 lotte/lotte-cli.scm -rwxr-xr-x 3.1 KiB
249929dashironeko update readme 11 months ago
                                                                                
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
#! /usr/bin/env -S guile -L . -s
!#

; lotte: The Fair Lottery Program
; Copyright 2020 shironeko
;
; This file is part of lotte.
;
; lotte is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; lotte is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU Affero General Public License for more details.
;
; You should have received a copy of the GNU Affero General Public License
; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(use-modules (lotte main)
             (lotte utils)
             (ice-9 rdelim))


(define (read-num prompt)
  (do ((i "" (read-line)))
    ((and (string->number i)
          (integer? (string->number i))
          (positive? (string->number i)))
          (string->number i))
    (display prompt)))

(define (fill-info mac-key)
  (lambda (p)
    (display "Enter the name of your lottery (needs to be unique each time): ")
    (format p "~A\n" (read-line))
    (format p "~A\n" (read-num "Enter your lucky number: "))
    (format p "~A\n" (read-num "Enter the number of winners: "))
    (format p "~A\n" mac-key)))

(define (get-users p)
  (define (iter users)
    (let ((user (read-line p)))
      (if (eof-object? user)
          users
          (iter (cons user users)))))
  (iter '()))

(define (print-winners winners)
  (if (not (null? winners))
      (let ((winner (car winners)))
        (if (not (null? winner))
            (begin
              (format #t "~A score: ~A\n" (car winner) (cdr winner))
              (print-winners (cdr winners)))))))

(define opt (cdr (command-line)))
(call-with-input-file
  (cond ((null? opt) (let* ((mac-key (gen-mac-key))
                            (lotte-file (string-append "lotte-" (substring mac-key 0 12) ".txt")))
                       (call-with-output-file lotte-file (fill-info mac-key))
                       lotte-file))
        ((string-suffix? ".txt" (car opt)) (car opt))
        (else (let* ((lotte-file (string-append (car opt) ".txt")))
                (call-with-output-file lotte-file (fill-info (gen-mac-key)))
                lotte-file)))
  (lambda (p)
    (let* ((lotte-name (read-line p))
           (lucky-num-str (read-line p))
           (lucky-num (string->number lucky-num-str))
           (num-of-winners-str (read-line p))
           (num-of-winners (string->number num-of-winners-str))
           (mac-key (read-line p))
           (tag (tag-msg mac-key lucky-num-str))
           (users (get-users p)))
      (if (null? users)
          (format #t "The info you need to post:\n~A\nLucky number's MAC: ~A\nNumber of Winners: ~A\n"
                  lotte-name tag num-of-winners-str)
          (let ((winners (lotte lotte-name lucky-num num-of-winners users)))
            (display "The winners are:\n")
            (print-winners winners)
            (format #t "The lucky number is: ~A\n" lucky-num-str)
            (format #t "The MAC key is: ~A\n" mac-key))))))