Skip to content

Commit 49227fa

Browse files
committed
Paper planes flying by
1 parent 8184340 commit 49227fa

File tree

13 files changed

+162
-96
lines changed

13 files changed

+162
-96
lines changed

src/Client/Experiments/Update.purs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ update ∷ Update ExperimentsModel ExperimentsMessage
2929
update model =
3030
case _ of
3131
ToggleVisibility modal → model { visible = modal == ShowExperiments } /\ []
32-
ToggleSection section → F.noMessages model { section = section }
3332
RedirectKarma → model /\
3433
[ do
3534
liftEffect <<< FS.send imAppId <<< SIT.SpecialRequest <<< ToggleModal $ Screen ShowKarmaPrivileges
@@ -41,6 +40,7 @@ update model =
4140
DisplayQuestions questions → displayQuestions questions model
4241
SelectChoice question choice → selectChoice question choice model
4342
AnswerQuestion → answerQuestion model
43+
ToggleDoppelgangerSection section → F.noMessages model { doppelganger = model.doppelganger { section = section } }
4444
AfterAnswerQuestion → afterAnswerQuestion model
4545
FetchMatches → fetchMatches model
4646
DisplayMatches matches → displayMatches matches model
@@ -50,6 +50,21 @@ update model =
5050
ThrowPlane → throwPlane model
5151
AfterThrowPlane id → afterThrowPlane id model
5252
ResizeMessageInput event → SIR.resizeInputFrom event model
53+
TogglePaperPlaneSection section → togglePaperPlaneSection section model
54+
DisplayFlyingPaperPlanes planes -> displayFlyingPaperPlanes planes model
55+
56+
displayFlyingPaperPlanes Array PaperPlane ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
57+
displayFlyingPaperPlanes planes model = model { paperPlane = model.paperPlane { flyingBy = planes } } /\ []
58+
59+
togglePaperPlaneSection PaperPlaneSection ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
60+
togglePaperPlaneSection section model = model { paperPlane = model.paperPlane { section = section } } /\ effects
61+
where
62+
fetch = do
63+
r ← CCN.silentResponse $ request.experiments.flying { }
64+
pure <<< Just $ DisplayFlyingPaperPlanes r
65+
effects
66+
| model.paperPlane.section /= section && section == ShowFlyingBy && DA.null model.paperPlane.flyingBy = [fetch]
67+
| otherwise = []
5368

5469
setPlaneMessage String ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
5570
setPlaneMessage message model = model { paperPlane = model.paperPlane { message = if DS.null message then Nothing else Just message } } /\ []
@@ -79,7 +94,7 @@ resumeQuestions model = model /\ [ resume ]
7994
pure <<< Just $ DisplayQuestions questions
8095

8196
displayQuestions Array Question ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
82-
displayQuestions questions model = model { section = ShowingDoppelganger ShowNextQuestion, doppelganger { questions = questions } } /\ []
97+
displayQuestions questions model = model { doppelganger { section = ShowNextQuestion, questions = questions } } /\ []
8398

8499
selectChoice Int Int ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
85100
selectChoice question choice model = model { doppelganger = model.doppelganger { selectedChoice = Just { question, choice } } } /\ []
@@ -124,7 +139,7 @@ fetchMatches model = model /\ [ fetch ]
124139
pure <<< Just $ DisplayMatches response
125140

126141
displayMatches Array Match ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
127-
displayMatches matches model = model { section = ShowingDoppelganger ShowMatches, doppelganger = model.doppelganger { matches = matches } } /\ []
142+
displayMatches matches model = model { doppelganger = model.doppelganger { section = ShowMatches, matches = matches } } /\ []
128143

129144
messageDoppelganger Int ExperimentsModel ExperimentsModel /\ (Array (Aff (Maybe ExperimentsMessage)))
130145
messageDoppelganger userId model = model /\ [ send ]

src/Client/css/experiments.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
align-self: flex-end;
4242
}
4343

44+
.flying-by {
45+
display: flex;
46+
flex-direction: column;
47+
width: 534px;
48+
}
49+
4450
.word-chain {
4551
margin: 23px 25px;
4652
}
@@ -89,6 +95,11 @@
8995
margin-bottom: 40px;
9096
}
9197

98+
.paper-flown-message {
99+
word-wrap: break-word;
100+
overflow: auto;
101+
}
102+
92103
@media (max-width:1279px) {
93104
#chat-experiments {
94105
width: 100%;
@@ -111,7 +122,7 @@
111122
width: auto;
112123
}
113124

114-
.paper-thrown-entry {
125+
.paper-thrown-entry, .flying-by {
115126
width: calc(100vw - 47px);
116127
}
117128
}

src/Server/Database/PaperPlanes.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type PaperPlanes =
1313
, thrower Int
1414
, message String
1515
, created Column DateTime Default
16-
, caughtByAt :: Maybe DateTime
16+
, by_at :: Maybe DateTime
1717
, by :: Maybe Int
1818
, status PlaperPlaneStatus
1919
)
@@ -30,5 +30,5 @@ _message = Proxy
3030
_by Proxy "by"
3131
_by = Proxy
3232

33-
_caughtByAt Proxy "caught_by_at"
34-
_caughtByAt = Proxy
33+
_byAt Proxy "by_at"
34+
_byAt = Proxy

src/Server/Experiments/Action.purs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Debug (spy)
1010
import Run.Except as RE
1111
import Server.Effect (ServerEffect)
1212
import Server.Experiments.Database as SED
13-
import Shared.Experiments.Types (Question, Match)
13+
import Shared.Experiments.Types (Match, Question, PaperPlane)
1414
import Shared.Options.Doppelganger (changelogEntry, totalQuestions)
1515
import Shared.Options.PaperPlane (maxPaperPlanes)
1616
import Shared.ResponseError (ResponseError(..))
@@ -21,7 +21,8 @@ experiments loggedUserId = do
2121
user ← SED.fetchExperimentUser loggedUserId
2222
count ← SED.fetchAnswerCount loggedUserId
2323
thrown ← SED.fetchPaperPlanes loggedUserId
24-
pure { experiments: list, user, completedDoppelganger: count == totalQuestions, thrown }
24+
flyingBy ← SED.fetchPaperPlanesFlying loggedUserId
25+
pure { experiments: list, user, completedDoppelganger: count == totalQuestions, thrown, flyingBy }
2526

2627
buildQuestions Int ServerEffect (Array Question)
2728
buildQuestions loggedUserId = do
@@ -42,8 +43,11 @@ fetchMatches loggedUserId = do
4243
saveAnswer Int Int ServerEffect Unit
4344
saveAnswer loggedUserId choice = SED.saveAnswer loggedUserId choice
4445

45-
throwPlane Int String ServerEffect {id :: Int }
46+
throwPlane Int String ServerEffect { id Int }
4647
throwPlane loggedUserId message = do
47-
c <- SED.countPaperPlanes loggedUserId
48-
when (c == Just (BI.fromInt maxPaperPlanes)) <<< RE.throw $ BadRequest { reason: "too many planes"}
49-
SED.savePlane loggedUserId message
48+
c ← SED.countPaperPlanes loggedUserId
49+
when (c == Just (BI.fromInt maxPaperPlanes)) <<< RE.throw $ BadRequest { reason: "too many planes" }
50+
SED.savePlane loggedUserId message
51+
52+
flyingPlanes Int ServerEffect (Array PaperPlane)
53+
flyingPlanes loggedUserId = SED.fetchPaperPlanesFlying loggedUserId

src/Server/Experiments/Database.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Server.Database.DoppelgangerChoices (_asked, _choice, doppelganger_choice
1919
import Server.Database.DoppelgangerQuestions (_question, doppelganger_questions)
2020
import Server.Database.LastSeen (_who)
2121
import Server.Database.Messages (_status)
22-
import Server.Database.PaperPlanes (_message, _thrower, paper_planes)
22+
import Server.Database.PaperPlanes (_by, _byAt, _message, _thrower, paper_planes)
2323
import Server.Database.Recoveries (_created)
2424
import Server.Effect (ServerEffect)
2525
import Server.Experiments.Database.Flat (FlatQuestion)
@@ -74,6 +74,9 @@ countPaperPlanes loggedUserId = do
7474
fetchPaperPlanes Int ServerEffect (Array PaperPlane)
7575
fetchPaperPlanes loggedUserId = SD.query $ select (_id /\ _message /\ _status) # from paper_planes # wher (_thrower .=. loggedUserId .&&. _status .<>. Crashed) # orderBy _created
7676

77+
fetchPaperPlanesFlying Int ServerEffect (Array PaperPlane)
78+
fetchPaperPlanesFlying loggedUserId = SD.query $ select (_id /\ _message /\ _status) # from paper_planes # wher (_by .=. loggedUserId) # orderBy _byAt
79+
7780
q Proxy "q"
7881
q = Proxy
7982

src/Server/Experiments/Handler.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Payload.ResponseTypes (Empty(..))
77
import Run as R
88
import Server.Experiments.Action as SEA
99
import Server.Experiments.Template as SET
10-
import Shared.Experiments.Types (Question, Match)
10+
import Shared.Experiments.Types (Match, Question, PaperPlane)
1111
import Shared.Html (Html(..))
1212

1313
experiments { guards { loggedUserId Int } } ServerEffect Html
@@ -29,3 +29,6 @@ answer request = do
2929
throw { guards { loggedUserId Int }, body { message String } } ServerEffect {id :: Int}
3030
throw request = SEA.throwPlane request.guards.loggedUserId request.body.message
3131

32+
flying { guards { loggedUserId Int } } ServerEffect (Array PaperPlane)
33+
flying request = SEA.flyingPlanes request.guards.loggedUserId
34+

src/Server/Experiments/Template.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ template payload = Html <$> F.preMount (QuerySelector "#chat-experiments")
1717
{ experiments: payload.experiments
1818
, visible: true
1919
, user: payload.user
20-
, section: HideExperiments
2120
, doppelganger:
2221
{ questions: []
2322
, matches: []
2423
, completed: payload.completedDoppelganger
2524
, loading: false
25+
, section: ShowDoppelganger
2626
, selectedChoice: Nothing
2727
}
2828
, paperPlane:
2929
{ loading: false
30+
, section: ShowNew
3031
, message: Nothing
3132
, thrown: payload.thrown
33+
, flyingBy : payload.flyingBy
3234
}
3335
}
3436
}

src/Server/Handler.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ handlers reading =
120120
, matches: runJson reading SEH.matches
121121
, answer: runJson reading SEH.answer
122122
, throw: runJson reading SEH.throw
123+
, flying: runJson reading SEH.flying
123124
}
124125
, sw
125126
, developmentFiles

src/Server/sql/index.sql

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,33 @@ language plpgsql;
204204
create or replace function handle_paper_planes()
205205
returns void as
206206
$$
207+
begin
208+
with updated as (update paper_planes p
209+
set by_at = utc_now(), thrown = p.thrown + 1, by = (
210+
select u.id
211+
from users u join last_seen l on u.id = l.who join karma_leaderboard k on u.id = k.ranker
212+
where p.thrower <> u.id and
213+
(p.by is null or u.id <> p.by) and
214+
not temporary and
215+
(extract(epoch from (utc_now()) - l.date) / 3600 <= (24 * 7)) and
216+
k.current_karma >= 80 and
217+
((select count(1) from paper_planes where by = u.id and status = 1) < 7)
218+
order by random() limit 1)
219+
where status = 1 and thrown < 7 and (by is null or extract(epoch from (utc_now()) - by_at) / 3600 > (24 * 7))
220+
returning by, 'There are planes flying by. Catch?', 2)
221+
insert into changelogs(changed, description, action) select * from updated;
222+
end;
223+
$$
224+
language plpgsql;
225+
226+
-- select cron.schedule('0 0 * * *', $$select handle_paper_planes()$$);
227+
create or replace function crash_paper_planes()
228+
returns void as
229+
$$
207230
begin
208231
update paper_planes p
209-
set by_at = utc_now(), by = (
210-
select u.id
211-
from users u join last_seen l on u.id = l.who join karma_leaderboard k on u.id = k.ranker
212-
where (p.by is null or u.id <> p.by) and
213-
not temporary and
214-
(extract(epoch from (utc_now()) - l.date) / 3600 <= (24 * 7)) and
215-
k.current_karma >= 80 and
216-
((select count(1) from paper_planes where by = u.id and status <> 1) < 7)
217-
order by random() limit 1)
218-
where status = 1 and (by is null or extract(epoch from (utc_now()) - by_at) / 3600 > (24 * 7));
232+
set by_at = utc_now(), by = null, status = 3
233+
where status = 1 and thrown >= 7;
219234
end;
220235
$$
221236
language plpgsql;
@@ -411,7 +426,7 @@ create table paper_planes(
411426
status smallint not null,
412427
by integer,
413428
by_at timestamptz,
414-
thrown smallint default 0,
429+
thrown smallint default ,
415430

416431
constraint thrower_user foreign key (thrower) references users(id) on delete cascade
417432
);

src/Shared/Experiments/Doppelganger.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import Data.Maybe as DM
99
import Flame (Html)
1010
import Flame.Html.Attribute as HA
1111
import Flame.Html.Element as HE
12-
import Shared.Experiments.Types (DoppelgangerSection(..), ExperimentsMessage(..), ExperimentsModel, ShowingExperiment(..))
12+
import Shared.Experiments.Types (DoppelgangerSection(..), ExperimentsMessage(..), ExperimentsModel)
1313
import Shared.Unsafe as SU
1414
import Web.HTML.HTMLInputElement as WHHI
1515

1616
view ExperimentsModel Html ExperimentsMessage
1717
view model = HE.div [ HA.class' "word-chain duller" ]
18-
[ case model.section of
19-
ShowingDoppelganger ShowNextQuestion → showNextQuestion
20-
ShowingDoppelganger ShowMatches → showMatches
18+
[ case model.doppelganger.section of
19+
ShowNextQuestion → showNextQuestion
20+
ShowMatches → showMatches
2121
_ →
2222
if model.doppelganger.completed then
2323
HE.input [ HA.type' "button", HA.onClick FetchMatches, HA.class' "green-button", HA.value "Check results" ]

0 commit comments

Comments
 (0)