You don't need the ChangeGameState Msg.
You can do what you want by simply computing the new model in FlipCard and
checking to see if you need to change the game state.

update msg ({cards, gameState} as model) =
    case msg of
            FlipCard id message ->
                let
                    newModel = { model | cards = checkCards (updateCards id
message cards) }

                in
                    if allCardsAreFlipped newModel then
                        ({ newModel | gameState = checkGameState
newModel.cards }, Cmd.none)
                    else
                        (newModel, Cmd.none)



If you want to keep the ChangeGameState msg for some other reason, you can
call update recursively with the ChangeGameState msg

update msg ({cards, gameState} as model) =
    case msg of
            FlipCard id message ->
                let
                    newModel = { model | cards = checkCards (updateCards id
message cards) }

                in
                    if allCardsAreFlipped newModel then
                        update (ChangeGameState EndGame) newModel
                    else
                        (newModel, Cmd.none)
            ChangeGameState state ->
                ({ model | gameState = checkGameState cards }, Cmd.none)


If for some reason you still want to trigger a Cmd asynchronously, you can
use something like:

succeedMsg msg =
    Task.perform (always msg) (always msg) (Task.succeed ())







On Thu, Jun 23, 2016 at 9:18 AM, Juan Martin Buireo <[email protected]>
wrote:

> Hi, I am developing a game in Elm 0.17. I am doing a memotest.
>
> I want to create my own Cmd.
>
> Here is what I want to do, I have in my:
>
> type Msg = FlipCard Int Card.Msg
>
> So every time I click on a card, this one calls that msg and flips the
> card.
> In my model, i have the List Card and GameState where
>
> type GameState = InProgress | Finished
>
> So, once all the cards are "found", I want the gameState to change to
> Finished. So I added a new Msg: ChangeGameState GameState
>
> So in my update function I do the following:
> update msg ({cards, gameState} as model) =
> case msg of
>         FlipCard id message -> ({ model | cards = checkCards (updateCards
> id message cards) }, HERE I WANT TO CALL ChangeGameState state ONCE IT
> FINISHEd)
>         ChangeGameState state -> ({ model | gameState = checkGameState
> cards }, Cmd.none)
>
> So, I want in the cmd of FlipCard to call the ChangeGameState state to
> check if the game is finished or not. If I do the checking of the gameState
> in the same line as FlipCard, my game is won once I click again on a card
> found. So I have to do it this way to make it work. But I dont know how to
> create my "own" CMD
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>



-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to