Skip to content

Commit 8f43e14

Browse files
committed
Merge branch 'fly'
2 parents e2166cf + 26af7fb commit 8f43e14

22 files changed

+480
-54
lines changed

.dockerignore

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file excludes paths from the Docker build context.
2+
#
3+
# By default, Docker's build context includes all files (and folders) in the
4+
# current directory. Even if a file isn't copied into the container it is still sent to
5+
# the Docker daemon.
6+
#
7+
# There are multiple reasons to exclude files from the build context:
8+
#
9+
# 1. Prevent nested folders from being copied into the container (ex: exclude
10+
# /assets/node_modules when copying /assets)
11+
# 2. Reduce the size of the build context and improve build time (ex. /build, /deps, /doc)
12+
# 3. Avoid sending files containing sensitive information
13+
#
14+
# More information on using .dockerignore is available here:
15+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
16+
17+
.dockerignore
18+
19+
# Ignore git, but keep git HEAD and refs to access current commit hash if needed:
20+
#
21+
# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat
22+
# d0b8727759e1e0e7aa3d41707d12376e373d5ecc
23+
.git
24+
!.git/HEAD
25+
!.git/refs
26+
27+
# Common development/test artifacts
28+
/cover/
29+
/doc/
30+
/test/
31+
/tmp/
32+
.elixir_ls
33+
34+
# Mix artifacts
35+
/_build/
36+
/deps/
37+
*.ez
38+
39+
# Generated on crash by the VM
40+
erl_crash.dump
41+
42+
# Static artifacts - These should be fetched and built inside the Docker image
43+
/assets/node_modules/
44+
/priv/static/assets/
45+
/priv/static/cache_manifest.json

.github/workflows/fly-deploy.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Fly Deploy
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
deploy:
8+
name: Deploy app
9+
runs-on: ubuntu-latest
10+
concurrency: deploy-group # optional: ensure only one action runs at a time
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: superfly/flyctl-actions/setup-flyctl@master
14+
- run: flyctl deploy --remote-only
15+
env:
16+
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

Dockerfile

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian
2+
# instead of Alpine to avoid DNS resolution issues in production.
3+
#
4+
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
5+
# https://hub.docker.com/_/ubuntu?tab=tags
6+
#
7+
# This file is based on these images:
8+
#
9+
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
10+
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20240612-slim - for the release image
11+
# - https://pkgs.org/ - resource for finding needed packages
12+
# - Ex: hexpm/elixir:1.17.1-erlang-26.2.5-debian-bullseye-20240612-slim
13+
#
14+
ARG ELIXIR_VERSION=1.17.1
15+
ARG OTP_VERSION=26.2.5
16+
ARG DEBIAN_VERSION=bullseye-20240612-slim
17+
18+
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
19+
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
20+
21+
FROM ${BUILDER_IMAGE} as builder
22+
23+
# install build dependencies
24+
RUN apt-get update -y && apt-get install -y build-essential git \
25+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
26+
27+
# prepare build dir
28+
WORKDIR /app
29+
30+
# install hex + rebar
31+
RUN mix local.hex --force && \
32+
mix local.rebar --force
33+
34+
# set build ENV
35+
ENV MIX_ENV="prod"
36+
37+
# install mix dependencies
38+
COPY mix.exs mix.lock ./
39+
RUN mix deps.get --only $MIX_ENV
40+
RUN mkdir config
41+
42+
# copy compile-time config files before we compile dependencies
43+
# to ensure any relevant config change will trigger the dependencies
44+
# to be re-compiled.
45+
COPY config/config.exs config/${MIX_ENV}.exs config/
46+
RUN mix deps.compile
47+
48+
COPY priv priv
49+
50+
COPY lib lib
51+
52+
COPY assets assets
53+
54+
# compile assets
55+
RUN mix assets.deploy
56+
57+
# Compile the release
58+
RUN mix compile
59+
60+
# Changes to config/runtime.exs don't require recompiling the code
61+
COPY config/runtime.exs config/
62+
63+
COPY rel rel
64+
RUN mix release
65+
66+
# start a new build stage so that the final image will only contain
67+
# the compiled release and other runtime necessities
68+
FROM ${RUNNER_IMAGE}
69+
70+
RUN apt-get update -y && \
71+
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
72+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
73+
74+
# Set the locale
75+
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
76+
77+
ENV LANG en_US.UTF-8
78+
ENV LANGUAGE en_US:en
79+
ENV LC_ALL en_US.UTF-8
80+
81+
WORKDIR "/app"
82+
RUN chown nobody /app
83+
84+
# set runner ENV
85+
ENV MIX_ENV="prod"
86+
87+
# Only copy the final release from the build stage
88+
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/mjw ./
89+
90+
USER nobody
91+
92+
# If using an environment that doesn't automatically reap zombie processes, it is
93+
# advised to add an init process such as tini via `apt-get install`
94+
# above and adding an entrypoint. See https://github.com/krallin/tini for details
95+
# ENTRYPOINT ["/tini", "--"]
96+
97+
CMD ["/app/bin/server"]

TODO.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# TODO
22

3+
- CD: https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/
34
- Put everyone's discards in front of their hand (suggested by Mom)
45
- Bug: Can't undo to get first discarded tile of the game from bot (human went last, missed a discarded tile, can't go back to beginning of game). If there are no human actions to go back to, Undo should return to beginning of game.
56
- Bug: Undoing a win after a bot drew a tile hung bot (workaround: paused & unpaused bots)
@@ -31,6 +32,8 @@
3132
- 2-click instead of drag & drop. First click will pause other players.
3233
- Display everyone's wind direction instead of staircase
3334
- Animate deal: 4 at a time goes into people's hand
35+
- Sanity check for (possible race condition) bug: rearranging hand leads to duplicate tiles. Last time it happened after peek tile got put into hand, and user drag & dropped that new tile.
36+
- Remember sorted hand so it doesn't get reshuffled when losing internet connection
3437

3538
## Future rule enforcement
3639
- Winning tile was the last one picked up

config/prod.exs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import Config
55
# manifest is generated by the `mix assets.deploy` task,
66
# which you should run after static files are built and
77
# before starting your production server.
8-
config :mjw, MjwWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
8+
config :mjw, MjwWeb.Endpoint,
9+
url: [host: "mahjongwind.com", port: 443, scheme: "https"],
10+
cache_static_manifest: "priv/static/cache_manifest.json",
11+
check_origin: ["//mahjongwind.com"],
12+
force_ssl: [
13+
host: nil,
14+
rewrite_on: [:x_forwarded_port, :x_forwarded_proto],
15+
hsts: true
16+
]
917

1018
# Configures Swoosh API Client
1119
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: Mjw.Finch

config/runtime.exs

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import Config
1717
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
1818
# script that automatically sets the env var above.
1919
if System.get_env("PHX_SERVER") do
20-
config :mjw, MjwWeb.Endpoint, server: true
20+
config :mjw, MjwWeb.Endpoint,
21+
server: true,
22+
url: [host: "mahjongwind.com"]
2123
end
2224

2325
if config_env() == :prod do
@@ -50,7 +52,7 @@ if config_env() == :prod do
5052
You can generate one by calling: mix phx.gen.secret
5153
"""
5254

53-
host = System.get_env("PHX_HOST") || "example.com"
55+
host = System.get_env("PHX_HOST") || "mahjongwind.com"
5456
port = String.to_integer(System.get_env("PORT") || "4000")
5557

5658
config :mjw, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")

fly.toml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# fly.toml app configuration file generated for mahjongwind on 2024-06-27T21:09:54+08:00
2+
#
3+
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
4+
#
5+
6+
app = 'mahjongwind'
7+
primary_region = 'nrt'
8+
kill_signal = 'SIGTERM'
9+
10+
[build]
11+
12+
[env]
13+
PHX_HOST = 'mahjongwind.com'
14+
PORT = '8080'
15+
16+
[http_service]
17+
internal_port = 8080
18+
force_https = true
19+
auto_stop_machines = true
20+
auto_start_machines = true
21+
min_machines_running = 0
22+
processes = ['app']
23+
24+
[http_service.concurrency]
25+
type = 'connections'
26+
hard_limit = 1000
27+
soft_limit = 1000
28+
29+
[[vm]]
30+
memory = '1gb'
31+
cpu_kind = 'shared'
32+
cpus = 1

lib/mjw_web/components/layouts/root.html.heex

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<meta name="csrf-token" content={get_csrf_token()} />
77
<.live_title><%= assigns[:page_title] || "Mahjong Wind" %></.live_title>
88
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
9-
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}></script>
9+
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
10+
</script>
1011
</head>
1112
<body>
1213
<%= @inner_content %>

lib/mjw_web/live/game_live/current_user_seat_component.html.heex

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
<div id={@id}>
22
<div class={"tiles flex-wrap turn-glow-0-#{if @current_user_drawing, do: "t"}"}>
3-
<div id="hiddengongs-0" phx-hook="Drag" phx-target="#game" class={"hiddengong-tiles dropzone#{if @win_declared_seatno && @win_declared_seatno != @current_user_seatno && @seat.win_expose, do: " exposed-loser-hand"}"}>
3+
<div
4+
id="hiddengongs-0"
5+
phx-hook="Drag"
6+
phx-target="#game"
7+
class={"hiddengong-tiles dropzone#{if @win_declared_seatno && @win_declared_seatno != @current_user_seatno && @seat.win_expose, do: " exposed-loser-hand"}"}
8+
>
49
<%= for tile <- @seat.hiddengongs do %>
5-
<img id={tile} src={"/images/tiles/#{Mjw.Tile.without_id(tile)}.png"} alt="" class="tile draggable" />
10+
<img
11+
id={tile}
12+
src={"/images/tiles/#{Mjw.Tile.without_id(tile)}.png"}
13+
alt=""
14+
class="tile draggable"
15+
/>
616
<% end %>
717
<div class="dropzone-description">Hidden gong</div>
818
</div>
919

1020
<div id="exposed-0" phx-hook="Drag" phx-target="#game" class="exposed-tiles dropzone">
1121
<%= for tile <- @seat.exposed do %>
12-
<img id={tile} src={"/images/tiles/#{Mjw.Tile.without_id(tile)}.png"} alt="" class="tile draggable" />
22+
<img
23+
id={tile}
24+
src={"/images/tiles/#{Mjw.Tile.without_id(tile)}.png"}
25+
alt=""
26+
class="tile draggable"
27+
/>
1328
<% end %>
1429
<div class="dropzone-description">Exposed tiles</div>
1530
</div>
1631

17-
<div id="wintile-0" phx-hook="Drag" phx-target="#game" class={"wintile-tiles#{if !@win_declared_seatno || @win_declared_seatno == @current_user_seatno, do: " dropzone"}"}>
32+
<div
33+
id="wintile-0"
34+
phx-hook="Drag"
35+
phx-target="#game"
36+
class={"wintile-tiles#{if !@win_declared_seatno || @win_declared_seatno == @current_user_seatno, do: " dropzone"}"}
37+
>
1838
<%= if @seat.wintile do %>
19-
<img id={@seat.wintile} src={"/images/tiles/#{Mjw.Tile.without_id(@seat.wintile)}.png"} alt="" class="tile cursor-not-allowed" />
39+
<img
40+
id={@seat.wintile}
41+
src={"/images/tiles/#{Mjw.Tile.without_id(@seat.wintile)}.png"}
42+
alt=""
43+
class="tile cursor-not-allowed"
44+
/>
2045
<% end %>
2146
<%= if !@win_declared_seatno || @win_declared_seatno == @current_user_seatno do %>
2247
<div class="dropzone-description">Winning tile</div>
@@ -25,7 +50,12 @@
2550

2651
<div class="line-break"></div>
2752

28-
<div id="concealed-0" phx-hook="Drag" phx-target="#game" class={"concealed-tiles dropzone current-user-discarding-#{if @current_user_discarding, do: "t"} enable-pull-from-discards-#{if @available_discard_tile, do: "t"} concealed-loser-hand-#{if @concealed_loser_hand, do: "t"}"}>
53+
<div
54+
id="concealed-0"
55+
phx-hook="Drag"
56+
phx-target="#game"
57+
class={"concealed-tiles dropzone current-user-discarding-#{if @current_user_discarding, do: "t"} enable-pull-from-discards-#{if @available_discard_tile, do: "t"} concealed-loser-hand-#{if @concealed_loser_hand, do: "t"}"}
58+
>
2959
<%= for tile <- @seat.concealed do %>
3060
<img
3161
id={tile}

lib/mjw_web/live/game_live/dice_component.html.heex

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
<div class={"dice-#{@previous_roller_relative_position}"}>
33
<%= if @rolled_dice do %>
44
<%= for {die, i} <- Enum.with_index(@dice) do %>
5-
<img src={"/images/dice/d#{die}.png"} alt="" class={"die die-#{i}#{if @raw_event in [:rolled_for_first_dealer, :rolled_for_deal], do: " #{@game_state}-#{@previous_roller_relative_position}"}"} />
5+
<img
6+
src={"/images/dice/d#{die}.png"}
7+
alt=""
8+
class={"die die-#{i}#{if @raw_event in [:rolled_for_first_dealer, :rolled_for_deal], do: " #{@game_state}-#{@previous_roller_relative_position}"}"}
9+
/>
610
<% end %>
711
<% end %>
812
</div>

0 commit comments

Comments
 (0)