├── .formatter.exs ├── .gitignore ├── README.md ├── assets ├── .babelrc ├── css │ └── app.scss ├── js │ └── app.js ├── package-lock.json ├── package.json ├── static │ ├── favicon.ico │ ├── images │ │ ├── card.jpg │ │ └── cards │ │ │ ├── eight.png │ │ │ ├── eleven.png │ │ │ ├── fifteen.png │ │ │ ├── five.png │ │ │ ├── four.png │ │ │ ├── fourteen.png │ │ │ ├── nine.png │ │ │ ├── one.png │ │ │ ├── seven.png │ │ │ ├── six.png │ │ │ ├── ten.png │ │ │ ├── thirteen.png │ │ │ ├── three.png │ │ │ ├── twelve.png │ │ │ └── two.png │ └── robots.txt └── webpack.config.js ├── config ├── config.exs ├── dev.exs ├── prod.exs ├── prod.secret.exs └── test.exs ├── gleam.toml ├── lib ├── game.ex ├── game │ ├── application.ex │ ├── card.ex │ ├── engine.ex │ ├── generator.ex │ ├── hash.ex │ ├── process.ex │ ├── random.ex │ ├── registry.ex │ ├── session.ex │ ├── session_supervisor.ex │ └── strucord.ex ├── game_web.ex └── game_web │ ├── channels │ └── user_socket.ex │ ├── controllers │ └── page_controller.ex │ ├── endpoint.ex │ ├── gettext.ex │ ├── live │ ├── page_live.ex │ └── page_live.html.leex │ ├── router.ex │ ├── telemetry.ex │ ├── templates │ ├── layout │ │ ├── app.html.eex │ │ ├── live.html.leex │ │ └── root.html.leex │ └── page │ │ └── index.html.eex │ └── views │ ├── error_helpers.ex │ ├── error_view.ex │ ├── layout_view.ex │ └── page_view.ex ├── mix.exs ├── mix.lock ├── priv └── gettext │ ├── en │ └── LC_MESSAGES │ │ └── errors.po │ └── errors.pot ├── rebar.config ├── src ├── game.app.src └── game.gleam └── test ├── game └── game_test.exs ├── game_web ├── live │ └── page_live_test.exs └── views │ ├── error_view_test.exs │ └── layout_view_test.exs ├── support ├── channel_case.ex └── conn_case.ex └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:phoenix], 3 | inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | game-*.tar 24 | 25 | # If NPM crashes, it generates a log, let's ignore it too. 26 | npm-debug.log 27 | 28 | # The directory NPM downloads your dependencies sources to. 29 | /assets/node_modules/ 30 | 31 | # Since we are building assets from assets/, 32 | # we ignore priv/static. You may want to comment 33 | # this depending on your deployment strategy. 34 | /priv/static/ 35 | 36 | # gleam 37 | /gen/ 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Installation 2 | 3 | To install on macOS 4 | 5 | ``` 6 | brew install gleam 7 | ``` 8 | 9 | ## Objectives 10 | 11 | The entire project centers around a single Gleam source file. The game [engine](https://github.com/toranb/elixir-gleam-match/blob/master/src/game.gleam) is driven from the elixir [wrapper](https://github.com/toranb/elixir-gleam-match/blob/master/lib/game/engine.ex) 12 | 13 | ```elixir 14 | defmodule Game.Engine do 15 | def flip(%__MODULE__{} = struct, flip_id) when is_binary(flip_id) do 16 | gleamify(struct, fn record -> 17 | :game.flip(record, flip_id) 18 | end) 19 | end 20 | 21 | def unflip(%__MODULE__{} = struct) do 22 | gleamify(struct, fn record -> 23 | :game.unflip(record) 24 | end) 25 | end 26 | 27 | def prepare_restart(%__MODULE__{} = struct) do 28 | gleamify(struct, fn record -> 29 | :game.prepare_restart(record) 30 | end) 31 | end 32 | end 33 | ``` 34 | 35 | ### flip 36 | 37 | ![flipp](https://user-images.githubusercontent.com/147411/67634906-5d400800-f88f-11e9-8d3e-125fc09268a1.gif) 38 | 39 | This function is executed when the player clicks a playing card. Simply enumerate the cards and mark the one with the id as `flipped` using a boolean. If 2 cards have been flipped at this point attempt to match them by the id. When a match is found mark each card as `paired` and set the `flipped` for both back to false. Finally, if all the cards are paired declare the game over by marking the `winner` using a boolean value. 40 | 41 | One edge case here is that if 2 cards are flipped but they do *not* match, you need to set the `animating` boolean to true. This will later instruct the engine to fire `unflip`. 42 | 43 | ### unflip 44 | 45 | ![unfliip](https://user-images.githubusercontent.com/147411/67634902-4ac5ce80-f88f-11e9-8bbe-451093d55e4d.gif) 46 | 47 | This function is executed after a 2nd card has flipped but failed to match. Simply enumerate the cards and mark the `flipped` attribute to false for any non paired card. You will also need to revert `animating` to false so the flip function works properly. 48 | 49 | ### prepare_restart 50 | 51 | ![prepareRestart](https://user-images.githubusercontent.com/147411/67634990-ed7e4d00-f88f-11e9-8af0-03c456c2e466.gif) 52 | 53 | This function is executed after the player decides to play again. Simply enumerate the cards and mark all `paired` and `flipped` attributes to false. 54 | 55 | ## Debugging Tips 56 | 57 | To print something in the Gleam source code import the io module and use `io.debug` 58 | 59 | ```elixir 60 | import gleam/io 61 | 62 | io.debug("Hello World!") 63 | ``` 64 | 65 | ## Learning Gleam 66 | 67 | Because the language is so young today the best place to dive in is the [getting started](https://gleam.run/) guide 68 | 69 | ## License 70 | 71 | Copyright © 2020 Toran Billups https://toranbillups.com 72 | 73 | Licensed under the MIT License 74 | -------------------------------------------------------------------------------- /assets/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /assets/css/app.scss: -------------------------------------------------------------------------------- 1 | /* This file is for your main application css. */ 2 | @import "../node_modules/nprogress/nprogress.css"; 3 | 4 | html { 5 | color: white; 6 | background: rgb(147,209,245); 7 | } 8 | .cards .card { 9 | position: relative; 10 | display: inline-block; 11 | width: 100px; 12 | height: 150px; 13 | margin: 1em 2em; 14 | } 15 | .cards .card .front, 16 | .cards .card .back { 17 | border-radius: 5px; 18 | position: absolute; 19 | left: 0; 20 | right: 0; 21 | top: 0; 22 | bottom: 0; 23 | width: 100%; 24 | height: 100%; 25 | background-color: white; 26 | backface-visibility: hidden; 27 | transition: transform 0.6s; 28 | transform-style: preserve-3d; 29 | } 30 | .cards .card .back { 31 | background-image: url("/images/card.jpg"); 32 | background-size: 90%; 33 | background-position: center; 34 | background-repeat: no-repeat; 35 | } 36 | .cards .card .front { 37 | transform: rotateY(-180deg); 38 | background-size: 90%; 39 | background-repeat: no-repeat; 40 | background-position: center; 41 | } 42 | .cards .card.flipped .back, 43 | .cards .card.found .back { 44 | transform: rotateY(180deg); 45 | } 46 | .cards .card.flipped .front, 47 | .cards .card.found .front { 48 | transform: rotateY(0deg); 49 | } 50 | .cards .card.found { 51 | opacity: 0.3; 52 | } 53 | .splash { 54 | position: absolute; 55 | left: 0; 56 | right: 0; 57 | top: 0; 58 | bottom: 0; 59 | background-color: rgba(0, 0, 0, 0.5); 60 | } 61 | .splash .content { 62 | position: absolute; 63 | left: 0; 64 | right: 0; 65 | top: 0; 66 | bottom: 0; 67 | width: 400px; 68 | height: 200px; 69 | margin: auto; 70 | text-align: center; 71 | background-color: rgba(51, 51, 51, 0.9); 72 | border-radius: 5px; 73 | padding: 1em; 74 | color: white; 75 | } 76 | .splash .content button { 77 | margin-top: 1.0em; 78 | background-color: #444; 79 | padding: 5px 20px; 80 | border-radius: 4px; 81 | border: 1px solid #555; 82 | color: white; 83 | font-size: 1.4em; 84 | } 85 | .center-all { 86 | display: flex; 87 | align-items: center; 88 | justify-content: center; 89 | } 90 | .text-white { 91 | color: white; 92 | } 93 | -------------------------------------------------------------------------------- /assets/js/app.js: -------------------------------------------------------------------------------- 1 | // We need to import the CSS so that webpack will load it. 2 | // The MiniCssExtractPlugin is used to separate it out into 3 | // its own CSS file. 4 | import "../css/app.scss" 5 | 6 | // webpack automatically bundles all modules in your 7 | // entry points. Those entry points can be configured 8 | // in "webpack.config.js". 9 | // 10 | // Import deps with the dep name or local files with a relative path, for example: 11 | // 12 | // import {Socket} from "phoenix" 13 | // import socket from "./socket" 14 | // 15 | import "phoenix_html" 16 | import {Socket} from "phoenix" 17 | import NProgress from "nprogress" 18 | import {LiveSocket} from "phoenix_live_view" 19 | 20 | let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") 21 | let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) 22 | 23 | // Show progress bar on live navigation and form submits 24 | window.addEventListener("phx:page-loading-start", info => NProgress.start()) 25 | window.addEventListener("phx:page-loading-stop", info => NProgress.done()) 26 | 27 | // connect if there are any LiveViews on the page 28 | liveSocket.connect() 29 | 30 | // expose liveSocket on window for web console debug logs and latency simulation: 31 | // >> liveSocket.enableDebug() 32 | // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session 33 | // >> liveSocket.disableLatencySim() 34 | window.liveSocket = liveSocket 35 | 36 | -------------------------------------------------------------------------------- /assets/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "repository": {}, 3 | "description": " ", 4 | "license": "MIT", 5 | "scripts": { 6 | "deploy": "webpack --mode production", 7 | "watch": "webpack --mode development --watch" 8 | }, 9 | "dependencies": { 10 | "phoenix": "file:../deps/phoenix", 11 | "phoenix_html": "file:../deps/phoenix_html", 12 | "phoenix_live_view": "file:../deps/phoenix_live_view", 13 | "nprogress": "^0.2.0" 14 | }, 15 | "devDependencies": { 16 | "@babel/core": "^7.0.0", 17 | "@babel/preset-env": "^7.0.0", 18 | "babel-loader": "^8.0.0", 19 | "copy-webpack-plugin": "^5.1.1", 20 | "css-loader": "^3.4.2", 21 | "sass-loader": "^8.0.2", 22 | "node-sass": "^4.13.1", 23 | "hard-source-webpack-plugin": "^0.13.1", 24 | "mini-css-extract-plugin": "^0.9.0", 25 | "optimize-css-assets-webpack-plugin": "^5.0.1", 26 | "terser-webpack-plugin": "^2.3.2", 27 | "webpack": "4.41.5", 28 | "webpack-cli": "^3.3.2" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /assets/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/favicon.ico -------------------------------------------------------------------------------- /assets/static/images/card.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/card.jpg -------------------------------------------------------------------------------- /assets/static/images/cards/eight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/eight.png -------------------------------------------------------------------------------- /assets/static/images/cards/eleven.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/eleven.png -------------------------------------------------------------------------------- /assets/static/images/cards/fifteen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/fifteen.png -------------------------------------------------------------------------------- /assets/static/images/cards/five.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/five.png -------------------------------------------------------------------------------- /assets/static/images/cards/four.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/four.png -------------------------------------------------------------------------------- /assets/static/images/cards/fourteen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/fourteen.png -------------------------------------------------------------------------------- /assets/static/images/cards/nine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/nine.png -------------------------------------------------------------------------------- /assets/static/images/cards/one.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/one.png -------------------------------------------------------------------------------- /assets/static/images/cards/seven.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/seven.png -------------------------------------------------------------------------------- /assets/static/images/cards/six.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/six.png -------------------------------------------------------------------------------- /assets/static/images/cards/ten.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/ten.png -------------------------------------------------------------------------------- /assets/static/images/cards/thirteen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/thirteen.png -------------------------------------------------------------------------------- /assets/static/images/cards/three.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/three.png -------------------------------------------------------------------------------- /assets/static/images/cards/twelve.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/twelve.png -------------------------------------------------------------------------------- /assets/static/images/cards/two.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toranb/elixir-gleam-match/2102b071de2d45a7b528fc4fc7172e62dc72ce50/assets/static/images/cards/two.png -------------------------------------------------------------------------------- /assets/static/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /assets/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const glob = require('glob'); 3 | const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); 4 | const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 5 | const TerserPlugin = require('terser-webpack-plugin'); 6 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 7 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 8 | 9 | module.exports = (env, options) => { 10 | const devMode = options.mode !== 'production'; 11 | 12 | return { 13 | optimization: { 14 | minimizer: [ 15 | new TerserPlugin({ cache: true, parallel: true, sourceMap: devMode }), 16 | new OptimizeCSSAssetsPlugin({}) 17 | ] 18 | }, 19 | entry: { 20 | 'app': glob.sync('./vendor/**/*.js').concat(['./js/app.js']) 21 | }, 22 | output: { 23 | filename: '[name].js', 24 | path: path.resolve(__dirname, '../priv/static/js'), 25 | publicPath: '/js/' 26 | }, 27 | devtool: devMode ? 'eval-cheap-module-source-map' : undefined, 28 | module: { 29 | rules: [ 30 | { 31 | test: /\.js$/, 32 | exclude: /node_modules/, 33 | use: { 34 | loader: 'babel-loader' 35 | } 36 | }, 37 | { 38 | test: /\.[s]?css$/, 39 | use: [ 40 | MiniCssExtractPlugin.loader, 41 | 'css-loader', 42 | 'sass-loader', 43 | ], 44 | } 45 | ] 46 | }, 47 | plugins: [ 48 | new MiniCssExtractPlugin({ filename: '../css/app.css' }), 49 | new CopyWebpackPlugin([{ from: 'static/', to: '../' }]) 50 | ] 51 | .concat(devMode ? [new HardSourceWebpackPlugin()] : []) 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | # 4 | # This configuration file is loaded before any dependency and 5 | # is restricted to this project. 6 | 7 | # General application configuration 8 | use Mix.Config 9 | 10 | # Configures the endpoint 11 | config :game, GameWeb.Endpoint, 12 | url: [host: "localhost"], 13 | secret_key_base: "GwLnkgaSOj7v/g2aAYLHYfwkOA+Si55i4EzIMdlMJfvDHNm8hyLKpe4FRFvemFC3", 14 | render_errors: [view: GameWeb.ErrorView, accepts: ~w(html json), layout: false], 15 | pubsub_server: Game.PubSub, 16 | live_view: [signing_salt: "oAKTjPAE"] 17 | 18 | # Configures Elixir's Logger 19 | config :logger, :console, 20 | format: "$time $metadata[$level] $message\n", 21 | metadata: [:request_id] 22 | 23 | # Use Jason for JSON parsing in Phoenix 24 | config :phoenix, :json_library, Jason 25 | 26 | # Import environment specific config. This must remain at the bottom 27 | # of this file so it overrides the configuration defined above. 28 | import_config "#{Mix.env()}.exs" 29 | -------------------------------------------------------------------------------- /config/dev.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # For development, we disable any cache and enable 4 | # debugging and code reloading. 5 | # 6 | # The watchers configuration can be used to run external 7 | # watchers to your application. For example, we use it 8 | # with webpack to recompile .js and .css sources. 9 | config :game, GameWeb.Endpoint, 10 | http: [port: 4000], 11 | debug_errors: true, 12 | code_reloader: true, 13 | check_origin: false, 14 | watchers: [ 15 | node: [ 16 | "node_modules/webpack/bin/webpack.js", 17 | "--mode", 18 | "development", 19 | "--watch-stdin", 20 | cd: Path.expand("../assets", __DIR__) 21 | ] 22 | ] 23 | 24 | # ## SSL Support 25 | # 26 | # In order to use HTTPS in development, a self-signed 27 | # certificate can be generated by running the following 28 | # Mix task: 29 | # 30 | # mix phx.gen.cert 31 | # 32 | # Note that this task requires Erlang/OTP 20 or later. 33 | # Run `mix help phx.gen.cert` for more information. 34 | # 35 | # The `http:` config above can be replaced with: 36 | # 37 | # https: [ 38 | # port: 4001, 39 | # cipher_suite: :strong, 40 | # keyfile: "priv/cert/selfsigned_key.pem", 41 | # certfile: "priv/cert/selfsigned.pem" 42 | # ], 43 | # 44 | # If desired, both `http:` and `https:` keys can be 45 | # configured to run both http and https servers on 46 | # different ports. 47 | 48 | # Watch static and templates for browser reloading. 49 | config :game, GameWeb.Endpoint, 50 | live_reload: [ 51 | patterns: [ 52 | ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", 53 | ~r"priv/gettext/.*(po)$", 54 | ~r"lib/game_web/(live|views)/.*(ex)$", 55 | ~r"lib/game_web/templates/.*(eex)$" 56 | ] 57 | ] 58 | 59 | # Do not include metadata nor timestamps in development logs 60 | config :logger, :console, format: "[$level] $message\n" 61 | 62 | # Set a higher stacktrace during development. Avoid configuring such 63 | # in production as building large stacktraces may be expensive. 64 | config :phoenix, :stacktrace_depth, 20 65 | 66 | # Initialize plugs at runtime for faster development compilation 67 | config :phoenix, :plug_init_mode, :runtime 68 | -------------------------------------------------------------------------------- /config/prod.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # For production, don't forget to configure the url host 4 | # to something meaningful, Phoenix uses this information 5 | # when generating URLs. 6 | # 7 | # Note we also include the path to a cache manifest 8 | # containing the digested version of static files. This 9 | # manifest is generated by the `mix phx.digest` task, 10 | # which you should run after static files are built and 11 | # before starting your production server. 12 | config :game, GameWeb.Endpoint, 13 | url: [host: "example.com", port: 80], 14 | cache_static_manifest: "priv/static/cache_manifest.json" 15 | 16 | # Do not print debug messages in production 17 | config :logger, level: :info 18 | 19 | # ## SSL Support 20 | # 21 | # To get SSL working, you will need to add the `https` key 22 | # to the previous section and set your `:url` port to 443: 23 | # 24 | # config :game, GameWeb.Endpoint, 25 | # ... 26 | # url: [host: "example.com", port: 443], 27 | # https: [ 28 | # port: 443, 29 | # cipher_suite: :strong, 30 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), 31 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH"), 32 | # transport_options: [socket_opts: [:inet6]] 33 | # ] 34 | # 35 | # The `cipher_suite` is set to `:strong` to support only the 36 | # latest and more secure SSL ciphers. This means old browsers 37 | # and clients may not be supported. You can set it to 38 | # `:compatible` for wider support. 39 | # 40 | # `:keyfile` and `:certfile` expect an absolute path to the key 41 | # and cert in disk or a relative path inside priv, for example 42 | # "priv/ssl/server.key". For all supported SSL configuration 43 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 44 | # 45 | # We also recommend setting `force_ssl` in your endpoint, ensuring 46 | # no data is ever sent via http, always redirecting to https: 47 | # 48 | # config :game, GameWeb.Endpoint, 49 | # force_ssl: [hsts: true] 50 | # 51 | # Check `Plug.SSL` for all available options in `force_ssl`. 52 | 53 | # Finally import the config/prod.secret.exs which loads secrets 54 | # and configuration from environment variables. 55 | import_config "prod.secret.exs" 56 | -------------------------------------------------------------------------------- /config/prod.secret.exs: -------------------------------------------------------------------------------- 1 | # In this file, we load production configuration and secrets 2 | # from environment variables. You can also hardcode secrets, 3 | # although such is generally not recommended and you have to 4 | # remember to add this file to your .gitignore. 5 | use Mix.Config 6 | 7 | secret_key_base = 8 | System.get_env("SECRET_KEY_BASE") || 9 | raise """ 10 | environment variable SECRET_KEY_BASE is missing. 11 | You can generate one by calling: mix phx.gen.secret 12 | """ 13 | 14 | config :game, GameWeb.Endpoint, 15 | http: [ 16 | port: String.to_integer(System.get_env("PORT") || "4000"), 17 | transport_options: [socket_opts: [:inet6]] 18 | ], 19 | secret_key_base: secret_key_base 20 | 21 | # ## Using releases (Elixir v1.9+) 22 | # 23 | # If you are doing OTP releases, you need to instruct Phoenix 24 | # to start each relevant endpoint: 25 | # 26 | # config :game, GameWeb.Endpoint, server: true 27 | # 28 | # Then you can assemble a release by calling `mix release`. 29 | # See `mix help release` for more information. 30 | -------------------------------------------------------------------------------- /config/test.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # We don't run a server during test. If one is required, 4 | # you can enable the server option below. 5 | config :game, GameWeb.Endpoint, 6 | http: [port: 4002], 7 | server: false 8 | 9 | # Print only warnings and errors during test 10 | config :logger, level: :warn 11 | -------------------------------------------------------------------------------- /gleam.toml: -------------------------------------------------------------------------------- 1 | name = "game" 2 | -------------------------------------------------------------------------------- /lib/game.ex: -------------------------------------------------------------------------------- 1 | defmodule Game do 2 | @moduledoc """ 3 | Game keeps the contexts that define your domain 4 | and business logic. 5 | 6 | Contexts are also responsible for managing your data, regardless 7 | if it comes from the database, an external API or others. 8 | """ 9 | end 10 | -------------------------------------------------------------------------------- /lib/game/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Application do 2 | # See https://hexdocs.pm/elixir/Application.html 3 | # for more information on OTP Applications 4 | @moduledoc false 5 | 6 | use Application 7 | 8 | def start(_type, _args) do 9 | children = [ 10 | # Start the Telemetry supervisor 11 | GameWeb.Telemetry, 12 | # Start the PubSub system 13 | {Phoenix.PubSub, name: Game.PubSub}, 14 | # Start the Endpoint (http/https) 15 | GameWeb.Endpoint, 16 | {Registry, keys: :unique, name: Game.Registry}, 17 | Game.SessionSupervisor 18 | ] 19 | 20 | # See https://hexdocs.pm/elixir/Supervisor.html 21 | # for other strategies and supported options 22 | opts = [strategy: :one_for_one, name: Game.Supervisor] 23 | Supervisor.start_link(children, opts) 24 | end 25 | 26 | # Tell Phoenix to update the endpoint configuration 27 | # whenever the application is updated. 28 | def config_change(changed, _new, removed) do 29 | GameWeb.Endpoint.config_change(changed, removed) 30 | :ok 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/game/card.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Card do 2 | use Game.Strucord, name: :card, from: "gen/src/game_Card.hrl" 3 | end 4 | -------------------------------------------------------------------------------- /lib/game/engine.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Engine do 2 | use Game.Strucord, name: :engine, from: "gen/src/game_Engine.hrl" 3 | 4 | def new(playing_cards, random) when is_boolean(random) do 5 | record = :game.init(playing_cards, random) 6 | from_record_custom(record) 7 | end 8 | 9 | def flip(%__MODULE__{} = struct, flip_id) when is_binary(flip_id) do 10 | gleamify(struct, fn record -> 11 | :game.flip(record, flip_id) 12 | end) 13 | end 14 | 15 | def unflip(%__MODULE__{} = struct) do 16 | gleamify(struct, fn record -> 17 | :game.unflip(record) 18 | end) 19 | end 20 | 21 | def prepare_restart(%__MODULE__{} = struct) do 22 | gleamify(struct, fn record -> 23 | :game.prepare_restart(record) 24 | end) 25 | end 26 | 27 | def restart(%__MODULE__{playing_cards: playing_cards, random: random}) do 28 | __MODULE__.new(playing_cards, random) 29 | end 30 | 31 | def gleamify(%__MODULE__{} = struct, f) when is_function(f, 1) do 32 | struct 33 | |> to_record_custom() 34 | |> f.() 35 | |> from_record_custom() 36 | end 37 | 38 | def to_record_custom(%__MODULE__{ 39 | cards: cards, 40 | winner: winner, 41 | animating: animating, 42 | score: score, 43 | playing_cards: playing_cards, 44 | random: random 45 | }) do 46 | cards = Enum.map(cards, fn c -> Game.Card.to_record(c) end) 47 | 48 | {:engine, cards, winner, animating, score, playing_cards, random} 49 | end 50 | 51 | def from_record_custom({:engine, cards, winner, animating, score, playing_cards, random}) do 52 | cards = Enum.map(cards, fn c -> Game.Card.from_record(c) end) 53 | 54 | %__MODULE__{ 55 | cards: cards, 56 | winner: winner, 57 | animating: animating, 58 | score: score, 59 | playing_cards: playing_cards, 60 | random: random 61 | } 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /lib/game/generator.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Generator do 2 | def haiku do 3 | [ 4 | Enum.random(foods()), 5 | :rand.uniform(9999) 6 | ] 7 | |> Enum.join("-") 8 | end 9 | 10 | def foods do 11 | ~w( 12 | apple banana orange 13 | grape kiwi mango 14 | pear pineapple strawberry 15 | tomato watermelon cantaloupe 16 | ) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/game/hash.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Hash do 2 | def hmac(key, value, length \\ 25) do 3 | :crypto.hmac(:sha256, key, value) 4 | |> Base.encode16() 5 | |> String.slice(0, length) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/game/process.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Process do 2 | def sleep(t) do 3 | Process.sleep(t * 100) 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/game/random.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Random do 2 | def take_random(items, number) do 3 | Enum.take_random(items, number) 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/game/registry.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Registry do 2 | def via(name) do 3 | {:via, Registry, {__MODULE__, name}} 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/game/session.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Session do 2 | use GenServer 3 | 4 | @timeout :timer.minutes(20) 5 | 6 | import Game.Process, only: [sleep: 1] 7 | 8 | def start_link(name, playing_cards, random) do 9 | GenServer.start_link(__MODULE__, {:ok, playing_cards, random}, name: via(name)) 10 | end 11 | 12 | defp via(name), do: Game.Registry.via(name) 13 | 14 | @impl GenServer 15 | def init({:ok, playing_cards, random}) do 16 | state = Game.Engine.new(playing_cards, random) 17 | 18 | {:ok, state, @timeout} 19 | end 20 | 21 | def session_pid(name) do 22 | name 23 | |> via() 24 | |> GenServer.whereis() 25 | end 26 | 27 | def game_state(name) do 28 | GenServer.call(via(name), {:game_state}) 29 | end 30 | 31 | def flip(name, flip_id) do 32 | GenServer.call(via(name), {:flip, flip_id}) 33 | end 34 | 35 | def unflip(name) do 36 | sleep(10) 37 | GenServer.call(via(name), {:unflip}) 38 | end 39 | 40 | def prepare_restart(name) do 41 | GenServer.call(via(name), {:prepare_restart}) 42 | end 43 | 44 | def restart(name) do 45 | sleep(1) 46 | GenServer.call(via(name), {:restart}) 47 | end 48 | 49 | @impl GenServer 50 | def handle_call({:game_state}, _from, state) do 51 | {:reply, state, state, @timeout} 52 | end 53 | 54 | @impl GenServer 55 | def handle_call({:flip, flip_id}, _from, state) do 56 | new_state = Game.Engine.flip(state, flip_id) 57 | {:reply, new_state, new_state, @timeout} 58 | end 59 | 60 | @impl GenServer 61 | def handle_call({:unflip}, _from, state) do 62 | new_state = Game.Engine.unflip(state) 63 | {:reply, new_state, new_state, @timeout} 64 | end 65 | 66 | @impl GenServer 67 | def handle_call({:prepare_restart}, _from, state) do 68 | new_state = Game.Engine.prepare_restart(state) 69 | {:reply, new_state, new_state, @timeout} 70 | end 71 | 72 | @impl GenServer 73 | def handle_call({:restart}, _from, state) do 74 | new_state = Game.Engine.restart(state) 75 | {:reply, new_state, new_state, @timeout} 76 | end 77 | 78 | @impl GenServer 79 | def handle_info(:timeout, session) do 80 | {:stop, {:shutdown, :timeout}, session} 81 | end 82 | 83 | @impl GenServer 84 | def terminate(_reason, _session) do 85 | :ok 86 | end 87 | 88 | def session_name do 89 | Registry.keys(Game.Registry, self()) |> List.first() 90 | end 91 | end 92 | -------------------------------------------------------------------------------- /lib/game/session_supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.SessionSupervisor do 2 | use DynamicSupervisor 3 | 4 | @default_playing_cards ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] 5 | 6 | def start_link(_args) do 7 | DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__) 8 | end 9 | 10 | def init(:ok) do 11 | DynamicSupervisor.init(strategy: :one_for_one) 12 | end 13 | 14 | def start_game(name, playing_cards \\ @default_playing_cards, random \\ true) do 15 | child_spec = %{ 16 | id: Game.Session, 17 | start: {Game.Session, :start_link, [name, playing_cards, random]}, 18 | restart: :transient 19 | } 20 | 21 | DynamicSupervisor.start_child(__MODULE__, child_spec) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/game/strucord.ex: -------------------------------------------------------------------------------- 1 | defmodule Game.Strucord do 2 | require Record 3 | 4 | defmacro __using__(opts) do 5 | name = Keyword.fetch!(opts, :name) 6 | from = Keyword.fetch!(opts, :from) 7 | 8 | fields = Record.extract(name, from: from) 9 | struct_fields = Keyword.keys(fields) 10 | vars = Macro.generate_arguments(length(struct_fields), __MODULE__) 11 | kvs = Enum.zip(struct_fields, vars) 12 | 13 | quote do 14 | defstruct unquote(struct_fields) 15 | 16 | def from_record({unquote(name), unquote_splicing(vars)}) do 17 | %__MODULE__{unquote_splicing(kvs)} 18 | end 19 | 20 | def to_record(%__MODULE__{unquote_splicing(kvs)}) do 21 | {unquote(name), unquote_splicing(vars)} 22 | end 23 | 24 | def with_record(%__MODULE__{} = struct, f) when is_function(f, 1) do 25 | struct 26 | |> to_record() 27 | |> f.() 28 | |> from_record() 29 | end 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/game_web.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb do 2 | @moduledoc """ 3 | The entrypoint for defining your web interface, such 4 | as controllers, views, channels and so on. 5 | 6 | This can be used in your application as: 7 | 8 | use GameWeb, :controller 9 | use GameWeb, :view 10 | 11 | The definitions below will be executed for every view, 12 | controller, etc, so keep them short and clean, focused 13 | on imports, uses and aliases. 14 | 15 | Do NOT define functions inside the quoted expressions 16 | below. Instead, define any helper function in modules 17 | and import those modules here. 18 | """ 19 | 20 | def controller do 21 | quote do 22 | use Phoenix.Controller, namespace: GameWeb 23 | 24 | import Plug.Conn 25 | import GameWeb.Gettext 26 | alias GameWeb.Router.Helpers, as: Routes 27 | end 28 | end 29 | 30 | def view do 31 | quote do 32 | use Phoenix.View, 33 | root: "lib/game_web/templates", 34 | namespace: GameWeb 35 | 36 | # Import convenience functions from controllers 37 | import Phoenix.Controller, 38 | only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] 39 | 40 | # Include shared imports and aliases for views 41 | unquote(view_helpers()) 42 | end 43 | end 44 | 45 | def live_view do 46 | quote do 47 | use Phoenix.LiveView, 48 | layout: {GameWeb.LayoutView, "live.html"} 49 | 50 | unquote(view_helpers()) 51 | end 52 | end 53 | 54 | def live_component do 55 | quote do 56 | use Phoenix.LiveComponent 57 | 58 | unquote(view_helpers()) 59 | end 60 | end 61 | 62 | def router do 63 | quote do 64 | use Phoenix.Router 65 | 66 | import Plug.Conn 67 | import Phoenix.Controller 68 | import Phoenix.LiveView.Router 69 | end 70 | end 71 | 72 | def channel do 73 | quote do 74 | use Phoenix.Channel 75 | import GameWeb.Gettext 76 | end 77 | end 78 | 79 | defp view_helpers do 80 | quote do 81 | # Use all HTML functionality (forms, tags, etc) 82 | use Phoenix.HTML 83 | 84 | # Import LiveView helpers (live_render, live_component, live_patch, etc) 85 | import Phoenix.LiveView.Helpers 86 | 87 | # Import basic rendering functionality (render, render_layout, etc) 88 | import Phoenix.View 89 | 90 | import GameWeb.ErrorHelpers 91 | import GameWeb.Gettext 92 | alias GameWeb.Router.Helpers, as: Routes 93 | end 94 | end 95 | 96 | @doc """ 97 | When used, dispatch to the appropriate controller/view/etc. 98 | """ 99 | defmacro __using__(which) when is_atom(which) do 100 | apply(__MODULE__, which, []) 101 | end 102 | end 103 | -------------------------------------------------------------------------------- /lib/game_web/channels/user_socket.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.UserSocket do 2 | use Phoenix.Socket 3 | 4 | ## Channels 5 | # channel "room:*", GameWeb.RoomChannel 6 | 7 | # Socket params are passed from the client and can 8 | # be used to verify and authenticate a user. After 9 | # verification, you can put default assigns into 10 | # the socket that will be set for all channels, ie 11 | # 12 | # {:ok, assign(socket, :user_id, verified_user_id)} 13 | # 14 | # To deny connection, return `:error`. 15 | # 16 | # See `Phoenix.Token` documentation for examples in 17 | # performing token verification on connect. 18 | @impl true 19 | def connect(_params, socket, _connect_info) do 20 | {:ok, socket} 21 | end 22 | 23 | # Socket id's are topics that allow you to identify all sockets for a given user: 24 | # 25 | # def id(socket), do: "user_socket:#{socket.assigns.user_id}" 26 | # 27 | # Would allow you to broadcast a "disconnect" event and terminate 28 | # all active sockets and channels for a given user: 29 | # 30 | # GameWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) 31 | # 32 | # Returning `nil` makes this socket anonymous. 33 | @impl true 34 | def id(_socket), do: nil 35 | end 36 | -------------------------------------------------------------------------------- /lib/game_web/controllers/page_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.PageController do 2 | use GameWeb, :controller 3 | 4 | def index(conn, _params) do 5 | render(conn, "index.html") 6 | end 7 | 8 | def new(conn, _params) do 9 | game_name = Game.Generator.haiku() 10 | 11 | case Game.SessionSupervisor.start_game(game_name) do 12 | {:ok, _pid} -> 13 | redirect(conn, to: Routes.page_path(conn, :play, game_name)) 14 | 15 | {:error, {:already_started, _pid}} -> 16 | redirect(conn, to: Routes.page_path(conn, :play, game_name)) 17 | 18 | {:error, _error} -> 19 | render(conn, "index.html") 20 | end 21 | end 22 | 23 | def play(conn, %{"id" => game_name}) do 24 | case Game.Session.session_pid(game_name) do 25 | pid when is_pid(pid) -> 26 | render_live_view(conn, game_name) 27 | 28 | nil -> 29 | redirect_user(conn) 30 | end 31 | end 32 | 33 | def redirect_user(conn) do 34 | conn 35 | |> put_flash(:error, "game not found") 36 | |> redirect(to: Routes.page_path(conn, :index)) 37 | end 38 | 39 | def render_live_view(conn, game_name) do 40 | Phoenix.LiveView.Controller.live_render(conn, GameWeb.PageLive, 41 | session: %{ 42 | "game_name" => game_name, 43 | "error" => nil 44 | } 45 | ) 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/game_web/endpoint.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.Endpoint do 2 | use Phoenix.Endpoint, otp_app: :game 3 | 4 | # The session will be stored in the cookie and signed, 5 | # this means its contents can be read but not tampered with. 6 | # Set :encryption_salt if you would also like to encrypt it. 7 | @session_options [ 8 | store: :cookie, 9 | key: "_game_key", 10 | signing_salt: "+O0jM8gZ" 11 | ] 12 | 13 | socket "/socket", GameWeb.UserSocket, 14 | websocket: true, 15 | longpoll: false 16 | 17 | socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]] 18 | 19 | # Serve at "/" the static files from "priv/static" directory. 20 | # 21 | # You should set gzip to true if you are running phx.digest 22 | # when deploying your static files in production. 23 | plug Plug.Static, 24 | at: "/", 25 | from: :game, 26 | gzip: false, 27 | only: ~w(css fonts images js favicon.ico robots.txt) 28 | 29 | # Code reloading can be explicitly enabled under the 30 | # :code_reloader configuration of your endpoint. 31 | if code_reloading? do 32 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket 33 | plug Phoenix.LiveReloader 34 | plug Phoenix.CodeReloader 35 | end 36 | 37 | plug Phoenix.LiveDashboard.RequestLogger, 38 | param_key: "request_logger", 39 | cookie_key: "request_logger" 40 | 41 | plug Plug.RequestId 42 | plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] 43 | 44 | plug Plug.Parsers, 45 | parsers: [:urlencoded, :multipart, :json], 46 | pass: ["*/*"], 47 | json_decoder: Phoenix.json_library() 48 | 49 | plug Plug.MethodOverride 50 | plug Plug.Head 51 | plug Plug.Session, @session_options 52 | plug GameWeb.Router 53 | end 54 | -------------------------------------------------------------------------------- /lib/game_web/gettext.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.Gettext do 2 | @moduledoc """ 3 | A module providing Internationalization with a gettext-based API. 4 | 5 | By using [Gettext](https://hexdocs.pm/gettext), 6 | your module gains a set of macros for translations, for example: 7 | 8 | import GameWeb.Gettext 9 | 10 | # Simple translation 11 | gettext("Here is the string to translate") 12 | 13 | # Plural translation 14 | ngettext("Here is the string to translate", 15 | "Here are the strings to translate", 16 | 3) 17 | 18 | # Domain-based translation 19 | dgettext("errors", "Here is the error message to translate") 20 | 21 | See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage. 22 | """ 23 | use Gettext, otp_app: :game 24 | end 25 | -------------------------------------------------------------------------------- /lib/game_web/live/page_live.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.PageLive do 2 | use GameWeb, :live_view 3 | 4 | @impl true 5 | def mount(_params, %{"game_name" => game_name}, socket) do 6 | state = Game.Session.game_state(game_name) 7 | 8 | {:ok, set_state(socket, state, %{game_name: game_name})} 9 | end 10 | 11 | @impl true 12 | def handle_event("flip", %{"flip-id" => flip_id}, socket) do 13 | %{:game_name => game_name} = socket.assigns 14 | 15 | case Game.Session.session_pid(game_name) do 16 | pid when is_pid(pid) -> 17 | state = Game.Session.flip(game_name, flip_id) 18 | %Game.Engine{animating: animating} = state 19 | 20 | if animating == true do 21 | send(self(), {:unflip, game_name}) 22 | end 23 | 24 | {:noreply, set_state(socket, state, socket.assigns)} 25 | 26 | nil -> 27 | {:noreply, set_error(socket)} 28 | end 29 | end 30 | 31 | @impl true 32 | def handle_event("prepare_restart", _value, socket) do 33 | %{:game_name => game_name} = socket.assigns 34 | 35 | case Game.Session.session_pid(game_name) do 36 | pid when is_pid(pid) -> 37 | state = Game.Session.prepare_restart(game_name) 38 | send(self(), {:restart, game_name}) 39 | {:noreply, set_state(socket, state, socket.assigns)} 40 | 41 | nil -> 42 | {:noreply, set_error(socket)} 43 | end 44 | end 45 | 46 | @impl true 47 | def handle_info({:unflip, game_name}, socket) do 48 | case Game.Session.session_pid(game_name) do 49 | pid when is_pid(pid) -> 50 | state = Game.Session.unflip(game_name) 51 | 52 | {:noreply, set_state(socket, state, socket.assigns)} 53 | 54 | nil -> 55 | {:noreply, set_error(socket)} 56 | end 57 | end 58 | 59 | @impl true 60 | def handle_info({:restart, game_name}, socket) do 61 | case Game.Session.session_pid(game_name) do 62 | pid when is_pid(pid) -> 63 | state = Game.Session.restart(game_name) 64 | 65 | {:noreply, set_state(socket, state, socket.assigns)} 66 | 67 | nil -> 68 | {:noreply, set_error(socket)} 69 | end 70 | end 71 | 72 | def rows(%{cards: cards}) do 73 | Enum.map(cards, &Map.from_struct(&1)) 74 | end 75 | 76 | def set_state(socket, state, %{game_name: game_name}) do 77 | %Game.Engine{cards: cards, winner: winner, score: score} = state 78 | 79 | assign(socket, 80 | game_name: game_name, 81 | cards: cards, 82 | winner: winner, 83 | score: score 84 | ) 85 | end 86 | 87 | def set_error(socket) do 88 | assign(socket, 89 | error: "an error occurred" 90 | ) 91 | end 92 | 93 | def clazz(%{flipped: flipped, paired: paired}) do 94 | case paired == true do 95 | true -> 96 | "found" 97 | 98 | false -> 99 | case flipped == true do 100 | true -> "flipped" 101 | false -> "" 102 | end 103 | end 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /lib/game_web/live/page_live.html.leex: -------------------------------------------------------------------------------- 1 |
2 |
3 | <%= for card <- rows(assigns) do %> 4 |
> 5 |
6 |
7 |
8 | <% end %> 9 |
10 | <%= if assigns.winner == true do %> 11 |
12 |
13 |

You Won!

14 | 15 |
16 |
17 | <% end %> 18 |
19 | -------------------------------------------------------------------------------- /lib/game_web/router.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.Router do 2 | use GameWeb, :router 3 | 4 | pipeline :browser do 5 | plug :accepts, ["html"] 6 | plug :fetch_session 7 | plug :fetch_live_flash 8 | plug :put_root_layout, {GameWeb.LayoutView, :root} 9 | plug :protect_from_forgery 10 | plug :put_secure_browser_headers 11 | end 12 | 13 | pipeline :api do 14 | plug :accepts, ["json"] 15 | end 16 | 17 | scope "/", GameWeb do 18 | pipe_through :browser 19 | 20 | get "/", PageController, :index 21 | get "/new", PageController, :new 22 | get "/play/:id", PageController, :play 23 | end 24 | 25 | # Other scopes may use custom stacks. 26 | # scope "/api", GameWeb do 27 | # pipe_through :api 28 | # end 29 | 30 | # Enables LiveDashboard only for development 31 | # 32 | # If you want to use the LiveDashboard in production, you should put 33 | # it behind authentication and allow only admins to access it. 34 | # If your application does not have an admins-only section yet, 35 | # you can use Plug.BasicAuth to set up some basic authentication 36 | # as long as you are also using SSL (which you should anyway). 37 | if Mix.env() in [:dev, :test] do 38 | import Phoenix.LiveDashboard.Router 39 | 40 | scope "/" do 41 | pipe_through :browser 42 | live_dashboard "/dashboard", metrics: GameWeb.Telemetry 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/game_web/telemetry.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.Telemetry do 2 | use Supervisor 3 | import Telemetry.Metrics 4 | 5 | def start_link(arg) do 6 | Supervisor.start_link(__MODULE__, arg, name: __MODULE__) 7 | end 8 | 9 | @impl true 10 | def init(_arg) do 11 | children = [ 12 | # Telemetry poller will execute the given period measurements 13 | # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics 14 | {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} 15 | # Add reporters as children of your supervision tree. 16 | # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} 17 | ] 18 | 19 | Supervisor.init(children, strategy: :one_for_one) 20 | end 21 | 22 | def metrics do 23 | [ 24 | # Phoenix Metrics 25 | summary("phoenix.endpoint.stop.duration", 26 | unit: {:native, :millisecond} 27 | ), 28 | summary("phoenix.router_dispatch.stop.duration", 29 | tags: [:route], 30 | unit: {:native, :millisecond} 31 | ), 32 | 33 | # VM Metrics 34 | summary("vm.memory.total", unit: {:byte, :kilobyte}), 35 | summary("vm.total_run_queue_lengths.total"), 36 | summary("vm.total_run_queue_lengths.cpu"), 37 | summary("vm.total_run_queue_lengths.io") 38 | ] 39 | end 40 | 41 | defp periodic_measurements do 42 | [ 43 | # A module, function and arguments to be invoked periodically. 44 | # This function must call :telemetry.execute/3 and a metric must be added above. 45 | # {GameWeb, :count_users, []} 46 | ] 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/game_web/templates/layout/app.html.eex: -------------------------------------------------------------------------------- 1 | <%= @inner_content %> 2 | -------------------------------------------------------------------------------- /lib/game_web/templates/layout/live.html.leex: -------------------------------------------------------------------------------- 1 | <%= @inner_content %> 2 | -------------------------------------------------------------------------------- /lib/game_web/templates/layout/root.html.leex: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | <%= csrf_meta_tag() %> 8 | <%= live_title_tag assigns[:page_title] || "Game", suffix: " · Phoenix Framework" %> 9 | "/> 10 | 11 | 12 | 13 | <%= @inner_content %> 14 | 15 | 16 | -------------------------------------------------------------------------------- /lib/game_web/templates/page/index.html.eex: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |

Match Game?

5 |
6 |
7 |
8 | -------------------------------------------------------------------------------- /lib/game_web/views/error_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.ErrorHelpers do 2 | @moduledoc """ 3 | Conveniences for translating and building error messages. 4 | """ 5 | 6 | use Phoenix.HTML 7 | 8 | @doc """ 9 | Generates tag for inlined form input errors. 10 | """ 11 | def error_tag(form, field) do 12 | Enum.map(Keyword.get_values(form.errors, field), fn error -> 13 | content_tag(:span, translate_error(error), 14 | class: "invalid-feedback", 15 | phx_feedback_for: input_id(form, field) 16 | ) 17 | end) 18 | end 19 | 20 | @doc """ 21 | Translates an error message using gettext. 22 | """ 23 | def translate_error({msg, opts}) do 24 | # When using gettext, we typically pass the strings we want 25 | # to translate as a static argument: 26 | # 27 | # # Translate "is invalid" in the "errors" domain 28 | # dgettext("errors", "is invalid") 29 | # 30 | # # Translate the number of files with plural rules 31 | # dngettext("errors", "1 file", "%{count} files", count) 32 | # 33 | # Because the error messages we show in our forms and APIs 34 | # are defined inside Ecto, we need to translate them dynamically. 35 | # This requires us to call the Gettext module passing our gettext 36 | # backend as first argument. 37 | # 38 | # Note we use the "errors" domain, which means translations 39 | # should be written to the errors.po file. The :count option is 40 | # set by Ecto and indicates we should also apply plural rules. 41 | if count = opts[:count] do 42 | Gettext.dngettext(GameWeb.Gettext, "errors", msg, msg, count, opts) 43 | else 44 | Gettext.dgettext(GameWeb.Gettext, "errors", msg, opts) 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/game_web/views/error_view.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.ErrorView do 2 | use GameWeb, :view 3 | 4 | # If you want to customize a particular status code 5 | # for a certain format, you may uncomment below. 6 | # def render("500.html", _assigns) do 7 | # "Internal Server Error" 8 | # end 9 | 10 | # By default, Phoenix returns the status message from 11 | # the template name. For example, "404.html" becomes 12 | # "Not Found". 13 | def template_not_found(template, _assigns) do 14 | Phoenix.Controller.status_message_from_template(template) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/game_web/views/layout_view.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.LayoutView do 2 | use GameWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /lib/game_web/views/page_view.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.PageView do 2 | use GameWeb, :view 3 | end 4 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Game.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :game, 7 | version: "0.1.0", 8 | elixir: "~> 1.7", 9 | elixirc_paths: elixirc_paths(Mix.env()), 10 | erlc_paths: ["src", "gen"], 11 | compilers: [:gleam, :phoenix, :gettext] ++ Mix.compilers(), 12 | start_permanent: Mix.env() == :prod, 13 | aliases: aliases(), 14 | deps: deps() 15 | ] 16 | end 17 | 18 | # Configuration for the OTP application. 19 | # 20 | # Type `mix help compile.app` for more information. 21 | def application do 22 | [ 23 | mod: {Game.Application, []}, 24 | extra_applications: [:logger, :runtime_tools] 25 | ] 26 | end 27 | 28 | # Specifies which paths to compile per environment. 29 | defp elixirc_paths(:test), do: ["lib", "test/support"] 30 | defp elixirc_paths(_), do: ["lib"] 31 | 32 | # Specifies your project dependencies. 33 | # 34 | # Type `mix help deps` for examples and options. 35 | defp deps do 36 | [ 37 | {:phoenix, "~> 1.5.7"}, 38 | {:phoenix_live_view, "~> 0.15.0"}, 39 | {:floki, ">= 0.0.0", only: :test}, 40 | {:phoenix_html, "~> 2.11"}, 41 | {:phoenix_live_reload, "~> 1.2", only: :dev}, 42 | {:phoenix_live_dashboard, "~> 0.4"}, 43 | {:telemetry_metrics, "~> 0.4"}, 44 | {:telemetry_poller, "~> 0.4"}, 45 | {:gettext, "~> 0.11"}, 46 | {:jason, "~> 1.0"}, 47 | {:plug_cowboy, "~> 2.0"}, 48 | {:mix_test_watch, "~> 1.0", only: :dev, runtime: false}, 49 | {:gleam_stdlib, "~> 0.13"}, 50 | {:mix_gleam, "~> 0.1.0"} 51 | ] 52 | end 53 | 54 | # Aliases are shortcuts or tasks specific to the current project. 55 | # For example, to install project dependencies and perform other setup tasks, run: 56 | # 57 | # $ mix setup 58 | # 59 | # See the documentation for `Mix` for more info on aliases. 60 | defp aliases do 61 | [ 62 | setup: ["deps.get", "cmd npm install --prefix assets"] 63 | ] 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, 3 | "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"}, 4 | "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, 5 | "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, 6 | "floki": {:hex, :floki, "0.28.0", "0d0795a17189510ee01323e6990f906309e9fc6e8570219135211f1264d78c7f", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "db1549560874ebba5a6367e46c3aec5fedd41f2757ad6efe567efb04b4d4ee55"}, 7 | "gettext": {:hex, :gettext, "0.18.1", "89e8499b051c7671fa60782faf24409b5d2306aa71feb43d79648a8bc63d0522", [:mix], [], "hexpm", "e70750c10a5f88cb8dc026fc28fa101529835026dec4a06dba3b614f2a99c7a9"}, 8 | "gleam_stdlib": {:hex, :gleam_stdlib, "0.13.0", "604a40e0fbe6c688651a5ad5e892913ed314ab626d18d361a7fd8bf367907551", [:rebar3], [], "hexpm", "35a005f4daca2775687e46f4e20cec799563a698a16f8bcc0cf281522ad717f1"}, 9 | "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"}, 10 | "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, 11 | "mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"}, 12 | "mix_gleam": {:hex, :mix_gleam, "0.1.0", "a0cee5d30de865124a32ca6cd53b64c3e2ac57f12adf6e47b88fb673f47c716e", [:mix], [], "hexpm", "9ff518e6aab444c7f2e74038f9383020ef89810cf1f4402911f33b202ffd72e7"}, 13 | "mix_test_watch": {:hex, :mix_test_watch, "1.0.2", "34900184cbbbc6b6ed616ed3a8ea9b791f9fd2088419352a6d3200525637f785", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "47ac558d8b06f684773972c6d04fcc15590abdb97aeb7666da19fcbfdc441a07"}, 14 | "phoenix": {:hex, :phoenix, "1.5.7", "2923bb3af924f184459fe4fa4b100bd25fa6468e69b2803dfae82698269aa5e0", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "774cd64417c5a3788414fdbb2be2eb9bcd0c048d9e6ad11a0c1fd67b7c0d0978"}, 15 | "phoenix_html": {:hex, :phoenix_html, "2.14.3", "51f720d0d543e4e157ff06b65de38e13303d5778a7919bcc696599e5934271b8", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "efd697a7fff35a13eeeb6b43db884705cba353a1a41d127d118fda5f90c8e80f"}, 16 | "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.4.0", "87990e68b60213d7487e65814046f9a2bed4a67886c943270125913499b3e5c3", [:mix], [{:ecto_psql_extras, "~> 0.4.1 or ~> 0.5", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.14.1 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.15.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.4.0 or ~> 0.5.0 or ~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "8d52149e58188e9e4497cc0d8900ab94d9b66f96998ec38c47c7a4f8f4f50e57"}, 17 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.4", "940c0344b1d66a2e46eef02af3a70e0c5bb45a4db0bf47917add271b76cd3914", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "38f9308357dea4cc77f247e216da99fcb0224e05ada1469167520bed4cb8cccd"}, 18 | "phoenix_live_view": {:hex, :phoenix_live_view, "0.15.3", "70c7917e5c421e32d1a1c8ddf8123378bb741748cd8091eb9d557fb4be92a94f", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.5.7", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 0.5", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cabcfb6738419a08600009219a5f0d861de97507fc1232121e1d5221aba849bd"}, 19 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, 20 | "plug": {:hex, :plug, "1.11.0", "f17217525597628298998bc3baed9f8ea1fa3f1160aa9871aee6df47a6e4d38e", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2d9c633f0499f9dc5c2fd069161af4e2e7756890b81adcbb2ceaa074e8308876"}, 21 | "plug_cowboy": {:hex, :plug_cowboy, "2.4.1", "779ba386c0915027f22e14a48919a9545714f849505fa15af2631a0d298abf0f", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d72113b6dff7b37a7d9b2a5b68892808e3a9a752f2bf7e503240945385b70507"}, 22 | "plug_crypto": {:hex, :plug_crypto, "1.2.0", "1cb20793aa63a6c619dd18bb33d7a3aa94818e5fd39ad357051a67f26dfa2df6", [:mix], [], "hexpm", "a48b538ae8bf381ffac344520755f3007cc10bd8e90b240af98ea29b69683fc2"}, 23 | "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, 24 | "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, 25 | "telemetry_metrics": {:hex, :telemetry_metrics, "0.6.0", "da9d49ee7e6bb1c259d36ce6539cd45ae14d81247a2b0c90edf55e2b50507f7b", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5cfe67ad464b243835512aa44321cee91faed6ea868d7fb761d7016e02915c3d"}, 26 | "telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"}, 27 | } 28 | -------------------------------------------------------------------------------- /priv/gettext/en/LC_MESSAGES/errors.po: -------------------------------------------------------------------------------- 1 | ## `msgid`s in this file come from POT (.pot) files. 2 | ## 3 | ## Do not add, change, or remove `msgid`s manually here as 4 | ## they're tied to the ones in the corresponding POT file 5 | ## (with the same domain). 6 | ## 7 | ## Use `mix gettext.extract --merge` or `mix gettext.merge` 8 | ## to merge POT files into PO files. 9 | msgid "" 10 | msgstr "" 11 | "Language: en\n" 12 | -------------------------------------------------------------------------------- /priv/gettext/errors.pot: -------------------------------------------------------------------------------- 1 | ## This is a PO Template file. 2 | ## 3 | ## `msgid`s here are often extracted from source code. 4 | ## Add new translations manually only if they're dynamic 5 | ## translations that can't be statically extracted. 6 | ## 7 | ## Run `mix gettext.extract` to bring this file up to 8 | ## date. Leave `msgstr`s empty as changing them here has no 9 | ## effect: edit them in PO (`.po`) files instead. 10 | 11 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {erl_opts, [debug_info]}. 2 | {src_dirs, ["src", "gen/src"]}. 3 | 4 | {profiles, [ 5 | {test, [{src_dirs, ["src", "test", "gen/src", "gen/test"]}]} 6 | ]}. 7 | 8 | {project_plugins, [rebar_gleam]}. 9 | 10 | {deps, []}. 11 | -------------------------------------------------------------------------------- /src/game.app.src: -------------------------------------------------------------------------------- 1 | {application, game, 2 | [{description, "A Gleam program"}, 3 | {vsn, "1.0.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib, 8 | gleam_stdlib 9 | ]}, 10 | {env,[]}, 11 | {modules, []}, 12 | 13 | {include_files, ["gleam.toml", "gen"]}, 14 | {licenses, ["Apache 2.0"]}, 15 | {links, []} 16 | ]}. 17 | -------------------------------------------------------------------------------- /src/game.gleam: -------------------------------------------------------------------------------- 1 | import gleam/map 2 | import gleam/bool 3 | import gleam/list 4 | import gleam/string 5 | 6 | pub type Card { 7 | Card(id: String, name: String, image: String, flipped: Bool, paired: Bool) 8 | } 9 | 10 | pub type Engine { 11 | Engine( 12 | cards: List(Card), 13 | winner: Bool, 14 | animating: Bool, 15 | score: Int, 16 | playing_cards: List(String), 17 | random: Bool, 18 | ) 19 | } 20 | 21 | pub fn pair_cards(cards: List(Card), engine: Engine) -> Engine { 22 | let paired_cards = 23 | list.map( 24 | cards, 25 | fn(card: Card) { 26 | case card.flipped { 27 | True -> Card(..card, paired: True, flipped: False) 28 | _ -> card 29 | } 30 | }, 31 | ) 32 | 33 | Engine(..engine, cards: paired_cards) 34 | } 35 | 36 | pub fn declare_winner(engine: Engine) -> Engine { 37 | let total = list.length(engine.cards) 38 | let paired = 39 | list.filter(engine.cards, fn(card: Card) { card.paired == True }) 40 | |> list.length() 41 | 42 | case total == paired { 43 | True -> Engine(..engine, winner: True) 44 | _ -> Engine(..engine, winner: False) 45 | } 46 | } 47 | 48 | pub fn attempt_match(cards: List(Card), engine: Engine) -> Engine { 49 | let flipped_cards = 50 | list.filter(cards, fn(card: Card) { card.flipped == True }) 51 | |> list.map(fn(card: Card) { card.name }) 52 | 53 | case list.length(flipped_cards) == 2 { 54 | True -> 55 | case flipped_cards { 56 | [one, two] if one == two -> pair_cards(cards, engine) 57 | _ -> Engine(..engine, cards: cards, animating: True) 58 | } 59 | 60 | _ -> Engine(..engine, cards: cards) 61 | } 62 | } 63 | 64 | pub fn flip(engine: Engine, flip_id: String) -> Engine { 65 | case engine.animating, engine.winner { 66 | _, True -> engine 67 | 68 | True, _ -> engine 69 | 70 | _, _ -> 71 | list.map( 72 | engine.cards, 73 | fn(card: Card) { 74 | case card.id == flip_id { 75 | True -> Card(..card, flipped: True) 76 | _ -> card 77 | } 78 | }, 79 | ) 80 | |> attempt_match(engine) 81 | |> declare_winner() 82 | } 83 | } 84 | 85 | pub fn unpair_cards(engine: Engine) -> Engine { 86 | let unpaired = 87 | list.map(engine.cards, fn(card: Card) { Card(..card, paired: False) }) 88 | Engine(..engine, cards: unpaired) 89 | } 90 | 91 | pub fn prepare_restart(engine: Engine) -> Engine { 92 | case engine.winner { 93 | True -> unpair_cards(engine) 94 | _ -> engine 95 | } 96 | } 97 | 98 | pub fn unflip(engine: Engine) -> Engine { 99 | let cards = 100 | list.map(engine.cards, fn(card: Card) { Card(..card, flipped: False) }) 101 | 102 | Engine(..engine, cards: cards, animating: False) 103 | } 104 | 105 | pub fn generate_cards(playing_cards: List(String)) -> List(Card) { 106 | list.map( 107 | playing_cards, 108 | fn(name: String) { 109 | let one = 110 | Card( 111 | id: string.join([name, "1"], with: ""), 112 | name: name, 113 | image: string.join(["/images/cards/", name, ".png"], with: ""), 114 | flipped: False, 115 | paired: False, 116 | ) 117 | let two = 118 | Card( 119 | id: string.join([name, "2"], with: ""), 120 | name: name, 121 | image: string.join(["/images/cards/", name, ".png"], with: ""), 122 | flipped: False, 123 | paired: False, 124 | ) 125 | [one, two] 126 | }, 127 | ) 128 | |> list.flatten() 129 | } 130 | 131 | pub fn init(playing_cards: List(String), random: Bool) -> Engine { 132 | let total = list.length(playing_cards) 133 | 134 | let cards = case random { 135 | True -> 136 | generate_cards(playing_cards) 137 | |> list.take(total * 2) 138 | 139 | False -> 140 | generate_cards(playing_cards) 141 | |> list.take(total * 2) 142 | } 143 | 144 | Engine( 145 | cards: cards, 146 | winner: False, 147 | animating: False, 148 | score: 0, 149 | playing_cards: playing_cards, 150 | random: random, 151 | ) 152 | } 153 | -------------------------------------------------------------------------------- /test/game/game_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Game.EngineTest do 2 | use ExUnit.Case, async: true 3 | 4 | alias Game.Card 5 | 6 | @playing_cards ["one", "two"] 7 | @image_one "/images/cards/one.png" 8 | @image_two "/images/cards/two.png" 9 | @id_one_a "one1" 10 | @id_one_b "one2" 11 | @id_two_a "two1" 12 | @id_two_b "two2" 13 | 14 | test "new returns game struct with list of cards" do 15 | state = Game.Engine.new(@playing_cards, false) 16 | 17 | %Game.Engine{cards: cards, winner: winner, animating: animating} = state 18 | 19 | assert winner == false 20 | assert animating == false 21 | assert Enum.count(cards) == 4 22 | 23 | [ 24 | %Card{id: id_one, name: name_one, image: image_one, flipped: flipped_one}, 25 | %Card{id: id_two, name: name_two, image: image_two, flipped: flipped_two}, 26 | %Card{id: id_three, name: name_three, image: image_three, flipped: flipped_three}, 27 | %Card{id: id_four, name: name_four, image: image_four, flipped: flipped_four} 28 | ] = cards 29 | 30 | assert id_one == @id_one_a 31 | assert image_one == @image_one 32 | assert name_one == "one" 33 | assert flipped_one == false 34 | 35 | assert id_two == @id_one_b 36 | assert image_two == @image_one 37 | assert name_two == "one" 38 | assert flipped_two == false 39 | 40 | assert id_three == @id_two_a 41 | assert image_three == @image_two 42 | assert name_three == "two" 43 | assert flipped_three == false 44 | 45 | assert id_four == @id_two_b 46 | assert image_four == @image_two 47 | assert name_four == "two" 48 | assert flipped_four == false 49 | end 50 | 51 | test "flip will mark a given card with flipped attribute" do 52 | state = Game.Engine.new(@playing_cards, false) 53 | new_state = Game.Engine.flip(state, @id_two_a) 54 | 55 | %Game.Engine{cards: cards, winner: winner, animating: animating} = new_state 56 | 57 | assert winner == false 58 | assert animating == false 59 | assert Enum.count(cards) == 4 60 | 61 | [ 62 | %Card{flipped: flip_one}, 63 | %Card{flipped: flip_two}, 64 | %Card{flipped: flip_three}, 65 | %Card{flipped: flip_four} 66 | ] = cards 67 | 68 | assert flip_one == false 69 | assert flip_two == false 70 | assert flip_three == true 71 | assert flip_four == false 72 | end 73 | 74 | test "flipping the 2nd card in a match will mark the cards as paired and revert flipped to false" do 75 | state = Game.Engine.new(@playing_cards, false) 76 | new_state = Game.Engine.flip(state, @id_two_a) 77 | paired_state = Game.Engine.flip(new_state, @id_two_b) 78 | 79 | %Game.Engine{cards: cards, winner: winner, animating: animating} = paired_state 80 | 81 | assert winner == false 82 | assert animating == false 83 | assert Enum.count(cards) == 4 84 | 85 | [ 86 | %Card{flipped: flip_one, paired: paired_one}, 87 | %Card{flipped: flip_two, paired: paired_two}, 88 | %Card{flipped: flip_three, paired: paired_three}, 89 | %Card{flipped: flip_four, paired: paired_four} 90 | ] = cards 91 | 92 | assert flip_one == false 93 | assert flip_two == false 94 | assert flip_three == false 95 | assert flip_four == false 96 | 97 | assert paired_one == false 98 | assert paired_two == false 99 | assert paired_three == true 100 | assert paired_four == true 101 | end 102 | 103 | test "flipping the 2nd card that is NOT a match will mark the cards as flipped but not paired" do 104 | state = Game.Engine.new(@playing_cards, false) 105 | new_state = Game.Engine.flip(state, @id_two_a) 106 | incorrect_state = Game.Engine.flip(new_state, @id_one_a) 107 | 108 | %Game.Engine{cards: cards, winner: winner, animating: animating} = incorrect_state 109 | 110 | assert winner == false 111 | assert animating == true 112 | assert Enum.count(cards) == 4 113 | 114 | [ 115 | %Card{flipped: flip_one, paired: paired_one}, 116 | %Card{flipped: flip_two, paired: paired_two}, 117 | %Card{flipped: flip_three, paired: paired_three}, 118 | %Card{flipped: flip_four, paired: paired_four} 119 | ] = cards 120 | 121 | assert flip_one == true 122 | assert flip_two == false 123 | assert flip_three == true 124 | assert flip_four == false 125 | 126 | assert paired_one == false 127 | assert paired_two == false 128 | assert paired_three == false 129 | assert paired_four == false 130 | end 131 | 132 | test "flipping when animating is marked as true flip does nothing" do 133 | state = %Game.Engine{ 134 | cards: [ 135 | %Card{:id => "one1", :flipped => false, :paired => false}, 136 | %Card{:id => "two1", :flipped => true, :paired => false}, 137 | %Card{:id => "one2", :flipped => true, :paired => false}, 138 | %Card{:id => "two2", :flipped => false, :paired => false} 139 | ], 140 | winner: nil, 141 | animating: true 142 | } 143 | 144 | new_state = Game.Engine.flip(state, @id_two_a) 145 | 146 | %Game.Engine{cards: cards, winner: winner, animating: animating} = new_state 147 | 148 | [ 149 | %Card{flipped: flip_one, paired: paired_one}, 150 | %Card{flipped: flip_two, paired: paired_two}, 151 | %Card{flipped: flip_three, paired: paired_three}, 152 | %Card{flipped: flip_four, paired: paired_four} 153 | ] = cards 154 | 155 | assert flip_one == false 156 | assert flip_two == true 157 | assert flip_three == true 158 | assert flip_four == false 159 | 160 | assert paired_one == false 161 | assert paired_two == false 162 | assert paired_three == false 163 | assert paired_four == false 164 | 165 | assert winner == nil 166 | assert animating == true 167 | end 168 | 169 | test "flipping when winner is marked as true flip does nothing" do 170 | state = %Game.Engine{ 171 | cards: [ 172 | %Card{:id => "one1", :flipped => false, :paired => true}, 173 | %Card{:id => "two1", :flipped => false, :paired => true}, 174 | %Card{:id => "one2", :flipped => false, :paired => true}, 175 | %Card{:id => "two2", :flipped => false, :paired => true} 176 | ], 177 | winner: true, 178 | animating: false 179 | } 180 | 181 | new_state = Game.Engine.flip(state, @id_two_a) 182 | 183 | %Game.Engine{cards: cards, winner: winner, animating: animating} = new_state 184 | 185 | [ 186 | %Card{flipped: flip_one, paired: paired_one}, 187 | %Card{flipped: flip_two, paired: paired_two}, 188 | %Card{flipped: flip_three, paired: paired_three}, 189 | %Card{flipped: flip_four, paired: paired_four} 190 | ] = cards 191 | 192 | assert flip_one == false 193 | assert flip_two == false 194 | assert flip_three == false 195 | assert flip_four == false 196 | 197 | assert paired_one == true 198 | assert paired_two == true 199 | assert paired_three == true 200 | assert paired_four == true 201 | 202 | assert winner == true 203 | assert animating == false 204 | end 205 | 206 | test "unflip will reset animating to false and revert any flipped cards" do 207 | state = Game.Engine.new(@playing_cards, false) 208 | new_state = Game.Engine.flip(state, @id_two_a) 209 | incorrect_state = Game.Engine.flip(new_state, @id_one_a) 210 | unflipped_state = Game.Engine.unflip(incorrect_state) 211 | 212 | %Game.Engine{cards: cards, winner: winner, animating: animating} = unflipped_state 213 | 214 | assert winner == false 215 | assert animating == false 216 | assert Enum.count(cards) == 4 217 | 218 | [ 219 | %Card{flipped: flip_one, paired: paired_one}, 220 | %Card{flipped: flip_two, paired: paired_two}, 221 | %Card{flipped: flip_three, paired: paired_three}, 222 | %Card{flipped: flip_four, paired: paired_four} 223 | ] = cards 224 | 225 | assert flip_one == false 226 | assert flip_two == false 227 | assert flip_three == false 228 | assert flip_four == false 229 | 230 | assert paired_one == false 231 | assert paired_two == false 232 | assert paired_three == false 233 | assert paired_four == false 234 | end 235 | 236 | test "flipping the last match will mark the winner as truthy" do 237 | state = Game.Engine.new(@playing_cards, false) 238 | flip_one_state = Game.Engine.flip(state, @id_two_a) 239 | paired_one_state = Game.Engine.flip(flip_one_state, @id_two_b) 240 | flip_two_state = Game.Engine.flip(paired_one_state, @id_one_a) 241 | paired_two_state = Game.Engine.flip(flip_two_state, @id_one_b) 242 | 243 | %Game.Engine{cards: cards, winner: winner, animating: animating} = paired_two_state 244 | 245 | assert winner == true 246 | assert animating == false 247 | assert Enum.count(cards) == 4 248 | 249 | [ 250 | %Card{flipped: flip_one, paired: paired_one}, 251 | %Card{flipped: flip_two, paired: paired_two}, 252 | %Card{flipped: flip_three, paired: paired_three}, 253 | %Card{flipped: flip_four, paired: paired_four} 254 | ] = cards 255 | 256 | assert flip_one == false 257 | assert flip_two == false 258 | assert flip_three == false 259 | assert flip_four == false 260 | 261 | assert paired_one == true 262 | assert paired_two == true 263 | assert paired_three == true 264 | assert paired_four == true 265 | end 266 | 267 | test "prepare restart will unpair each card" do 268 | state = %Game.Engine{ 269 | cards: [ 270 | %Card{:id => "one1", :flipped => false, :paired => true}, 271 | %Card{:id => "two1", :flipped => false, :paired => true}, 272 | %Card{:id => "one2", :flipped => false, :paired => true}, 273 | %Card{:id => "two2", :flipped => false, :paired => true} 274 | ], 275 | winner: true, 276 | animating: false 277 | } 278 | 279 | prepare_restart_state = Game.Engine.prepare_restart(state) 280 | 281 | %Game.Engine{cards: cards} = prepare_restart_state 282 | 283 | [ 284 | %Card{flipped: flip_one, paired: paired_one}, 285 | %Card{flipped: flip_two, paired: paired_two}, 286 | %Card{flipped: flip_three, paired: paired_three}, 287 | %Card{flipped: flip_four, paired: paired_four} 288 | ] = cards 289 | 290 | assert flip_one == false 291 | assert flip_two == false 292 | assert flip_three == false 293 | assert flip_four == false 294 | 295 | assert paired_one == false 296 | assert paired_two == false 297 | assert paired_three == false 298 | assert paired_four == false 299 | end 300 | 301 | test "prepare restart does nothing if winner is nil" do 302 | state = %Game.Engine{ 303 | cards: [ 304 | %Card{:id => "one1", :flipped => false, :paired => true}, 305 | %Card{:id => "two1", :flipped => true, :paired => false}, 306 | %Card{:id => "one2", :flipped => false, :paired => true}, 307 | %Card{:id => "two2", :flipped => false, :paired => false} 308 | ], 309 | winner: nil, 310 | animating: false 311 | } 312 | 313 | new_state = Game.Engine.prepare_restart(state) 314 | 315 | %Game.Engine{cards: cards, winner: winner, animating: animating} = new_state 316 | 317 | [ 318 | %Card{flipped: flip_one, paired: paired_one}, 319 | %Card{flipped: flip_two, paired: paired_two}, 320 | %Card{flipped: flip_three, paired: paired_three}, 321 | %Card{flipped: flip_four, paired: paired_four} 322 | ] = cards 323 | 324 | assert flip_one == false 325 | assert flip_two == true 326 | assert flip_three == false 327 | assert flip_four == false 328 | 329 | assert paired_one == true 330 | assert paired_two == false 331 | assert paired_three == true 332 | assert paired_four == false 333 | 334 | assert winner == nil 335 | assert animating == false 336 | end 337 | 338 | test "restart will flip winner to false" do 339 | state = %Game.Engine{ 340 | cards: [ 341 | %Card{:id => "one1", :flipped => false, :paired => false}, 342 | %Card{:id => "two1", :flipped => false, :paired => false}, 343 | %Card{:id => "one2", :flipped => false, :paired => false}, 344 | %Card{:id => "two2", :flipped => false, :paired => false} 345 | ], 346 | winner: true, 347 | animating: false, 348 | playing_cards: @playing_cards, 349 | random: false 350 | } 351 | 352 | restart_state = Game.Engine.restart(state) 353 | 354 | %Game.Engine{winner: winner} = restart_state 355 | 356 | assert winner == false 357 | end 358 | end 359 | -------------------------------------------------------------------------------- /test/game_web/live/page_live_test.exs: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.PageLiveTest do 2 | use ExUnit.Case, async: true 3 | 4 | import Phoenix.ConnTest 5 | import Phoenix.LiveViewTest 6 | 7 | @endpoint GameWeb.Endpoint 8 | @one "/images/cards/one.png" 9 | @two "/images/cards/two.png" 10 | @id_one_a "one1" 11 | @id_one_b "one2" 12 | @id_two_a "two1" 13 | @id_two_b "two2" 14 | 15 | setup config do 16 | patch_process() 17 | 18 | playing_cards = ["one", "two"] 19 | game_name = Game.Generator.haiku() 20 | {:ok, pid} = Game.SessionSupervisor.start_game(game_name, playing_cards, false) 21 | 22 | on_exit(fn -> 23 | Process.exit(pid, :kill) 24 | purge(Game.Process) 25 | end) 26 | 27 | conn = Plug.Test.init_test_session(Phoenix.ConnTest.build_conn(), config[:session] || %{}) 28 | 29 | %{conn: conn, game_name: game_name} 30 | end 31 | 32 | test "each card will be rendered with correct click handler, value and background image", %{ 33 | conn: conn, 34 | game_name: game_name 35 | } do 36 | {:ok, _view, html} = live(conn, "/play/#{game_name}") 37 | 38 | {:ok, html} = html |> Floki.parse_document() 39 | cards = Floki.find(html, ".card") 40 | assert Enum.count(cards) == 4 41 | 42 | assert ["card", "card", "card", "card"] == card_classes(cards) 43 | assert ["flip", "flip", "flip", "flip"] == click_handlers(cards) 44 | assert [@id_one_a, @id_one_b, @id_two_a, @id_two_b] == click_values(cards) 45 | 46 | assert [ 47 | "background-image: url(#{@one})", 48 | "background-image: url(#{@one})", 49 | "background-image: url(#{@two})", 50 | "background-image: url(#{@two})" 51 | ] == child_styles(cards) 52 | end 53 | 54 | test "flipping 2 incorrect matches will unflip after a brief pause", %{ 55 | conn: conn, 56 | game_name: game_name 57 | } do 58 | {:ok, view, html} = live(conn, "/play/#{game_name}") 59 | 60 | {:ok, html} = html |> Floki.parse_document() 61 | cards = Floki.find(html, ".card") 62 | assert ["card", "card", "card", "card"] == card_classes(cards) 63 | 64 | flip_one_html = render_click(view, :flip, %{"flip-id" => @id_two_a}) 65 | {:ok, flip_one_html} = flip_one_html |> Floki.parse_document() 66 | flip_one_cards = Floki.find(flip_one_html, ".card") 67 | assert ["card", "card", "card flipped", "card"] == card_classes(flip_one_cards) 68 | 69 | flip_two_html = render_click(view, :flip, %{"flip-id" => @id_one_b}) 70 | {:ok, flip_two_html} = flip_two_html |> Floki.parse_document() 71 | flip_two_cards = Floki.find(flip_two_html, ".card") 72 | assert ["card", "card flipped", "card flipped", "card"] == card_classes(flip_two_cards) 73 | 74 | Process.sleep(20) 75 | 76 | {:ok, final_html} = render(view) |> Floki.parse_document() 77 | final_cards = Floki.find(final_html, ".card") 78 | assert ["card", "card", "card", "card"] == card_classes(final_cards) 79 | end 80 | 81 | test "flipping 2 correct matches will mark a pair", %{conn: conn, game_name: game_name} do 82 | {:ok, view, html} = live(conn, "/play/#{game_name}") 83 | 84 | {:ok, html} = html |> Floki.parse_document() 85 | cards = Floki.find(html, ".card") 86 | assert ["card", "card", "card", "card"] == card_classes(cards) 87 | 88 | flip_one_html = render_click(view, :flip, %{"flip-id" => @id_two_a}) 89 | {:ok, flip_one_html} = flip_one_html |> Floki.parse_document() 90 | flip_one_cards = Floki.find(flip_one_html, ".card") 91 | assert ["card", "card", "card flipped", "card"] == card_classes(flip_one_cards) 92 | 93 | flip_two_html = render_click(view, :flip, %{"flip-id" => @id_two_b}) 94 | {:ok, flip_two_html} = flip_two_html |> Floki.parse_document() 95 | flip_two_cards = Floki.find(flip_two_html, ".card") 96 | assert ["card", "card", "card found", "card found"] == card_classes(flip_two_cards) 97 | end 98 | 99 | test "flipping all correct matches will show modal", %{conn: conn, game_name: game_name} do 100 | {:ok, view, html} = live(conn, "/play/#{game_name}") 101 | 102 | {:ok, html} = html |> Floki.parse_document() 103 | assert Enum.count(modal(html)) == 0 104 | 105 | render_click(view, :flip, %{"flip-id" => @id_two_a}) 106 | render_click(view, :flip, %{"flip-id" => @id_two_b}) 107 | 108 | {:ok, one_pair_html} = render(view) |> Floki.parse_document() 109 | assert Enum.count(modal(one_pair_html)) == 0 110 | 111 | render_click(view, :flip, %{"flip-id" => @id_one_a}) 112 | render_click(view, :flip, %{"flip-id" => @id_one_b}) 113 | 114 | {:ok, two_pair_html} = render(view) |> Floki.parse_document() 115 | 116 | assert Enum.count(modal(two_pair_html)) == 1 117 | assert winner(two_pair_html) == "You Won!" 118 | end 119 | 120 | test "clicking play again will reset the game and hide the modal", %{ 121 | conn: conn, 122 | game_name: game_name 123 | } do 124 | {:ok, view, _html} = live(conn, "/play/#{game_name}") 125 | 126 | render_click(view, :flip, %{"flip-id" => @id_two_a}) 127 | render_click(view, :flip, %{"flip-id" => @id_two_b}) 128 | 129 | render_click(view, :flip, %{"flip-id" => @id_one_a}) 130 | render_click(view, :flip, %{"flip-id" => @id_one_b}) 131 | 132 | winner_html = render(view) 133 | {:ok, winner_html} = winner_html |> Floki.parse_document() 134 | winner_cards = Floki.find(winner_html, ".card") 135 | assert ["card found", "card found", "card found", "card found"] == card_classes(winner_cards) 136 | assert Enum.count(modal(winner_html)) == 1 137 | 138 | restart_html = render_click(view, :prepare_restart) 139 | Process.sleep(2) 140 | 141 | {:ok, restart_html} = restart_html |> Floki.parse_document() 142 | restart_cards = Floki.find(restart_html, ".card") 143 | assert ["card", "card", "card", "card"] == card_classes(restart_cards) 144 | 145 | {:ok, restarted_html} = render(view) |> Floki.parse_document() 146 | assert Enum.count(modal(restarted_html)) == 0 147 | end 148 | 149 | defp modal(html) do 150 | Floki.find(html, ".splash, .overlay") 151 | end 152 | 153 | defp winner(html) do 154 | Floki.find(html, ".content h1") |> Floki.text() 155 | end 156 | 157 | defp card_classes(cards) do 158 | cards 159 | |> Floki.attribute("class") 160 | |> Enum.map(&String.trim(&1)) 161 | end 162 | 163 | defp click_handlers(cards) do 164 | cards 165 | |> Floki.attribute("phx-click") 166 | end 167 | 168 | defp click_values(cards) do 169 | cards 170 | |> Floki.attribute("phx-value-flip-id") 171 | end 172 | 173 | defp child_styles(cards) do 174 | cards 175 | |> Enum.map(fn {_tag, _attr, child} -> 176 | [_, front] = child 177 | [attribute] = Floki.attribute(front, "style") 178 | attribute 179 | end) 180 | end 181 | 182 | defp patch_process do 183 | Code.eval_string(""" 184 | defmodule Game.Process do 185 | def sleep(t) do 186 | Process.sleep(t) 187 | end 188 | end 189 | """) 190 | end 191 | 192 | defp purge(module) do 193 | :code.purge(module) 194 | :code.delete(module) 195 | end 196 | end 197 | -------------------------------------------------------------------------------- /test/game_web/views/error_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.ErrorViewTest do 2 | use GameWeb.ConnCase, async: true 3 | 4 | # Bring render/3 and render_to_string/3 for testing custom views 5 | import Phoenix.View 6 | 7 | test "renders 404.html" do 8 | assert render_to_string(GameWeb.ErrorView, "404.html", []) == "Not Found" 9 | end 10 | 11 | test "renders 500.html" do 12 | assert render_to_string(GameWeb.ErrorView, "500.html", []) == "Internal Server Error" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /test/game_web/views/layout_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.LayoutViewTest do 2 | use GameWeb.ConnCase, async: true 3 | 4 | # When testing helpers, you may want to import Phoenix.HTML and 5 | # use functions such as safe_to_string() to convert the helper 6 | # result into an HTML string. 7 | # import Phoenix.HTML 8 | end 9 | -------------------------------------------------------------------------------- /test/support/channel_case.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.ChannelCase do 2 | @moduledoc """ 3 | This module defines the test case to be used by 4 | channel tests. 5 | 6 | Such tests rely on `Phoenix.ChannelTest` and also 7 | import other functionality to make it easier 8 | to build common data structures and query the data layer. 9 | 10 | Finally, if the test case interacts with the database, 11 | we enable the SQL sandbox, so changes done to the database 12 | are reverted at the end of every test. If you are using 13 | PostgreSQL, you can even run database tests asynchronously 14 | by setting `use GameWeb.ChannelCase, async: true`, although 15 | this option is not recommended for other databases. 16 | """ 17 | 18 | use ExUnit.CaseTemplate 19 | 20 | using do 21 | quote do 22 | # Import conveniences for testing with channels 23 | import Phoenix.ChannelTest 24 | import GameWeb.ChannelCase 25 | 26 | # The default endpoint for testing 27 | @endpoint GameWeb.Endpoint 28 | end 29 | end 30 | 31 | setup _tags do 32 | :ok 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /test/support/conn_case.ex: -------------------------------------------------------------------------------- 1 | defmodule GameWeb.ConnCase do 2 | @moduledoc """ 3 | This module defines the test case to be used by 4 | tests that require setting up a connection. 5 | 6 | Such tests rely on `Phoenix.ConnTest` and also 7 | import other functionality to make it easier 8 | to build common data structures and query the data layer. 9 | 10 | Finally, if the test case interacts with the database, 11 | we enable the SQL sandbox, so changes done to the database 12 | are reverted at the end of every test. If you are using 13 | PostgreSQL, you can even run database tests asynchronously 14 | by setting `use GameWeb.ConnCase, async: true`, although 15 | this option is not recommended for other databases. 16 | """ 17 | 18 | use ExUnit.CaseTemplate 19 | 20 | using do 21 | quote do 22 | # Import conveniences for testing with connections 23 | import Plug.Conn 24 | import Phoenix.ConnTest 25 | import GameWeb.ConnCase 26 | 27 | alias GameWeb.Router.Helpers, as: Routes 28 | 29 | # The default endpoint for testing 30 | @endpoint GameWeb.Endpoint 31 | end 32 | end 33 | 34 | setup _tags do 35 | {:ok, conn: Phoenix.ConnTest.build_conn()} 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------