Skip to content

Commit

Permalink
Multiplayer sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
chouzar committed Jan 27, 2024
1 parent fff5868 commit d306627
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 296 deletions.
12 changes: 6 additions & 6 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
# You typically do not need to edit this file

packages = [
{ name = "chip", version = "0.1.8", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "chip", source = "hex", outer_checksum = "F7968E064117EC08C40EA7BAC4F36FFCD72E3FC3340558EFC1E843AC628AD2F2" },
{ name = "chip", version = "0.1.9", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "chip", source = "hex", outer_checksum = "3B8BC46E21E4163F85F5FD0EEE4702E5DB3E5F7A9DB52F625C30C1EE7E3FBAF7" },
{ name = "envoy", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "F9D3AFCF8627E8CBD0FA7296D7187BD024B8DBCF56A152E111A8ECEE27E5E45D" },
{ name = "exception", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "984401CFC95BCA87C391E36194D2B9E5B946467D44893FADB1CA4ACD4B7A29CE" },
{ name = "gleam_crypto", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "DE1FC4E631CA374AB29CCAEAC043EE171B86114D7DC66DD483F0A93BF0C4C6FF" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_http", version = "3.5.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "C2FC3322203B16F897C1818D9810F5DEFCE347F0751F3B44421E1261277A7373" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "glisten", version = "0.10.2", build_tools = ["gleam"], requirements = ["gleam_otp", "gleam_stdlib", "gleam_erlang"], otp_app = "glisten", source = "hex", outer_checksum = "461AE0EC3C2BDCC8B581A0CE07D49597A61226B410A3FE7E237EB924D0D18536" },
{ name = "glisten", version = "0.10.2", build_tools = ["gleam"], requirements = ["gleam_otp", "gleam_erlang", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "461AE0EC3C2BDCC8B581A0CE07D49597A61226B410A3FE7E237EB924D0D18536" },
{ name = "marceau", version = "1.1.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "1AAD727A30BE0F95562C3403BB9B27C823797AD90037714255EEBF617B1CDA81" },
{ name = "mist", version = "0.17.0", build_tools = ["gleam"], requirements = ["gleam_otp", "gleam_http", "glisten", "gleam_erlang", "gleam_stdlib"], otp_app = "mist", source = "hex", outer_checksum = "DA8ACEE52C1E4892A75181B3166A4876D8CBC69D555E4770250BC84C80F75524" },
{ name = "mist", version = "0.17.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_erlang", "gleam_otp", "glisten", "gleam_stdlib"], otp_app = "mist", source = "hex", outer_checksum = "DA8ACEE52C1E4892A75181B3166A4876D8CBC69D555E4770250BC84C80F75524" },
{ name = "nakai", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "nakai", source = "hex", outer_checksum = "F6FFED9EF4B0E14C7A09B2FB87B42D3A93EFE024FD0299C11F041E92321163A6" },
{ name = "simplifile", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "359CD7006E2F69255025C858CCC6407C11A876EC179E6ED1E46809E8DC6B1AAD" },
{ name = "simplifile", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5B6E70E55D9361F360F0C0ECE8FD8E45F0BC2051657E0E94A50277108322E992" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
{ name = "wisp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_http", "exception", "gleam_crypto", "simplifile", "gleam_erlang", "marceau", "gleam_json", "gleam_stdlib", "mist"], otp_app = "wisp", source = "hex", outer_checksum = "744FF91702078301BDF8FE76F26C14B658A7D151D867FA6995762318ED2536A0" },
{ name = "wisp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "mist", "gleam_crypto", "marceau", "gleam_http", "gleam_json", "simplifile", "gleam_stdlib", "exception"], otp_app = "wisp", source = "hex", outer_checksum = "744FF91702078301BDF8FE76F26C14B658A7D151D867FA6995762318ED2536A0" },
]

[requirements]
Expand Down
4 changes: 2 additions & 2 deletions priv/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const body = document.querySelector("body");
const socket = new WebSocket("wss://localhost:4444/events");

socket.onopen = (_event) => {
socket.send("session:" + session)
};

socket.onmessage = (event) => {
Expand All @@ -12,10 +13,9 @@ socket.onmessage = (event) => {
};

window.addEventListener('click', (event) => {
console.log(event.target);
if (event.target.dataset.event) {
const action = event.target.dataset.event
const data = JSON.stringify(event.target.dataset);
socket.send(new Blob([session, "\n\n", action, "\n\n", data]));
socket.send(new Blob([action, "\n\n", data]));
}
});
8 changes: 3 additions & 5 deletions src/luster.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import gleam/http/request
import gleam/http/response
import luster/store
import luster/web
import luster/web/pages/game
import luster/web/tea_game
import mist
import chip
import luster/session

// TODO: Create a web, games and luster/runtime contexts
// TODO: Add a proper supervision tree
Expand All @@ -20,12 +19,11 @@ import luster/session
pub fn main() -> Nil {
let assert Ok(store) = store.start()
let assert Ok(registry) = chip.start()
let session = session.Session(store: store, registry: registry)

let request_pipeline = fn(request: request.Request(mist.Connection)) -> response.Response(
mist.ResponseData,
) {
web.router(request, session)
web.router(request, registry, store)
}

let assert Ok(_server) =
Expand All @@ -36,7 +34,7 @@ pub fn main() -> Nil {
keyfile: env("LUSTER_KEY"),
)

store.create(store, game.init())
store.create(store, tea_game.init())
process.sleep_forever()
}

Expand Down
155 changes: 0 additions & 155 deletions src/luster/events.gleam

This file was deleted.

10 changes: 5 additions & 5 deletions src/luster/game/cardfield.gleam → src/luster/game.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ fn formation_triplet(card_a: Card, card_b: Card, card_c: Card) -> Formation {

let is_pair = ra == rb || rb == rc || rc == ra
let is_triplet = ra == rb && rb == rc
let is_straight = sa == sb && sb == sc
let is_flush = { ra + 1 == rb } && { rb + 1 == rc }
let is_flush = sa == sb && sb == sc
let is_straight = { ra + 1 == rb } && { rb + 1 == rc }

case is_pair, is_triplet, is_straight, is_flush {
case is_pair, is_triplet, is_flush, is_straight {
_bool, False, True, True -> StraightFlush
_bool, True, False, False -> ThreeOfAKind
_bool, False, True, False -> Straight
_bool, False, False, True -> Flush
_bool, False, False, True -> Straight
_bool, False, True, False -> Flush
True, False, False, False -> Pair
_bool, _bool, _bool, _bool -> HighCard
}
Expand Down
Empty file added src/luster/player.gleam
Empty file.
10 changes: 0 additions & 10 deletions src/luster/session.gleam

This file was deleted.

28 changes: 15 additions & 13 deletions src/luster/web.gleam
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
import gleam/bit_array
import gleam/bytes_builder
import gleam/erlang
import gleam/erlang/process
import gleam/http
import gleam/http/request
import gleam/http/response
import gleam/string
import gleam/uri
import luster/events
import luster/store
import luster/web/pages/game
import luster/web/pages/home
import luster/web/socket
import luster/web/tea_game
import luster/web/tea_home
import chip
import mist
import nakai
import nakai/html
import nakai/html/attrs
import luster/session

pub fn router(
request: request.Request(mist.Connection),
session: session.Session(game.Model, message),
registry: process.Subject(chip.Message(String, socket.Message)),
store: process.Subject(store.Message(tea_game.Model)),
) -> response.Response(mist.ResponseData) {
case request.method, request.path_segments(request) {
http.Get, [] -> {
let records = store.all(session.store)
let records = store.all(store)

home.Model(records)
|> home.view()
tea_home.Model(records)
|> tea_home.view()
|> render(with: fn(body) { layout("", body) })
}

http.Post, ["battleline"] -> {
let model = game.init()
let _ = store.create(session.store, model)
let model = tea_game.init()
let _ = store.create(store, model)
redirect("/")
}

http.Get, ["battleline", id] -> {
case store.one(session.store, id) {
case store.one(store, id) {
Ok(model) ->
model
|> game.view()
|> tea_game.view()
|> render(with: fn(body) { layout(id, body) })

Error(_) -> redirect("/")
}
}

http.Get, ["events"] -> {
events.start(request, session)
socket.start(request, registry, store)
}

http.Get, ["assets", ..] -> {
Expand Down
4 changes: 2 additions & 2 deletions src/luster/web/codec.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dynamic.{type DecodeError, type Dynamic}
import gleam/int
import gleam/result.{try}
import luster/game/cardfield.{
import luster/game.{
type Card, type Player, type Slot, type Suit, Club, Diamond, Heart, Player1,
Player2, Slot1, Slot2, Slot3, Slot4, Slot5, Slot6, Slot7, Slot8, Slot9, Spade,
}
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn decoder_slot(data: Dynamic) -> Result(Slot, List(DecodeError)) {

pub fn decoder_card(data: Dynamic) -> Result(Card, List(DecodeError)) {
dynamic.decode2(
cardfield.Card,
game.Card,
dynamic.field("rank", of: decode_rank),
dynamic.field("suit", of: decode_suit),
)(data)
Expand Down
Loading

0 comments on commit d306627

Please sign in to comment.