<%= get_flash(@conn, :info) %>
32 |<%= get_flash(@conn, :error) %>
33 | <%= render @view_module, @view_template, assigns %> 34 |├── .formatter.exs ├── .gitignore ├── Procfile ├── README.md ├── assets ├── .babelrc ├── css │ ├── app.css │ └── phoenix.css ├── elm │ ├── elm.json │ └── src │ │ ├── Games │ │ └── Platformer.elm │ │ └── Main.elm ├── js │ ├── app.js │ └── socket.js ├── package-lock.json ├── package.json ├── static │ ├── favicon.ico │ ├── images │ │ ├── character-left.gif │ │ ├── character-right.gif │ │ ├── character.gif │ │ ├── coin.svg │ │ ├── phoenix.png │ │ └── platformer-thumbnail.png │ └── robots.txt └── webpack.config.js ├── compile ├── config ├── config.exs ├── dev.exs ├── prod.exs └── test.exs ├── elixir_buildpack.config ├── lib ├── platform.ex ├── platform │ ├── accounts │ │ ├── accounts.ex │ │ └── player.ex │ ├── application.ex │ ├── products │ │ ├── game.ex │ │ ├── gameplay.ex │ │ └── products.ex │ └── repo.ex ├── platform_web.ex └── platform_web │ ├── channels │ ├── score_channel.ex │ └── user_socket.ex │ ├── controllers │ ├── fallback_controller.ex │ ├── game_controller.ex │ ├── gameplay_controller.ex │ ├── page_controller.ex │ ├── player_api_controller.ex │ ├── player_auth_controller.ex │ ├── player_controller.ex │ └── player_session_controller.ex │ ├── endpoint.ex │ ├── gettext.ex │ ├── router.ex │ ├── templates │ ├── game │ │ └── show.html.eex │ ├── layout │ │ └── app.html.eex │ ├── page │ │ └── index.html.eex │ ├── player │ │ ├── edit.html.eex │ │ ├── index.html.eex │ │ ├── new.html.eex │ │ └── show.html.eex │ └── player_session │ │ └── new.html.eex │ └── views │ ├── changeset_view.ex │ ├── error_helpers.ex │ ├── error_view.ex │ ├── game_view.ex │ ├── gameplay_view.ex │ ├── layout_view.ex │ ├── page_view.ex │ ├── player_api_view.ex │ ├── player_session_view.ex │ └── player_view.ex ├── mix.exs ├── mix.lock ├── phoenix_static_buildpack.config ├── priv ├── gettext │ ├── en │ │ └── LC_MESSAGES │ │ │ └── errors.po │ └── errors.pot └── repo │ ├── migrations │ ├── .formatter.exs │ ├── 20181211130948_create_players.exs │ ├── 20181211140804_add_fields_to_player_accounts.exs │ ├── 20181212131945_create_games.exs │ ├── 20181212132244_create_gameplays.exs │ └── 20181213131353_add_slug_to_games.exs │ └── seeds.exs └── test ├── platform ├── accounts │ └── accounts_test.exs └── products │ └── products_test.exs ├── platform_web ├── controllers │ ├── game_controller_test.exs │ ├── gameplay_controller_test.exs │ ├── page_controller_test.exs │ └── player_controller_test.exs └── views │ ├── error_view_test.exs │ ├── layout_view_test.exs │ └── page_view_test.exs ├── support ├── channel_case.ex ├── conn_case.ex └── data_case.ex └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:ecto, :phoenix], 3 | inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"], 4 | subdirectories: ["priv/*/migrations"] 5 | ] 6 | -------------------------------------------------------------------------------- /.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 | platform-*.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 | # Files matching config/*.secret.exs pattern contain sensitive 37 | # data and you should not commit them into version control. 38 | # 39 | # Alternatively, you may comment the line below and commit the 40 | # secrets files as long as you replace their contents by environment 41 | # variables. 42 | /config/*.secret.exs 43 | 44 | # Elm 45 | /assets/elm/elm-stuff 46 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | release: MIX_ENV=prod mix ecto.migrate 2 | web: MIX_ENV=prod mix phx.server 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Platform 2 | 3 | _This project has been archived, but the source code is still available here for reference._ 4 | 5 | This repository contains the source code for 6 | [elixir-elm-tutorial.herokuapp.com](https://elixir-elm-tutorial.herokuapp.com), 7 | which is the demo application from the 8 | [Elixir and Elm Tutorial](https://leanpub.com/elixir-elm-tutorial). 9 | 10 | ## Requirements 11 | 12 | - Elixir 1.7 13 | - Phoenix 1.4 14 | - Elm 0.19 15 | 16 | ## Setup 17 | 18 | 1. `git clone https://github.com/elixir-and-elm-tutorial/platform.git` 19 | 2. `mix deps.get` to install Phoenix dependencies. 20 | 3. `config/dev.exs` and `config/test.exs` to configure local database. 21 | 4. `mix ecto.setup` to create, migrate, and seed the database. 22 | 5. `cd assets && npm install` to install Node dependencies. 23 | 6. `mix phx.server` to start Phoenix server. 24 | 7. `localhost:4000` to see application! 25 | -------------------------------------------------------------------------------- /assets/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /assets/css/app.css: -------------------------------------------------------------------------------- 1 | /* This file is for your main application css. */ 2 | 3 | @import "./phoenix.css"; 4 | 5 | /* Header */ 6 | 7 | header { 8 | border-bottom: none; 9 | margin-bottom: 0; 10 | } 11 | 12 | .logo { 13 | font-weight: bold; 14 | } 15 | 16 | .nav-text { 17 | display: inline-flex; 18 | margin-right: 5px; 19 | } 20 | 21 | /* Featured */ 22 | 23 | .featured { 24 | height: 360px; 25 | background-color: black; 26 | color: white; 27 | } 28 | 29 | .featured-img { 30 | margin-top: 50px; 31 | margin-right: 50px; 32 | float: left; 33 | } 34 | 35 | .featured-thumbnail { 36 | height: 260px; 37 | } 38 | 39 | .featured-data { 40 | margin-top: 50px; 41 | overflow: hidden; 42 | } 43 | 44 | /* Games */ 45 | 46 | .games-index { 47 | margin-top: 2rem; 48 | } 49 | 50 | .game-item { 51 | display: flex; 52 | } 53 | 54 | .game-image { 55 | width: 300px; 56 | } 57 | 58 | .game-info { 59 | margin-left: 2em; 60 | } 61 | 62 | /* Players */ 63 | 64 | .player-score { 65 | margin-left: 1em; 66 | } 67 | 68 | .player-name { 69 | margin-left: 1em; 70 | } 71 | 72 | /* Gameplays */ 73 | 74 | .gameplays, .gameplays-empty { 75 | margin-left: 2em; 76 | } 77 | 78 | /* Buttons */ 79 | 80 | .button { 81 | margin-right: 1em; 82 | } 83 | -------------------------------------------------------------------------------- /assets/css/phoenix.css: -------------------------------------------------------------------------------- 1 | /* Includes some default style for the starter application. 2 | * This can be safely deleted to start fresh. 3 | */ 4 | 5 | /* Milligram v1.3.0 https://milligram.github.io 6 | * Copyright (c) 2017 CJ Patoilo Licensed under the MIT license 7 | */ 8 | 9 | *,*:after,*:before{box-sizing:inherit}html{box-sizing:border-box;font-size:62.5%}body{color:#000000;font-family:'Helvetica', 'Arial', sans-serif;font-size:1.6em;font-weight:300;line-height:1.6}blockquote{border-left:0.3rem solid #d1d1d1;margin-left:0;margin-right:0;padding:1rem 1.5rem}blockquote *:last-child{margin-bottom:0}.button,button,input[type='button'],input[type='reset'],input[type='submit']{background-color:#0069d9;border:0.1rem solid #0069d9;border-radius:.4rem;color:#fff;cursor:pointer;display:inline-block;font-size:1.1rem;font-weight:700;height:3.8rem;letter-spacing:.1rem;line-height:3.8rem;padding:0 3.0rem;text-align:center;text-decoration:none;text-transform:uppercase;white-space:nowrap}.button:focus,.button:hover,button:focus,button:hover,input[type='button']:focus,input[type='button']:hover,input[type='reset']:focus,input[type='reset']:hover,input[type='submit']:focus,input[type='submit']:hover{background-color:#606c76;border-color:#606c76;color:#fff;outline:0}.button[disabled],button[disabled],input[type='button'][disabled],input[type='reset'][disabled],input[type='submit'][disabled]{cursor:default;opacity:.5}.button[disabled]:focus,.button[disabled]:hover,button[disabled]:focus,button[disabled]:hover,input[type='button'][disabled]:focus,input[type='button'][disabled]:hover,input[type='reset'][disabled]:focus,input[type='reset'][disabled]:hover,input[type='submit'][disabled]:focus,input[type='submit'][disabled]:hover{background-color:#0069d9;border-color:#0069d9}.button.button-outline,button.button-outline,input[type='button'].button-outline,input[type='reset'].button-outline,input[type='submit'].button-outline{background-color:transparent;color:#0069d9}.button.button-outline:focus,.button.button-outline:hover,button.button-outline:focus,button.button-outline:hover,input[type='button'].button-outline:focus,input[type='button'].button-outline:hover,input[type='reset'].button-outline:focus,input[type='reset'].button-outline:hover,input[type='submit'].button-outline:focus,input[type='submit'].button-outline:hover{background-color:transparent;border-color:#606c76;color:#606c76}.button.button-outline[disabled]:focus,.button.button-outline[disabled]:hover,button.button-outline[disabled]:focus,button.button-outline[disabled]:hover,input[type='button'].button-outline[disabled]:focus,input[type='button'].button-outline[disabled]:hover,input[type='reset'].button-outline[disabled]:focus,input[type='reset'].button-outline[disabled]:hover,input[type='submit'].button-outline[disabled]:focus,input[type='submit'].button-outline[disabled]:hover{border-color:inherit;color:#0069d9}.button.button-clear,button.button-clear,input[type='button'].button-clear,input[type='reset'].button-clear,input[type='submit'].button-clear{background-color:transparent;border-color:transparent;color:#0069d9}.button.button-clear:focus,.button.button-clear:hover,button.button-clear:focus,button.button-clear:hover,input[type='button'].button-clear:focus,input[type='button'].button-clear:hover,input[type='reset'].button-clear:focus,input[type='reset'].button-clear:hover,input[type='submit'].button-clear:focus,input[type='submit'].button-clear:hover{background-color:transparent;border-color:transparent;color:#606c76}.button.button-clear[disabled]:focus,.button.button-clear[disabled]:hover,button.button-clear[disabled]:focus,button.button-clear[disabled]:hover,input[type='button'].button-clear[disabled]:focus,input[type='button'].button-clear[disabled]:hover,input[type='reset'].button-clear[disabled]:focus,input[type='reset'].button-clear[disabled]:hover,input[type='submit'].button-clear[disabled]:focus,input[type='submit'].button-clear[disabled]:hover{color:#0069d9}code{background:#f4f5f6;border-radius:.4rem;font-size:86%;margin:0 .2rem;padding:.2rem .5rem;white-space:nowrap}pre{background:#f4f5f6;border-left:0.3rem solid #0069d9;overflow-y:hidden}pre>code{border-radius:0;display:block;padding:1rem 1.5rem;white-space:pre}hr{border:0;border-top:0.1rem solid #f4f5f6;margin:3.0rem 0}input[type='email'],input[type='number'],input[type='password'],input[type='search'],input[type='tel'],input[type='text'],input[type='url'],textarea,select{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:transparent;border:0.1rem solid #d1d1d1;border-radius:.4rem;box-shadow:none;box-sizing:inherit;height:3.8rem;padding:.6rem 1.0rem;width:100%}input[type='email']:focus,input[type='number']:focus,input[type='password']:focus,input[type='search']:focus,input[type='tel']:focus,input[type='text']:focus,input[type='url']:focus,textarea:focus,select:focus{border-color:#0069d9;outline:0}select{background:url('data:image/svg+xml;utf8,') center right no-repeat;padding-right:3.0rem}select:focus{background-image:url('data:image/svg+xml;utf8,')}textarea{min-height:6.5rem}label,legend{display:block;font-size:1.6rem;font-weight:700;margin-bottom:.5rem}fieldset{border-width:0;padding:0}input[type='checkbox'],input[type='radio']{display:inline}.label-inline{display:inline-block;font-weight:normal;margin-left:.5rem}.row{display:flex;flex-direction:column;padding:0;width:100%}.row.row-no-padding{padding:0}.row.row-no-padding>.column{padding:0}.row.row-wrap{flex-wrap:wrap}.row.row-top{align-items:flex-start}.row.row-bottom{align-items:flex-end}.row.row-center{align-items:center}.row.row-stretch{align-items:stretch}.row.row-baseline{align-items:baseline}.row .column{display:block;flex:1 1 auto;margin-left:0;max-width:100%;width:100%}.row .column.column-offset-10{margin-left:10%}.row .column.column-offset-20{margin-left:20%}.row .column.column-offset-25{margin-left:25%}.row .column.column-offset-33,.row .column.column-offset-34{margin-left:33.3333%}.row .column.column-offset-50{margin-left:50%}.row .column.column-offset-66,.row .column.column-offset-67{margin-left:66.6666%}.row .column.column-offset-75{margin-left:75%}.row .column.column-offset-80{margin-left:80%}.row .column.column-offset-90{margin-left:90%}.row .column.column-10{flex:0 0 10%;max-width:10%}.row .column.column-20{flex:0 0 20%;max-width:20%}.row .column.column-25{flex:0 0 25%;max-width:25%}.row .column.column-33,.row .column.column-34{flex:0 0 33.3333%;max-width:33.3333%}.row .column.column-40{flex:0 0 40%;max-width:40%}.row .column.column-50{flex:0 0 50%;max-width:50%}.row .column.column-60{flex:0 0 60%;max-width:60%}.row .column.column-66,.row .column.column-67{flex:0 0 66.6666%;max-width:66.6666%}.row .column.column-75{flex:0 0 75%;max-width:75%}.row .column.column-80{flex:0 0 80%;max-width:80%}.row .column.column-90{flex:0 0 90%;max-width:90%}.row .column .column-top{align-self:flex-start}.row .column .column-bottom{align-self:flex-end}.row .column .column-center{-ms-grid-row-align:center;align-self:center}@media (min-width: 40rem){.row{flex-direction:row;margin-left:-1.0rem;width:calc(100% + 2.0rem)}.row .column{margin-bottom:inherit;padding:0 1.0rem}}a{color:#0069d9;text-decoration:none}a:focus,a:hover{color:#606c76}dl,ol,ul{list-style:none;margin-top:0;padding-left:0}dl dl,dl ol,dl ul,ol dl,ol ol,ol ul,ul dl,ul ol,ul ul{font-size:90%;margin:1.5rem 0 1.5rem 3.0rem}ol{list-style:decimal inside}ul{list-style:circle inside}.button,button,dd,dt,li{margin-bottom:1.0rem}fieldset,input,select,textarea{margin-bottom:1.5rem}blockquote,dl,figure,form,ol,p,pre,table,ul{margin-bottom:2.5rem}table{border-spacing:0;width:100%}td,th{border-bottom:0.1rem solid #e1e1e1;padding:1.2rem 1.5rem;text-align:left}td:first-child,th:first-child{padding-left:0}td:last-child,th:last-child{padding-right:0}b,strong{font-weight:bold}p{margin-top:0}h1,h2,h3,h4,h5,h6{font-weight:300;letter-spacing:-.1rem;margin-bottom:2.0rem;margin-top:0}h1{font-size:4.6rem;line-height:1.2}h2{font-size:3.6rem;line-height:1.25}h3{font-size:2.8rem;line-height:1.3}h4{font-size:2.2rem;letter-spacing:-.08rem;line-height:1.35}h5{font-size:1.8rem;letter-spacing:-.05rem;line-height:1.5}h6{font-size:1.6rem;letter-spacing:0;line-height:1.4}img{max-width:100%}.clearfix:after{clear:both;content:' ';display:table}.float-left{float:left}.float-right{float:right} 10 | 11 | /* General style */ 12 | h1{font-size: 3.6rem; line-height: 1.25} 13 | h2{font-size: 2.8rem; line-height: 1.3} 14 | h3{font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35} 15 | h4{font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5} 16 | h5{font-size: 1.6rem; letter-spacing: 0; line-height: 1.4} 17 | h6{font-size: 1.4rem; letter-spacing: 0; line-height: 1.2} 18 | 19 | .container{ 20 | margin: 0 auto; 21 | max-width: 80.0rem; 22 | padding: 0 2.0rem; 23 | position: relative; 24 | width: 100% 25 | } 26 | select { 27 | width: auto; 28 | } 29 | 30 | /* Alerts and form errors */ 31 | .alert { 32 | padding: 15px; 33 | margin-bottom: 20px; 34 | border: 1px solid transparent; 35 | border-radius: 4px; 36 | } 37 | .alert-info { 38 | color: #31708f; 39 | background-color: #d9edf7; 40 | border-color: #bce8f1; 41 | } 42 | .alert-warning { 43 | color: #8a6d3b; 44 | background-color: #fcf8e3; 45 | border-color: #faebcc; 46 | } 47 | .alert-danger { 48 | color: #a94442; 49 | background-color: #f2dede; 50 | border-color: #ebccd1; 51 | } 52 | .alert p { 53 | margin-bottom: 0; 54 | } 55 | .alert:empty { 56 | display: none; 57 | } 58 | .help-block { 59 | color: #a94442; 60 | display: block; 61 | margin: -1rem 0 2rem; 62 | } 63 | 64 | /* Phoenix promo and logo */ 65 | .phx-hero { 66 | text-align: center; 67 | border-bottom: 1px solid #e3e3e3; 68 | background: #eee; 69 | border-radius: 6px; 70 | padding: 3em; 71 | margin-bottom: 3rem; 72 | font-weight: 200; 73 | font-size: 120%; 74 | } 75 | .phx-hero p { 76 | margin: 0; 77 | } 78 | .phx-logo { 79 | min-width: 300px; 80 | margin: 1rem; 81 | display: block; 82 | } 83 | .phx-logo img { 84 | width: auto; 85 | display: block; 86 | } 87 | 88 | /* Headers */ 89 | header { 90 | width: 100%; 91 | background: #fdfdfd; 92 | border-bottom: 1px solid #eaeaea; 93 | margin-bottom: 2rem; 94 | } 95 | header section { 96 | align-items: center; 97 | display: flex; 98 | flex-direction: column; 99 | justify-content: space-between; 100 | } 101 | header section :first-child { 102 | order: 2; 103 | } 104 | header section :last-child { 105 | order: 1; 106 | } 107 | header nav ul, 108 | header nav li { 109 | margin: 0; 110 | padding: 0; 111 | display: block; 112 | text-align: right; 113 | white-space: nowrap; 114 | } 115 | header nav ul { 116 | margin: 1rem; 117 | margin-top: 0; 118 | } 119 | header nav a { 120 | display: block; 121 | } 122 | 123 | @media (min-width: 40.0rem) { /* Small devices (landscape phones, 576px and up) */ 124 | header section { 125 | flex-direction: row; 126 | } 127 | header nav ul { 128 | margin: 1rem; 129 | } 130 | .phx-logo { 131 | flex-basis: 527px; 132 | margin: 2rem 1rem; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /assets/elm/elm.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "application", 3 | "source-directories": [ 4 | "src" 5 | ], 6 | "elm-version": "0.19.0", 7 | "dependencies": { 8 | "direct": { 9 | "elm/browser": "1.0.1", 10 | "elm/core": "1.0.2", 11 | "elm/html": "1.0.0", 12 | "elm/http": "2.0.0", 13 | "elm/json": "1.1.2", 14 | "elm/random": "1.0.0", 15 | "elm/svg": "1.0.1", 16 | "elm/time": "1.0.0" 17 | }, 18 | "indirect": { 19 | "elm/bytes": "1.0.7", 20 | "elm/file": "1.0.1", 21 | "elm/url": "1.0.0", 22 | "elm/virtual-dom": "1.0.2" 23 | } 24 | }, 25 | "test-dependencies": { 26 | "direct": {}, 27 | "indirect": {} 28 | } 29 | } -------------------------------------------------------------------------------- /assets/elm/src/Games/Platformer.elm: -------------------------------------------------------------------------------- 1 | port module Games.Platformer exposing (main) 2 | 3 | import Browser 4 | import Browser.Events 5 | import Html exposing (Html, button, div, h2, li, span, strong, ul) 6 | import Html.Attributes 7 | import Html.Events 8 | import Http 9 | import Json.Decode as Decode 10 | import Json.Encode as Encode 11 | import Random 12 | import Svg exposing (..) 13 | import Svg.Attributes exposing (..) 14 | import Time 15 | 16 | 17 | 18 | -- MAIN 19 | 20 | 21 | main = 22 | Browser.element 23 | { init = init 24 | , update = update 25 | , subscriptions = subscriptions 26 | , view = view 27 | } 28 | 29 | 30 | 31 | -- MODEL 32 | 33 | 34 | type Direction 35 | = Left 36 | | Right 37 | 38 | 39 | type alias Flags = 40 | { token : String 41 | } 42 | 43 | 44 | type alias Gameplay = 45 | { gameId : Int 46 | , playerId : Int 47 | , playerScore : Int 48 | } 49 | 50 | 51 | type GameState 52 | = StartScreen 53 | | Playing 54 | | Success 55 | | GameOver 56 | 57 | 58 | type alias Player = 59 | { displayName : Maybe String 60 | , id : Int 61 | , username : String 62 | } 63 | 64 | 65 | type alias Model = 66 | { characterDirection : Direction 67 | , characterPositionX : Int 68 | , characterPositionY : Int 69 | , gameplays : List Gameplay 70 | , gameState : GameState 71 | , itemPositionX : Int 72 | , itemPositionY : Int 73 | , itemsCollected : Int 74 | , players : List Player 75 | , playerScore : Int 76 | , playerToken : String 77 | , timeRemaining : Int 78 | } 79 | 80 | 81 | initialModel : Flags -> Model 82 | initialModel flags = 83 | { characterDirection = Right 84 | , characterPositionX = 50 85 | , characterPositionY = 300 86 | , gameplays = [] 87 | , gameState = StartScreen 88 | , itemPositionX = 500 89 | , itemPositionY = 300 90 | , itemsCollected = 0 91 | , players = [] 92 | , playerScore = 0 93 | , playerToken = flags.token 94 | , timeRemaining = 10 95 | } 96 | 97 | 98 | initialCommand : Cmd Msg 99 | initialCommand = 100 | Cmd.batch 101 | [ fetchGameplaysList 102 | , fetchPlayersList 103 | ] 104 | 105 | 106 | init : Flags -> ( Model, Cmd Msg ) 107 | init flags = 108 | ( initialModel flags, initialCommand ) 109 | 110 | 111 | 112 | -- API 113 | 114 | 115 | fetchGameplaysList : Cmd Msg 116 | fetchGameplaysList = 117 | Http.get 118 | { url = "/api/gameplays" 119 | , expect = Http.expectJson FetchGameplaysList decodeGameplaysList 120 | } 121 | 122 | 123 | decodeGameplaysList : Decode.Decoder (List Gameplay) 124 | decodeGameplaysList = 125 | decodeGameplay 126 | |> Decode.list 127 | |> Decode.at [ "data" ] 128 | 129 | 130 | decodeGameplay : Decode.Decoder Gameplay 131 | decodeGameplay = 132 | Decode.map3 Gameplay 133 | (Decode.field "game_id" Decode.int) 134 | (Decode.field "player_id" Decode.int) 135 | (Decode.field "player_score" Decode.int) 136 | 137 | 138 | fetchPlayersList : Cmd Msg 139 | fetchPlayersList = 140 | Http.get 141 | { url = "/api/players" 142 | , expect = Http.expectJson FetchPlayersList decodePlayersList 143 | } 144 | 145 | 146 | decodePlayersList : Decode.Decoder (List Player) 147 | decodePlayersList = 148 | decodePlayer 149 | |> Decode.list 150 | |> Decode.at [ "data" ] 151 | 152 | 153 | decodePlayer : Decode.Decoder Player 154 | decodePlayer = 155 | Decode.map3 Player 156 | (Decode.maybe (Decode.field "display_name" Decode.string)) 157 | (Decode.field "id" Decode.int) 158 | (Decode.field "username" Decode.string) 159 | 160 | 161 | 162 | -- UPDATE 163 | 164 | 165 | type Msg 166 | = BroadcastScore Encode.Value 167 | | CountdownTimer Time.Posix 168 | | FetchGameplaysList (Result Http.Error (List Gameplay)) 169 | | FetchPlayersList (Result Http.Error (List Player)) 170 | | GameLoop Float 171 | | KeyDown String 172 | | NoOp 173 | | ReceiveScoreFromPhoenix Encode.Value 174 | | SaveScore Encode.Value 175 | | SetNewItemPositionX Int 176 | 177 | 178 | update : Msg -> Model -> ( Model, Cmd Msg ) 179 | update msg model = 180 | case msg of 181 | BroadcastScore value -> 182 | ( model, broadcastScore value ) 183 | 184 | CountdownTimer time -> 185 | if model.gameState == Playing && model.timeRemaining > 0 then 186 | ( { model | timeRemaining = model.timeRemaining - 1 }, Cmd.none ) 187 | 188 | else 189 | ( model, Cmd.none ) 190 | 191 | FetchGameplaysList result -> 192 | case result of 193 | Ok fetchedGameplays -> 194 | ( { model | gameplays = fetchedGameplays }, Cmd.none ) 195 | 196 | Err _ -> 197 | Debug.log "Error fetching gameplays from API." 198 | ( model, Cmd.none ) 199 | 200 | FetchPlayersList result -> 201 | case result of 202 | Ok fetchedPlayers -> 203 | ( { model | players = fetchedPlayers }, Cmd.none ) 204 | 205 | Err _ -> 206 | Debug.log "Error fetching players from API." 207 | ( model, Cmd.none ) 208 | 209 | GameLoop time -> 210 | if characterFoundItem model then 211 | ( { model 212 | | itemsCollected = model.itemsCollected + 1 213 | , playerScore = model.playerScore + 100 214 | } 215 | , Random.generate SetNewItemPositionX (Random.int 50 500) 216 | ) 217 | 218 | else if model.itemsCollected >= 10 then 219 | ( { model | gameState = Success }, Cmd.none ) 220 | 221 | else if model.itemsCollected < 10 && model.timeRemaining == 0 then 222 | ( { model | gameState = GameOver }, Cmd.none ) 223 | 224 | else 225 | ( model, Cmd.none ) 226 | 227 | KeyDown key -> 228 | case key of 229 | "ArrowLeft" -> 230 | if model.gameState == Playing then 231 | ( { model 232 | | characterDirection = Left 233 | , characterPositionX = model.characterPositionX - 15 234 | } 235 | , Cmd.none 236 | ) 237 | 238 | else 239 | ( model, Cmd.none ) 240 | 241 | "ArrowRight" -> 242 | if model.gameState == Playing then 243 | ( { model 244 | | characterDirection = Right 245 | , characterPositionX = model.characterPositionX + 15 246 | } 247 | , Cmd.none 248 | ) 249 | 250 | else 251 | ( model, Cmd.none ) 252 | 253 | " " -> 254 | if model.gameState /= Playing then 255 | ( { model 256 | | characterDirection = Right 257 | , characterPositionX = 50 258 | , itemsCollected = 0 259 | , gameState = Playing 260 | , playerScore = 0 261 | , timeRemaining = 10 262 | } 263 | , Cmd.none 264 | ) 265 | 266 | else 267 | ( model, Cmd.none ) 268 | 269 | _ -> 270 | ( model, Cmd.none ) 271 | 272 | NoOp -> 273 | ( model, Cmd.none ) 274 | 275 | ReceiveScoreFromPhoenix incomingJsonData -> 276 | case Decode.decodeValue decodeGameplay incomingJsonData of 277 | Ok gameplay -> 278 | -- Debug.log "Successfully received score data." 279 | ( { model | gameplays = gameplay :: model.gameplays }, Cmd.none ) 280 | 281 | Err message -> 282 | -- Debug.log ("Error receiving score data: " ++ Debug.toString message) 283 | ( model, Cmd.none ) 284 | 285 | SaveScore value -> 286 | ( model, saveScore value ) 287 | 288 | SetNewItemPositionX newPositionX -> 289 | ( { model | itemPositionX = newPositionX }, Cmd.none ) 290 | 291 | 292 | characterFoundItem : Model -> Bool 293 | characterFoundItem model = 294 | let 295 | approximateItemLowerBound = 296 | model.itemPositionX - 35 297 | 298 | approximateItemUpperBound = 299 | model.itemPositionX 300 | 301 | approximateItemRange = 302 | List.range approximateItemLowerBound approximateItemUpperBound 303 | in 304 | List.member model.characterPositionX approximateItemRange 305 | 306 | 307 | 308 | -- SUBSCRIPTIONS 309 | 310 | 311 | subscriptions : Model -> Sub Msg 312 | subscriptions model = 313 | Sub.batch 314 | [ Browser.Events.onKeyDown (Decode.map KeyDown keyDecoder) 315 | , Browser.Events.onAnimationFrameDelta GameLoop 316 | , Time.every 1000 CountdownTimer 317 | , receiveScoreFromPhoenix ReceiveScoreFromPhoenix 318 | ] 319 | 320 | 321 | keyDecoder : Decode.Decoder String 322 | keyDecoder = 323 | Decode.field "key" Decode.string 324 | 325 | 326 | 327 | -- PORTS 328 | 329 | 330 | port broadcastScore : Encode.Value -> Cmd msg 331 | 332 | 333 | port receiveScoreFromPhoenix : (Encode.Value -> msg) -> Sub msg 334 | 335 | 336 | port saveScore : Encode.Value -> Cmd msg 337 | 338 | 339 | 340 | -- VIEW 341 | 342 | 343 | view : Model -> Html Msg 344 | view model = 345 | div [ class "container" ] 346 | [ viewGame model 347 | , viewBroadcastScoreButton model 348 | , viewSaveScoreButton model 349 | , viewGameplaysIndex model 350 | ] 351 | 352 | 353 | 354 | -- GAME 355 | 356 | 357 | viewGame : Model -> Svg Msg 358 | viewGame model = 359 | svg [ version "1.1", width "600", height "400" ] 360 | (viewGameState model) 361 | 362 | 363 | 364 | -- GAME STATES 365 | 366 | 367 | viewGameState : Model -> List (Svg Msg) 368 | viewGameState model = 369 | case model.gameState of 370 | StartScreen -> 371 | [ viewGameWindow 372 | , viewGameSky 373 | , viewGameGround 374 | , viewCharacter model 375 | , viewItem model 376 | , viewStartScreenText 377 | ] 378 | 379 | Playing -> 380 | [ viewGameWindow 381 | , viewGameSky 382 | , viewGameGround 383 | , viewCharacter model 384 | , viewItem model 385 | , viewGameScore model 386 | , viewItemsCollected model 387 | , viewGameTime model 388 | ] 389 | 390 | Success -> 391 | [ viewGameWindow 392 | , viewGameSky 393 | , viewGameGround 394 | , viewCharacter model 395 | , viewItem model 396 | , viewSuccessScreenText 397 | ] 398 | 399 | GameOver -> 400 | [ viewGameWindow 401 | , viewGameSky 402 | , viewGameGround 403 | , viewCharacter model 404 | , viewItem model 405 | , viewGameOverScreenText 406 | ] 407 | 408 | 409 | 410 | -- GAME WINDOW 411 | 412 | 413 | viewGameWindow : Svg Msg 414 | viewGameWindow = 415 | rect 416 | [ width "600" 417 | , height "400" 418 | , fill "none" 419 | , stroke "black" 420 | ] 421 | [] 422 | 423 | 424 | viewGameSky : Svg Msg 425 | viewGameSky = 426 | rect 427 | [ x "0" 428 | , y "0" 429 | , width "600" 430 | , height "300" 431 | , fill "#4b7cfb" 432 | ] 433 | [] 434 | 435 | 436 | viewGameGround : Svg Msg 437 | viewGameGround = 438 | rect 439 | [ x "0" 440 | , y "300" 441 | , width "600" 442 | , height "100" 443 | , fill "green" 444 | ] 445 | [] 446 | 447 | 448 | 449 | -- DISPLAY GAME TEXT DATA 450 | 451 | 452 | viewGameText : Int -> Int -> String -> Svg Msg 453 | viewGameText positionX positionY str = 454 | Svg.text_ 455 | [ x (String.fromInt positionX) 456 | , y (String.fromInt positionY) 457 | , fontFamily "Courier" 458 | , fontWeight "bold" 459 | , fontSize "16" 460 | ] 461 | [ Svg.text str ] 462 | 463 | 464 | viewStartScreenText : Svg Msg 465 | viewStartScreenText = 466 | Svg.svg [] 467 | [ viewGameText 140 160 "Collect ten coins in ten seconds!" 468 | , viewGameText 140 180 "Press the SPACE BAR key to start." 469 | ] 470 | 471 | 472 | viewSuccessScreenText : Svg Msg 473 | viewSuccessScreenText = 474 | Svg.svg [] 475 | [ viewGameText 260 160 "Success!" 476 | , viewGameText 140 180 "Press the SPACE BAR key to restart." 477 | ] 478 | 479 | 480 | viewGameOverScreenText : Svg Msg 481 | viewGameOverScreenText = 482 | Svg.svg [] 483 | [ viewGameText 260 160 "Game Over" 484 | , viewGameText 140 180 "Press the SPACE BAR key to restart." 485 | ] 486 | 487 | 488 | viewGameScore : Model -> Svg Msg 489 | viewGameScore model = 490 | let 491 | currentScore = 492 | model.playerScore 493 | |> String.fromInt 494 | |> String.padLeft 5 '0' 495 | in 496 | Svg.svg [] 497 | [ viewGameText 25 25 "SCORE" 498 | , viewGameText 25 40 currentScore 499 | ] 500 | 501 | 502 | viewItemsCollected : Model -> Svg Msg 503 | viewItemsCollected model = 504 | let 505 | currentItemCount = 506 | model.itemsCollected 507 | |> String.fromInt 508 | |> String.padLeft 3 '0' 509 | in 510 | Svg.svg [] 511 | [ image 512 | [ xlinkHref "/images/coin.svg" 513 | , x "275" 514 | , y "18" 515 | , width "15" 516 | , height "15" 517 | ] 518 | [] 519 | , viewGameText 300 30 ("x " ++ currentItemCount) 520 | ] 521 | 522 | 523 | viewGameTime : Model -> Svg Msg 524 | viewGameTime model = 525 | let 526 | currentTime = 527 | model.timeRemaining 528 | |> String.fromInt 529 | |> String.padLeft 4 '0' 530 | in 531 | Svg.svg [] 532 | [ viewGameText 525 25 "TIME" 533 | , viewGameText 525 40 currentTime 534 | ] 535 | 536 | 537 | 538 | -- CHARACTER 539 | 540 | 541 | viewCharacter : Model -> Svg Msg 542 | viewCharacter model = 543 | let 544 | characterImage = 545 | case model.characterDirection of 546 | Left -> 547 | "/images/character-left.gif" 548 | 549 | Right -> 550 | "/images/character-right.gif" 551 | in 552 | image 553 | [ xlinkHref characterImage 554 | , x (String.fromInt model.characterPositionX) 555 | , y (String.fromInt model.characterPositionY) 556 | , width "50" 557 | , height "50" 558 | ] 559 | [] 560 | 561 | 562 | 563 | -- ITEM 564 | 565 | 566 | viewItem : Model -> Svg Msg 567 | viewItem model = 568 | image 569 | [ xlinkHref "/images/coin.svg" 570 | , x (String.fromInt model.itemPositionX) 571 | , y (String.fromInt model.itemPositionY) 572 | , width "20" 573 | , height "20" 574 | ] 575 | [] 576 | 577 | 578 | 579 | -- BUTTONS 580 | 581 | 582 | viewBroadcastScoreButton : Model -> Html Msg 583 | viewBroadcastScoreButton model = 584 | let 585 | broadcastEvent = 586 | model.playerScore 587 | |> Encode.int 588 | |> BroadcastScore 589 | |> Html.Events.onClick 590 | in 591 | button 592 | [ broadcastEvent 593 | , Html.Attributes.class "button" 594 | ] 595 | [ text "Broadcast Score Over Socket" ] 596 | 597 | 598 | viewSaveScoreButton : Model -> Html Msg 599 | viewSaveScoreButton model = 600 | let 601 | saveEvent = 602 | model.playerScore 603 | |> Encode.int 604 | |> SaveScore 605 | |> Html.Events.onClick 606 | in 607 | if model.playerToken == "" then 608 | div [] [] 609 | 610 | else 611 | button 612 | [ saveEvent 613 | , Html.Attributes.class "button" 614 | ] 615 | [ text "Save Score to Database" ] 616 | 617 | 618 | 619 | -- GAMEPLAYS 620 | 621 | 622 | viewGameplaysIndex : Model -> Html Msg 623 | viewGameplaysIndex model = 624 | if List.isEmpty model.gameplays then 625 | div [] [] 626 | 627 | else 628 | div [ Html.Attributes.class "gameplays-index container" ] 629 | [ h2 [] [ text "Player Scores" ] 630 | , viewGameplaysList model 631 | ] 632 | 633 | 634 | viewGameplaysList : Model -> Html Msg 635 | viewGameplaysList model = 636 | ul [ Html.Attributes.class "gameplays-list" ] 637 | (List.map (viewGameplayItem model) model.gameplays) 638 | 639 | 640 | viewGameplayItem : Model -> Gameplay -> Html Msg 641 | viewGameplayItem model gameplay = 642 | let 643 | displayPlayer = 644 | findPlayerWithGameplay model gameplay 645 | |> viewPlayerName 646 | 647 | displayScore = 648 | String.fromInt gameplay.playerScore 649 | in 650 | li [ Html.Attributes.class "gameplay-item" ] 651 | [ strong [] [ text (displayPlayer ++ ": ") ] 652 | , span [] [ text displayScore ] 653 | ] 654 | 655 | 656 | findPlayerWithGameplay : Model -> Gameplay -> Maybe Player 657 | findPlayerWithGameplay model gameplay = 658 | model.players 659 | |> List.filter (\player -> player.id == gameplay.playerId) 660 | |> List.head 661 | 662 | 663 | viewPlayerName : Maybe Player -> String 664 | viewPlayerName maybePlayer = 665 | case maybePlayer of 666 | Just player -> 667 | Maybe.withDefault player.username player.displayName 668 | 669 | Nothing -> 670 | "Anonymous Player" 671 | -------------------------------------------------------------------------------- /assets/elm/src/Main.elm: -------------------------------------------------------------------------------- 1 | module Main exposing (main) 2 | 3 | import Browser 4 | import Html exposing (..) 5 | import Html.Attributes exposing (..) 6 | import Html.Events exposing (onClick) 7 | import Http 8 | import Json.Decode as Decode 9 | 10 | 11 | 12 | -- MAIN 13 | 14 | 15 | main = 16 | Browser.element 17 | { init = init 18 | , update = update 19 | , subscriptions = subscriptions 20 | , view = view 21 | } 22 | 23 | 24 | 25 | -- MODEL 26 | 27 | 28 | type alias Model = 29 | { gamesList : List Game 30 | , gameplaysList : List Gameplay 31 | , playersList : List Player 32 | } 33 | 34 | 35 | type alias Game = 36 | { description : String 37 | , featured : Bool 38 | , id : Int 39 | , slug : String 40 | , thumbnail : String 41 | , title : String 42 | } 43 | 44 | 45 | type alias Gameplay = 46 | { gameId : Int 47 | , playerId : Int 48 | , playerScore : Int 49 | } 50 | 51 | 52 | type alias Player = 53 | { displayGameplays : Bool 54 | , displayName : Maybe String 55 | , id : Int 56 | , score : Int 57 | , username : String 58 | } 59 | 60 | 61 | initialModel : Model 62 | initialModel = 63 | { gamesList = [] 64 | , gameplaysList = [] 65 | , playersList = [] 66 | } 67 | 68 | 69 | initialCommand : Cmd Msg 70 | initialCommand = 71 | Cmd.batch 72 | [ fetchGamesList 73 | , fetchGameplaysList 74 | , fetchPlayersList 75 | ] 76 | 77 | 78 | init : () -> ( Model, Cmd Msg ) 79 | init _ = 80 | ( initialModel, initialCommand ) 81 | 82 | 83 | 84 | -- API 85 | 86 | 87 | fetchGamesList : Cmd Msg 88 | fetchGamesList = 89 | Http.get 90 | { url = "/api/games" 91 | , expect = Http.expectJson FetchGamesList decodeGamesList 92 | } 93 | 94 | 95 | fetchGameplaysList : Cmd Msg 96 | fetchGameplaysList = 97 | Http.get 98 | { url = "/api/gameplays" 99 | , expect = Http.expectJson FetchGameplaysList decodeGameplaysList 100 | } 101 | 102 | 103 | fetchPlayersList : Cmd Msg 104 | fetchPlayersList = 105 | Http.get 106 | { url = "/api/players" 107 | , expect = Http.expectJson FetchPlayersList decodePlayersList 108 | } 109 | 110 | 111 | decodeGamesList : Decode.Decoder (List Game) 112 | decodeGamesList = 113 | decodeGame 114 | |> Decode.list 115 | |> Decode.at [ "data" ] 116 | 117 | 118 | decodeGame : Decode.Decoder Game 119 | decodeGame = 120 | Decode.map6 Game 121 | (Decode.field "description" Decode.string) 122 | (Decode.field "featured" Decode.bool) 123 | (Decode.field "id" Decode.int) 124 | (Decode.field "slug" Decode.string) 125 | (Decode.field "thumbnail" Decode.string) 126 | (Decode.field "title" Decode.string) 127 | 128 | 129 | decodeGameplaysList : Decode.Decoder (List Gameplay) 130 | decodeGameplaysList = 131 | decodeGameplay 132 | |> Decode.list 133 | |> Decode.at [ "data" ] 134 | 135 | 136 | decodeGameplay : Decode.Decoder Gameplay 137 | decodeGameplay = 138 | Decode.map3 Gameplay 139 | (Decode.field "game_id" Decode.int) 140 | (Decode.field "player_id" Decode.int) 141 | (Decode.field "player_score" Decode.int) 142 | 143 | 144 | decodePlayersList : Decode.Decoder (List Player) 145 | decodePlayersList = 146 | decodePlayer 147 | |> Decode.list 148 | |> Decode.at [ "data" ] 149 | 150 | 151 | decodePlayer : Decode.Decoder Player 152 | decodePlayer = 153 | Decode.map5 Player 154 | (Decode.succeed False) 155 | (Decode.maybe (Decode.field "display_name" Decode.string)) 156 | (Decode.field "id" Decode.int) 157 | (Decode.field "score" Decode.int) 158 | (Decode.field "username" Decode.string) 159 | 160 | 161 | 162 | -- UPDATE 163 | 164 | 165 | type Msg 166 | = FetchGamesList (Result Http.Error (List Game)) 167 | | FetchGameplaysList (Result Http.Error (List Gameplay)) 168 | | FetchPlayersList (Result Http.Error (List Player)) 169 | | TogglePlayerGameplays Player 170 | 171 | 172 | update : Msg -> Model -> ( Model, Cmd Msg ) 173 | update msg model = 174 | case msg of 175 | FetchGamesList result -> 176 | case result of 177 | Ok games -> 178 | ( { model | gamesList = games }, Cmd.none ) 179 | 180 | Err _ -> 181 | Debug.log "Error fetching games from API." 182 | ( model, Cmd.none ) 183 | 184 | FetchGameplaysList result -> 185 | case result of 186 | Ok gameplays -> 187 | ( { model | gameplaysList = gameplays }, Cmd.none ) 188 | 189 | Err _ -> 190 | Debug.log "Error fetching gameplays from API." 191 | ( model, Cmd.none ) 192 | 193 | FetchPlayersList result -> 194 | case result of 195 | Ok players -> 196 | let 197 | updatedPlayersList = 198 | players 199 | |> List.map 200 | (\player -> { player | score = findTotalScoreForPlayer model player }) 201 | in 202 | ( { model | playersList = updatedPlayersList }, Cmd.none ) 203 | 204 | Err message -> 205 | Debug.log "Error fetching players from API." 206 | ( model, Cmd.none ) 207 | 208 | TogglePlayerGameplays player -> 209 | let 210 | updatedPlayersList = 211 | List.map 212 | (\p -> 213 | if p.id == player.id then 214 | { player | displayGameplays = not player.displayGameplays } 215 | 216 | else 217 | p 218 | ) 219 | model.playersList 220 | in 221 | ( { model | playersList = updatedPlayersList }, Cmd.none ) 222 | 223 | 224 | 225 | -- SUBSCRIPTIONS 226 | 227 | 228 | subscriptions : Model -> Sub Msg 229 | subscriptions model = 230 | Sub.none 231 | 232 | 233 | 234 | -- VIEW 235 | 236 | 237 | view : Model -> Html Msg 238 | view model = 239 | div [] 240 | [ featured model 241 | , gamesIndex model 242 | , playersIndex model 243 | ] 244 | 245 | 246 | 247 | -- FEATURED 248 | 249 | 250 | featured : Model -> Html msg 251 | featured model = 252 | case featuredGame model.gamesList of 253 | Just game -> 254 | div [ class "row featured" ] 255 | [ div [ class "container" ] 256 | [ div [ class "featured-img" ] 257 | [ img [ class "featured-thumbnail", src game.thumbnail ] [] ] 258 | , div [ class "featured-data" ] 259 | [ h2 [] [ text "Featured" ] 260 | , h3 [] [ text game.title ] 261 | , p [] [ text game.description ] 262 | , a 263 | [ class "button" 264 | , href ("games/" ++ game.slug) 265 | ] 266 | [ text "Play Now!" ] 267 | ] 268 | ] 269 | ] 270 | 271 | Nothing -> 272 | div [] [] 273 | 274 | 275 | featuredGame : List Game -> Maybe Game 276 | featuredGame games = 277 | games 278 | |> List.filter .featured 279 | |> List.head 280 | 281 | 282 | 283 | -- GAMES 284 | 285 | 286 | gamesIndex : Model -> Html msg 287 | gamesIndex model = 288 | if List.isEmpty model.gamesList then 289 | div [] [] 290 | 291 | else 292 | div [ class "games-index container" ] 293 | [ h2 [] [ text "Games" ] 294 | , gamesList model.gamesList 295 | ] 296 | 297 | 298 | gamesList : List Game -> Html msg 299 | gamesList games = 300 | ul [ class "games-list" ] (List.map gamesListItem games) 301 | 302 | 303 | gamesListItem : Game -> Html msg 304 | gamesListItem game = 305 | a [ href ("games/" ++ game.slug) ] 306 | [ li [ class "game-item" ] 307 | [ div [ class "game-image" ] 308 | [ img [ src game.thumbnail ] [] 309 | ] 310 | , div [ class "game-info" ] 311 | [ h3 [] [ text game.title ] 312 | , p [] [ text game.description ] 313 | ] 314 | ] 315 | ] 316 | 317 | 318 | 319 | -- PLAYERS 320 | 321 | 322 | playersIndex : Model -> Html Msg 323 | playersIndex model = 324 | if List.isEmpty model.playersList then 325 | div [] [] 326 | 327 | else 328 | div [ class "players-index container" ] 329 | [ h2 [] [ text "Player Scores" ] 330 | , playersList model 331 | ] 332 | 333 | 334 | playersList : Model -> Html Msg 335 | playersList model = 336 | model.playersList 337 | |> sortPlayersByScore 338 | |> List.map (playersListItem model) 339 | |> ul [ class "players-list" ] 340 | 341 | 342 | playersListItem : Model -> Player -> Html Msg 343 | playersListItem model player = 344 | li [ class "player-item" ] 345 | [ playersListItemName player 346 | , playersListItemScore model player 347 | , if player.displayGameplays then 348 | gameplaysList model player 349 | 350 | else 351 | div [] [] 352 | ] 353 | 354 | 355 | playersListItemName : Player -> Html Msg 356 | playersListItemName player = 357 | let 358 | displayName = 359 | case player.displayName of 360 | Just name -> 361 | name 362 | 363 | Nothing -> 364 | player.username 365 | in 366 | strong [] 367 | [ a [ onClick (TogglePlayerGameplays player) ] 368 | [ text displayName ] 369 | ] 370 | 371 | 372 | playersListItemScore : Model -> Player -> Html Msg 373 | playersListItemScore model player = 374 | let 375 | playerScore = 376 | findTotalScoreForPlayer model player 377 | in 378 | span [ class "player-score" ] 379 | [ text (String.fromInt playerScore) ] 380 | 381 | 382 | 383 | -- GAMEPLAYS 384 | 385 | 386 | gameplaysList : Model -> Player -> Html msg 387 | gameplaysList model player = 388 | let 389 | gameplays = 390 | findGameplaysForPlayer model player 391 | in 392 | if List.isEmpty gameplays then 393 | div [ class "gameplays-empty" ] 394 | [ text "No gameplays to display yet!" ] 395 | 396 | else 397 | div [ class "gameplays" ] 398 | (List.map (gameplaysListItem model) gameplays) 399 | 400 | 401 | gameplaysListItem : Model -> Gameplay -> Html msg 402 | gameplaysListItem model gameplay = 403 | let 404 | gameTitle = 405 | case findGameForGameplay model gameplay of 406 | Just game -> 407 | game.title 408 | 409 | Nothing -> 410 | String.fromInt gameplay.gameId 411 | in 412 | div [] 413 | [ strong [] [ text (gameTitle ++ ": ") ] 414 | , span [] [ text (String.fromInt gameplay.playerScore) ] 415 | ] 416 | 417 | 418 | 419 | -- HELPERS 420 | 421 | 422 | findGameplaysForPlayer : Model -> Player -> List Gameplay 423 | findGameplaysForPlayer model player = 424 | List.filter (\gameplay -> gameplay.playerId == player.id) model.gameplaysList 425 | 426 | 427 | findGameForGameplay : Model -> Gameplay -> Maybe Game 428 | findGameForGameplay model gameplay = 429 | model.gamesList 430 | |> List.filter (\game -> game.id == gameplay.gameId) 431 | |> List.head 432 | 433 | 434 | findTotalScoreForPlayer : Model -> Player -> Int 435 | findTotalScoreForPlayer model player = 436 | findGameplaysForPlayer model player 437 | |> List.map .playerScore 438 | |> List.sum 439 | 440 | 441 | sortPlayersByScore : List Player -> List Player 442 | sortPlayersByScore players = 443 | players 444 | |> List.sortBy .score 445 | |> List.reverse 446 | -------------------------------------------------------------------------------- /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 from "../css/app.css" 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 dependencies 11 | // 12 | import "phoenix_html" 13 | 14 | // Phoenix Socket 15 | import { Socket } from "phoenix" 16 | 17 | let socketParams = (window.userToken == "") ? {} : { token: window.userToken }; 18 | 19 | let socket = new Socket("/socket", { 20 | params: socketParams 21 | }) 22 | 23 | socket.connect() 24 | 25 | // Elm 26 | import { Elm } from "../elm/src/Main.elm"; 27 | 28 | const elmContainer = document.querySelector("#elm-container"); 29 | const platformer = document.querySelector("#platformer"); 30 | 31 | if (elmContainer) { 32 | Elm.Main.init({ node: elmContainer }); 33 | } 34 | if (platformer) { 35 | let app = Elm.Games.Platformer.init({ 36 | node: platformer, 37 | flags: { token: window.userToken } 38 | }); 39 | 40 | let channel = socket.channel("score:platformer", {}) 41 | 42 | channel.join() 43 | .receive("ok", resp => { console.log("Joined successfully", resp) }) 44 | .receive("error", resp => { console.log("Unable to join", resp) }) 45 | 46 | app.ports.broadcastScore.subscribe(function (scoreData) { 47 | console.log(`Broadcasting ${scoreData} score data from Elm using the broadcastScore port.`); 48 | channel.push("broadcast_score", { player_score: scoreData }); 49 | }); 50 | 51 | app.ports.saveScore.subscribe(function (scoreData) { 52 | console.log(`Saving ${scoreData} score data from Elm using the saveScore port.`); 53 | channel.push("save_score", { player_score: scoreData }); 54 | }); 55 | 56 | channel.on("broadcast_score", payload => { 57 | console.log(`Receiving payload data from Phoenix using the receivingScoreFromPhoenix port.`); 58 | 59 | app.ports.receiveScoreFromPhoenix.send({ 60 | game_id: payload.game_id || 0, 61 | player_id: payload.player_id || 0, 62 | player_score: payload.player_score || 0 63 | }); 64 | }); 65 | } 66 | 67 | // Disable default space bar and arrow keys in favor of game interactions 68 | document.documentElement.addEventListener( 69 | "keydown", 70 | function (e) { 71 | let spaceBarKeyCode = 32; 72 | let upArrowKeyCode = 38; 73 | let downArrowKeyCode = 40; 74 | 75 | if ( 76 | (e.keycode || e.which) == spaceBarKeyCode || 77 | (e.keycode || e.which) == upArrowKeyCode || 78 | (e.keycode || e.which) == downArrowKeyCode 79 | ) { 80 | e.preventDefault(); 81 | } 82 | }, 83 | false 84 | ); 85 | -------------------------------------------------------------------------------- /assets/js/socket.js: -------------------------------------------------------------------------------- 1 | // NOTE: The contents of this file will only be executed if 2 | // you uncomment its entry in "assets/js/app.js". 3 | 4 | // To use Phoenix channels, the first step is to import Socket, 5 | // and connect at the socket path in "lib/web/endpoint.ex". 6 | // 7 | // Pass the token on params as below. Or remove it 8 | // from the params if you are not using authentication. 9 | import {Socket} from "phoenix" 10 | 11 | let socket = new Socket("/socket", {params: {token: window.userToken}}) 12 | 13 | // When you connect, you'll often need to authenticate the client. 14 | // For example, imagine you have an authentication plug, `MyAuth`, 15 | // which authenticates the session and assigns a `:current_user`. 16 | // If the current user exists you can assign the user's token in 17 | // the connection for use in the layout. 18 | // 19 | // In your "lib/web/router.ex": 20 | // 21 | // pipeline :browser do 22 | // ... 23 | // plug MyAuth 24 | // plug :put_user_token 25 | // end 26 | // 27 | // defp put_user_token(conn, _) do 28 | // if current_user = conn.assigns[:current_user] do 29 | // token = Phoenix.Token.sign(conn, "user socket", current_user.id) 30 | // assign(conn, :user_token, token) 31 | // else 32 | // conn 33 | // end 34 | // end 35 | // 36 | // Now you need to pass this token to JavaScript. You can do so 37 | // inside a script tag in "lib/web/templates/layout/app.html.eex": 38 | // 39 | // 40 | // 41 | // You will need to verify the user token in the "connect/3" function 42 | // in "lib/web/channels/user_socket.ex": 43 | // 44 | // def connect(%{"token" => token}, socket, _connect_info) do 45 | // # max_age: 1209600 is equivalent to two weeks in seconds 46 | // case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do 47 | // {:ok, user_id} -> 48 | // {:ok, assign(socket, :user, user_id)} 49 | // {:error, reason} -> 50 | // :error 51 | // end 52 | // end 53 | // 54 | // Finally, connect to the socket: 55 | socket.connect() 56 | 57 | // Now that you are connected, you can join channels with a topic: 58 | let channel = socket.channel("topic:subtopic", {}) 59 | channel.join() 60 | .receive("ok", resp => { console.log("Joined successfully", resp) }) 61 | .receive("error", resp => { console.log("Unable to join", resp) }) 62 | 63 | export default socket 64 | -------------------------------------------------------------------------------- /assets/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "repository": {}, 3 | "license": "MIT", 4 | "scripts": { 5 | "deploy": "webpack --mode production", 6 | "watch": "webpack --mode development --watch" 7 | }, 8 | "dependencies": { 9 | "elm": "^0.19.0-no-deps", 10 | "elm-webpack-loader": "^5.0.0", 11 | "phoenix": "file:../deps/phoenix", 12 | "phoenix_html": "file:../deps/phoenix_html" 13 | }, 14 | "devDependencies": { 15 | "@babel/core": "^7.0.0", 16 | "@babel/preset-env": "^7.0.0", 17 | "babel-loader": "^8.0.0", 18 | "copy-webpack-plugin": "^4.5.0", 19 | "css-loader": "^0.28.10", 20 | "mini-css-extract-plugin": "^0.4.0", 21 | "optimize-css-assets-webpack-plugin": "^4.0.0", 22 | "uglifyjs-webpack-plugin": "^1.2.4", 23 | "webpack": "4.4.0", 24 | "webpack-cli": "^2.0.10" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /assets/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/favicon.ico -------------------------------------------------------------------------------- /assets/static/images/character-left.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/images/character-left.gif -------------------------------------------------------------------------------- /assets/static/images/character-right.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/images/character-right.gif -------------------------------------------------------------------------------- /assets/static/images/character.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/images/character.gif -------------------------------------------------------------------------------- /assets/static/images/coin.svg: -------------------------------------------------------------------------------- 1 | 55 | -------------------------------------------------------------------------------- /assets/static/images/phoenix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/images/phoenix.png -------------------------------------------------------------------------------- /assets/static/images/platformer-thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elixir-elm-tutorial/platform/aebd273125d86c58e339e0dd3f0147741906d9e0/assets/static/images/platformer-thumbnail.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 MiniCssExtractPlugin = require('mini-css-extract-plugin'); 4 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); 5 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 6 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 7 | 8 | module.exports = (env, options) => ({ 9 | optimization: { 10 | minimizer: [ 11 | new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }), 12 | new OptimizeCSSAssetsPlugin({}) 13 | ] 14 | }, 15 | entry: { 16 | './js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js')) 17 | }, 18 | output: { 19 | filename: 'app.js', 20 | path: path.resolve(__dirname, '../priv/static/js') 21 | }, 22 | module: { 23 | rules: [ 24 | { 25 | test: /\.js$/, 26 | exclude: /node_modules/, 27 | use: { 28 | loader: 'babel-loader' 29 | } 30 | }, 31 | { 32 | test: /\.css$/, 33 | use: [MiniCssExtractPlugin.loader, 'css-loader'] 34 | }, 35 | { 36 | test: /\.elm$/, 37 | exclude: [/elm-stuff/, /node_modules/], 38 | use: { 39 | loader: 'elm-webpack-loader', 40 | options: { 41 | cwd: path.resolve(__dirname, 'elm'), 42 | files: [ 43 | path.resolve(__dirname, "elm/src/Main.elm"), 44 | path.resolve(__dirname, "elm/src/Games/Platformer.elm") 45 | ] 46 | } 47 | } 48 | } 49 | ] 50 | }, 51 | plugins: [ 52 | new MiniCssExtractPlugin({ filename: '../css/app.css' }), 53 | new CopyWebpackPlugin([{ from: 'static/', to: '../' }]) 54 | ] 55 | }); 56 | -------------------------------------------------------------------------------- /compile: -------------------------------------------------------------------------------- 1 | npm run deploy 2 | cd $phoenix_dir 3 | mix "${phoenix_ex}.digest" 4 | -------------------------------------------------------------------------------- /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 | config :platform, 11 | ecto_repos: [Platform.Repo] 12 | 13 | # Configures the endpoint 14 | config :platform, PlatformWeb.Endpoint, 15 | url: [host: "localhost"], 16 | secret_key_base: "/G9FWwO7YY3ToHR3udWaPFICtaTB5S4qMHFcdWOd0UxzY13quCgI3hvEWQnIXb2e", 17 | render_errors: [view: PlatformWeb.ErrorView, accepts: ~w(html json)], 18 | pubsub: [name: Platform.PubSub, adapter: Phoenix.PubSub.PG2] 19 | 20 | # Configures Elixir's Logger 21 | config :logger, :console, 22 | format: "$time $metadata[$level] $message\n", 23 | metadata: [:request_id] 24 | 25 | # Use Jason for JSON parsing in Phoenix 26 | config :phoenix, :json_library, Jason 27 | 28 | # Import environment specific config. This must remain at the bottom 29 | # of this file so it overrides the configuration defined above. 30 | import_config "#{Mix.env()}.exs" 31 | -------------------------------------------------------------------------------- /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 :platform, PlatformWeb.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 :platform, PlatformWeb.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/platform_web/views/.*(ex)$}, 55 | ~r{lib/platform_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 | 69 | # Configure your database 70 | config :platform, Platform.Repo, 71 | username: "postgres", 72 | password: "postgres", 73 | database: "platform_dev", 74 | hostname: "localhost", 75 | pool_size: 10 76 | -------------------------------------------------------------------------------- /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 :platform, PlatformWeb.Endpoint, 13 | http: [:inet6, port: System.get_env("PORT") || 4000], 14 | url: [scheme: "https", host: "elixir-elm-tutorial.herokuapp.com", port: 443], 15 | force_ssl: [rewrite_on: [:x_forwarded_proto]], 16 | cache_static_manifest: "priv/static/cache_manifest.json", 17 | secret_key_base: Map.fetch!(System.get_env(), "SECRET_KEY_BASE") 18 | 19 | # Database configuration 20 | config :platform, Platform.Repo, 21 | adapter: Ecto.Adapters.Postgres, 22 | url: System.get_env("DATABASE_URL"), 23 | pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), 24 | ssl: true 25 | 26 | # Do not print debug messages in production 27 | config :logger, level: :info 28 | 29 | # ## SSL Support 30 | # 31 | # To get SSL working, you will need to add the `https` key 32 | # to the previous section and set your `:url` port to 443: 33 | # 34 | # config :platform, PlatformWeb.Endpoint, 35 | # ... 36 | # url: [host: "example.com", port: 443], 37 | # https: [ 38 | # :inet6, 39 | # port: 443, 40 | # cipher_suite: :strong, 41 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), 42 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") 43 | # ] 44 | # 45 | # The `cipher_suite` is set to `:strong` to support only the 46 | # latest and more secure SSL ciphers. This means old browsers 47 | # and clients may not be supported. You can set it to 48 | # `:compatible` for wider support. 49 | # 50 | # `:keyfile` and `:certfile` expect an absolute path to the key 51 | # and cert in disk or a relative path inside priv, for example 52 | # "priv/ssl/server.key". For all supported SSL configuration 53 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 54 | # 55 | # We also recommend setting `force_ssl` in your endpoint, ensuring 56 | # no data is ever sent via http, always redirecting to https: 57 | # 58 | # config :platform, PlatformWeb.Endpoint, 59 | # force_ssl: [hsts: true] 60 | # 61 | # Check `Plug.SSL` for all available options in `force_ssl`. 62 | 63 | # ## Using releases (distillery) 64 | # 65 | # If you are doing OTP releases, you need to instruct Phoenix 66 | # to start the server for all endpoints: 67 | # 68 | # config :phoenix, :serve_endpoints, true 69 | # 70 | # Alternatively, you can configure exactly which server to 71 | # start per endpoint: 72 | # 73 | # config :platform, PlatformWeb.Endpoint, server: true 74 | # 75 | # Note you can't rely on `System.get_env/1` when using releases. 76 | # See the releases documentation accordingly. 77 | 78 | # Finally import the config/prod.secret.exs which should be versioned 79 | # separately. 80 | # import_config "prod.secret.exs" 81 | -------------------------------------------------------------------------------- /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 :platform, PlatformWeb.Endpoint, 6 | http: [port: 4002], 7 | server: false 8 | 9 | # Print only warnings and errors during test 10 | config :logger, level: :warn 11 | 12 | # Configure your database 13 | config :platform, Platform.Repo, 14 | username: "postgres", 15 | password: "postgres", 16 | database: "platform_test", 17 | hostname: "localhost", 18 | pool: Ecto.Adapters.SQL.Sandbox 19 | 20 | # Reduce bcrypt rounds to speed up tests 21 | config :bcrypt_elixir, :log_rounds, 4 22 | -------------------------------------------------------------------------------- /elixir_buildpack.config: -------------------------------------------------------------------------------- 1 | erlang_version=20.0 2 | elixir_version=1.7.0 3 | always_rebuild=true 4 | -------------------------------------------------------------------------------- /lib/platform.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform do 2 | @moduledoc """ 3 | Platform 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/platform/accounts/accounts.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Accounts do 2 | @moduledoc """ 3 | The Accounts context. 4 | """ 5 | 6 | import Ecto.Query, warn: false 7 | alias Platform.Repo 8 | 9 | alias Platform.Accounts.Player 10 | 11 | @doc """ 12 | Returns the list of players. 13 | 14 | ## Examples 15 | 16 | iex> list_players() 17 | [%Player{}, ...] 18 | 19 | """ 20 | def list_players do 21 | Repo.all(Player) 22 | end 23 | 24 | @doc """ 25 | Gets a single player. 26 | 27 | Raises `Ecto.NoResultsError` if the Player does not exist. 28 | 29 | ## Examples 30 | 31 | iex> get_player!(123) 32 | %Player{} 33 | 34 | iex> get_player!(456) 35 | ** (Ecto.NoResultsError) 36 | 37 | """ 38 | def get_player!(id), do: Repo.get!(Player, id) 39 | 40 | @doc """ 41 | Creates a player. 42 | 43 | ## Examples 44 | 45 | iex> create_player(%{field: value}) 46 | {:ok, %Player{}} 47 | 48 | iex> create_player(%{field: bad_value}) 49 | {:error, %Ecto.Changeset{}} 50 | 51 | """ 52 | def create_player(attrs \\ %{}) do 53 | %Player{} 54 | |> Player.registration_changeset(attrs) 55 | |> Repo.insert() 56 | end 57 | 58 | @doc """ 59 | Updates a player. 60 | 61 | ## Examples 62 | 63 | iex> update_player(player, %{field: new_value}) 64 | {:ok, %Player{}} 65 | 66 | iex> update_player(player, %{field: bad_value}) 67 | {:error, %Ecto.Changeset{}} 68 | 69 | """ 70 | def update_player(%Player{} = player, attrs) do 71 | player 72 | |> Player.changeset(attrs) 73 | |> Repo.update() 74 | end 75 | 76 | @doc """ 77 | Deletes a Player. 78 | 79 | ## Examples 80 | 81 | iex> delete_player(player) 82 | {:ok, %Player{}} 83 | 84 | iex> delete_player(player) 85 | {:error, %Ecto.Changeset{}} 86 | 87 | """ 88 | def delete_player(%Player{} = player) do 89 | Repo.delete(player) 90 | end 91 | 92 | @doc """ 93 | Returns an `%Ecto.Changeset{}` for tracking player changes. 94 | 95 | ## Examples 96 | 97 | iex> change_player(player) 98 | %Ecto.Changeset{source: %Player{}} 99 | 100 | """ 101 | def change_player(%Player{} = player) do 102 | Player.changeset(player, %{}) 103 | end 104 | end 105 | -------------------------------------------------------------------------------- /lib/platform/accounts/player.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Accounts.Player do 2 | use Ecto.Schema 3 | import Ecto.Changeset 4 | 5 | alias Platform.Products.Game 6 | alias Platform.Products.Gameplay 7 | 8 | schema "players" do 9 | many_to_many :games, Game, join_through: Gameplay 10 | 11 | field :display_name, :string 12 | field :password, :string, virtual: true 13 | field :password_digest, :string 14 | field :score, :integer, default: 0 15 | field :username, :string, unique: true 16 | 17 | timestamps() 18 | end 19 | 20 | @doc false 21 | def changeset(player, attrs) do 22 | player 23 | |> cast(attrs, [:display_name, :password, :score, :username]) 24 | |> validate_required([:username]) 25 | |> unique_constraint(:username) 26 | |> validate_length(:username, min: 2, max: 100) 27 | |> validate_length(:password, min: 2, max: 100) 28 | |> put_pass_digest() 29 | end 30 | 31 | @doc false 32 | def registration_changeset(player, attrs) do 33 | player 34 | |> cast(attrs, [:password, :username]) 35 | |> validate_required([:password, :username]) 36 | |> unique_constraint(:username) 37 | |> validate_length(:username, min: 2, max: 100) 38 | |> validate_length(:password, min: 2, max: 100) 39 | |> put_pass_digest() 40 | end 41 | 42 | defp put_pass_digest(changeset) do 43 | case changeset do 44 | %Ecto.Changeset{valid?: true, changes: %{password: pass}} -> 45 | put_change(changeset, :password_digest, Comeonin.Bcrypt.hashpwsalt(pass)) 46 | 47 | _ -> 48 | changeset 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/platform/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.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 | # List all child processes to be supervised 10 | children = [ 11 | # Start the Ecto repository 12 | Platform.Repo, 13 | # Start the endpoint when the application starts 14 | PlatformWeb.Endpoint 15 | # Starts a worker by calling: Platform.Worker.start_link(arg) 16 | # {Platform.Worker, arg}, 17 | ] 18 | 19 | # See https://hexdocs.pm/elixir/Supervisor.html 20 | # for other strategies and supported options 21 | opts = [strategy: :one_for_one, name: Platform.Supervisor] 22 | Supervisor.start_link(children, opts) 23 | end 24 | 25 | # Tell Phoenix to update the endpoint configuration 26 | # whenever the application is updated. 27 | def config_change(changed, _new, removed) do 28 | PlatformWeb.Endpoint.config_change(changed, removed) 29 | :ok 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/platform/products/game.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Products.Game do 2 | use Ecto.Schema 3 | import Ecto.Changeset 4 | 5 | alias Platform.Products.Gameplay 6 | alias Platform.Accounts.Player 7 | 8 | schema "games" do 9 | many_to_many :players, Player, join_through: Gameplay 10 | 11 | field :description, :string 12 | field :featured, :boolean, default: false 13 | field :slug, :string, unique: true 14 | field :thumbnail, :string 15 | field :title, :string 16 | 17 | timestamps() 18 | end 19 | 20 | @doc false 21 | def changeset(game, attrs) do 22 | game 23 | |> cast(attrs, [:description, :featured, :slug, :thumbnail, :title]) 24 | |> validate_required([:description, :featured, :slug, :thumbnail, :title]) 25 | |> unique_constraint(:slug) 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/platform/products/gameplay.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Products.Gameplay do 2 | use Ecto.Schema 3 | import Ecto.Changeset 4 | 5 | alias Platform.Products.Game 6 | alias Platform.Accounts.Player 7 | 8 | schema "gameplays" do 9 | belongs_to :game, Game 10 | belongs_to :player, Player 11 | 12 | field :player_score, :integer, default: 0 13 | 14 | timestamps() 15 | end 16 | 17 | @doc false 18 | def changeset(gameplay, attrs) do 19 | gameplay 20 | |> cast(attrs, [:game_id, :player_id, :player_score]) 21 | |> validate_required([:player_score]) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/platform/products/products.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Products do 2 | @moduledoc """ 3 | The Products context. 4 | """ 5 | 6 | import Ecto.Query, warn: false 7 | alias Platform.Repo 8 | 9 | alias Platform.Products.Game 10 | 11 | @doc """ 12 | Returns the list of games. 13 | 14 | ## Examples 15 | 16 | iex> list_games() 17 | [%Game{}, ...] 18 | 19 | """ 20 | def list_games do 21 | Repo.all(Game) 22 | end 23 | 24 | @doc """ 25 | Gets a single game. 26 | 27 | Raises `Ecto.NoResultsError` if the Game does not exist. 28 | 29 | ## Examples 30 | 31 | iex> get_game!(123) 32 | %Game{} 33 | 34 | iex> get_game!(456) 35 | ** (Ecto.NoResultsError) 36 | 37 | """ 38 | def get_game!(id), do: Repo.get!(Game, id) 39 | def get_game_by_slug!(slug), do: Repo.get_by!(Game, slug: slug) 40 | 41 | @doc """ 42 | Creates a game. 43 | 44 | ## Examples 45 | 46 | iex> create_game(%{field: value}) 47 | {:ok, %Game{}} 48 | 49 | iex> create_game(%{field: bad_value}) 50 | {:error, %Ecto.Changeset{}} 51 | 52 | """ 53 | def create_game(attrs \\ %{}) do 54 | %Game{} 55 | |> Game.changeset(attrs) 56 | |> Repo.insert() 57 | end 58 | 59 | @doc """ 60 | Updates a game. 61 | 62 | ## Examples 63 | 64 | iex> update_game(game, %{field: new_value}) 65 | {:ok, %Game{}} 66 | 67 | iex> update_game(game, %{field: bad_value}) 68 | {:error, %Ecto.Changeset{}} 69 | 70 | """ 71 | def update_game(%Game{} = game, attrs) do 72 | game 73 | |> Game.changeset(attrs) 74 | |> Repo.update() 75 | end 76 | 77 | @doc """ 78 | Deletes a Game. 79 | 80 | ## Examples 81 | 82 | iex> delete_game(game) 83 | {:ok, %Game{}} 84 | 85 | iex> delete_game(game) 86 | {:error, %Ecto.Changeset{}} 87 | 88 | """ 89 | def delete_game(%Game{} = game) do 90 | Repo.delete(game) 91 | end 92 | 93 | @doc """ 94 | Returns an `%Ecto.Changeset{}` for tracking game changes. 95 | 96 | ## Examples 97 | 98 | iex> change_game(game) 99 | %Ecto.Changeset{source: %Game{}} 100 | 101 | """ 102 | def change_game(%Game{} = game) do 103 | Game.changeset(game, %{}) 104 | end 105 | 106 | alias Platform.Products.Gameplay 107 | 108 | @doc """ 109 | Returns the list of gameplays. 110 | 111 | ## Examples 112 | 113 | iex> list_gameplays() 114 | [%Gameplay{}, ...] 115 | 116 | """ 117 | def list_gameplays do 118 | Repo.all(Gameplay) 119 | end 120 | 121 | @doc """ 122 | Gets a single gameplay. 123 | 124 | Raises `Ecto.NoResultsError` if the Gameplay does not exist. 125 | 126 | ## Examples 127 | 128 | iex> get_gameplay!(123) 129 | %Gameplay{} 130 | 131 | iex> get_gameplay!(456) 132 | ** (Ecto.NoResultsError) 133 | 134 | """ 135 | def get_gameplay!(id), do: Repo.get!(Gameplay, id) 136 | 137 | @doc """ 138 | Creates a gameplay. 139 | 140 | ## Examples 141 | 142 | iex> create_gameplay(%{field: value}) 143 | {:ok, %Gameplay{}} 144 | 145 | iex> create_gameplay(%{field: bad_value}) 146 | {:error, %Ecto.Changeset{}} 147 | 148 | """ 149 | def create_gameplay(attrs \\ %{}) do 150 | %Gameplay{} 151 | |> Gameplay.changeset(attrs) 152 | |> Repo.insert() 153 | end 154 | 155 | @doc """ 156 | Updates a gameplay. 157 | 158 | ## Examples 159 | 160 | iex> update_gameplay(gameplay, %{field: new_value}) 161 | {:ok, %Gameplay{}} 162 | 163 | iex> update_gameplay(gameplay, %{field: bad_value}) 164 | {:error, %Ecto.Changeset{}} 165 | 166 | """ 167 | def update_gameplay(%Gameplay{} = gameplay, attrs) do 168 | gameplay 169 | |> Gameplay.changeset(attrs) 170 | |> Repo.update() 171 | end 172 | 173 | @doc """ 174 | Deletes a Gameplay. 175 | 176 | ## Examples 177 | 178 | iex> delete_gameplay(gameplay) 179 | {:ok, %Gameplay{}} 180 | 181 | iex> delete_gameplay(gameplay) 182 | {:error, %Ecto.Changeset{}} 183 | 184 | """ 185 | def delete_gameplay(%Gameplay{} = gameplay) do 186 | Repo.delete(gameplay) 187 | end 188 | 189 | @doc """ 190 | Returns an `%Ecto.Changeset{}` for tracking gameplay changes. 191 | 192 | ## Examples 193 | 194 | iex> change_gameplay(gameplay) 195 | %Ecto.Changeset{source: %Gameplay{}} 196 | 197 | """ 198 | def change_gameplay(%Gameplay{} = gameplay) do 199 | Gameplay.changeset(gameplay, %{}) 200 | end 201 | end 202 | -------------------------------------------------------------------------------- /lib/platform/repo.ex: -------------------------------------------------------------------------------- 1 | defmodule Platform.Repo do 2 | use Ecto.Repo, 3 | otp_app: :platform, 4 | adapter: Ecto.Adapters.Postgres 5 | end 6 | -------------------------------------------------------------------------------- /lib/platform_web.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb 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 PlatformWeb, :controller 9 | use PlatformWeb, :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: PlatformWeb 23 | 24 | import Plug.Conn 25 | import PlatformWeb.Gettext 26 | alias PlatformWeb.Router.Helpers, as: Routes 27 | end 28 | end 29 | 30 | def view do 31 | quote do 32 | use Phoenix.View, 33 | root: "lib/platform_web/templates", 34 | namespace: PlatformWeb 35 | 36 | # Import convenience functions from controllers 37 | import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1] 38 | 39 | # Use all HTML functionality (forms, tags, etc) 40 | use Phoenix.HTML 41 | 42 | import PlatformWeb.ErrorHelpers 43 | import PlatformWeb.Gettext 44 | alias PlatformWeb.Router.Helpers, as: Routes 45 | end 46 | end 47 | 48 | def router do 49 | quote do 50 | use Phoenix.Router 51 | import Plug.Conn 52 | import Phoenix.Controller 53 | end 54 | end 55 | 56 | def channel do 57 | quote do 58 | use Phoenix.Channel 59 | import PlatformWeb.Gettext 60 | end 61 | end 62 | 63 | @doc """ 64 | When used, dispatch to the appropriate controller/view/etc. 65 | """ 66 | defmacro __using__(which) when is_atom(which) do 67 | apply(__MODULE__, which, []) 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /lib/platform_web/channels/score_channel.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.ScoreChannel do 2 | use PlatformWeb, :channel 3 | 4 | def join("score:" <> game_slug, _payload, socket) do 5 | game = Platform.Products.get_game_by_slug!(game_slug) 6 | socket = assign(socket, :game_id, game.id) 7 | {:ok, socket} 8 | end 9 | 10 | # Broadcast for authenticated players 11 | def handle_in( 12 | "broadcast_score", 13 | %{"player_score" => player_score} = payload, 14 | %{assigns: %{game_id: game_id, player_id: player_id}} = socket 15 | ) do 16 | payload = %{ 17 | game_id: game_id, 18 | player_id: player_id, 19 | player_score: player_score 20 | } 21 | 22 | IO.inspect(payload, label: "Broadcasting the score payload over the channel") 23 | broadcast(socket, "broadcast_score", payload) 24 | {:noreply, socket} 25 | end 26 | 27 | # Broadcast for anonymous players 28 | def handle_in("broadcast_score", payload, socket) do 29 | broadcast(socket, "broadcast_score", payload) 30 | {:noreply, socket} 31 | end 32 | 33 | # Save scores for authenticated players 34 | def handle_in( 35 | "save_score", 36 | %{"player_score" => player_score} = payload, 37 | %{assigns: %{game_id: game_id, player_id: player_id}} = socket 38 | ) do 39 | payload = %{ 40 | game_id: game_id, 41 | player_id: player_id, 42 | player_score: player_score 43 | } 44 | 45 | IO.inspect(payload, label: "Saving the score payload to the database") 46 | Platform.Products.create_gameplay(payload) 47 | {:noreply, socket} 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/platform_web/channels/user_socket.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.UserSocket do 2 | use Phoenix.Socket 3 | 4 | ## Channels 5 | channel "score:*", PlatformWeb.ScoreChannel 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 | def connect(%{"token" => token}, socket) do 19 | case Phoenix.Token.verify(socket, "user salt", token, max_age: 1_209_600) do 20 | {:ok, current_user_id} -> 21 | socket = assign(socket, :player_id, current_user_id) 22 | {:ok, socket} 23 | 24 | {:error, _} -> 25 | :error 26 | end 27 | end 28 | 29 | def connect(_params, socket) do 30 | {:ok, socket} 31 | end 32 | 33 | # Socket id's are topics that allow you to identify all sockets for a given user: 34 | # 35 | # def id(socket), do: "user_socket:#{socket.assigns.user_id}" 36 | # 37 | # Would allow you to broadcast a "disconnect" event and terminate 38 | # all active sockets and channels for a given user: 39 | # 40 | # PlatformWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) 41 | # 42 | # Returning `nil` makes this socket anonymous. 43 | def id(_socket), do: nil 44 | end 45 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/fallback_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.FallbackController do 2 | @moduledoc """ 3 | Translates controller action results into valid `Plug.Conn` responses. 4 | 5 | See `Phoenix.Controller.action_fallback/1` for more details. 6 | """ 7 | use PlatformWeb, :controller 8 | 9 | def call(conn, {:error, %Ecto.Changeset{} = changeset}) do 10 | conn 11 | |> put_status(:unprocessable_entity) 12 | |> put_view(PlatformWeb.ChangesetView) 13 | |> render("error.json", changeset: changeset) 14 | end 15 | 16 | def call(conn, {:error, :not_found}) do 17 | conn 18 | |> put_status(:not_found) 19 | |> put_view(PlatformWeb.ErrorView) 20 | |> render(:"404") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/game_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.GameController do 2 | use PlatformWeb, :controller 3 | 4 | alias Platform.Products 5 | alias Platform.Products.Game 6 | 7 | action_fallback PlatformWeb.FallbackController 8 | 9 | def index(conn, _params) do 10 | games = Products.list_games() 11 | render(conn, "index.json", games: games) 12 | end 13 | 14 | def create(conn, %{"game" => game_params}) do 15 | with {:ok, %Game{} = game} <- Products.create_game(game_params) do 16 | conn 17 | |> put_status(:created) 18 | |> put_resp_header("location", Routes.game_path(conn, :show, game)) 19 | |> render("show.json", game: game) 20 | end 21 | end 22 | 23 | def show(conn, %{"id" => id}) do 24 | game = Products.get_game!(id) 25 | render(conn, "show.json", game: game) 26 | end 27 | 28 | def play(conn, %{"slug" => slug}) do 29 | game = Products.get_game_by_slug!(slug) 30 | render(conn, "show.html", game: game) 31 | end 32 | 33 | def update(conn, %{"id" => id, "game" => game_params}) do 34 | game = Products.get_game!(id) 35 | 36 | with {:ok, %Game{} = game} <- Products.update_game(game, game_params) do 37 | render(conn, "show.json", game: game) 38 | end 39 | end 40 | 41 | def delete(conn, %{"id" => id}) do 42 | game = Products.get_game!(id) 43 | 44 | with {:ok, %Game{}} <- Products.delete_game(game) do 45 | send_resp(conn, :no_content, "") 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/gameplay_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.GameplayController do 2 | use PlatformWeb, :controller 3 | 4 | alias Platform.Products 5 | alias Platform.Products.Gameplay 6 | 7 | action_fallback PlatformWeb.FallbackController 8 | 9 | def index(conn, _params) do 10 | gameplays = Products.list_gameplays() 11 | render(conn, "index.json", gameplays: gameplays) 12 | end 13 | 14 | def create(conn, %{"gameplay" => gameplay_params}) do 15 | with {:ok, %Gameplay{} = gameplay} <- Products.create_gameplay(gameplay_params) do 16 | conn 17 | |> put_status(:created) 18 | |> put_resp_header("location", Routes.gameplay_path(conn, :show, gameplay)) 19 | |> render("show.json", gameplay: gameplay) 20 | end 21 | end 22 | 23 | def show(conn, %{"id" => id}) do 24 | gameplay = Products.get_gameplay!(id) 25 | render(conn, "show.json", gameplay: gameplay) 26 | end 27 | 28 | def update(conn, %{"id" => id, "gameplay" => gameplay_params}) do 29 | gameplay = Products.get_gameplay!(id) 30 | 31 | with {:ok, %Gameplay{} = gameplay} <- Products.update_gameplay(gameplay, gameplay_params) do 32 | render(conn, "show.json", gameplay: gameplay) 33 | end 34 | end 35 | 36 | def delete(conn, %{"id" => id}) do 37 | gameplay = Products.get_gameplay!(id) 38 | 39 | with {:ok, %Gameplay{}} <- Products.delete_gameplay(gameplay) do 40 | send_resp(conn, :no_content, "") 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/page_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.PageController do 2 | use PlatformWeb, :controller 3 | 4 | plug :authenticate when action in [:index] 5 | 6 | def index(conn, _params) do 7 | render(conn, "index.html") 8 | end 9 | 10 | defp authenticate(conn, _opts) do 11 | if conn.assigns.current_user() do 12 | conn 13 | else 14 | conn 15 | |> put_flash(:error, "You must be signed in to access that page.") 16 | |> redirect(to: Routes.player_path(conn, :new)) 17 | |> halt() 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/player_api_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.PlayerApiController do 2 | use PlatformWeb, :controller 3 | 4 | alias Platform.Accounts 5 | alias Platform.Accounts.Player 6 | 7 | action_fallback PlatformWeb.FallbackController 8 | 9 | def index(conn, _params) do 10 | players = Accounts.list_players() 11 | render(conn, "index.json", players: players) 12 | end 13 | 14 | def create(conn, %{"player" => player_params}) do 15 | with {:ok, player} <- Accounts.create_player(player_params) do 16 | conn 17 | |> put_status(:created) 18 | |> put_resp_header("location", Routes.player_path(conn, :show, player)) 19 | |> render("show.json", player: player) 20 | end 21 | end 22 | 23 | def show(conn, %{"id" => id}) do 24 | player = Accounts.get_player!(id) 25 | render(conn, "show.json", player: player) 26 | end 27 | 28 | def update(conn, %{"id" => id, "player" => player_params}) do 29 | player = Accounts.get_player!(id) 30 | 31 | with {:ok, player} <- Accounts.update_player(player, player_params) do 32 | render(conn, "show.json", player: player) 33 | end 34 | end 35 | 36 | def delete(conn, %{"id" => id}) do 37 | player = Accounts.get_player!(id) 38 | 39 | with {:ok, %Player{}} <- Accounts.delete_player(player) do 40 | send_resp(conn, :no_content, "") 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/player_auth_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.PlayerAuthController do 2 | import Plug.Conn 3 | 4 | alias Platform.Accounts.Player 5 | 6 | def init(opts) do 7 | Keyword.fetch!(opts, :repo) 8 | end 9 | 10 | def call(conn, repo) do 11 | player_id = get_session(conn, :player_id) 12 | player = player_id && repo.get(Player, player_id) 13 | assign(conn, :current_user, player) 14 | end 15 | 16 | def sign_in(conn, player) do 17 | conn 18 | |> assign(:current_user, player) 19 | |> put_session(:player_id, player.id) 20 | |> configure_session(renew: true) 21 | end 22 | 23 | def sign_in_with_username_and_password(conn, username, given_pass, opts) do 24 | repo = Keyword.fetch!(opts, :repo) 25 | player = repo.get_by(Player, username: username) 26 | 27 | cond do 28 | player && Comeonin.Bcrypt.checkpw(given_pass, player.password_digest) -> 29 | {:ok, sign_in(conn, player)} 30 | 31 | player -> 32 | {:error, :unauthorized, conn} 33 | 34 | true -> 35 | Comeonin.Bcrypt.dummy_checkpw() 36 | {:error, :not_found, conn} 37 | end 38 | end 39 | 40 | def sign_out(conn) do 41 | configure_session(conn, drop: true) 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/player_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.PlayerController do 2 | use PlatformWeb, :controller 3 | 4 | alias Platform.Accounts 5 | alias Platform.Accounts.Player 6 | 7 | plug(:authorize when action in [:edit]) 8 | 9 | def index(conn, _params) do 10 | players = Accounts.list_players() 11 | render(conn, "index.html", players: players) 12 | end 13 | 14 | def new(conn, _params) do 15 | changeset = Accounts.change_player(%Player{}) 16 | render(conn, "new.html", changeset: changeset) 17 | end 18 | 19 | def create(conn, %{"player" => player_params}) do 20 | case Accounts.create_player(player_params) do 21 | {:ok, player} -> 22 | conn 23 | |> PlatformWeb.PlayerAuthController.sign_in(player) 24 | |> put_flash(:info, "Player created successfully.") 25 | |> redirect(to: Routes.player_path(conn, :show, player)) 26 | 27 | {:error, %Ecto.Changeset{} = changeset} -> 28 | render(conn, "new.html", changeset: changeset) 29 | end 30 | end 31 | 32 | def show(conn, %{"id" => id}) do 33 | player = Accounts.get_player!(id) 34 | render(conn, "show.html", player: player) 35 | end 36 | 37 | def edit(conn, %{"id" => id}) do 38 | player = Accounts.get_player!(id) 39 | changeset = Accounts.change_player(player) 40 | render(conn, "edit.html", player: player, changeset: changeset) 41 | end 42 | 43 | def update(conn, %{"id" => id, "player" => player_params}) do 44 | player = Accounts.get_player!(id) 45 | 46 | case Accounts.update_player(player, player_params) do 47 | {:ok, player} -> 48 | conn 49 | |> put_flash(:info, "Player updated successfully.") 50 | |> redirect(to: Routes.player_path(conn, :show, player)) 51 | 52 | {:error, %Ecto.Changeset{} = changeset} -> 53 | render(conn, "edit.html", player: player, changeset: changeset) 54 | end 55 | end 56 | 57 | def delete(conn, %{"id" => id}) do 58 | player = Accounts.get_player!(id) 59 | {:ok, _player} = Accounts.delete_player(player) 60 | 61 | conn 62 | |> put_flash(:info, "Player deleted successfully.") 63 | |> redirect(to: Routes.player_path(conn, :index)) 64 | end 65 | 66 | defp authorize(conn, _opts) do 67 | if Mix.env() == :test do 68 | conn 69 | else 70 | current_player_id = conn.assigns.current_user().id 71 | 72 | requested_player_id = 73 | conn.path_params["id"] 74 | |> String.to_integer() 75 | 76 | if current_player_id == requested_player_id do 77 | conn 78 | else 79 | conn 80 | |> put_flash(:error, "Your account is not authorized to access that page.") 81 | |> redirect(to: Routes.page_path(conn, :index)) 82 | |> halt() 83 | end 84 | end 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /lib/platform_web/controllers/player_session_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.PlayerSessionController do 2 | use PlatformWeb, :controller 3 | 4 | def new(conn, _) do 5 | render(conn, "new.html") 6 | end 7 | 8 | def create(conn, %{"session" => %{"username" => user, "password" => pass}}) do 9 | case PlatformWeb.PlayerAuthController.sign_in_with_username_and_password( 10 | conn, 11 | user, 12 | pass, 13 | repo: Platform.Repo 14 | ) do 15 | {:ok, conn} -> 16 | conn 17 | |> put_flash(:info, "Welcome back!") 18 | |> redirect(to: Routes.page_path(conn, :index)) 19 | 20 | {:error, _reason, conn} -> 21 | conn 22 | |> put_flash(:error, "Invalid username/password combination.") 23 | |> render("new.html") 24 | end 25 | end 26 | 27 | def delete(conn, _) do 28 | conn 29 | |> PlatformWeb.PlayerAuthController.sign_out() 30 | |> redirect(to: Routes.player_session_path(conn, :new)) 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/platform_web/endpoint.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.Endpoint do 2 | use Phoenix.Endpoint, otp_app: :platform 3 | 4 | socket "/socket", PlatformWeb.UserSocket, 5 | websocket: true, 6 | longpoll: false 7 | 8 | # Serve at "/" the static files from "priv/static" directory. 9 | # 10 | # You should set gzip to true if you are running phx.digest 11 | # when deploying your static files in production. 12 | plug Plug.Static, 13 | at: "/", 14 | from: :platform, 15 | gzip: false, 16 | only: ~w(css fonts images js favicon.ico robots.txt) 17 | 18 | # Code reloading can be explicitly enabled under the 19 | # :code_reloader configuration of your endpoint. 20 | if code_reloading? do 21 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket 22 | plug Phoenix.LiveReloader 23 | plug Phoenix.CodeReloader 24 | end 25 | 26 | plug Plug.RequestId 27 | plug Plug.Logger 28 | 29 | plug Plug.Parsers, 30 | parsers: [:urlencoded, :multipart, :json], 31 | pass: ["*/*"], 32 | json_decoder: Phoenix.json_library() 33 | 34 | plug Plug.MethodOverride 35 | plug Plug.Head 36 | 37 | # The session will be stored in the cookie and signed, 38 | # this means its contents can be read but not tampered with. 39 | # Set :encryption_salt if you would also like to encrypt it. 40 | plug Plug.Session, 41 | store: :cookie, 42 | key: "_platform_key", 43 | signing_salt: "ISX0blx+" 44 | 45 | plug PlatformWeb.Router 46 | end 47 | -------------------------------------------------------------------------------- /lib/platform_web/gettext.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.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 PlatformWeb.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: :platform 24 | end 25 | -------------------------------------------------------------------------------- /lib/platform_web/router.ex: -------------------------------------------------------------------------------- 1 | defmodule PlatformWeb.Router do 2 | use PlatformWeb, :router 3 | 4 | pipeline :browser do 5 | plug :accepts, ["html"] 6 | plug :fetch_session 7 | plug :fetch_flash 8 | plug :protect_from_forgery 9 | plug :put_secure_browser_headers 10 | plug PlatformWeb.PlayerAuthController, repo: Platform.Repo 11 | plug :put_user_token 12 | end 13 | 14 | pipeline :api do 15 | plug :accepts, ["json"] 16 | end 17 | 18 | scope "/", PlatformWeb do 19 | pipe_through :browser 20 | 21 | get "/", PageController, :index 22 | get "/games/:slug", GameController, :play 23 | resources "/players", PlayerController 24 | resources "/sessions", PlayerSessionController, only: [:new, :create, :delete] 25 | end 26 | 27 | scope "/api", PlatformWeb do 28 | pipe_through :api 29 | 30 | resources "/games", GameController, except: [:new, :edit] 31 | resources "/gameplays", GameplayController, except: [:new, :edit] 32 | resources "/players", PlayerApiController, except: [:new, :edit] 33 | end 34 | 35 | defp put_user_token(conn, _) do 36 | if current_user = conn.assigns[:current_user] do 37 | token = Phoenix.Token.sign(conn, "user salt", current_user.id) 38 | assign(conn, :user_token, token) 39 | else 40 | conn 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/platform_web/templates/game/show.html.eex: -------------------------------------------------------------------------------- 1 |
2 | -------------------------------------------------------------------------------- /lib/platform_web/templates/layout/app.html.eex: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |<%= get_flash(@conn, :info) %>
32 |<%= get_flash(@conn, :error) %>
33 | <%= render @view_module, @view_template, assigns %> 34 |Oops, something went wrong! Please check the errors below.
7 |Username | 7 |Score | 8 | 9 |10 | |
---|---|---|
<%= player.username %> | 16 |<%= player.score %> | 17 | 18 |19 | <%= link "Show", to: Routes.player_path(@conn, :show, player) %> 20 | | 21 |
Oops, something went wrong! Please check the errors below.
7 |