<commit_before>module Game (Model, init, view, update) where

import Card exposing (Card)
import Seat
import View

import Array
import Effects exposing (Effects)
import Html exposing (Html)
import Random
import Random.Array
import Signal
import Time exposing (Time)


type alias Model = Maybe GameState


type alias GameState =
  { hands : Seat.Each (List Card)
  , seed : Random.Seed
  }


type Action
  = Reseed Time


init : (Model, Effects Action)
init =
  ( Nothing, Effects.tick Reseed )


update : Action -> Model -> (Model, Effects Action)
update action _ =
  case action of
    Reseed time ->
      let
        seed = Random.initialSeed (round time)
        (shuffled, seed') = Random.generate (Random.Array.shuffle Card.deck) seed
        cardsPerHand = Array.length shuffled // 4
        takeCards n = Array.toList (Array.slice (cardsPerHand * n) (cardsPerHand * (n + 1)) shuffled)
        hands =
          { west = takeCards 0
          , north = takeCards 1
          , east = takeCards 2
          , south = takeCards 3
          }
        state =
          { hands = hands
          , seed = seed'
          }
      in
        (Just state, Effects.none)


view : Signal.Address Action -> Model -> Html
view address model =
  case model of
    Just state -> viewState state
    Nothing -> Html.div [] []


viewState : GameState -> Html
viewState state =
  let
    emptyCell = Html.td [] []
    seatCell seat = Html.td [] [View.viewHand seat (Seat.lookup seat state.hands)]
  in
    Html.table []
      [ Html.tr [] [ emptyCell, seatCell Seat.North, emptyCell ]
      , Html.tr [] [ seatCell Seat.West, emptyCell, seatCell Seat.East ]
      , Html.tr [] [ emptyCell, seatCell Seat.South, emptyCell ]
      ]<commit_msg>Add button to redeal the cards<commit_after>module Game (Model, init, view, update) where

import Card exposing (Card)
import Seat
import View

import Array
import Effects exposing (Effects)
import Html exposing (Html)
import Html.Events as Events
import Random
import Random.Array
import Signal
import Time exposing (Time)


type alias Model = Maybe GameState


type alias GameState =
  { hands : Seat.Each (List Card)
  , seed : Random.Seed
  }


type Action
  = Reseed Time
  | NewDeal


init : (Model, Effects Action)
init =
  ( Nothing, Effects.tick Reseed )


update : Action -> Model -> (Model, Effects Action)
update action model =
  case (action, model) of
    (Reseed time, _) ->
      let
        seed = Random.initialSeed (round time)
        state = deal seed
      in
        (Just state, Effects.none)
    (NewDeal, Just oldState) ->
      let
        newState = deal oldState.seed
      in
        (Just newState, Effects.none)
    (_, Nothing) ->
      {- TODO: indicate some kind of error for this impossible state -}
      (Nothing, Effects.none)


deal : Random.Seed -> GameState
deal seed =
  let
    (shuffled, seed') = Random.generate (Random.Array.shuffle Card.deck) seed
    cardsPerHand = Array.length shuffled // 4
    takeCards n = Array.toList (Array.slice (cardsPerHand * n) (cardsPerHand * (n + 1)) shuffled)
    hands =
      { west = takeCards 0
      , north = takeCards 1
      , east = takeCards 2
      , south = takeCards 3
      }
    state =
      { hands = hands
      , seed = seed'
      }
  in
    state


view : Signal.Address Action -> Model -> Html
view address model =
  case model of
    Just state -> viewState address state
    Nothing -> Html.div [] []


viewState : Signal.Address Action -> GameState -> Html
viewState address state =
  let
    emptyCell = Html.td [] []
    seatCell seat = Html.td [] [View.viewHand seat (Seat.lookup seat state.hands)]
  in
    Html.div []
      [ Html.table []
          [ Html.tr [] [ emptyCell, seatCell Seat.North, emptyCell ]
          , Html.tr [] [ seatCell Seat.West, emptyCell, seatCell Seat.East ]
          , Html.tr [] [ emptyCell, seatCell Seat.South, emptyCell ]
          ]
      , Html.button [ Events.onClick address NewDeal ] [ Html.text "Rage Quit" ]
      ]
{'fraction_non_alphanumeric': 0.10030015007503752, 'fraction_numerical': 0.0030015007503751876, 'mean_word_length': 2.4238013698630136, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6147118', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1215, 'n_words': 403}