effect.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. (library (effect)
  2. (export query-user-for-new-route-part
  3. query-user-for-custom-action
  4. modify-route
  5. ;; effects
  6. journey-delay-effect
  7. additional-costs-effect
  8. unusable-effect
  9. route-change-effect
  10. disable-other-events-effect)
  11. (import
  12. (except (rnrs base) let-values map error)
  13. (only (guile)
  14. lambda* λ)
  15. (srfi srfi-1)
  16. (srfi srfi-9 gnu)
  17. (user-input-output)
  18. (lib list-procs)
  19. (lib alist-procs)
  20. (lib string-procs)
  21. (prefix (lib logger) log:)
  22. (model)
  23. (data-abstraction)))
  24. (define query-user-for-effects
  25. (λ ()
  26. (log:debug "querying for custom effects")
  27. (let ([known-effects '("delay" "additional-costs" "route-change")])
  28. (list->vector
  29. (filter (λ (eff)
  30. (ask-user-for-yes-no-decision
  31. (string-format "Does your action cause ~a?" eff)
  32. '("y" "yes") '("n" "no")))
  33. known-effects)))))
  34. (define query-user-for-custom-action
  35. (λ ()
  36. (log:debug "querying for custom action")
  37. (confirm-info-message "Please describe your action.")
  38. (let ([label (ask-user-for-text "label")]
  39. [karma
  40. (ask-user-for-number "karma (number, [-5,+5])"
  41. (λ (num) (and (>= num -5) (<= num 5))))]
  42. [description (ask-user-for-text "description")]
  43. [effects (query-user-for-effects)])
  44. `(("label" . ,label)
  45. ("karma" . ,karma)
  46. ("description" . ,description)
  47. ("effects" . ,effects)))))
  48. (define query-user-for-new-route-part
  49. (λ (story-params)
  50. (let ([from-location (ask-user-for-text "From?")]
  51. [to-location (ask-user-for-text "To?")]
  52. [transportation
  53. (let ([means-of-transportation
  54. (get-all-means-of-transportation story-params)])
  55. (ask-user-for-decision-with-continuations
  56. "Means of transportation?"
  57. (map number->string
  58. (range 1 (length means-of-transportation) #:end-inclusive #t))
  59. means-of-transportation
  60. (map (λ (transp) (λ () transp)) means-of-transportation)))
  61. #;(ask-user-for-decision "Means of transportation?"
  62. (get-all-means-of-transportation story-params))])
  63. `(("from" . ,from-location)
  64. ("to" . ,to-location)
  65. ("transportation" . ,transportation)))))
  66. (define modify-route
  67. (λ (route story-params)
  68. (let next-change ([modified-route route])
  69. (confirm-info-message (string-format "Currently planned route:\n~a" (route->string modified-route)))
  70. (let ([choice-numbers
  71. (if (null? modified-route) '("1" "2" "3") '("1" "2" "3" "4"))]
  72. [choice-texts
  73. (if (null? modified-route)
  74. '("add route part"
  75. "specify new route"
  76. "finish modifying route")
  77. '("remove first route part"
  78. "prepend route part"
  79. "specify new route"
  80. "finish modifying route"))]
  81. [choice-actions
  82. (if (null? modified-route)
  83. (list (λ ()
  84. (next-change (cons (query-user-for-new-route-part story-params) modified-route)))
  85. (λ ()
  86. (confirm-info-message "not yet implemented") (next-change modified-route))
  87. (λ ()
  88. (let ([response (ask-user-for-yes-no-decision
  89. "Are you sure you are finished modifying your route?"
  90. '("y" "yes") '("n" "no"))])
  91. (if response modified-route (next-change modified-route)))))
  92. (list (λ ()
  93. (next-change (rest modified-route)))
  94. (λ ()
  95. (next-change (cons (query-user-for-new-route-part story-params) modified-route)))
  96. (λ ()
  97. (confirm-info-message "not yet implemented") (next-change modified-route))
  98. (λ ()
  99. (let ([response (ask-user-for-yes-no-decision
  100. "Are you sure you are finished modifying your route?"
  101. '("y" "yes")
  102. '("n" "no"))])
  103. (if response modified-route (next-change modified-route))))))])
  104. (ask-user-for-decision-with-continuations
  105. "What do you want to do?" choice-numbers choice-texts choice-actions)))))
  106. ;; =================
  107. ;; EFFECT PROCEDURES
  108. ;; =================
  109. (define journey-delay-effect
  110. (λ (event-outcome traveler-state transportation-configs)
  111. (set-event-outcome-additional-delay
  112. event-outcome
  113. (+ (event-outcome-additional-delay event-outcome)
  114. (ask-user-for-number "How much delay was caused by this?"
  115. (λ (num) #t))))))
  116. (define additional-costs-effect
  117. (λ (event-outcome traveler-state transportation-configs)
  118. (set-event-outcome-additional-costs
  119. event-outcome
  120. (+ (event-outcome-additional-costs event-outcome)
  121. (ask-user-for-number "How much additional costs were caused by this?"
  122. (λ (num) #t))))))
  123. (define unusable-effect
  124. (λ (event-outcome traveler-state transportation-configs)
  125. (confirm-info-message "The event rendered your current means of transporation unusable.")
  126. ;; NOTE: Perhaps we need to implement something here,
  127. ;; but currently the "unusable" effect functionality is
  128. ;; covered by the route-change effect.
  129. event-outcome))
  130. (define route-change-effect
  131. (λ (event-outcome traveler-state transportation-configs)
  132. (confirm-info-message "The event requires you to change your route.")
  133. (let ([route (traveler-route traveler-state)])
  134. (confirm-info-message (string-format "Your current route is:\n~a" (route->string route)))
  135. (confirm-info-message (string-format "The first route part will be removed: ~a" (route-part->string (first route))))
  136. (confirm-info-message
  137. (string-format
  138. "Your current location is between ~a and ~a."
  139. (alist-ref (first route) "from")
  140. (alist-ref (first route) "to")))
  141. (set-fields event-outcome
  142. ((event-outcome-updated-route)
  143. (modify-route (rest route) transportation-configs))
  144. ((event-outcome-route-changed)
  145. #t)))))
  146. (define disable-other-events-effect
  147. (λ (event-outcome traveler-state transportation-configs)
  148. (confirm-info-message
  149. "(The event disables other events of the same event group.).")
  150. (set-event-outcome-disable-event-group event-outcome #t)))