<commit_before>module Network exposing (..)

import Json.Decode as Decode
import Json.Encode as Encode
import Keyboard exposing (KeyCode)
import Result
import WebSocket


send : PlayerMessage -> Cmd msg
send =
    WebSocket.send "ws://127.0.0.1:9160" << encodePlayerMessage


connect : String -> Cmd msg
connect =
    send << ConnectionRequest


type ServerMessage
    = Connected String



-- TODO: Refactor for the looks and (efficiency?)


parseServerMessage : String -> Maybe ServerMessage
parseServerMessage str =
    let
        typeDecoder =
            Decode.field "type" Decode.string

        resultMessageType =
            Decode.decodeString typeDecoder str

        maybeDataDecoder =
            case resultMessageType of
                Ok msgType ->
                    mapTypeToDecoder msgType

                _ ->
                    Nothing
    in
        case maybeDataDecoder of
            Just decoder ->
                case Decode.decodeString decoder str of
                    Ok msg ->
                        Just msg

                    Err _ ->
                        Nothing

            Nothing ->
                Nothing


mapTypeToDecoder : String -> Maybe (Decode.Decoder ServerMessage)
mapTypeToDecoder messageType =
    case messageType of
        "connected" ->
            Just <| parse1 Connected Decode.string

        _ ->
            Nothing


parse1 : (a -> ServerMessage) -> Decode.Decoder a -> Decode.Decoder ServerMessage
parse1 msg decoder =
    Decode.map msg (Decode.field "1" decoder)


type PlayerMessage
    = ConnectionRequest String
    | KeyChange KeyCode Bool


encodePlayerMessage : PlayerMessage -> String
encodePlayerMessage msg =
    let
        ( msgType, args ) =
            case msg of
                ConnectionRequest username ->
                    ( "connection_request", [ Encode.string username ] )

                KeyChange keyCode newState ->
                    ( "keychange", [ Encode.int keyCode, Encode.bool newState ] )

        mapArgs n args =
            case args of
                [] ->
                    []<commit_msg>Added MouseMove Message to Network<commit_after>module Network exposing (..)

import Json.Decode as Decode
import Json.Encode as Encode
import Keyboard exposing (KeyCode)
import Result
import WebSocket


send : PlayerMessage -> Cmd msg
send =
    WebSocket.send "ws://127.0.0.1:9160" << encodePlayerMessage


connect : String -> Cmd msg
connect =
    send << ConnectionRequest


type ServerMessage
    = Connected String



-- TODO: Refactor for the looks and (efficiency?)


parseServerMessage : String -> Maybe ServerMessage
parseServerMessage str =
    let
        typeDecoder =
            Decode.field "type" Decode.string

        resultMessageType =
            Decode.decodeString typeDecoder str

        maybeDataDecoder =
            case resultMessageType of
                Ok msgType ->
                    mapTypeToDecoder msgType

                _ ->
                    Nothing
    in
        case maybeDataDecoder of
            Just decoder ->
                case Decode.decodeString decoder str of
                    Ok msg ->
                        Just msg

                    Err _ ->
                        Nothing

            Nothing ->
                Nothing


mapTypeToDecoder : String -> Maybe (Decode.Decoder ServerMessage)
mapTypeToDecoder messageType =
    case messageType of
        "connected" ->
            Just <| parse1 Connected Decode.string

        _ ->
            Nothing


parse1 : (a -> ServerMessage) -> Decode.Decoder a -> Decode.Decoder ServerMessage
parse1 msg decoder =
    Decode.map msg (Decode.field "0" decoder)


type PlayerMessage
    = ConnectionRequest String
    | KeyChange KeyCode Bool
    | MouseMove Float Float


encodePlayerMessage : PlayerMessage -> String
encodePlayerMessage msg =
    let
        ( msgType, args ) =
            case msg of
                ConnectionRequest username ->
                    ( "connection_request", [ Encode.string username ] )

                KeyChange keyCode newState ->
                    ( "keychange", [ Encode.int keyCode, Encode.bool newState ] )

                MouseMove x y ->
                    ( "mousemove", [ Encode.float x, Encode.float y ] )

        mapArgs n args =
            case args of
                [] ->
                    []
{'fraction_non_alphanumeric': 0.06599680292304179, 'fraction_numerical': 0.0063941539164192734, 'mean_word_length': 1.635379061371841, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3987161', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1020, 'n_words': 327}