Guest User

Untitled

a guest
Jun 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. module Captured exposing (..)
  2.  
  3. import Html exposing (Html, beginnerProgram, br, div, text, button)
  4. import Html.Attributes exposing (style)
  5. import Html.Events exposing (onClick)
  6.  
  7. import Select
  8. import Random
  9.  
  10.  
  11. -- MODEL
  12.  
  13. type alias Model =
  14. { ball : Ball
  15. , ballHPFactor : BallHPFactor
  16. , ballRNG : Int
  17. , status : Status
  18. , statusFactor : StatusFactor
  19. , catchRate : Int
  20. , currentHP : Int
  21. , maxHP : Int
  22. , captured : Captured
  23. }
  24.  
  25. type Ball
  26. = Pokeball
  27. | Greatball
  28. | Ultraball
  29. | Masterball
  30.  
  31. type BallHPFactor
  32. = PokeballHPFactor
  33. | GreatballHPFactor
  34. | NoBallHPFactor
  35.  
  36. type Status
  37. = Asleep
  38. | Burned
  39. | Frozen
  40. | Paralysed
  41. | Poisoned
  42. | NoStatus
  43.  
  44. type StatusFactor
  45. = HighStatusFactor
  46. | MidStatusFactor
  47. | NoStatusFactor
  48.  
  49. type Captured
  50. = Caught
  51. | Escaped
  52.  
  53. type Msg
  54. = NewBall Ball
  55. | NewStatus Status
  56. | Throw
  57. | UpdateBallRNGValue Int
  58.  
  59. emptyModel : Model
  60. emptyModel =
  61. { ball = Pokeball
  62. , ballHPFactor = PokeballHPFactor
  63. , ballRNG = 0
  64. , status = NoStatus
  65. , statusFactor = NoStatusFactor
  66. , catchRate = 255
  67. , currentHP = 100
  68. , maxHP = 100
  69. , captured = Escaped
  70. }
  71.  
  72. init : (Model, Cmd Msg)
  73. init =
  74. ( emptyModel
  75. , ballRNGCmd Pokeball
  76. )
  77.  
  78. -- UPDATE
  79. update : Msg -> Model -> (Model, Cmd Msg)
  80. update msg model =
  81. (case msg of
  82. NewBall newBall ->
  83. let
  84. newModel =
  85. updateBall newBall model
  86. in
  87. (newModel, ballRNGCmd newBall)
  88. NewStatus newStatus ->
  89. let
  90. newModel =
  91. updateStatus newStatus model
  92. in
  93. (newModel, Cmd.none)
  94. Throw ->
  95. let
  96. newModel =
  97. wasItCaptured model
  98. in
  99. (newModel, Cmd.none)
  100. |> Debug.log "update.result"
  101.  
  102. UpdateBallRNGValue ballRNGValue ->
  103. let
  104. newModel =
  105. { model | ballRNG = ballRNGValue }
  106. in
  107. ( newModel, Cmd.none )
  108.  
  109. )
  110.  
  111.  
  112. -- VIEW
  113. view : Model -> Html Msg
  114. view model =
  115. let
  116. balls =
  117. [ Pokeball, Greatball, Ultraball, Masterball ]
  118. statuses =
  119. [ NoStatus, Asleep, Burned, Frozen, Paralysed, Poisoned ]
  120. in
  121. div
  122. [ style [ ( "padding", "5rem" ) ] ]
  123. [ text <| "Calculator"
  124. , br [] []
  125. , Select.from balls NewBall
  126. , Select.from statuses NewStatus
  127. , button [ onClick Throw ] [ text "Throw" ]
  128. ]
  129.  
  130.  
  131. -- FUNCTIONS
  132.  
  133. ballRNGCmd : Ball -> Cmd Msg
  134. ballRNGCmd ball =
  135. Random.generate UpdateBallRNGValue (ballRNGGenerator ball)
  136.  
  137. ballRNGGenerator : Ball -> Random.Generator Int
  138. ballRNGGenerator ball =
  139. let
  140. upperLimit =
  141. ballRNGLimitValueFromBall(ball)
  142. in
  143. Random.int 0 upperLimit
  144.  
  145. ballRNGLimitValueFromBall : Ball -> Int
  146. ballRNGLimitValueFromBall ball =
  147. case ball of
  148. Pokeball ->
  149. 255
  150. Greatball ->
  151. 200
  152. Ultraball ->
  153. 150
  154. _ ->
  155. 0
  156.  
  157. statusValueFromStatusFactor : StatusFactor -> Int
  158. statusValueFromStatusFactor statusFactor =
  159. case statusFactor of
  160. HighStatusFactor ->
  161. 25
  162. MidStatusFactor ->
  163. 12
  164. _ ->
  165. 0
  166.  
  167. wasItCaptured : Model -> Model
  168. wasItCaptured model =
  169. case model.ball of
  170. Masterball ->
  171. { model | captured = Caught }
  172. _ ->
  173. let
  174. -- If the RNG ball value is less that the status factor, immediately caught.
  175. calculation_r_star = model.ballRNG - statusValueFromStatusFactor(model.statusFactor)
  176. in
  177. case (calculation_r_star < 0) of
  178. True ->
  179. { model | captured = Caught}
  180. False ->
  181. { model | captured = Escaped}
  182.  
  183.  
  184. updateBall : Ball -> Model -> Model
  185. updateBall ball model =
  186. let
  187. ballHPFactorValue =
  188. ballHPFactorValueFromBall(ball)
  189. in
  190. { model | ball = ball, ballHPFactor = ballHPFactorValue }
  191.  
  192. ballHPFactorValueFromBall : Ball -> BallHPFactor
  193. ballHPFactorValueFromBall ball =
  194. case ball of
  195. Pokeball ->
  196. PokeballHPFactor
  197. Greatball ->
  198. GreatballHPFactor
  199. _ ->
  200. NoBallHPFactor
  201.  
  202. updateStatus : Status -> Model -> Model
  203. updateStatus status model =
  204. let
  205. statusFactorValue =
  206. statusFactorValueFromStatus(status)
  207. in
  208. { model | status = status, statusFactor = statusFactorValue }
  209.  
  210. statusFactorValueFromStatus : Status -> StatusFactor
  211. statusFactorValueFromStatus status =
  212. case status of
  213. NoStatus ->
  214. NoStatusFactor
  215. Asleep ->
  216. HighStatusFactor
  217. Frozen ->
  218. HighStatusFactor
  219. _ ->
  220. MidStatusFactor
Add Comment
Please, Sign In to add comment