<commit_before>module Acquire exposing (main)

{-| Comment
@docs main
-}

import Char
import Random
import Platform.Sub as Sub
import Debug
import String
import Json.Decode as Json
import Json.Encode as Json
import Html exposing (..)
import Html.Attributes exposing (href, src, placeholder, min, max, value, id, class, type')
import Html.Attributes as A
import Html.Events exposing (..)
import Html.App as App
import WebSocket exposing (..)
import Messages exposing (..)
import Dict

{-| Main -}
main : Program Never
main =
  App.program
    { init          = init
    , view          = view
    , update        = update
    , subscriptions = subscriptions
    }

type alias Model = { strings : List String, showMessages: Bool
                   , games : List GameDescription
                   , numPlayers : Int, numRobots : Int
                   , board : GameBoard, possiblePlays : List Messages.Order, player : Player
                   , errors : List String
                   , gameResult : Maybe Players
                   , wsServerUrl : String
                   }

type Msg = Output String
         | UseKey String
         | SetName String
         | ListGames
         | Join GameId
         | CreateGame
         | Play Int
         | SetNumPlayers String
         | SetNumRobots String
         | ShowMessages Bool
         | Reset

subscriptions model =
    Debug.log "subscribing to WS "<|
        Sub.batch [ listen model.wsServerUrl Output ]
           
init : (Model, Cmd Msg)
init =
    let randomClientKey = Random.map String.fromList (Random.list 16 <| Random.map Char.fromCode (Random.int 65 90))
    in (Model [] True [] 1 5 Dict.empty [] (player "") [] Nothing ""
       , Random.generate UseKey randomClientKey)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Output s        -> handleServerMessages model s
        UseKey k        -> ({model | wsServerUrl = "ws://localhost:9090/" ++ k}, Cmd.none)
        SetName s       -> ({model | player = player s},Cmd.none)
        ShowMessages b  -> ({model | showMessages = b },Cmd.none)
        SetNumPlayers s -> case String.toInt s of
                               Ok i -> ({model | numPlayers = i},Cmd.none)
                               _    -> (model,Cmd.none)
        SetNumRobots s  -> case String.toInt s of
                               Ok i -> ({model | numRobots = i},Cmd.none)
                               _    -> (model,Cmd.none)
        ListGames       -> (model, sendCommand model List)
        Play n          -> ({model| possiblePlays = []}, sendCommand model (Action { selectedPlay = n }))
        Join g          -> (model, sendCommand model (JoinGame { playerName = model.player.playerName, gameId =  g}))
        CreateGame      -> (model, sendCommand model (NewGame { numHumans = model.numPlayers, numRobots = model.numRobots}))
        Reset           -> ({model
                                | strings = []
                                , board = Dict.empty, possiblePlays = [], player = player (model.player.playerName)
                                , errors = []
                                , gameResult = Nothing}, Cmd.none)

handleServerMessages : Model -> String -> (Model, Cmd Msg)
handleServerMessages model s =
    case Json.decodeString decodeServerMessages s of
        Ok (R (GamesList l)) ->
            ({model | games = l}, Cmd.none)
        Ok (R (NewGameStarted _)) ->
            (model, sendCommand model List)
        Ok (R (PlayerRegistered n gid)) ->
            (model, sendCommand model List)
        Ok (R (GameStarts gid)) ->
            (model, sendCommand model List)
        Ok (R (ErrorMessage m)) ->
            ({model | errors = m :: model.errors}, Cmd.none)
        Ok (M (GameState gs)) ->
            ({model | board = gs.gsBoard, possiblePlays = gs.gsPlayables, player = gs.gsPlayer}, Cmd.none)
        Ok (M (Played pl)) ->
            ({model | board = pl.gsBoard, possiblePlays = [], strings = showOrder pl.gsPlayed :: model.strings }, Cmd.none)
        Ok (M (GameEnds g)) ->
            ({model | board = g.gsEndGame.gameBoard
             , possiblePlays = []
             , gameResult = Just g.gsEndGame.players }, Cmd.none)
        _                ->
            ({model | strings = s :: model.strings}, Cmd.none)
                
sendCommand : Model -> Message -> Cmd Msg
sendCommand model m = send model.wsServerUrl (Json.encode 0 <| encodeMessage m)
                
view : Model -> Html Msg
view model = div []
             [ displayErrors model
             , playerInput model
             , gamesList model
             , gameBoard model
             , gameResult model
             , messages model
             ]

messages: Model -> Html Msg
messages model =
    let toggle = if model.showMessages
                 then span [ class "fa fa-toggle-down", onClick <| ShowMessages False ] []
                 else span [ class "fa fa-toggle-right", onClick <| ShowMessages True ] []
        doDisplayList = if   model.showMessages
                       then [A.style [("height", "10em")]]
                       else [ A.style [("height", "0")]]
    in div [ id "messages" ]
        [ div [ id "messages-header" ]
                    [ h1 []  [text "Messages" ]
                    , toggle 
                    ]
        , div ([ id "messages-content"] ++ doDisplayList)
            [ ul []  <| List.map showMessage model.strings
            ]
        ]
                     
displayErrors : Model -> Html Msg
displayErrors model = div [ id "errors" ]
                      (List.map displayError model.errors)
displayError : String -> Html Msg<commit_msg>separate context from game state in type<commit_after>module Acquire exposing (main)

{-| Comment
@docs main
-}

import Char
import Random
import Platform.Sub as Sub
import Debug
import String
import Json.Decode as Json
import Json.Encode as Json
import Html exposing (..)
import Html.Attributes exposing (href, src, placeholder, min, max, value, id, class, type')
import Html.Attributes as A
import Html.Events exposing (..)
import Html.App as App
import WebSocket exposing (..)
import Messages exposing (..)
import Dict

{-| Main -}
main : Program Never
main =
  App.program
    { init          = init
    , view          = view
    , update        = update
    , subscriptions = subscriptions
    }

type alias Model = GameContext (GameState {})

type alias GameContext a  = { a | strings : List String, showMessages: Bool
                            , errors : List String
                            , wsServerUrl : String
                            }
    
type alias GameState a = { a | games : List GameDescription
                         , numPlayers : Int, numRobots : Int
                         , board : GameBoard, possiblePlays : List Messages.Order
                         , player : Player
                         , gameResult : Maybe Players
                         }

type Msg = Output String
         | UseKey String
         | SetName String
         | ListGames
         | Join GameId
         | CreateGame
         | Play Int
         | SetNumPlayers String
         | SetNumRobots String
         | ShowMessages Bool
         | Reset

subscriptions model =
    Debug.log "subscribing to WS "<|
        Sub.batch [ listen model.wsServerUrl Output ]
           
init : (Model, Cmd Msg)
init =
    let randomClientKey = Random.map String.fromList (Random.list 16 <| Random.map Char.fromCode (Random.int 65 90))
    in ({ strings = [], showMessages =  True, errors = [], wsServerUrl = ""
        , games = [], numPlayers = 1, numRobots= 5, board =  Dict.empty, possiblePlays = []
        , player = player ""
        , gameResult = Nothing }
       , Random.generate UseKey randomClientKey)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        Output s        -> handleServerMessages model s
        UseKey k        -> ({model | wsServerUrl = "ws://localhost:9090/" ++ k}, Cmd.none)
        SetName s       -> ({model | player = player s},Cmd.none)
        ShowMessages b  -> ({model | showMessages = b },Cmd.none)
        SetNumPlayers s -> case String.toInt s of
                               Ok i -> ({model | numPlayers = i},Cmd.none)
                               _    -> (model,Cmd.none)
        SetNumRobots s  -> case String.toInt s of
                               Ok i -> ({model | numRobots = i},Cmd.none)
                               _    -> (model,Cmd.none)
        ListGames       -> (model, sendCommand model List)
        Play n          -> ({model| possiblePlays = []}, sendCommand model (Action { selectedPlay = n }))
        Join g          -> (model, sendCommand model (JoinGame { playerName = model.player.playerName, gameId =  g}))
        CreateGame      -> (model, sendCommand model (NewGame { numHumans = model.numPlayers, numRobots = model.numRobots}))
        Reset           -> ({model
                                | strings = []
                                , board = Dict.empty, possiblePlays = [], player = player (model.player.playerName)
                                , errors = []
                                , gameResult = Nothing}, Cmd.none)

handleServerMessages : Model -> String -> (Model, Cmd Msg)
handleServerMessages model s =
    case Json.decodeString decodeServerMessages s of
        Ok (R (GamesList l)) ->
            ({model | games = l}, Cmd.none)
        Ok (R (NewGameStarted _)) ->
            (model, sendCommand model List)
        Ok (R (PlayerRegistered n gid)) ->
            (model, sendCommand model List)
        Ok (R (GameStarts gid)) ->
            (model, sendCommand model List)
        Ok (R (ErrorMessage m)) ->
            ({model | errors = m :: model.errors}, Cmd.none)
        Ok (M (GameState gs)) ->
            ({model | board = gs.gsBoard, possiblePlays = gs.gsPlayables, player = gs.gsPlayer}, Cmd.none)
        Ok (M (Played pl)) ->
            ({model | board = pl.gsBoard, possiblePlays = [], strings = showOrder pl.gsPlayed :: model.strings }, Cmd.none)
        Ok (M (GameEnds g)) ->
            ({model | board = g.gsEndGame.gameBoard
             , possiblePlays = []
             , gameResult = Just g.gsEndGame.players }, Cmd.none)
        _                ->
            ({model | strings = s :: model.strings}, Cmd.none)
                
sendCommand : Model -> Message -> Cmd Msg
sendCommand model m = send model.wsServerUrl (Json.encode 0 <| encodeMessage m)
                
view : Model -> Html Msg
view model = div []
             [ displayErrors model
             , playerInput model
             , gamesList model
             , gameBoard model
             , gameResult model
             , messages model
             ]

messages: Model -> Html Msg
messages model =
    let toggle = if model.showMessages
                 then span [ class "fa fa-toggle-down", onClick <| ShowMessages False ] []
                 else span [ class "fa fa-toggle-right", onClick <| ShowMessages True ] []
        doDisplayList = if   model.showMessages
                       then [A.style [("height", "10em")]]
                       else [ A.style [("height", "0")]]
    in div [ id "messages" ]
        [ div [ id "messages-header" ]
                    [ h1 []  [text "Messages" ]
                    , toggle 
                    ]
        , div ([ id "messages-content"] ++ doDisplayList)
            [ ul []  <| List.map showMessage model.strings
            ]
        ]
                     
displayErrors : Model -> Html Msg
displayErrors model = div [ id "errors" ]
                      (List.map displayError model.errors)
                          
displayError : String -> Html Msg
{'fraction_non_alphanumeric': 0.10437307823710283, 'fraction_numerical': 0.0029039972668261018, 'mean_word_length': 1.621222296843519, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5130748', 'n_tokens_mistral': 3184, 'n_tokens_neox': 3083, 'n_words': 969}