<commit_before>module Update exposing (..)

import Model exposing (Model)
import Msg exposing (Msg(..))
import Ports
import Mouse
import Math.Vector2 as V2 exposing (Vec2, vec2)
import Array exposing (Array)
import Array.Extra
import Random
import Extras
import Material
import PlayfieldComponents exposing (Piece, PieceType(..), Spaces, SpaceType(..))


update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
    case message of
        PlayClack ->
            model ! [ Ports.sound "clack" ]

        SelectPiece id ->
            { model | pieceSelected = Just id } ! []

        MovePiece pieceId spaceId ->
            { model
                | pieceSelected = Nothing
                , pieces =
                    setPieceLocation model.pieces pieceId
                        <| Array.get spaceId model.spaces.positions
            }
                ! []

        GenerateBoard ->
            let
                ( spaces, postSpacesSeed ) =
                    PlayfieldComponents.makeSpaces model.gridWidth
                        model.gridHeight
                        model.spaceDeck
                        model.seed

                ( pieces, newSeed ) =
                    PlayfieldComponents.makePieces model.spaces
                        model.pieceDeck
                        postSpacesSeed
            in
                { model | seed = newSeed, spaces = spaces, pieces = pieces } ! []

        SelectTab tabIndex ->
            { model | tabIndex = tabIndex } ! []

        SpaceDeckIncrement item ->
            { model | spaceDeck = item :: model.spaceDeck } ! []

        SpaceDeckDecrement item ->
            { model | spaceDeck = Extras.remove item model.spaceDeck } ! []

        Animate _ ->
            model ! []

        GetSeed time ->
            { model
                | seed = Random.initialSeed <| Debug.log "seed" <| round time
            }
                ! []

        -- When the `Mdl` messages come through, update appropriately.
        Mdl msg' ->
            -- Material.update msg'
            model ! []


setPieceLocation : Array Piece -> Int -> Maybe Vec2 -> Array Piece
setPieceLocation pieces pieceId maybePosition =
    case maybePosition of
        Just position ->
            Array.Extra.update pieceId
                (\piece ->
                    { piece | position = position }
                )
                pieces

        Nothing ->
            pieces


v2FromPosition : Mouse.Position -> Vec2
v2FromPosition position =
    vec2 (toFloat position.x) (toFloat position.y)
<commit_msg>fix pieces on empty spaces bug<commit_after>module Update exposing (..)

import Model exposing (Model)
import Msg exposing (Msg(..))
import Ports
import Mouse
import Math.Vector2 as V2 exposing (Vec2, vec2)
import Array exposing (Array)
import Array.Extra
import Random
import Extras
import Material
import PlayfieldComponents exposing (Piece, PieceType(..), Spaces, SpaceType(..))


update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
    case message of
        PlayClack ->
            model ! [ Ports.sound "clack" ]

        SelectPiece id ->
            { model | pieceSelected = Just id } ! []

        MovePiece pieceId spaceId ->
            { model
                | pieceSelected = Nothing
                , pieces =
                    setPieceLocation model.pieces pieceId
                        <| Array.get spaceId model.spaces.positions
            }
                ! []

        GenerateBoard ->
            let
                ( spaces, postSpacesSeed ) =
                    PlayfieldComponents.makeSpaces model.gridWidth
                        model.gridHeight
                        model.spaceDeck
                        model.seed

                ( pieces, newSeed ) =
                    PlayfieldComponents.makePieces spaces
                        model.pieceDeck
                        postSpacesSeed
            in
                { model | seed = newSeed, spaces = spaces, pieces = pieces } ! []

        SelectTab tabIndex ->
            { model | tabIndex = tabIndex } ! []

        SpaceDeckIncrement item ->
            { model | spaceDeck = item :: model.spaceDeck } ! []

        SpaceDeckDecrement item ->
            { model | spaceDeck = Extras.remove item model.spaceDeck } ! []

        Animate _ ->
            model ! []

        GetSeed time ->
            { model
                | seed = Random.initialSeed <| Debug.log "seed" <| round time
            }
                ! []

        -- When the `Mdl` messages come through, update appropriately.
        Mdl msg' ->
            -- Material.update msg'
            model ! []


setPieceLocation : Array Piece -> Int -> Maybe Vec2 -> Array Piece
setPieceLocation pieces pieceId maybePosition =
    case maybePosition of
        Just position ->
            Array.Extra.update pieceId
                (\piece ->
                    { piece | position = position }
                )
                pieces

        Nothing ->
            pieces


v2FromPosition : Mouse.Position -> Vec2
v2FromPosition position =
    vec2 (toFloat position.x) (toFloat position.y)

{'fraction_non_alphanumeric': 0.0817952205168059, 'fraction_numerical': 0.003497182824946571, 'mean_word_length': 1.4203102961918195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, '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': '3452009', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1249, 'n_words': 359}