effect.scm 6.7 KB

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