<commit_before>module OS.SessionManager.Models
    exposing
        ( Model
        , Sessions
        , ID
        , WindowRef
        , initialModel
        , get
        , insert
        , openApp
        , openOrRestoreApp
        , refresh
        , remove
        )

import Dict exposing (Dict)
import Maybe exposing (Maybe(..))
import Random.Pcg as Random
import Uuid
import Apps.Apps as Apps
import Game.Network.Types exposing (IP)
import OS.SessionManager.WindowManager.Models as WindowManager


type alias Model =
    { sessions : Sessions
    , seed : Random.Seed
    }


type alias Sessions =
    Dict ID WindowManager.Model


type alias ID =
    String


type alias WindowRef =
    ( ID, WindowManager.ID )


initialModel : Model
initialModel =
    -- TODO: fetch this from game and stop keeping the active one
    { sessions = Dict.empty
    , seed = initialSeed
    }


get : ID -> Model -> Maybe WindowManager.Model
get session { sessions } =
    Dict.get session sessions


insert : ID -> Model -> Model
insert id ({ sessions, seed } as model) =
    if not (Dict.member id sessions) then
        let
            sessions_ =
                Dict.insert
                    id
                    WindowManager.initialModel
                    sessions

            seed_ =
                newSeed (Dict.size sessions_)
        in
            { model | sessions = sessions_ }
    else
        model


openApp : ID -> Maybe IP -> Apps.App -> Model -> Model
openApp id ip app ({ sessions } as model0) =
    case Dict.get id sessions of
        Just wm ->
            let
                ( uuid, model ) =
                    getUID model0

                wm_ =
                    WindowManager.insert uuid ip app wm

                sessions_ =
                    Dict.insert id wm_ sessions

                model_ =
                    { model | sessions = sessions_ }
            in
                model_

        Nothing ->
            model0


openOrRestoreApp : ID -> Maybe IP -> Apps.App -> Model -> Model
openOrRestoreApp id ip app ({ sessions } as model0) =
    case Dict.get id sessions of
        Just wm ->
            let
                ( uuid, model ) =
                    getUID model0

                wm_ =
                    WindowManager.resert uuid ip app wm

                sessions_ =
                    Dict.insert id wm_ sessions

                model_ =
                    { model | sessions = sessions_ }
            in
                model_

        Nothing ->
            model0


getUID : Model -> ( String, Model )
getUID ({ sessions, seed } as model) =
    let
        ( uuid, seed_ ) =
            Random.step Uuid.uuidGenerator seed

        model_ =
            { model | seed = seed_ }

        uuid_ =
            Uuid.toString uuid
    in
        ( uuid_, model_ )


refresh : ID -> WindowManager.Model -> Model -> Model
refresh id wm ({ sessions } as model) =
    case Dict.get id sessions of
        Just _ ->
            let
                sessions_ =
                    Dict.insert id wm sessions
            in
                { model | sessions = sessions_ }

        Nothing ->
            model


remove : ID -> Model -> Model
remove id ({ sessions } as model) =
    let
        sessions_ =
            Dict.remove id sessions
    in
        { model | sessions = sessions_ }


getWindowID : WindowRef -> WindowManager.ID
getWindowID ( _, id ) =
    id



-- internals


seed : Int
seed =
    -- a magic number from some other game
    844121764423


newSeed : Int -> Random.Seed
newSeed a =
    Random.initialSeed (seed + a)


initialSeed : Random.Seed
initialSeed =
    newSeed 0<commit_msg>Change SessionManager to use Utils.Model.RandomUuid.<commit_after>module OS.SessionManager.Models
    exposing
        ( Model
        , Sessions
        , ID
        , WindowRef
        , initialModel
        , get
        , insert
        , openApp
        , openOrRestoreApp
        , refresh
        , remove
        )

import Dict exposing (Dict)
import Maybe exposing (Maybe(..))
import Random.Pcg as Random
import Utils.Model.RandomUuid as RandomUuid
import Apps.Apps as Apps
import Game.Network.Types exposing (IP)
import OS.SessionManager.WindowManager.Models as WindowManager


type alias Model =
    RandomUuid.Model { sessions : Sessions }


type alias Sessions =
    Dict ID WindowManager.Model


type alias ID =
    String


type alias WindowRef =
    ( ID, WindowManager.ID )


initialModel : Model
initialModel =
    -- TODO: fetch this from game and stop keeping the active one
    { randomUuidSeed = Random.initialSeed 844121764423
    , sessions = Dict.empty
    }


get : ID -> Model -> Maybe WindowManager.Model
get session { sessions } =
    Dict.get session sessions


insert : ID -> Model -> Model
insert id ({ sessions } as model) =
    if not (Dict.member id sessions) then
        let
            sessions_ =
                Dict.insert
                    id
                    WindowManager.initialModel
                    sessions
        in
            { model | sessions = sessions_ }
    else
        model


openApp : ID -> Maybe IP -> Apps.App -> Model -> Model
openApp id ip app ({ sessions } as model0) =
    case Dict.get id sessions of
        Just wm ->
            let
                ( model, uuid ) =
                    getUID model0

                wm_ =
                    WindowManager.insert uuid ip app wm

                sessions_ =
                    Dict.insert id wm_ sessions

                model_ =
                    { model | sessions = sessions_ }
            in
                model_

        Nothing ->
            model0


openOrRestoreApp : ID -> Maybe IP -> Apps.App -> Model -> Model
openOrRestoreApp id ip app ({ sessions } as model0) =
    case Dict.get id sessions of
        Just wm ->
            let
                ( model, uuid ) =
                    getUID model0

                wm_ =
                    WindowManager.resert uuid ip app wm

                sessions_ =
                    Dict.insert id wm_ sessions

                model_ =
                    { model | sessions = sessions_ }
            in
                model_

        Nothing ->
            model0


getUID : Model -> ( Model, String )
getUID =
    RandomUuid.newUuid


refresh : ID -> WindowManager.Model -> Model -> Model
refresh id wm ({ sessions } as model) =
    case Dict.get id sessions of
        Just _ ->
            let
                sessions_ =
                    Dict.insert id wm sessions
            in
                { model | sessions = sessions_ }

        Nothing ->
            model


remove : ID -> Model -> Model
remove id ({ sessions } as model) =
    let
        sessions_ =
            Dict.remove id sessions
    in
        { model | sessions = sessions_ }


getWindowID : WindowRef -> WindowManager.ID
getWindowID ( _, id ) =
    id
{'fraction_non_alphanumeric': 0.06835994194484761, 'fraction_numerical': 0.005370101596516691, 'mean_word_length': 1.36722775678461, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '5915629', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1771, 'n_words': 588}