Skip to content

Commit

Permalink
story
Browse files Browse the repository at this point in the history
  • Loading branch information
rtodt committed Dec 7, 2016
1 parent 9db85ea commit 6d7e062
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
elm-stuff/
# elm-repl generated files
repl-temp-*
# Distribution
dist/
# Dependency directories
node_modules
# Desktop Services Store on macOS
.DS_Store
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# elm-vst
VST Visualisierungs-Simulations-Tool

Experiment zur Portierung von odt-vst nach Elm.

- [Simulation](#simulation)
- [Visualisierung](#visualisierung)
- [Story](#story)
- [SubTitles](#subtitles)

## Simulation
Simulation von Maschinen, Sensoren, Kabeln und Datenpaketen.
Erkennen von Nadelohren.

## Visualisierung
SVG or 3D?

## Story
Definition der Ereignisse, die in der Simulation im Laufe der Zeit passieren.

## SubTitles
Untertitel
23 changes: 23 additions & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"summary": "VST - Visualisierungs-Simulations-Tool",
"repository": "https://github.com/odtch/elm-vst.git",
"license": "",
"source-directories": [
".",
"src/"
],
"exposed-modules": [],
"dependencies": {
"elm-community/linear-algebra": "1.0.0 <= v < 2.0.0",
"elm-community/webgl": "1.0.0 <= v < 2.0.0",
"elm-lang/animation-frame": "1.0.1 <= v < 2.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"elm-lang/http": "1.0.0 <= v < 2.0.0",
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
"elm-lang/virtual-dom": "2.0.1 <= v < 3.0.0",
"mgold/elm-animation": "1.0.5 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
137 changes: 137 additions & 0 deletions src/Story.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module Story exposing (..)

import Time exposing (Time)


type Action msg
= Wait Time
| Send msg


type alias Event msg =
{ occursAt : Time
, msg : msg
}


type alias Story msg =
{ events : List (Event msg) }


type alias Player msg =
{ story : Story msg
, time : Time
, paused : Bool
, speed : Float
}


create : List (Action msg) -> Story msg
create actions =
let
( time, events ) =
List.foldl
(\action ( time, events ) ->
case action of
Wait delay ->
( time + delay, events )

Send msg ->
( time, events ++ [ { occursAt = time, msg = msg } ] )
)
( 0, [] )
actions
in
{ events = events
}


messagesBetween : Time -> Time -> Story msg -> List msg
messagesBetween start end story =
List.map (\event -> event.msg) <| List.filter (\event -> occursIn start end event) story.events


occursIn : Time -> Time -> Event msg -> Bool
occursIn start end event =
(start <= event.occursAt) && (event.occursAt < end)


start : Story msg -> Player msg
start story =
{ story = story
, time = 0
, paused = False
, speed = 1
}


setPaused : Bool -> Player msg -> Player msg
setPaused paused player =
{ player | paused = paused }


setSpeed : Float -> Player msg -> Player msg
setSpeed speed player =
if (speed <= 0 || 100 < speed) then
Debug.crash "speed out of range"
else
{ player | speed = speed }


tick : Time -> Player msg -> ( Player msg, List msg )
tick time player =
if (player.paused) then
( player, [] )
else
let
newtime =
player.time + time * player.speed
in
( { player
| time = newtime
}
, messagesBetween player.time newtime player.story
)



--
--
-- tick : Time -> (msg -> model -> ( model, Cmd msg )) -> Player msg -> model -> ( Player msg, model, Cmd msg )
-- tick time update player model =
-- if (player.paused) then
-- ( player, model, Cmd.none )
-- else
-- let
-- newtime =
-- player.time + time * player.speed
--
-- events =
-- List.filter (occursIn player.time newtime) player.story.events
--
-- ( newmodel, msg ) =
-- executeEvents update events model
-- in
-- ( { player
-- | time = newtime
-- }
-- , newmodel
-- , msg
-- )
--
--
-- executeEvents : (msg -> model -> ( model, Cmd msg )) -> List (Event msg) -> model -> ( model, Cmd msg )
-- executeEvents update events model =
-- List.foldl
-- (\event ( premodel, precmd ) ->
-- case event of
-- Empty message ->
-- ( premodel, precmd )
--
-- Soon time data ->
-- update data.msg premodel
-- )
-- ( model, Cmd.none )
-- events
--
--
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/elm-stuff/
18 changes: 18 additions & 0 deletions tests/Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
port module Main exposing (..)

import Test exposing (..)
import Test.Runner.Node exposing (run, TestProgram)
import Json.Encode exposing (Value)
import StoryTest


main : TestProgram
main =
run emit <|
describe "VST - Story Test Suite"
[ --Tests.all,
StoryTest.all
]


port emit : ( String, Value ) -> Cmd msg
109 changes: 109 additions & 0 deletions tests/StoryTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module StoryTest exposing (..)

import Test exposing (..)
import Time exposing (..)
import Expect
import Fuzz exposing (list, int, tuple, string)
import String
import Story exposing (Story)


story : Story String
story =
Story.create
[ Story.Send "A"
, Story.Wait <| 1 * second
, Story.Send "B"
, Story.Send "C"
, Story.Wait <| 2 * second
, Story.Send "D"
]


eventsToString : List (Story.Event String) -> String
eventsToString events =
List.foldl
(\event text ->
(if (String.length text == 0) then
""
else
text ++ ", "
)
++ "at "
++ (toString <| inSeconds event.occursAt)
++ " "
++ event.msg
)
""
events


messagesToString : List String -> String
messagesToString messages =
List.foldl
(\message text ->
(if (String.length text == 0) then
""
else
text ++ ", "
)
++ message
)
""
messages


runPlayer : Time -> Story String -> String
runPlayer stepTime story =
let
stepCount =
round <| (second * 6.0) / stepTime

ticks =
List.repeat stepCount stepTime

player =
Story.start story

( newplayer, text ) =
(List.foldr
(\time ( player, text ) ->
let
( newplayer, messages ) =
Story.tick time player
in
( newplayer, text ++ (messagesToString messages) ++ " | " )
)
( player, "" )
ticks
)
in
text


all : Test
all =
describe "Story Test"
[ test "all events" <|
\() -> Expect.equal (eventsToString story.events) "at 0 A, at 1 B, at 1 C, at 3 D"
, test "events 0 - 1" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (0 * second) (1 * second) story)) "A"
, test "events 0 - 2" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (0 * second) (2 * second) story)) "A, B, C"
, test "events 0 - 4" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (0 * second) (4 * second) story)) "A, B, C, D"
, test "events 1 - 2" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (1 * second) (2 * second) story)) "B, C"
, test "events 1 - 4" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (1 * second) (4 * second) story)) "B, C, D"
, test "events 3 - 4" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (3 * second) (4 * second) story)) "D"
, test "events 4 - 5" <|
\() -> Expect.equal (messagesToString (Story.messagesBetween (4 * second) (5 * second) story)) ""
, test "player 1s" <|
\() -> Expect.equal (runPlayer (1 * second) story) "A | B, C | | D | | | "
, test "player 0.5s" <|
\() -> Expect.equal (runPlayer (0.5 * second) story) "A | | B, C | | | | D | | | | | | "
, test "player 2s" <|
\() -> Expect.equal (runPlayer (2 * second) story) "A, B, C | D | | "
]
20 changes: 20 additions & 0 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0.0",
"summary": "Sample Elm Test",
"repository": "https://github.com/user/project.git",
"license": "BSD-3-Clause",
"source-directories": [
".",
"../src"
],
"exposed-modules": [],
"dependencies": {
"elm-community/json-extra": "2.0.0 <= v < 3.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"mgold/elm-random-pcg": "4.0.2 <= v < 5.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "3.0.0 <= v < 4.0.0",
"rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}

0 comments on commit 6d7e062

Please sign in to comment.