|
| 1 | +<h1 align="center"> |
| 2 | + <br> |
| 3 | + <img src="https://i.imgur.com/njuVb1g.png" alt="Markdownify" width="200"> |
| 4 | + <br> |
| 5 | + Games API |
| 6 | + <br> |
| 7 | +</h1> |
| 8 | + |
| 9 | +## What is it? |
| 10 | + |
| 11 | +An API to handle most of the things related to XTeam Games. The main idea is to have a communication bridge between new games developed by the XTeam community (and game jam winners) and the XTeam Universe. So we can have a cohesive universe around the same user sessions, achievements, experiences, etc. With this API a game dev can publish their game within the [XTeam Universe](https://xtu.x-team.com/) (XTU). They can also manage things like CRUD leaderboards, CRUD achievements, and CRUD Items. Monitor game's usage data, and authenticate with the same user across all XTU platforms. |
| 12 | + |
| 13 | +The Admin Panel (Frontend APP) is connected to this API allowing XTeam admins to run games on Slack (like The Tower and The Arena) and the rest of the things a regular game dev can do. |
| 14 | + |
| 15 | +## 📑Contents |
| 16 | + |
| 17 | +- [Tech Stack](#tech-stack) |
| 18 | +- [Requirements](#requirements) |
| 19 | +- [Project](#project) |
| 20 | + - [Database](#database) \* [New Migration](#new-migration) |
| 21 | + - [Environmental Variables](#environmental-variables) |
| 22 | + - [API](#api) |
| 23 | + - [API Documentation](#api-documentation) |
| 24 | +- [How to contribute](#how-to-contribute) |
| 25 | + - [JIRA](#jira) |
| 26 | + - [Commits](#commits) |
| 27 | + - [Pull Requests](#pull-requests) |
| 28 | +- [Continous Integration](#continous-integration) |
| 29 | +- [Confluence Documentation](#confluence-documentation) |
| 30 | + |
| 31 | +## 📦Tech Stack |
| 32 | + |
| 33 | +- [Node.js](https://nodejs.org/) |
| 34 | +- [Hapi](https://hapi.dev/) |
| 35 | +- [Sequelize v5](https://sequelize.org/v5/) |
| 36 | +- [PostgreSQL](https://www.postgresql.org/) |
| 37 | +- [TypeScript](https://www.typescriptlang.org/) |
| 38 | +- [Mocha](https://mochajs.org/) - [Chai](https://www.chaijs.com/) - [Sinon](https://sinonjs.org/) |
| 39 | + |
| 40 | +* [Swagger](https://swagger.io/) |
| 41 | + |
| 42 | +## 🔎Requirements |
| 43 | + |
| 44 | +- [Docker](https://www.docker.com/) |
| 45 | +- [NVM - Node Version Manager](https://github.com/nvm-sh/nvm) |
| 46 | + |
| 47 | +## 🚀Project |
| 48 | + |
| 49 | +Let's setup the project!🥹 |
| 50 | + |
| 51 | +### **📟Database** |
| 52 | + |
| 53 | +Spin up the containers by running one of these two commands: |
| 54 | + |
| 55 | +```bash |
| 56 | +docker-compose up -d |
| 57 | +``` |
| 58 | + |
| 59 | +You can now access the DB with your favorite client. If you don't use any, we recommend you [OmniDB](https://github.com/OmniDB/OmniDB) |
| 60 | + |
| 61 | +Here's an example of the OmniDB client with the project DB running. |
| 62 | + |
| 63 | + |
| 64 | +#### **🆕New Migration** |
| 65 | + |
| 66 | +Create a new migration file running: `npm run db:migrate:add some-new-table-name`. |
| 67 | + |
| 68 | +Change the extension of the created file from `.js` to `.ts`. |
| 69 | + |
| 70 | +Paste the following template to your migration: |
| 71 | + |
| 72 | +```typescript |
| 73 | +import type { QueryInterface, Sequelize } from 'sequelize'; |
| 74 | + |
| 75 | +interface SequelizeContext { |
| 76 | + context: { |
| 77 | + queryInterface: QueryInterface; |
| 78 | + Sequelize: Sequelize; |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +module.exports = { |
| 83 | + async up({ context: { queryInterface } }: SequelizeContext) { |
| 84 | + return queryInterface.sequelize.transaction(async (transaction) => {}); |
| 85 | + }, |
| 86 | + |
| 87 | + async down({ context: { queryInterface } }: SequelizeContext) { |
| 88 | + return queryInterface.sequelize.transaction(async (transaction) => {}); |
| 89 | + }, |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +Implement those two functions. `up` is the migration and `down` is the reverse of it. For example, in order to add a new column `isEnabled` to the `Game` table: |
| 94 | + |
| 95 | +```typescript |
| 96 | +import type { QueryInterface, Sequelize } from 'sequelize'; |
| 97 | + |
| 98 | +interface SequelizeContext { |
| 99 | + context: { |
| 100 | + queryInterface: QueryInterface; |
| 101 | + Sequelize: Sequelize; |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = { |
| 106 | + async up({ context: { queryInterface } }: SequelizeContext) { |
| 107 | + return queryInterface.sequelize.transaction(async (transaction) => { |
| 108 | + await queryInterface.addColumn( |
| 109 | + 'Game', |
| 110 | + 'isEnabled', |
| 111 | + { type: Sequelize.BOOLEAN }, |
| 112 | + { transaction } |
| 113 | + ); |
| 114 | + }); |
| 115 | + }, |
| 116 | + |
| 117 | + async down({ context: { queryInterface } }: SequelizeContext) { |
| 118 | + return queryInterface.sequelize.transaction(async (transaction) => { |
| 119 | + await queryInterface.removeColumn('Game', 'isEnabled', { transaction }); |
| 120 | + }); |
| 121 | + }, |
| 122 | +}; |
| 123 | +``` |
| 124 | + |
| 125 | +Now modify the model class, `Game.ts` in this case, and add a new field: |
| 126 | + |
| 127 | +```typescript |
| 128 | +export class Game extends Model<GameAttributes, GameCreationAttributes> { |
| 129 | + @AllowNull(false) |
| 130 | + @Column(DataType.BOOLEAN) |
| 131 | + declare isEnabled: boolean; |
| 132 | + // … |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +You're all set! Make sure everything works locally before pushing or merging. |
| 137 | + |
| 138 | +#### **🚫`backup.sql` file** |
| 139 | + |
| 140 | +**Never** ever ever modify it. **Not even once**. |
| 141 | + |
| 142 | +#### 👴🏻**Past migrations** |
| 143 | + |
| 144 | +**Never ever modify any past migrations.** If a migration was created and deployed, you can't ever change it. If you need to modify it, just create a new migration which will run after the previous one. |
| 145 | + |
| 146 | +### **🔒Environmental Variables** |
| 147 | + |
| 148 | +The API needs some env vars to work properly. You can ask @ccmoralesj for this. |
| 149 | + |
| 150 | +Just grab the `.env.example` file and copy everything into a new file called `.env.dev`. It will look something like this at first. |
| 151 | + |
| 152 | +``` |
| 153 | +# API |
| 154 | +HOST=0.0.0.0 |
| 155 | +#HOST=127.0.0.1 |
| 156 | +PORT=3000 |
| 157 | +
|
| 158 | +# DATABASE |
| 159 | +DB_USERNAME= |
| 160 | +DB_PASSWORD= |
| 161 | +DB_NAME=gameshq_api |
| 162 | +DB_HOSTNAME=127.0.0.1 |
| 163 | +DB_PORT=5434 |
| 164 | +
|
| 165 | +# THE ARENA APP DEV |
| 166 | +SLACK_ARENA_TOKEN= |
| 167 | +SLACK_ARENA_SIGNING_SECRET= |
| 168 | +# Private Channel |
| 169 | +SLACK_ARENA_XHQ_CHANNEL= |
| 170 | +
|
| 171 | +# THE TOWER APP |
| 172 | +SLACK_TOWER_SIGNING_SECRET= |
| 173 | +SLACK_TOWER_TOKEN= |
| 174 | +SLACK_THE_TOWER_CHANNEL= |
| 175 | +
|
| 176 | +# CAMPAIGN APP |
| 177 | +SLACK_CAMPAIGN_SIGNING_SECRET= |
| 178 | +SLACK_CAMPAIGN_TOKEN= |
| 179 | +SLACK_NEUTRAL_ZONE_CHANNEL= |
| 180 | +
|
| 181 | +# FRONT-END-APP |
| 182 | +FRONT_END_APP_BOT_TOKEN= |
| 183 | +FRONT_END_SIGNING_SECRET= |
| 184 | +
|
| 185 | +# COOKIE |
| 186 | +COOKIE_PASSWORD= |
| 187 | +
|
| 188 | +# GOOGLE API CREDS |
| 189 | +GOOGLE_APPLICATION_CLIENT_ID= |
| 190 | +GOOGLE_APPLICATION_CLIENT_SECRET= |
| 191 | +GOOGLE_APPLICATION_CLIENT_RANDOM_PASSWORD= |
| 192 | +
|
| 193 | +# FIREBASE |
| 194 | +GOOGLE_APPLICATION_CREDENTIALS=/some/path/to/google_credentials_games_api.json |
| 195 | +``` |
| 196 | + |
| 197 | +### **🖥️API** |
| 198 | + |
| 199 | +We are almost there with the setup.🥲 Now it's time to run the API |
| 200 | + |
| 201 | +First, make sure you're using our recommended version of Node and yarn by running these commands: |
| 202 | + |
| 203 | +```bash |
| 204 | +nvm use |
| 205 | +``` |
| 206 | + |
| 207 | +Following up, we have to install all required dependencies to run the project: |
| 208 | + |
| 209 | +```bash |
| 210 | +npm install |
| 211 | +``` |
| 212 | + |
| 213 | +Finally, run the application in development mode with the following command: |
| 214 | + |
| 215 | +```bash |
| 216 | +npm run dev |
| 217 | +# If you want to run "production" mode use npm start |
| 218 | +``` |
| 219 | + |
| 220 | +The API should be up and running 🎉at port 3000!🎉 You can verify by browsing to [http://localhost:3000/documentation](http://localhost:3000/documentation) |
| 221 | + |
| 222 | +Let's start coding!🤓 |
| 223 | + |
| 224 | +(GIF: Wait for it...) |
| 225 | + |
| 226 | + |
| 227 | +#### **API Documentation** |
| 228 | + |
| 229 | +Documentation is auto-generated based on the Joi validation provided. You can access it at [`/documentation`](https://xhq-api.x-team.com/documentation). |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +## 🫂How to contribute |
| 234 | + |
| 235 | +Collaborate in this repo is quite easy. |
| 236 | + |
| 237 | +### 📊JIRA |
| 238 | + |
| 239 | +You only need to pick up a ticket from the [JIRA board](https://x-team-internal.atlassian.net/jira/software/c/projects/XTG/boards/48) (If you don't have access you can ask @ccmoralesj) |
| 240 | + |
| 241 | +Each JIRA ticket has an identifier based on a code and a number like XTG-123 which you will use later. |
| 242 | + |
| 243 | +### 💾Commits |
| 244 | + |
| 245 | +Each commit you do needs to have the JIRA ticket identifier so it can be related to the board. |
| 246 | + |
| 247 | +You can use this commit format suggestion. |
| 248 | + |
| 249 | +``` |
| 250 | +:optional-emoji: XTG-123: New endpoint to handle login |
| 251 | +``` |
| 252 | + |
| 253 | +| **Emoji** | **Description** | |
| 254 | +| --------- | --------------------------------------- | |
| 255 | +| **🚀** | New features, or enhancements to code | |
| 256 | +| **🐞** | Bug fixes | |
| 257 | +| **⚙️** | Refactors | |
| 258 | +| **📦** | Build files, dependencies, config files | |
| 259 | +| **🔎** | Minor fixes, typos, imports, etc | |
| 260 | +| **🪄** | Others | |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | +### 🕵🏻Pull Requests |
| 265 | + |
| 266 | +Once you're ready. You can create a new Pull Request (PR) by adding the JIRA ticket identifier in the title. The repo will give you a template to fill in the details of your amazing work! |
| 267 | + |
| 268 | +You can use this PR title format suggestion. |
| 269 | + |
| 270 | +``` |
| 271 | +XTG-123: Login |
| 272 | +``` |
| 273 | + |
| 274 | +You'll need at least 1 review from your teammates to merge the pull request. |
| 275 | + |
| 276 | +## 🪄Continous Integration |
| 277 | + |
| 278 | +This project is connected to an EC2 instance from AWS. |
| 279 | + |
| 280 | +All code from `main` branch will be deployed to staging. |
| 281 | + |
| 282 | +To **deploy to production** you must create a `new release` and follow the [semantic versioning](https://semver.org/lang/es/) fundamentals. That will trigger an automated deployment to **production**. |
| 283 | + |
| 284 | +## 📰Confluence Documentation |
| 285 | + |
| 286 | +Some further information can be found on the X-team GamesHQ API [Confluence page](https://x-team-internal.atlassian.net/wiki/spaces/XTG/pages/2300706828/GamesHQ+API) |
0 commit comments