Improve room joining

This commit is contained in:
Correl Roush 2020-05-06 20:03:31 -04:00
parent a534184ff1
commit 8e4a7ff564
5 changed files with 100 additions and 114 deletions

View file

@ -29,23 +29,26 @@ var app = Elm.Main.init({
})
app.ports.joinRoom.subscribe(options => {
let channel = socket.channel(
"room:" + options.room,
{playerName: options.playerName}
)
let channel = socket.channel("room:" + options.room, {})
let presences = {}
channel.on("presence_state", state => {
console.log("presence state", state)
presences = Presence.syncState(presences, state)
app.ports.gotPresence.send(presences)
})
channel.on("presence_diff", diff => {
console.log("presence diff", diff)
presences = Presence.syncDiff(presences, diff)
app.ports.gotPresence.send(presences)
})
app.ports.newProfile.subscribe(profile => {
channel.push("new_profile", { "name": profile.playerName })
})
channel.join()
.receive("ok", resp => {
console.log("Joined successfully", resp);
app.ports.joinedRoom.send(options.room);
})
.receive("error", resp => { console.log("Unable to join", resp) })
channel.on("presence_state", state => {
presences = Presence.syncState(presences, state)
app.ports.gotPresence.send(presences)
})
channel.on("presence_diff", diff => {
presences = Presence.syncDiff(presences, diff)
app.ports.gotPresence.send(presences)
})
})

View file

@ -2,12 +2,16 @@ port module PlanningPokerAPI exposing
( gotPresence
, joinRoom
, joinedRoom
, newProfile
)
import Json.Decode exposing (Value)
port joinRoom : { room : String, playerName : String } -> Cmd msg
port joinRoom : { room : String } -> Cmd msg
port newProfile : { playerName : String } -> Cmd msg
port joinedRoom : (String -> msg) -> Sub msg

View file

@ -53,7 +53,10 @@ update key msg model =
"a0fd1422-abd9-434e-9d7c-883294b2992c"
in
( model
, API.joinRoom { room = room, playerName = model.playerName }
, Cmd.batch
[ API.joinRoom { room = room }
, API.newProfile { playerName = model.playerName }
]
)
JoinedRoom room ->

View file

@ -22,7 +22,7 @@ import PlanningPokerUI as UI
type alias Model =
{ room : Maybe Room
{ room : Room
, player : String
, playerName : String
, showVotes : Bool
@ -77,24 +77,25 @@ init { id, player, roomName, playerName } =
, players = Dict.empty
}
in
( { room = Just room
( { room = room
, player = player
, playerName = playerName
, showVotes = False
}
, Cmd.none
, API.joinRoom { room = id }
)
update : Nav.Key -> Msg -> Model -> ( Model, Cmd Msg )
update key msg model =
case model.room of
Just room ->
let
room =
model.room
in
case msg of
Vote value ->
( { model
| room =
Just
{ room
| players =
Dict.update
@ -114,7 +115,6 @@ update key msg model =
Reset ->
( { model
| room =
Just
{ room
| players =
Dict.map
@ -130,20 +130,8 @@ update key msg model =
( { model | playerName = newName }, Cmd.none )
JoinRoom ->
let
newRoom =
{ room
| players =
Dict.insert model.player
{ level = Participant
, name = model.playerName
, vote = Nothing
}
room.players
}
in
( model
, API.joinRoom { room = room.id, playerName = model.playerName }
, API.newProfile { playerName = model.playerName }
)
GotPresence value ->
@ -153,49 +141,34 @@ update key msg model =
newRoom =
{ room | players = players }
in
( { model | room = Just newRoom }, Cmd.none )
( { model | room = newRoom }, Cmd.none )
Err _ ->
( model, Cmd.none )
Nothing ->
case msg of
_ ->
( model, Cmd.none )
view : Model -> Document Msg
view model =
case model.room of
Just room ->
let
maybePlayer =
Dict.get model.player room.players
Dict.get model.player model.room.players
in
case maybePlayer of
Just player ->
UI.toDocument
{ title = room.name
{ title = model.room.name
, body =
[ navBar { title = room.name, playerName = player.name }
, viewRoom model.player room model.showVotes
[ navBar { title = model.room.name, playerName = player.name }
, viewRoom model.player model.room model.showVotes
]
}
Nothing ->
UI.toDocument
{ title = room.name
{ title = model.room.name
, body =
[ navBar { title = room.name, playerName = "" }
, joinForm room model.playerName
]
}
_ ->
UI.toDocument
{ title = "Loading Room..."
, body =
[ UI.heroText [ centerX, centerY ] "Loading..."
[ navBar { title = model.room.name, playerName = "" }
, joinForm model.room model.playerName
]
}

View file

@ -10,14 +10,17 @@ defmodule PlanningpokerWeb.RoomChannel do
|> assign(:player_name, params["playerName"])}
end
def handle_info(:after_join, socket) do
push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
def handle_in("new_profile", %{"name" => name}, socket) do
{:ok, _} = Presence.track(
socket,
"player:#{socket.assigns.player_id}",
%{
name: socket.assigns.player_name,
online_at: inspect(System.system_time(:second))
})
push(socket, "presence_state", Presence.list(socket))
name: name
}
)
{:noreply, socket}
end
end