<commit_before>{-# LANGUAGE RecursiveDo, TupleSections #-}

module Board.Settings.Dom where

import Rules

import Types
import Board.Settings.Types
import Board.Settings.Style
import Board.Player.Dom
import Board.Worker.Style
import Common.DomUtil

import Reflex.Dom
import Control.Monad
import Data.Map.Strict
import Data.Monoid

drawSettingsIcon :: (UniverseReader t m x, PlayerSettingsReader t m x, MonadWidget t m) => m (Dynamic t PlayerSettings)
drawSettingsIcon = do
  rec
    (el, _) <- divCssClass settingsIconClass $ return ()
    let settingsIconClicks = domEvent Click el
        combinedEvents = leftmost [settingsIconClicks, shroudClicks, closePopupClicks]
    settingsVisible <- toggle False combinedEvents
    (settingsChanges, closePopupClicks) <- mapDynExtract drawSettingsWindow settingsVisible
    shroudClicks <- mapDynExtract drawShroud settingsVisible
  foldDyn updatePlayerSettings initialSettings settingsChanges

drawShroud :: MonadWidget t m => Bool -> m (Event t ())
drawShroud False = return never
drawShroud True = do
  (el, _) <- divCssClass shroudClass $ return ()
  return (domEvent Click el)

drawSettingsWindow :: (UniverseReader t m x, PlayerSettingsReader t m x, MonadWidget t m) => Bool -> m (Event t SinglePlayerSettings, Event t ())
drawSettingsWindow False = return (never, never)
drawSettingsWindow True = do
  (_, result) <- divCssClass settingsPopupClass $ do
    closeClicks <- drawSettingsClose
    players <- askPlayers
    playersAsMap <- mapDyn (fromList . fmap (, ())) players<commit_msg>Initial settings better<commit_after>{-# LANGUAGE RecursiveDo, TupleSections #-}

module Board.Settings.Dom where

import Rules

import Types
import Board.Settings.Types
import Board.Settings.Style
import Board.Player.Dom
import Board.Worker.Style
import Common.DomUtil

import Reflex.Dom
import Control.Monad
import Data.Map.Strict
import Data.Monoid

drawSettingsIcon :: (UniverseReader t m x, PlayerSettingsReader t m x, MonadWidget t m) => m (Dynamic t PlayerSettings)
drawSettingsIcon = do
  rec
    (el, _) <- divCssClass settingsIconClass $ return ()
    let settingsIconClicks = domEvent Click el
        combinedEvents = leftmost [settingsIconClicks, shroudClicks, closePopupClicks]
    settingsVisible <- toggle False combinedEvents
    (settingsChanges, closePopupClicks) <- mapDynExtract drawSettingsWindow settingsVisible
    shroudClicks <- mapDynExtract drawShroud settingsVisible
    postBuild <- getPostBuild
    universe <- askUniverse
    let postBuildSettings = createInitialSettings <$> tag (current universe) postBuild
        settingsEvents = attachWith (flip updatePlayerSettings) (current settingsDyn) settingsChanges
        allSettingsEvents = leftmost [settingsEvents, postBuildSettings]
    settingsDyn <- holdDyn initialSettings allSettingsEvents
  return settingsDyn

createInitialSettings universe =
  let playerNames = (("Player " ++) . show) <$> [1..]
      zipped = zip (zip playerNames (cycle allPlayerColors)) (getPlayers universe)
  in uncurry (uncurry SinglePlayerSettings) <$> zipped

drawShroud :: MonadWidget t m => Bool -> m (Event t ())
drawShroud False = return never
drawShroud True = do
  (el, _) <- divCssClass shroudClass $ return ()
  return (domEvent Click el)

drawSettingsWindow :: (UniverseReader t m x, PlayerSettingsReader t m x, MonadWidget t m) => Bool -> m (Event t SinglePlayerSettings, Event t ())
drawSettingsWindow False = return (never, never)
drawSettingsWindow True = do
  (_, result) <- divCssClass settingsPopupClass $ do
    closeClicks <- drawSettingsClose
    players <- askPlayers
    playersAsMap <- mapDyn (fromList . fmap (, ())) players
{'fraction_non_alphanumeric': 0.08084406686763497, 'fraction_numerical': 0.0002740476842970677, 'mean_word_length': 4.67651632970451, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 16, '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': '891569', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1051, 'n_words': 329}