Skip to content

Commit

Permalink
Base architecture (MetaFam#1)
Browse files Browse the repository at this point in the history
* Hasura and base models

* Added role public SELECT permissions

* Added role player UPDATE permissions

* basic backend api

* Added SELECT permissions for player

* Update backend to typescript

* init app-react

* Add apollo

* graphql-codegen not working well...

* Added web3 to web app

* connecting frontend with web3

* Auth webhook verifies eth signature

* Update frontend to fetch player_id
  • Loading branch information
Pacien Boisson authored Apr 16, 2020
1 parent 30c2df6 commit c684b9d
Show file tree
Hide file tree
Showing 66 changed files with 20,070 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
COMPOSE_PROJECT_NAME=the-game

DATABASE_USER=metagame
DATABASE_PASSWORD=metagame_secret
DATABASE_NAME=thegame

HASURA_GRAPHQL_ADMIN_SECRET=metagame_secret

HASURA_PORT=8080
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store
.idea

node_modules

.yarn/*
!.yarn/releases
!.yarn/plugins
.pnp.*

.pnp.js
yarn-error.log

.env
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
# The Game

Monorepo for the MetaGame applications, backend and databases.


## Development

### Bootstrap

```shell script
cp .env.sample .env
npm run docker:start
```

### Tooling

Start Hasura console

```shell script
npm run hasura:console
```

Hasura CLI example

```shell script
npm run hasura -- migrate squash 1586952135212
```

[Hasura CLI documentation](https://hasura.io/docs/1.0/graphql/manual/hasura-cli/index.html)

### Restart with fresh database

```shell script
npm run docker:clean
npm run docker:start:local
```
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: '3.6'

services:
hasura:
build: ./hasura
depends_on:
- database
ports:
- ${HASURA_PORT}:8080
environment:
PORT: 8080
DATABASE_URL: postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@database:5432/${DATABASE_NAME}
HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET}
HASURA_GRAPHQL_AUTH_HOOK: http://backend:4000/auth-webhook

database:
image: postgres:12
volumes:
- database:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}

backend:
build: ./packages/backend
environment:
PORT: 4000
GRAPHQL_URL: http://hasura:8080/v1/graphql
HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET}

volumes:
database:
...
19 changes: 19 additions & 0 deletions hasura/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM hasura/graphql-engine:v1.1.1.cli-migrations

## Default setup

ENV HASURA_GRAPHQL_ENABLE_TELEMETRY false
ENV HASURA_GRAPHQL_ENABLE_CONSOLE false
ENV HASURA_GRAPHQL_ENABLED_LOG_TYPES startup, http-log, webhook-log, websocket-log, query-log

## Migrations

COPY migrations /hasura-migrations
ENV HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR DATABASE_URL

## Execution

CMD graphql-engine \
--database-url $DATABASE_URL \
serve \
--server-port $PORT
2 changes: 2 additions & 0 deletions hasura/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
endpoint: http://localhost:8080
admin_secret: metagame_secret
99 changes: 99 additions & 0 deletions hasura/migrations/1575910000000_init/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- Enums

CREATE TYPE "Profile_Type" AS ENUM (
'ETHEREUM',
'DISCORD',
'GITHUB',
'DISCOURSE'
);

CREATE TYPE "Rank" AS ENUM (
'PLAYER',
'BRONZE',
'SILVER',
'GOLDEN',
'PLATINIUM',
'DIAMOND'
);

-- Tables

CREATE TABLE "Player" (
"id" uuid DEFAULT public.gen_random_uuid() NOT NULL,
"totalXp" numeric DEFAULT 0,
"rank" "Rank" NOT NULL DEFAULT 'PLAYER',
"links" json,
"sentences" json
);

CREATE TABLE "Profile" (
"player_id" uuid NOT NULL,
"identifier" text NOT NULL,
"linkToProof" text,
"type" "Profile_Type" NOT NULL
);

CREATE TABLE "Quest" (
"id" uuid DEFAULT public.gen_random_uuid() NOT NULL,
"name" text NOT NULL,
"description" text,
"url" text NOT NULL,
"xp" numeric NOT NULL DEFAULT 0
);

CREATE TABLE "Quest_Completed" (
"quest_id" uuid NOT NULL,
"player_id" uuid NOT NULL,
"time" timestamp
);

CREATE TABLE "XPInterval" (
"player_id" uuid NOT NULL,
"startTime" date,
"endTime" date,
"xp" numeric NOT NULL
);

CREATE TABLE "Guild" (
"id" uuid DEFAULT public.gen_random_uuid() NOT NULL,
"name" text
);

CREATE TABLE "Guild_Member" (
"guild_id" uuid NOT NULL,
"player_id" uuid NOT NULL
);

-- Primary keys

ALTER TABLE ONLY public."Player"
ADD CONSTRAINT "Player_pkey" PRIMARY KEY (id);

ALTER TABLE ONLY public."Quest"
ADD CONSTRAINT "Quest_pkey" PRIMARY KEY (id);

ALTER TABLE ONLY public."Guild"
ADD CONSTRAINT "Guild_pkey" PRIMARY KEY (id);

ALTER TABLE ONLY public."Quest_Completed"
ADD CONSTRAINT "Quest_Completed_pkey" PRIMARY KEY (quest_id, player_id);

ALTER TABLE ONLY public."Guild_Member"
ADD CONSTRAINT "Guild_Member_pkey" PRIMARY KEY (guild_id, player_id);

-- Uniques

ALTER TABLE ONLY public."Profile"
ADD CONSTRAINT "Profile_identifier_key" UNIQUE (identifier);

-- Foreign keys

ALTER TABLE "Profile" ADD CONSTRAINT "Profile_player_id_fkey" FOREIGN KEY ("player_id") REFERENCES "Player" ("id");

ALTER TABLE "Quest_Completed" ADD CONSTRAINT "Quest_Completed_player_id_fkey" FOREIGN KEY ("player_id") REFERENCES "Player" ("id");
ALTER TABLE "Quest_Completed" ADD CONSTRAINT "Quest_Completed_quest_id_fkey" FOREIGN KEY ("quest_id") REFERENCES "Quest" ("id");

ALTER TABLE "Guild_Member" ADD CONSTRAINT "Guild_Member_player_id_fkey" FOREIGN KEY ("player_id") REFERENCES "Player" ("id");
ALTER TABLE "Guild_Member" ADD CONSTRAINT "Guild_Member_guild_id_fkey" FOREIGN KEY ("guild_id") REFERENCES "Guild" ("id");

ALTER TABLE "XPInterval" ADD CONSTRAINT "XPInterval_player_id_fkey" FOREIGN KEY ("player_id") REFERENCES "Player" ("id");
92 changes: 92 additions & 0 deletions hasura/migrations/1575910000000_init/up.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
- args:
tables:
- array_relationships:
- name: Guild_Members
using:
foreign_key_constraint_on:
column: guild_id
table:
name: Guild_Member
schema: public
table:
name: Guild
schema: public
- object_relationships:
- name: Guild
using:
foreign_key_constraint_on: guild_id
- name: Player
using:
foreign_key_constraint_on: player_id
table:
name: Guild_Member
schema: public
- array_relationships:
- name: Guild_Members
using:
foreign_key_constraint_on:
column: player_id
table:
name: Guild_Member
schema: public
- name: Profiles
using:
foreign_key_constraint_on:
column: player_id
table:
name: Profile
schema: public
- name: Quest_Completeds
using:
foreign_key_constraint_on:
column: player_id
table:
name: Quest_Completed
schema: public
- name: XPIntervals
using:
foreign_key_constraint_on:
column: player_id
table:
name: XPInterval
schema: public
table:
name: Player
schema: public
- object_relationships:
- name: Player
using:
foreign_key_constraint_on: player_id
table:
name: Profile
schema: public
- array_relationships:
- name: Quest_Completeds
using:
foreign_key_constraint_on:
column: quest_id
table:
name: Quest_Completed
schema: public
table:
name: Quest
schema: public
- object_relationships:
- name: Player
using:
foreign_key_constraint_on: player_id
- name: Quest
using:
foreign_key_constraint_on: quest_id
table:
name: Quest_Completed
schema: public
- object_relationships:
- name: Player
using:
foreign_key_constraint_on: player_id
table:
name: XPInterval
schema: public
version: 2
type: replace_metadata
42 changes: 42 additions & 0 deletions hasura/migrations/1586952306059_role_public/down.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
- args:
role: public
table:
name: XPInterval
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Quest_Completed
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Quest
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Profile
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Guild_Member
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Guild
schema: public
type: drop_select_permission
- args:
role: public
table:
name: Player
schema: public
type: drop_select_permission
Loading

0 comments on commit c684b9d

Please sign in to comment.