Compare commits
17 Commits
refactor-p
...
99b4d9af1a
| Author | SHA1 | Date | |
|---|---|---|---|
| 99b4d9af1a | |||
| 462ca81a15 | |||
| c2ecce0324 | |||
| 8d2460c60e | |||
| 528ce0335c | |||
| 993a0834b4 | |||
| d29da66536 | |||
| ffe6a771d2 | |||
| d3208303ba | |||
| f71d2ae70a | |||
| 0fd6ef8817 | |||
| 8ad6967b7a | |||
| 528682d9b1 | |||
| 5d28013f30 | |||
| 5cb67786bf | |||
|
|
01eaf059dd | ||
| cb5da9b966 |
4
.dev/.env
Normal file
4
.dev/.env
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
POSTGRES_DB=elo_app
|
||||||
|
POSTGRES_USER=elo
|
||||||
|
POSTGRES_PASSWORD=elo
|
||||||
|
DATABASE_URL=postgres://elo:elo@db:5432/elo_app
|
||||||
18
.dev/docker-compose.yml
Normal file
18
.dev/docker-compose.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
services:
|
||||||
|
db:
|
||||||
|
image: postgres:15.3-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
networks:
|
||||||
|
- db
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
db:
|
||||||
67
.github/workflows/github-actions-demo.yml
vendored
67
.github/workflows/github-actions-demo.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
# TODO -> Change to actions/checkout@v3 once this is resolved https://github.com/actions/checkout/issues/1370
|
# TODO -> Change to actions/checkout@v3 once this is resolved https://github.com/actions/checkout/issues/1370
|
||||||
uses: https://gitea.com/ScMi1/checkout@v1
|
uses: https://gitea.com/ScMi1/checkout@v1
|
||||||
- name: Get rust
|
- name: Get rust
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
@@ -25,7 +25,7 @@ jobs:
|
|||||||
- name: Add wasm32 to cargo
|
- name: Add wasm32 to cargo
|
||||||
run: rustup target add wasm32-unknown-unknown
|
run: rustup target add wasm32-unknown-unknown
|
||||||
- name: Cache rust
|
- name: Cache rust
|
||||||
uses: Swatinem/rust-cache@v2
|
uses: Swatinem/rust-cache@v2
|
||||||
with:
|
with:
|
||||||
cache-on-failure: "true"
|
cache-on-failure: "true"
|
||||||
- name: Install perseus
|
- name: Install perseus
|
||||||
@@ -42,57 +42,14 @@ jobs:
|
|||||||
run: npm install -D tailwindcss
|
run: npm install -D tailwindcss
|
||||||
- name: Compile css
|
- name: Compile css
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
# TODO -> Remove wasm-opt-version once perseus is updated
|
||||||
- name: Build the project
|
- name: Build the project
|
||||||
run: perseus deploy
|
run: perseus --wasm-opt-version version_118 deploy --verbose
|
||||||
- name: Download private docker registry cert
|
- name: Run clippy for server
|
||||||
uses: nicklasfrahm/scp-action@v1.0.1
|
run: RUSTFLAGS="--cfg=engine" cargo clippy --all --future-incompat-report -- -D warnings
|
||||||
with:
|
continue-on-error: true
|
||||||
direction: download
|
- name: Run clippy for frontend
|
||||||
host: ${{ secrets.REGISTRY_HOST }}
|
run: RUSTFLAGS="--cfg=client" cargo clippy --all --future-incompat-report -- -D warnings
|
||||||
username: ${{ secrets.REGISTRY_HOST_USERNAME }}
|
continue-on-error: true
|
||||||
insecure_password: ${{ secrets.REGISTRY_HOST_PASSWORD }}
|
- name: Check for formatting issues
|
||||||
source: ${{ secrets.REGISTRY_CRT_PATH }}
|
run: cargo fmt --check
|
||||||
target: ca.crt
|
|
||||||
insecure_ignore_fingerprint: true
|
|
||||||
|
|
||||||
- name: Add directory for docker registry cert
|
|
||||||
run: 'mkdir -p /etc/docker/certs.d/${{ secrets.REGISTRY_HOST }}'
|
|
||||||
|
|
||||||
- name: Move private docker registry cert
|
|
||||||
run: 'mv ca.crt /etc/docker/certs.d/${{ secrets.REGISTRY_HOST }}'
|
|
||||||
|
|
||||||
- name: Install docker
|
|
||||||
uses: papodaca/install-docker-action@main
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v2
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v2
|
|
||||||
with:
|
|
||||||
config-inline: |
|
|
||||||
[registry."${{ secrets.REGISTRY_HOST }}"]
|
|
||||||
insecure = true
|
|
||||||
ca=["/etc/docker/certs.d/${{ secrets.REGISTRY_HOST }}/ca.crt"]
|
|
||||||
|
|
||||||
- name: Login to private docker registry
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
|
||||||
registry: ${{ secrets.REGISTRY_HOST }}
|
|
||||||
username: ${{ secrets.REGISTRY_USERNAME }}
|
|
||||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v4
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ secrets.REGISTRY_HOST }}/vsquad/pool-elo:${{ gitea.ref_name }}
|
|
||||||
|
|
||||||
- name: Trigger deployment
|
|
||||||
uses: fjogeleit/http-request-action@v1
|
|
||||||
with:
|
|
||||||
url: ${{ secrets.TRIGGER_DEPLOY_URL }}
|
|
||||||
method: POST
|
|
||||||
customHeaders: '{"Content-Type": "application/json"}'
|
|
||||||
data: '{"actionName": "deploy", "arguments": []}'
|
|
||||||
preventFailureOnNoResponse: 'true'
|
|
||||||
|
|||||||
17
Cargo.toml
17
Cargo.toml
@@ -18,7 +18,9 @@ log = "0.4.20"
|
|||||||
once_cell = "1.18.0"
|
once_cell = "1.18.0"
|
||||||
web-sys = "0.3.64"
|
web-sys = "0.3.64"
|
||||||
cfg-if = "1.0.0"
|
cfg-if = "1.0.0"
|
||||||
chrono = { version = "0.4.31", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde", "wasm-bindgen"] }
|
||||||
|
password-auth = "1.0.0"
|
||||||
|
lazy_static = "1.5"
|
||||||
|
|
||||||
[target.'cfg(engine)'.dev-dependencies]
|
[target.'cfg(engine)'.dev-dependencies]
|
||||||
fantoccini = "0.19"
|
fantoccini = "0.19"
|
||||||
@@ -28,7 +30,18 @@ tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }
|
|||||||
perseus-axum = { version = "0.4.2" }
|
perseus-axum = { version = "0.4.2" }
|
||||||
axum = "0.6"
|
axum = "0.6"
|
||||||
tower-http = { version = "0.3", features = ["fs"] }
|
tower-http = { version = "0.3", features = ["fs"] }
|
||||||
|
futures = "0.3.28"
|
||||||
|
sea-orm = { version = "1.0", features = [
|
||||||
|
"sqlx-postgres",
|
||||||
|
"runtime-tokio-native-tls",
|
||||||
|
"macros",
|
||||||
|
"with-chrono",
|
||||||
|
] }
|
||||||
|
|
||||||
[target.'cfg(client)'.dependencies]
|
[target.'cfg(client)'.dependencies]
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2.93"
|
||||||
reqwest = { version = "0.11", features = ["json"] }
|
reqwest = { version = "0.11", features = ["json"] }
|
||||||
|
sea-orm = { version = "1.0" }
|
||||||
|
|
||||||
|
[lints.rust]
|
||||||
|
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(engine)', 'cfg(client)'] }
|
||||||
|
|||||||
27
README.md
27
README.md
@@ -1,7 +1,7 @@
|
|||||||
# Installing requirements
|
# Installing requirements
|
||||||
|
|
||||||
## 1. Install rust:
|
## 1. Install rust:
|
||||||
### Windows:
|
### Windows:
|
||||||
|
|
||||||
Download installer from https://www.rust-lang.org/tools/install
|
Download installer from https://www.rust-lang.org/tools/install
|
||||||
|
|
||||||
@@ -11,9 +11,11 @@ Run `curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh`
|
|||||||
|
|
||||||
## 2. Install npm
|
## 2. Install npm
|
||||||
|
|
||||||
### Windows:
|
### Windows:
|
||||||
|
|
||||||
https://nodejs.org/en
|
https://nodejs.org/en
|
||||||
|
(todo look into:)
|
||||||
|
https://pnpm.io/
|
||||||
|
|
||||||
### Unix based systems:
|
### Unix based systems:
|
||||||
|
|
||||||
@@ -23,13 +25,22 @@ https://nodejs.org/en
|
|||||||
## 3. Install Perseus, for real-time updates while developing
|
## 3. Install Perseus, for real-time updates while developing
|
||||||
|
|
||||||
`cargo install perseus-cli`
|
`cargo install perseus-cli`
|
||||||
|
|
||||||
|
(temporarily broken, if this doensn't work run `cargo install perseus-cli --locked` )
|
||||||
|
|
||||||
`rustup target add wasm32-unknown-unknown`
|
`rustup target add wasm32-unknown-unknown`
|
||||||
|
|
||||||
## 4. Install tailwindcss, for styling
|
## 4. Install docker for Postgresql
|
||||||
|
|
||||||
|
## 5. Install SeaORM for database
|
||||||
|
|
||||||
|
`cargo install sea-orm-cli@1.0.0-rc.5`
|
||||||
|
|
||||||
|
## 5. Install tailwindcss, for styling
|
||||||
|
|
||||||
`npm install -D tailwindcss`
|
`npm install -D tailwindcss`
|
||||||
|
|
||||||
Also take a look at
|
Also take a look at
|
||||||
|
|
||||||
Website:
|
Website:
|
||||||
https://framesurge.sh/perseus/en-US/
|
https://framesurge.sh/perseus/en-US/
|
||||||
@@ -39,11 +50,19 @@ https://blog.logrocket.com/building-rust-app-perseus/
|
|||||||
|
|
||||||
# Building the project
|
# Building the project
|
||||||
|
|
||||||
|
To set up the database, run:
|
||||||
|
`$env:DATABASE_URL = "postgres://elo:elo@localhost:5432/elo_app"; sea-orm-cli migrate up`
|
||||||
|
|
||||||
|
Updating entities after updating database:
|
||||||
|
`$env:DATABASE_URL = "postgres://elo:elo@localhost:5432/elo_app"; sea-orm-cli generate entity -o src/entity --with-serde both`
|
||||||
|
|
||||||
To build CSS run:
|
To build CSS run:
|
||||||
`npm run build`
|
`npm run build`
|
||||||
|
|
||||||
To build the project for testing, run
|
To build the project for testing, run
|
||||||
`perseus serve --verbose`
|
`perseus serve --verbose`
|
||||||
|
(if broken: todo remove once fixed)
|
||||||
|
`perseus --wasm-opt-version version_118 serve --verbose`
|
||||||
|
|
||||||
# Deploying the project
|
# Deploying the project
|
||||||
|
|
||||||
|
|||||||
16
migration/Cargo.toml
Normal file
16
migration/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "migration"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "migration"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
async-std = { version = "1", features = ["attributes", "tokio1"] }
|
||||||
|
|
||||||
|
[dependencies.sea-orm-migration]
|
||||||
|
version = "1.0.0"
|
||||||
|
features = ["sqlx-postgres", "runtime-tokio-native-tls"]
|
||||||
46
migration/README.md
Normal file
46
migration/README.md
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
# References
|
||||||
|
|
||||||
|
https://www.sea-ql.org/sea-orm-tutorial/ch01-02-migration-cli.html
|
||||||
|
https://www.sea-ql.org/SeaORM/docs/migration/writing-migration/
|
||||||
|
|
||||||
|
# Running Migrator CLI
|
||||||
|
|
||||||
|
- Generate a new migration file
|
||||||
|
```sh
|
||||||
|
cargo run -- generate MIGRATION_NAME
|
||||||
|
```
|
||||||
|
- Apply all pending migrations
|
||||||
|
```sh
|
||||||
|
cargo run
|
||||||
|
```
|
||||||
|
```sh
|
||||||
|
cargo run -- up
|
||||||
|
```
|
||||||
|
- Apply first 10 pending migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- up -n 10
|
||||||
|
```
|
||||||
|
- Rollback last applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- down
|
||||||
|
```
|
||||||
|
- Rollback last 10 applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- down -n 10
|
||||||
|
```
|
||||||
|
- Drop all tables from the database, then reapply all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- fresh
|
||||||
|
```
|
||||||
|
- Rollback all applied migrations, then reapply all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- refresh
|
||||||
|
```
|
||||||
|
- Rollback all applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- reset
|
||||||
|
```
|
||||||
|
- Check the status of all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- status
|
||||||
|
```
|
||||||
16
migration/src/lib.rs
Normal file
16
migration/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
pub use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
mod m20240813_000001_create_users;
|
||||||
|
mod m20240813_000002_create_game;
|
||||||
|
|
||||||
|
pub struct Migrator;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigratorTrait for Migrator {
|
||||||
|
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||||
|
vec![
|
||||||
|
Box::new(m20240813_000001_create_users::Migration),
|
||||||
|
Box::new(m20240813_000002_create_game::Migration),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
49
migration/src/m20240813_000001_create_users.rs
Normal file
49
migration/src/m20240813_000001_create_users.rs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
use sea_orm_migration::{prelude::*, schema::*};
|
||||||
|
|
||||||
|
#[derive(DeriveMigrationName)]
|
||||||
|
pub struct Migration;
|
||||||
|
|
||||||
|
// https://github.com/SeaQL/sea-orm/blob/368b1126f73f47c7ec30fe523834f6a0962a193b/sea-orm-migration/src/schema.rs
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigrationTrait for Migration {
|
||||||
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
// User table
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(User::Table)
|
||||||
|
.col(pk_auto(User::Id))
|
||||||
|
.col(string(User::Username))
|
||||||
|
.col(string(User::Password))
|
||||||
|
.col(string(User::Salt))
|
||||||
|
.col(timestamp_with_time_zone(User::CreationTime))
|
||||||
|
.col(timestamp_with_time_zone(User::LastActiveTime))
|
||||||
|
.col(boolean(User::IsAdmin))
|
||||||
|
.col(string_null(User::Email))
|
||||||
|
.col(string_null(User::Avatar))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
pub enum User {
|
||||||
|
Table,
|
||||||
|
Id,
|
||||||
|
Username,
|
||||||
|
Password,
|
||||||
|
Salt,
|
||||||
|
CreationTime,
|
||||||
|
LastActiveTime,
|
||||||
|
IsAdmin,
|
||||||
|
Email,
|
||||||
|
Avatar,
|
||||||
|
}
|
||||||
163
migration/src/m20240813_000002_create_game.rs
Normal file
163
migration/src/m20240813_000002_create_game.rs
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
use sea_orm::{DbBackend, DeriveActiveEnum, EnumIter, Schema};
|
||||||
|
use sea_orm_migration::sea_orm::ActiveEnum;
|
||||||
|
use sea_orm_migration::{prelude::*, schema::*, sea_query::extension::postgres::Type};
|
||||||
|
|
||||||
|
use super::m20240813_000001_create_users::User;
|
||||||
|
|
||||||
|
#[derive(DeriveMigrationName)]
|
||||||
|
pub struct Migration;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigrationTrait for Migration {
|
||||||
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
let schema = Schema::new(DbBackend::Postgres);
|
||||||
|
|
||||||
|
// Game type enum
|
||||||
|
manager
|
||||||
|
.create_type(schema.create_enum_from_active_enum::<GameType>())
|
||||||
|
.await?;
|
||||||
|
// Game table
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(Game::Table)
|
||||||
|
.col(pk_auto(Game::Id))
|
||||||
|
.col(timestamp_with_time_zone(Game::Time))
|
||||||
|
.col(ColumnDef::new(Game::GameType).custom(GameType::name()))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
// TeamResult table
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(TeamResult::Table)
|
||||||
|
.col(pk_auto(TeamResult::Id))
|
||||||
|
.col(integer(TeamResult::Place))
|
||||||
|
.col(integer_null(TeamResult::Score))
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
// Game to TeamResult assoc
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(GameToTeamResult::Table)
|
||||||
|
.col(integer(GameToTeamResult::GameId))
|
||||||
|
.col(integer(GameToTeamResult::TeamResultId))
|
||||||
|
.primary_key(
|
||||||
|
Index::create()
|
||||||
|
.name("pk-game_to_team_result")
|
||||||
|
.col(GameToTeamResult::GameId)
|
||||||
|
.col(GameToTeamResult::TeamResultId),
|
||||||
|
)
|
||||||
|
.foreign_key(
|
||||||
|
ForeignKey::create()
|
||||||
|
.name("fk-game_to_team_result-game_id")
|
||||||
|
.from(GameToTeamResult::Table, GameToTeamResult::GameId)
|
||||||
|
.to(Game::Table, Game::Id),
|
||||||
|
)
|
||||||
|
.foreign_key(
|
||||||
|
ForeignKey::create()
|
||||||
|
.name("fk-game_to_team_result-team_result_id")
|
||||||
|
.from(GameToTeamResult::Table, GameToTeamResult::TeamResultId)
|
||||||
|
.to(TeamResult::Table, TeamResult::Id),
|
||||||
|
)
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
// TeamResult to User assoc
|
||||||
|
manager
|
||||||
|
.create_table(
|
||||||
|
Table::create()
|
||||||
|
.table(TeamResultToUser::Table)
|
||||||
|
.col(integer(TeamResultToUser::TeamResultId))
|
||||||
|
.col(integer(TeamResultToUser::UserId))
|
||||||
|
.primary_key(
|
||||||
|
Index::create()
|
||||||
|
.name("pk-team_result_to_user")
|
||||||
|
.col(TeamResultToUser::TeamResultId)
|
||||||
|
.col(TeamResultToUser::UserId),
|
||||||
|
)
|
||||||
|
.foreign_key(
|
||||||
|
ForeignKey::create()
|
||||||
|
.name("fk-team_result_to_user-team_result_id")
|
||||||
|
.from(TeamResultToUser::Table, TeamResultToUser::TeamResultId)
|
||||||
|
.to(TeamResult::Table, TeamResult::Id),
|
||||||
|
)
|
||||||
|
.foreign_key(
|
||||||
|
ForeignKey::create()
|
||||||
|
.name("fk-team_result_to_user-user_id")
|
||||||
|
.from(TeamResultToUser::Table, TeamResultToUser::UserId)
|
||||||
|
.to(User::Table, User::Id),
|
||||||
|
)
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
// Drop tables in reverse
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(TeamResultToUser::Table).to_owned())
|
||||||
|
.await?;
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(GameToTeamResult::Table).to_owned())
|
||||||
|
.await?;
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(TeamResult::Table).to_owned())
|
||||||
|
.await?;
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(Game::Table).to_owned())
|
||||||
|
.await?;
|
||||||
|
// Drop types
|
||||||
|
manager
|
||||||
|
.drop_type(Type::drop().name(GameType::name()).to_owned())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enums
|
||||||
|
|
||||||
|
#[derive(EnumIter, DeriveActiveEnum)]
|
||||||
|
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "game_type")]
|
||||||
|
enum GameType {
|
||||||
|
#[sea_orm(string_value = "TableTennis")]
|
||||||
|
TableTennis,
|
||||||
|
#[sea_orm(string_value = "Pool")]
|
||||||
|
Pool,
|
||||||
|
#[sea_orm(string_value = "PickleBall")]
|
||||||
|
PickleBall,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tables
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum Game {
|
||||||
|
Table,
|
||||||
|
Id,
|
||||||
|
Time,
|
||||||
|
GameType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum TeamResult {
|
||||||
|
Table,
|
||||||
|
Id,
|
||||||
|
Place,
|
||||||
|
Score,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assoc
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum GameToTeamResult {
|
||||||
|
Table,
|
||||||
|
GameId,
|
||||||
|
TeamResultId,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum TeamResultToUser {
|
||||||
|
Table,
|
||||||
|
TeamResultId,
|
||||||
|
UserId,
|
||||||
|
}
|
||||||
6
migration/src/main.rs
Normal file
6
migration/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() {
|
||||||
|
cli::run_cli(migration::Migrator).await;
|
||||||
|
}
|
||||||
@@ -11,6 +11,6 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"tailwindcss": "^3.3.3"
|
"tailwindcss": "^3.4.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/auth.rs
Normal file
19
src/auth.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use crate::user;
|
||||||
|
use axum_login::{AuthUser, AuthnBackend, UserId};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
// References
|
||||||
|
// https://github.com/maxcountryman/axum-login/tree/main/examples/sqlite/src
|
||||||
|
// https://framesurge.sh/perseus/en-US/docs/0.4.x/state/intro
|
||||||
|
|
||||||
|
impl AuthUser for user::Model {
|
||||||
|
type Id = i32;
|
||||||
|
|
||||||
|
fn id(&self) -> Self::Id {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session_auth_hash(&self) -> &[u8] {
|
||||||
|
self.password.as_bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
98
src/capsules/login_form.rs
Normal file
98
src/capsules/login_form.rs
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
|
use perseus::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sycamore::prelude::*;
|
||||||
|
use web_sys::Event;
|
||||||
|
|
||||||
|
use crate::{state_enums::OpenState, templates::global_state::AppStateRx};
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref LOGIN_FORM: Capsule<PerseusNodeType, LoginFormProps> = get_capsule();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||||
|
#[rx(alias = "LoginFormStateRx")]
|
||||||
|
struct LoginFormState {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct LoginFormProps {
|
||||||
|
pub remember_me: bool,
|
||||||
|
pub endpoint: String,
|
||||||
|
pub lost_password_url: Option<String>,
|
||||||
|
pub forgot_password_url: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[auto_scope]
|
||||||
|
fn login_form_capsule<G: Html>(
|
||||||
|
cx: Scope,
|
||||||
|
state: &LoginFormStateRx,
|
||||||
|
props: LoginFormProps,
|
||||||
|
) -> View<G> {
|
||||||
|
let close_modal = move |_event: Event| {
|
||||||
|
#[cfg(client)]
|
||||||
|
{
|
||||||
|
spawn_local_scoped(cx, async move {
|
||||||
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
global_state.modals_open.login.set(OpenState::Closed)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
view! { cx,
|
||||||
|
div (class="overflow-x-hidden overflow-y-auto fixed h-modal md:h-full top-4 left-0 right-0 md:inset-0 z-50 justify-center items-center"){
|
||||||
|
div (class="relative md:mx-auto w-full md:w-1/2 lg:w-1/3 z-0 my-10") {
|
||||||
|
div (class="bg-white rounded-lg shadow relative dark:bg-gray-700"){
|
||||||
|
div (class="flex justify-end p-2"){
|
||||||
|
button (on:click = close_modal, class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-800 dark:hover:text-white"){
|
||||||
|
"Back"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
form (class="space-y-6 px-6 lg:px-8 pb-4 sm:pb-6 xl:pb-8") {
|
||||||
|
h3 (class="text-xl font-medium text-gray-900 dark:text-white"){"Sign in to our platform"}
|
||||||
|
div {
|
||||||
|
label (class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300") {"Your email"}
|
||||||
|
input (class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white") {}
|
||||||
|
}
|
||||||
|
div {
|
||||||
|
label (class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300"){"Your password"}
|
||||||
|
input (class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"){}
|
||||||
|
}
|
||||||
|
div (class="flex justify-between"){
|
||||||
|
div (class="flex items-start"){
|
||||||
|
div (class="flex items-center h-5"){
|
||||||
|
input (class="bg-gray-50 border border-gray-300 focus:ring-3 focus:ring-blue-300 h-4 w-4 rounded dark:bg-gray-600 dark:border-gray-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800") {}
|
||||||
|
}
|
||||||
|
div (class="text-sm ml-3"){
|
||||||
|
label (class="font-medium text-gray-900 dark:text-gray-300"){"Remember me"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a (class="text-sm text-blue-700 hover:underline dark:text-blue-500"){"Lost Password?"}
|
||||||
|
}
|
||||||
|
button (class="w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"){"Login to your account"}
|
||||||
|
div (class="text-sm font-medium text-gray-500 dark:text-gray-300"){
|
||||||
|
a (class="text-blue-700 hover:underline dark:text-blue-500"){"Create account"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_capsule<G: Html>() -> Capsule<G, LoginFormProps> {
|
||||||
|
Capsule::build(Template::build("login_form").build_state_fn(get_build_state))
|
||||||
|
.empty_fallback()
|
||||||
|
.view_with_state(login_form_capsule)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
async fn get_build_state(_info: StateGeneratorInfo<()>) -> LoginFormState {
|
||||||
|
LoginFormState {
|
||||||
|
username: "".to_string(),
|
||||||
|
password: "".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/capsules/mod.rs
Normal file
1
src/capsules/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod login_form;
|
||||||
97
src/components/header.rs
Normal file
97
src/components/header.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use perseus::prelude::*;
|
||||||
|
use sycamore::prelude::*;
|
||||||
|
use web_sys::Event;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
capsules::login_form::{LoginFormProps, LOGIN_FORM},
|
||||||
|
state_enums::{GameState, LoginState, OpenState},
|
||||||
|
templates::global_state::AppStateRx,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Prop)]
|
||||||
|
pub struct HeaderProps<'a> {
|
||||||
|
pub game: GameState,
|
||||||
|
pub title: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn Header<'a, G: Html>(cx: Scope<'a>, HeaderProps { game, title }: HeaderProps<'a>) -> View<G> {
|
||||||
|
// Get global state to get authentication info
|
||||||
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
|
||||||
|
let handle_log_in = move |_event: Event| {
|
||||||
|
#[cfg(client)]
|
||||||
|
{
|
||||||
|
spawn_local_scoped(cx, async move {
|
||||||
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
global_state.modals_open.login.set(OpenState::Open);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
view! { cx,
|
||||||
|
header {
|
||||||
|
div (class = "flex items-center justify-between w-full md:text-center h-20") {
|
||||||
|
div(class = "flex-1") {}
|
||||||
|
|
||||||
|
// Title
|
||||||
|
div(class = "text-gray-700 text-2xl font-semibold py-2") {
|
||||||
|
"Pool Elo - Season 1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login / register or user buttons
|
||||||
|
div(class = "flex-1 py-2") {(
|
||||||
|
match *global_state.auth.state.get() {
|
||||||
|
LoginState::NotAuthenticated => {
|
||||||
|
view! { cx,
|
||||||
|
button(class = "text-gray-900 bg-white border border-gray-300 focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700") {
|
||||||
|
"Register"
|
||||||
|
}
|
||||||
|
button(on:click = handle_log_in,class = "text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800") {
|
||||||
|
"Login"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LoginState::Authenticated => {
|
||||||
|
view! { cx,
|
||||||
|
div {
|
||||||
|
"Hello {username}!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Will only appear for a few seconds
|
||||||
|
LoginState::Unknown => {
|
||||||
|
view! { cx,
|
||||||
|
div (class = "px-5 py-2.5 me-2 mb-2"){
|
||||||
|
"Loading..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
section(class = "flex-2") {
|
||||||
|
(match *global_state.modals_open.login.get() {
|
||||||
|
OpenState::Open => {
|
||||||
|
view! { cx,
|
||||||
|
(LOGIN_FORM.widget(cx, "",
|
||||||
|
LoginFormProps{
|
||||||
|
remember_me: true,
|
||||||
|
endpoint: "".to_string(),
|
||||||
|
lost_password_url: Some("".to_string()),
|
||||||
|
forgot_password_url: Some("".to_string()),
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OpenState::Closed => {
|
||||||
|
view!{ cx, }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,44 +1,65 @@
|
|||||||
|
use crate::{
|
||||||
|
capsules::login_form::{LoginFormProps, LOGIN_FORM},
|
||||||
|
components::header::{Header, HeaderProps},
|
||||||
|
state_enums::{GameState, LoginState},
|
||||||
|
templates::global_state::AppStateRx,
|
||||||
|
};
|
||||||
|
use perseus::prelude::*;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
use web_sys::Event;
|
||||||
|
|
||||||
#[derive(Prop)]
|
#[derive(Prop)]
|
||||||
pub struct LayoutProps<'a, G: Html> {
|
pub struct LayoutProps<'a, G: Html> {
|
||||||
|
pub game: GameState,
|
||||||
pub title: &'a str,
|
pub title: &'a str,
|
||||||
pub children: Children<'a, G>,
|
pub children: Children<'a, G>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Using elements from here: https://flowbite.com/docs/components/buttons/
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Layout<'a, G: Html>(
|
pub fn Layout<'a, G: Html>(
|
||||||
cx: Scope<'a>,
|
cx: Scope<'a>,
|
||||||
LayoutProps { title: _, children }: LayoutProps<'a, G>,
|
LayoutProps {
|
||||||
|
game,
|
||||||
|
title,
|
||||||
|
children,
|
||||||
|
}: LayoutProps<'a, G>,
|
||||||
) -> View<G> {
|
) -> View<G> {
|
||||||
let children = children.call(cx);
|
let children = children.call(cx);
|
||||||
|
|
||||||
view! { cx,
|
// Get global state to get authentication info
|
||||||
header {
|
#[cfg(client)]
|
||||||
div (class = "flex items-center justify-between") {
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
div (class = "w-full text-gray-700 md:text-center text-2xl font-semibold") {
|
|
||||||
"Pool Elo - Season 1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
div (class = "container mx-auto px-6 py-3") {
|
// Check if the client is authenticated or not
|
||||||
nav (class = "sm:flex sm:justify-center sm:items-center mt-4 hidden") {
|
#[cfg(client)]
|
||||||
div (class = "flex flex-col sm:flex-row"){
|
global_state.auth.detect_state();
|
||||||
a(href = "add-game-form",
|
|
||||||
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
view! { cx,
|
||||||
) { "Add game result" }
|
// Main page header, including login functionality
|
||||||
a(href = "one-v-one-board",
|
Header(game = game, title = title)
|
||||||
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
|
||||||
) { "1v1 Leaderboard" }
|
main(style = "my-8") {
|
||||||
a(href = "overall-board",
|
// Body header
|
||||||
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
div {
|
||||||
) { "Overall Leaderboard" }
|
div (class = "container mx-auto px-6 py-3") {
|
||||||
|
nav (class = "sm:flex sm:justify-center sm:items-center mt-4 hidden") {
|
||||||
|
div (class = "flex flex-col sm:flex-row"){
|
||||||
|
a(href = "add-game-form",
|
||||||
|
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
||||||
|
) { "Add game result" }
|
||||||
|
a(href = "one-v-one-board",
|
||||||
|
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
||||||
|
) { "1v1 Leaderboard" }
|
||||||
|
a(href = "overall-board",
|
||||||
|
class = "mt-3 text-gray-600 hover:underline sm:mx-3 sm:mt-0"
|
||||||
|
) { "Overall Leaderboard" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// Actual body
|
||||||
|
|
||||||
main(style = "my-8") {
|
|
||||||
div(class = "container mx-auto px-6") {
|
div(class = "container mx-auto px-6") {
|
||||||
div(class = "md:flex mt-8 md:-mx-4") {
|
div(class = "md:flex mt-8 md:-mx-4") {
|
||||||
div(class = "rounded-md overflow-hidden bg-cover bg-center") {
|
div(class = "rounded-md overflow-hidden bg-cover bg-center") {
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
|
mod header;
|
||||||
pub mod layout;
|
pub mod layout;
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
pub mod pool_match;
|
|
||||||
pub mod user;
|
|
||||||
|
|
||||||
#[cfg(engine)]
|
|
||||||
pub mod store;
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
use crate::data::user::PlayerId;
|
|
||||||
use chrono::serde::ts_seconds;
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
pub type MatchId = u32;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub enum MatchType {
|
|
||||||
Standard8Ball,
|
|
||||||
Standard9Ball,
|
|
||||||
CutThroat,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct MatchData {
|
|
||||||
pub type_: MatchType,
|
|
||||||
pub winners: Vec<PlayerId>,
|
|
||||||
pub losers: Vec<PlayerId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct PoolMatch {
|
|
||||||
pub id: MatchId,
|
|
||||||
pub data: MatchData,
|
|
||||||
#[serde(with = "ts_seconds")]
|
|
||||||
pub time: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct PoolMatchList {
|
|
||||||
pub pool_matches: Vec<PoolMatch>,
|
|
||||||
pub max_id: MatchId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PoolMatch {
|
|
||||||
pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch {
|
|
||||||
PoolMatch { id: 0, data, time }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PoolMatchList {
|
|
||||||
pub fn new() -> PoolMatchList {
|
|
||||||
PoolMatchList {
|
|
||||||
pool_matches: vec![],
|
|
||||||
max_id: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_pool_match(&mut self, mut pool_match: PoolMatch) {
|
|
||||||
pool_match.id = self.max_id + 1;
|
|
||||||
self.max_id += 1;
|
|
||||||
self.pool_matches.push(pool_match);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
// (Server only) In-memory data storage and persistent storage
|
|
||||||
|
|
||||||
use crate::data::pool_match::PoolMatchList;
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{fs, path::Path, sync::Mutex};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
|
||||||
pub struct Store {
|
|
||||||
pub matches: PoolMatchList,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Store {
|
|
||||||
fn new() -> Store {
|
|
||||||
fs::create_dir_all("data").unwrap();
|
|
||||||
match Path::new("data/store.json").exists() {
|
|
||||||
false => Store {
|
|
||||||
matches: PoolMatchList::new(),
|
|
||||||
},
|
|
||||||
true => {
|
|
||||||
let contents = fs::read_to_string("data/store.json").unwrap();
|
|
||||||
serde_json::from_str(&contents).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO -> Store data
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn write(&self) {
|
|
||||||
let contents = serde_json::to_string(&self).unwrap();
|
|
||||||
fs::write("data/store.json", contents).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub static DATA: Lazy<Mutex<Store>> = Lazy::new(|| Mutex::new(Store::new()));
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
|
|
||||||
pub type PlayerId = u32;
|
|
||||||
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
pub const MATCH: &str = "/api/post-match";
|
pub const MATCH: &str = "/api/post-match";
|
||||||
|
pub const USER: &str = "/api/post-user";
|
||||||
|
|||||||
37
src/entity/game.rs
Normal file
37
src/entity/game.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use super::sea_orm_active_enums::GameType;
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "game")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
pub time: DateTimeWithTimeZone,
|
||||||
|
pub game_type: Option<GameType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::game_to_team_result::Entity")]
|
||||||
|
GameToTeamResult,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::game_to_team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::GameToTeamResult.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::game_to_team_result::Relation::TeamResult.def()
|
||||||
|
}
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::game_to_team_result::Relation::Game.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
47
src/entity/game_to_team_result.rs
Normal file
47
src/entity/game_to_team_result.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "game_to_team_result")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub game_id: i32,
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub team_result_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::game::Entity",
|
||||||
|
from = "Column::GameId",
|
||||||
|
to = "super::game::Column::Id",
|
||||||
|
on_update = "NoAction",
|
||||||
|
on_delete = "NoAction"
|
||||||
|
)]
|
||||||
|
Game,
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::team_result::Entity",
|
||||||
|
from = "Column::TeamResultId",
|
||||||
|
to = "super::team_result::Column::Id",
|
||||||
|
on_update = "NoAction",
|
||||||
|
on_delete = "NoAction"
|
||||||
|
)]
|
||||||
|
TeamResult,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::game::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Game.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::TeamResult.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
10
src/entity/mod.rs
Normal file
10
src/entity/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
pub mod prelude;
|
||||||
|
|
||||||
|
pub mod game;
|
||||||
|
pub mod game_to_team_result;
|
||||||
|
pub mod sea_orm_active_enums;
|
||||||
|
pub mod team_result;
|
||||||
|
pub mod team_result_to_user;
|
||||||
|
pub mod user;
|
||||||
7
src/entity/prelude.rs
Normal file
7
src/entity/prelude.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
pub use super::game::Entity as Game;
|
||||||
|
pub use super::game_to_team_result::Entity as GameToTeamResult;
|
||||||
|
pub use super::team_result::Entity as TeamResult;
|
||||||
|
pub use super::team_result_to_user::Entity as TeamResultToUser;
|
||||||
|
pub use super::user::Entity as User;
|
||||||
15
src/entity/sea_orm_active_enums.rs
Normal file
15
src/entity/sea_orm_active_enums.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "game_type")]
|
||||||
|
pub enum GameType {
|
||||||
|
#[sea_orm(string_value = "PickleBall")]
|
||||||
|
PickleBall,
|
||||||
|
#[sea_orm(string_value = "Pool")]
|
||||||
|
Pool,
|
||||||
|
#[sea_orm(string_value = "TableTennis")]
|
||||||
|
TableTennis,
|
||||||
|
}
|
||||||
53
src/entity/team_result.rs
Normal file
53
src/entity/team_result.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "team_result")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
pub place: i32,
|
||||||
|
pub score: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::game_to_team_result::Entity")]
|
||||||
|
GameToTeamResult,
|
||||||
|
#[sea_orm(has_many = "super::team_result_to_user::Entity")]
|
||||||
|
TeamResultToUser,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::game_to_team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::GameToTeamResult.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result_to_user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::TeamResultToUser.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::game::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::game_to_team_result::Relation::Game.def()
|
||||||
|
}
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::game_to_team_result::Relation::TeamResult.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::team_result_to_user::Relation::User.def()
|
||||||
|
}
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::team_result_to_user::Relation::TeamResult.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
47
src/entity/team_result_to_user.rs
Normal file
47
src/entity/team_result_to_user.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "team_result_to_user")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub team_result_id: i32,
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub user_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::team_result::Entity",
|
||||||
|
from = "Column::TeamResultId",
|
||||||
|
to = "super::team_result::Column::Id",
|
||||||
|
on_update = "NoAction",
|
||||||
|
on_delete = "NoAction"
|
||||||
|
)]
|
||||||
|
TeamResult,
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::user::Entity",
|
||||||
|
from = "Column::UserId",
|
||||||
|
to = "super::user::Column::Id",
|
||||||
|
on_update = "NoAction",
|
||||||
|
on_delete = "NoAction"
|
||||||
|
)]
|
||||||
|
User,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::TeamResult.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::User.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
42
src/entity/user.rs
Normal file
42
src/entity/user.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0-rc.5
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "user")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
pub salt: String,
|
||||||
|
pub creation_time: DateTimeWithTimeZone,
|
||||||
|
pub last_active_time: DateTimeWithTimeZone,
|
||||||
|
pub is_admin: bool,
|
||||||
|
pub email: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::team_result_to_user::Entity")]
|
||||||
|
TeamResultToUser,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result_to_user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::TeamResultToUser.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::team_result::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::team_result_to_user::Relation::TeamResult.def()
|
||||||
|
}
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::team_result_to_user::Relation::User.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
15
src/main.rs
15
src/main.rs
@@ -1,9 +1,12 @@
|
|||||||
|
mod capsules;
|
||||||
mod components;
|
mod components;
|
||||||
mod data;
|
|
||||||
mod endpoints;
|
mod endpoints;
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
mod entity;
|
||||||
mod error_views;
|
mod error_views;
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
mod server;
|
mod server;
|
||||||
|
mod state_enums;
|
||||||
mod templates;
|
mod templates;
|
||||||
|
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
@@ -18,6 +21,8 @@ cfg_if::cfg_if! {
|
|||||||
stores::MutableStore,
|
stores::MutableStore,
|
||||||
turbine::Turbine,
|
turbine::Turbine,
|
||||||
};
|
};
|
||||||
|
use futures::executor::block_on;
|
||||||
|
use sea_orm::{Database};
|
||||||
use crate::server::routes::register_routes;
|
use crate::server::routes::register_routes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,6 +40,13 @@ pub async fn dflt_server<M: MutableStore + 'static, T: TranslationsManager + 'st
|
|||||||
|
|
||||||
app = register_routes(app);
|
app = register_routes(app);
|
||||||
|
|
||||||
|
// TODO -> Update to use environment variable
|
||||||
|
if let Err(err) = block_on(Database::connect(
|
||||||
|
"postgres://elo:elo@localhost:5432/elo_app",
|
||||||
|
)) {
|
||||||
|
panic!("{}", err);
|
||||||
|
}
|
||||||
|
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
.await
|
.await
|
||||||
@@ -51,6 +63,7 @@ pub fn main<G: Html>() -> PerseusApp<G> {
|
|||||||
.template(crate::templates::add_game_form::get_template())
|
.template(crate::templates::add_game_form::get_template())
|
||||||
.template(crate::templates::one_v_one_board::get_template())
|
.template(crate::templates::one_v_one_board::get_template())
|
||||||
.template(crate::templates::overall_board::get_template())
|
.template(crate::templates::overall_board::get_template())
|
||||||
|
.capsule_ref(&*crate::capsules::login_form::LOGIN_FORM)
|
||||||
.error_views(crate::error_views::get_error_views())
|
.error_views(crate::error_views::get_error_views())
|
||||||
.index_view(|cx| {
|
.index_view(|cx| {
|
||||||
view! { cx,
|
view! { cx,
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +1,24 @@
|
|||||||
// (Server only) Routes
|
// (Server only) Routes
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
data::{
|
endpoints::{MATCH, USER},
|
||||||
pool_match::{PoolMatch, PoolMatchList},
|
entity::{game, user},
|
||||||
store::DATA,
|
|
||||||
},
|
|
||||||
endpoints::MATCH,
|
|
||||||
};
|
};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Json,
|
extract::Json,
|
||||||
routing::{post, Router},
|
routing::{post, Router},
|
||||||
};
|
};
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
pub fn register_routes(app: Router) -> Router {
|
pub fn register_routes(app: Router) -> Router {
|
||||||
let app = app.route(MATCH, post(post_match));
|
let app = app.route(USER, post(post_user));
|
||||||
app
|
app.route(MATCH, post(post_match))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_match(Json(pool_match): Json<PoolMatch>) -> Json<PoolMatchList> {
|
async fn post_user(_user: String) -> Json<user::Model> {
|
||||||
// Update the store with the new match
|
// Update the store with the new match
|
||||||
let matches = thread::spawn(move || {
|
todo!()
|
||||||
// Get the store
|
}
|
||||||
let mut data = DATA.lock().unwrap();
|
|
||||||
(*data).matches.add_pool_match(pool_match);
|
async fn post_match(_user: String) -> Json<game::Model> {
|
||||||
println!("{:?}", (*data).matches.pool_matches);
|
// Update the store with the new match
|
||||||
(*data).matches.clone()
|
todo!()
|
||||||
})
|
|
||||||
.join()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Json(matches)
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/state_enums.rs
Normal file
22
src/state_enums.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub enum LoginState {
|
||||||
|
Authenticated,
|
||||||
|
NotAuthenticated,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub enum GameState {
|
||||||
|
None,
|
||||||
|
Pool,
|
||||||
|
Pickleball,
|
||||||
|
TableTennis,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub enum OpenState {
|
||||||
|
Open,
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
use crate::data::pool_match::MatchType;
|
use crate::{components::layout::Layout, state_enums::GameState};
|
||||||
use crate::{components::layout::Layout, data::pool_match::MatchData};
|
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
@@ -7,9 +6,8 @@ use web_sys::Event;
|
|||||||
|
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(client)] {
|
if #[cfg(client)] {
|
||||||
use crate::data::pool_match::{PoolMatch, PoolMatchList};
|
|
||||||
use crate::templates::global_state::AppStateRx;
|
use crate::templates::global_state::AppStateRx;
|
||||||
use crate::endpoints::MATCH;
|
use crate::endpoints::{MATCH, USER};
|
||||||
use crate::templates::get_api_path;
|
use crate::templates::get_api_path;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
}
|
}
|
||||||
@@ -20,43 +18,37 @@ cfg_if::cfg_if! {
|
|||||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||||
#[rx(alias = "PageStateRx")]
|
#[rx(alias = "PageStateRx")]
|
||||||
struct PageState {
|
struct PageState {
|
||||||
name: String,
|
winner: String,
|
||||||
|
new_user: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
||||||
let handle_add_match = move |_event: Event| {
|
let handle_add_match = move |_event: Event| {
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
{
|
{
|
||||||
// state.name.get().as_ref().clone()
|
// state.winner.get().as_ref().clone()
|
||||||
spawn_local_scoped(cx, async move {
|
spawn_local_scoped(cx, async move {})
|
||||||
let new_match = PoolMatch::new(
|
}
|
||||||
MatchData {
|
};
|
||||||
type_: MatchType::Standard8Ball,
|
|
||||||
winners: vec![1],
|
let handle_add_user = move |_event: Event| {
|
||||||
losers: vec![2, 3, 4],
|
#[cfg(client)]
|
||||||
},
|
{
|
||||||
Utc::now(),
|
// state.winner.get().as_ref().clone()
|
||||||
);
|
spawn_local_scoped(cx, async move {})
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let new_matches = client
|
|
||||||
.post(get_api_path(MATCH).as_str())
|
|
||||||
.json(&new_match)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.json::<PoolMatchList>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
|
||||||
global_state.matches.set(new_matches);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "Add Game Results") {
|
Layout(title = "Add Game Results", game = GameState::Pool) {
|
||||||
div (class = "flex flex-wrap") {
|
div (class = "flex flex-wrap") {
|
||||||
input (bind:value = state.name,
|
select {
|
||||||
|
option (value="red")
|
||||||
|
option (value="blue")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
div (class = "flex flex-wrap") {
|
||||||
|
input (bind:value = state.winner,
|
||||||
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
|
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
|
||||||
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
|
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
|
||||||
focus:bg-white",)
|
focus:bg-white",)
|
||||||
@@ -69,6 +61,20 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
|||||||
"Add result"
|
"Add result"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
div (class = "flex flex-wrap") {
|
||||||
|
input (bind:value = state.new_user,
|
||||||
|
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
|
||||||
|
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
|
||||||
|
focus:bg-white",)
|
||||||
|
}
|
||||||
|
div (class = "flex flex-wrap") {
|
||||||
|
button(on:click = handle_add_user,
|
||||||
|
class = "flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 \
|
||||||
|
hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded",
|
||||||
|
) {
|
||||||
|
"Add new user"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +85,8 @@ async fn get_request_state(
|
|||||||
_req: Request,
|
_req: Request,
|
||||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||||
Ok(PageState {
|
Ok(PageState {
|
||||||
name: "Ferris".to_string(),
|
winner: "Ferris".to_string(),
|
||||||
|
new_user: "newguy".to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +1,70 @@
|
|||||||
// Not a page, global state that is shared between all pages
|
// Not a page, global state that is shared between all pages
|
||||||
|
|
||||||
use crate::data::pool_match::PoolMatchList;
|
|
||||||
use perseus::{prelude::*, state::GlobalStateCreator};
|
use perseus::{prelude::*, state::GlobalStateCreator};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::state_enums::{LoginState, OpenState};
|
||||||
|
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(engine)] {
|
if #[cfg(engine)] {
|
||||||
use std::thread;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use crate::data::store::DATA;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
||||||
#[rx(alias = "AppStateRx")]
|
#[rx(alias = "AppStateRx")]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub matches: PoolMatchList,
|
#[rx(nested)]
|
||||||
|
pub auth: AuthData,
|
||||||
|
#[rx(nested)]
|
||||||
|
pub modals_open: ModalOpenData,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
||||||
|
#[rx(alias = "AuthDataRx")]
|
||||||
|
pub struct AuthData {
|
||||||
|
pub state: LoginState,
|
||||||
|
pub username: Option<String>,
|
||||||
|
pub claims: Claims,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
||||||
|
#[rx(alias = "ModalOpenDataRx")]
|
||||||
|
pub struct ModalOpenData {
|
||||||
|
pub login: OpenState,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
||||||
|
#[rx(alias = "ClaimsRx")]
|
||||||
|
pub struct Claims {}
|
||||||
|
|
||||||
pub fn get_global_state_creator() -> GlobalStateCreator {
|
pub fn get_global_state_creator() -> GlobalStateCreator {
|
||||||
GlobalStateCreator::new()
|
GlobalStateCreator::new().build_state_fn(get_build_state)
|
||||||
.build_state_fn(get_build_state)
|
|
||||||
.request_state_fn(get_request_state)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[engine_only_fn]
|
|
||||||
fn get_state() -> AppState {
|
|
||||||
let matches = thread::spawn(move || DATA.lock().unwrap().deref().matches.clone())
|
|
||||||
.join()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
AppState { matches }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
pub async fn get_build_state() -> AppState {
|
pub async fn get_build_state() -> AppState {
|
||||||
get_state()
|
AppState {
|
||||||
|
auth: AuthData {
|
||||||
|
state: LoginState::Unknown,
|
||||||
|
username: None,
|
||||||
|
claims: Claims {},
|
||||||
|
},
|
||||||
|
modals_open: ModalOpenData {
|
||||||
|
login: OpenState::Closed,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[engine_only_fn]
|
// Client only code to check if they're authenticated
|
||||||
pub async fn get_request_state(_req: Request) -> AppState {
|
#[cfg(client)]
|
||||||
get_state()
|
impl AuthDataRx {
|
||||||
|
pub fn detect_state(&self) {
|
||||||
|
// If the user is in a known state, return
|
||||||
|
if let LoginState::Authenticated | LoginState::NotAuthenticated = *self.state.get() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO -> Get state from storage
|
||||||
|
self.state.set(LoginState::NotAuthenticated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use crate::components::layout::Layout;
|
use crate::{components::layout::Layout, state_enums::GameState};
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
|
||||||
fn index_page<G: Html>(cx: Scope) -> View<G> {
|
fn index_page<G: Html>(cx: Scope) -> View<G> {
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "Index") {
|
Layout(title = "Index", game = GameState::Pool) {
|
||||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
||||||
p { "Hello World!" }
|
p { "Hello World!" }
|
||||||
br {}
|
br {}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::components::layout::Layout;
|
use crate::{components::layout::Layout, state_enums::GameState};
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
@@ -9,7 +9,7 @@ struct PageState {}
|
|||||||
|
|
||||||
fn one_v_one_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
fn one_v_one_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "1v1 Leaderboard") {
|
Layout(title = "1v1 Leaderboard", game = GameState::Pool) {
|
||||||
p { "leaderboard" }
|
p { "leaderboard" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,23 @@
|
|||||||
use crate::{components::layout::Layout, templates::global_state::AppStateRx, data::user::PlayerId};
|
use crate::{
|
||||||
|
components::layout::Layout, state_enums::GameState, templates::global_state::AppStateRx,
|
||||||
|
};
|
||||||
|
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
|
|
||||||
use crate::data::pool_match::PoolMatch;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||||
#[rx(alias = "PageStateRx")]
|
#[rx(alias = "PageStateRx")]
|
||||||
struct PageState {}
|
struct PageState {}
|
||||||
|
|
||||||
fn format_list_or_single(to_format: &Vec<PlayerId>) -> String{
|
|
||||||
match to_format.len() {
|
|
||||||
1 => to_format[0].to_string(),
|
|
||||||
_ => format!("{:?}", to_format),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
||||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
let _global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "Overall Leaderboard") {
|
Layout(title = "Overall Leaderboard", game = GameState::Pool) {
|
||||||
ul {
|
ul {
|
||||||
(View::new_fragment(
|
(View::new_fragment(
|
||||||
global_state.matches.get()
|
vec![],
|
||||||
.pool_matches
|
|
||||||
.iter()
|
|
||||||
.rev()
|
|
||||||
.map(|item: &PoolMatch| {
|
|
||||||
let game = item.clone();
|
|
||||||
|
|
||||||
view! { cx,
|
|
||||||
li (class = "text-blue-700", id = "ha",) {
|
|
||||||
(game.id)
|
|
||||||
(" ")
|
|
||||||
(format_list_or_single(&game.data.winners))
|
|
||||||
(" ")
|
|
||||||
(format_list_or_single(&game.data.losers))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user