Skip to content

Commit

Permalink
Add a built-in deck source and the CaH base deck.
Browse files Browse the repository at this point in the history
This does a lot of the work needed for #120.
  • Loading branch information
Lattyware committed May 1, 2020
1 parent 3024b94 commit 1733158
Show file tree
Hide file tree
Showing 52 changed files with 2,451 additions and 302 deletions.
2 changes: 1 addition & 1 deletion client/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/svg": "1.0.1",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
Expand All @@ -29,7 +30,6 @@
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/regex": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/list-extra": "8.2.3",
"owanturist/elm-union-find": "1.0.0"
Expand Down
6 changes: 3 additions & 3 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"extract-loader": "^5.0.1",
"file-loader": "^6.0.0",
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.2.1",
"html-webpack-plugin": "^4.3.0",
"node-sass": "^4.14.0",
"postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0",
Expand Down
18 changes: 16 additions & 2 deletions client/src/elm/MassiveDecks.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Browser
import Browser.Navigation as Navigation
import Html exposing (Html)
import Html.Attributes as HtmlA
import Http
import Json.Decode as Json
import MassiveDecks.Cast.Client as Cast
import MassiveDecks.Cast.Model as Cast
Expand All @@ -28,6 +29,8 @@ import MassiveDecks.Pages.Route as Route exposing (Route)
import MassiveDecks.Pages.Start as Start
import MassiveDecks.Pages.Start.Route as Start
import MassiveDecks.Pages.Unknown as Unknown
import MassiveDecks.Requests.Api as Api
import MassiveDecks.Requests.Request as Request
import MassiveDecks.ServerConnection as ServerConnection
import MassiveDecks.Settings as Settings
import MassiveDecks.Settings.Messages as Settings
Expand Down Expand Up @@ -85,6 +88,7 @@ init flags url key =
, speech = speech
, notifications = Notifications.init
, remoteMode = remoteMode
, sources = { builtIn = Nothing, cardcast = False }
}

( page, pageCmd ) =
Expand All @@ -93,9 +97,12 @@ init flags url key =

else
( Pages.Loading, Cmd.none )

sourceCmd =
Request.map (Error.Add >> ErrorMsg) never UpdateSources |> Api.sourceInfo |> Http.request
in
( { page = page, shared = shared, errorOverlay = Overlay.init }
, Cmd.batch [ pageCmd, settingsCmd, speechCmd ]
, Cmd.batch [ sourceCmd, pageCmd, settingsCmd, speechCmd ]
)


Expand Down Expand Up @@ -264,6 +271,13 @@ update msg model =
in
( { model | shared = { oldShared | notifications = notifications } }, notificationsCmd )

UpdateSources info ->
let
oldShared =
model.shared
in
( { model | shared = { oldShared | sources = info } }, Cmd.none )

Refresh ->
( model, Navigation.reload )

Expand All @@ -284,7 +298,7 @@ update msg model =
Settings.update model.shared (Settings.ChangeLang (Just language))

( lobby, lobbyCmd ) =
Lobby.initWithAuth { gameCode = auth.claims.gc, section = Just Lobby.Spectate } auth
Lobby.initWithAuth shared { gameCode = auth.claims.gc, section = Just Lobby.Spectate } auth
in
( { model
| page = Pages.Lobby lobby
Expand Down
56 changes: 38 additions & 18 deletions client/src/elm/MassiveDecks/Card/Parts.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module MassiveDecks.Card.Parts exposing
( Part(..)
, Parts
, Style(..)
, Transform(..)
, fromList
, map
Expand All @@ -19,16 +20,24 @@ import MassiveDecks.Util.String as String
{-| A transform to apply to the value in a slot.
-}
type Transform
= UpperCase
= NoTransform
| UpperCase
| Capitalize
| Stay


{-| A style to be applied to some text.
-}
type Style
= NoStyle
| Em
| Strong


{-| A part of a call's text. This is either just text or a position for a call to be inserted in-game.
-}
type Part
= Text String
| Slot Transform
= Text String Style
| Slot Transform Style


{-| Represents a line as a part of a part. Between each one the text will be forced to line break.
Expand All @@ -48,10 +57,10 @@ type Parts
isSlot : Part -> Bool
isSlot part =
case part of
Text _ ->
Text _ _ ->
False

Slot _ ->
Slot _ _ ->
True


Expand Down Expand Up @@ -136,18 +145,18 @@ viewLinesString blankPhrase =
viewLines (\s -> \p -> viewPartsString blankPhrase s p |> String.join "")


viewParts : (Bool -> String -> List a) -> a -> List String -> List Part -> List a
viewParts : (Bool -> String -> Style -> List a) -> a -> List String -> List Part -> List a
viewParts viewText emptySlot play parts =
case parts of
firstPart :: restParts ->
case firstPart of
Text string ->
viewText False string ++ viewParts viewText emptySlot play restParts
Text string style ->
viewText False string style ++ viewParts viewText emptySlot play restParts

Slot transform ->
Slot transform style ->
case play of
firstPlay :: restPlay ->
viewText True (applyTransform transform firstPlay) ++ viewParts viewText emptySlot restPlay restParts
viewText True (applyTransform transform firstPlay) style ++ viewParts viewText emptySlot restPlay restParts

[] ->
emptySlot :: viewParts viewText emptySlot [] restParts
Expand All @@ -163,27 +172,38 @@ viewPartsHtml =

viewPartsString : String -> List String -> List Part -> List String
viewPartsString blankPhrase =
viewParts (\_ -> \s -> [ s ]) blankPhrase
viewParts (\_ -> \s -> \_ -> [ s ]) blankPhrase


applyTransform : Transform -> String -> String
applyTransform transform value =
case transform of
NoTransform ->
value

UpperCase ->
String.toUpper value

Capitalize ->
String.capitalise value

Stay ->
value


viewTextHtml : Bool -> String -> List (Html msg)
viewTextHtml slot string =
viewTextHtml : Bool -> String -> Style -> List (Html msg)
viewTextHtml slot string style =
let
element =
case style of
NoStyle ->
Html.span

Em ->
Html.em

Strong ->
Html.strong

words =
string |> splitWords |> List.map (\word -> Html.span [] [ Html.text word ])
string |> splitWords |> List.map (\word -> element [] [ Html.text word ])
in
if slot then
[ Html.span [ HtmlA.class "slot" ] words ]
Expand Down
17 changes: 16 additions & 1 deletion client/src/elm/MassiveDecks/Card/Response.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MassiveDecks.Model exposing (Shared)
import MassiveDecks.Pages.Lobby.Configure.Decks as Decks
import MassiveDecks.Pages.Lobby.Configure.Model exposing (Config)
import MassiveDecks.Util.String as String
import Regex exposing (Regex)


{-| Render the response to HTML.
Expand Down Expand Up @@ -71,9 +72,23 @@ viewCustom shared config side update canonicalize attributes response fill =
{- Private -}


punctuation : Regex
punctuation =
-- TODO: This should probably get localized.
Regex.fromString "[.?!]$" |> Maybe.withDefault Regex.never


viewBody : Response -> ViewBody msg
viewBody response =
ViewBody (\() -> [ Html.p [] [ Html.span [] [ response.body |> String.capitalise |> Html.text ] ] ])
let
end =
if response.body |> Regex.contains punctuation then
[]

else
[ Html.text "." ]
in
ViewBody (\() -> [ Html.p [] [ Html.span [] ((response.body |> String.capitalise |> Html.text) :: end) ] ])


viewCustomBody : String -> (String -> msg) -> (String -> msg) -> String -> Maybe String -> ViewBody msg
Expand Down
57 changes: 40 additions & 17 deletions client/src/elm/MassiveDecks/Card/Source.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module MassiveDecks.Card.Source exposing
import Html exposing (Html)
import Html.Attributes as HtmlA
import Html.Events as HtmlE
import MassiveDecks.Card.Source.BuiltIn as BuiltIn
import MassiveDecks.Card.Source.Cardcast as Cardcast
import MassiveDecks.Card.Source.Custom as Player
import MassiveDecks.Card.Source.Fake as Fake
Expand All @@ -27,15 +28,16 @@ import MassiveDecks.Model exposing (..)
import MassiveDecks.Pages.Lobby.Configure.Decks.Model exposing (DeckOrError)
import MassiveDecks.Strings as Strings exposing (MdString)
import MassiveDecks.Strings.Languages as Lang
import MassiveDecks.Util.Maybe as Maybe
import Weightless as Wl
import Weightless.Attributes as WlA


{-| The default source for an editor.
-}
default : External
default : Shared -> External
default =
Cardcast.generalMethods.empty ()
BuiltIn.generalMethods.empty


{-| Check if two sources are equal.
Expand All @@ -57,23 +59,33 @@ externalAndEquals a b =
False


{-| Get an empty source of the given type.
{-| Get an general methods of the given type.
-}
empty : String -> Maybe External
empty n =
generalMethods : String -> Maybe (ExternalGeneralMethods msg)
generalMethods n =
case n of
"BuiltIn" ->
BuiltIn.generalMethods |> Just

"Cardcast" ->
() |> Cardcast.generalMethods.empty |> Just
Cardcast.generalMethods |> Just

_ ->
Nothing


{-| Get an empty source of the given type.
-}
empty : Shared -> String -> Maybe External
empty shared n =
generalMethods n |> Maybe.map (\m -> m.empty shared)


{-| An empty source of the same general type as the given one.
-}
emptyMatching : External -> External
emptyMatching source =
() |> (externalMethods source |> .empty)
emptyMatching : Shared -> External -> External
emptyMatching shared source =
shared |> (externalMethods source |> .empty)


{-| The name of a source.
Expand All @@ -99,9 +111,9 @@ defaultDetails shared source =

{-| A tooltip for a source.
-}
tooltip : Source -> Maybe ( String, Html msg )
tooltip source =
case () |> (methods source |> .tooltip) of
tooltip : Shared -> Source -> Maybe ( String, Html msg )
tooltip shared source =
case shared |> (methods source |> .tooltip) of
Just ( id, rendered ) ->
Just
( id
Expand Down Expand Up @@ -134,15 +146,23 @@ logo source =
-}
generalEditor : Shared -> List DeckOrError -> External -> (External -> msg) -> List (Html msg)
generalEditor shared existing currentValue update =
let
enabledSources =
[ shared.sources.builtIn |> Maybe.map (\_ -> BuiltIn.generalMethods)
, Cardcast.generalMethods |> Maybe.justIf shared.sources.cardcast
]

toOption source =
Html.option [ HtmlA.value (source.id ()) ]
[ () |> source.name |> Lang.html shared
]
in
[ Wl.select
[ HtmlA.id "source-selector"
, WlA.outlined
, HtmlE.onInput (empty >> Maybe.withDefault default >> update)
]
[ Html.option [ HtmlA.value "Cardcast" ]
[ Strings.Cardcast |> Lang.html shared
]
, HtmlE.onInput (empty shared >> Maybe.withDefault (default shared) >> update)
]
(enabledSources |> List.filterMap (Maybe.map toOption))
, editor shared existing currentValue update
]

Expand Down Expand Up @@ -197,3 +217,6 @@ externalMethods external =
case external of
Cardcast playCode ->
Cardcast.methods playCode

BuiltIn id ->
BuiltIn.methods id
Loading

0 comments on commit 1733158

Please sign in to comment.