1 Commits

Author SHA1 Message Date
ec06340def Add simple data store
All checks were successful
Build Crate / build (push) Successful in 8m5s
2023-09-20 00:28:16 -04:00
10 changed files with 98 additions and 13 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ static
pkg pkg
./Cargo.lock ./Cargo.lock
package-lock.json package-lock.json
data

View File

@@ -11,6 +11,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
env_logger = "0.10.0" env_logger = "0.10.0"
log = "0.4.20" log = "0.4.20"
once_cell = "1.18.0"
[target.'cfg(engine)'.dev-dependencies] [target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.19" fantoccini = "0.19"

View File

@@ -47,16 +47,11 @@ pub fn Layout<'a, G: Html>(
main(style = "my-8") { 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 = "h-64 rounded-md overflow-hidden bg-cover bg-center") { div(class = "rounded-md overflow-hidden bg-cover bg-center") {
(children) (children)
} }
} }
} }
} }
footer(class = "bg-gray-200") {
p(class = "container mx-auto px-6 py-3 flex justify-between items-center"){
"Hey there, I'm a footer!"
}
}
} }
} }

2
src/data/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod pool_match;
pub mod store;

11
src/data/pool_match.rs Normal file
View File

@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub struct PoolMatch {
pub players: Vec<String>,
pub winner: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct PoolMatchList {
pub pool_matches: Vec<PoolMatch>,
}

40
src/data/store.rs Normal file
View File

@@ -0,0 +1,40 @@
#![cfg(engine)]
use std::collections::HashMap;
use once_cell::sync::Lazy;
use std::sync::Mutex;
use serde::{Serialize, Deserialize};
use crate::data::pool_match::{PoolMatchList, PoolMatch};
use std::fs;
use std::path::Path;
#[derive(Serialize, Deserialize, Clone)]
pub struct Store {
pub matches: PoolMatchList,
}
impl Store {
fn new() -> Store {
fs::create_dir_all("data");
match Path::new("data/store.json").exists() {
false => {
Store {
matches: PoolMatchList { pool_matches: vec![] },
}
}
true => {
let contents = fs::read_to_string("data/store.json").unwrap();
serde_json::from_str(&contents).unwrap()
}
}
}
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())
});

View File

@@ -1,5 +1,6 @@
mod components; mod components;
mod templates; mod templates;
mod data;
mod error_views; mod error_views;
use perseus::prelude::*; use perseus::prelude::*;
@@ -7,7 +8,6 @@ use sycamore::prelude::view;
#[perseus::main(perseus_axum::dflt_server)] #[perseus::main(perseus_axum::dflt_server)]
pub fn main<G: Html>() -> PerseusApp<G> { pub fn main<G: Html>() -> PerseusApp<G> {
use log::{info, warn};
env_logger::init(); env_logger::init();
PerseusApp::new() PerseusApp::new()

View File

@@ -1,7 +1,6 @@
use crate::components::layout::Layout; use crate::components::layout::Layout;
use perseus::prelude::*; use perseus::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs;
use sycamore::prelude::*; use sycamore::prelude::*;
// Reactive page // Reactive page

View File

@@ -1,7 +1,6 @@
use crate::components::layout::Layout; use crate::components::layout::Layout;
use perseus::prelude::*; use perseus::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs;
use sycamore::prelude::*; use sycamore::prelude::*;
// Reactive page // Reactive page

View File

@@ -1,22 +1,46 @@
use crate::components::layout::Layout; use crate::components::layout::Layout;
use perseus::prelude::*; use perseus::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs; #[cfg(engine)]
use crate::data::store::DATA;
#[cfg(engine)]
use std::thread;
use sycamore::prelude::*; use sycamore::prelude::*;
use crate::data::pool_match::{
PoolMatchList, PoolMatch
};
// Reactive page // Reactive page
#[derive(Serialize, Deserialize, Clone, ReactiveState)] #[derive(Serialize, Deserialize, Clone, ReactiveState)]
#[rx(alias = "PageStateRx")] #[rx(alias = "PageStateRx")]
struct PageState { struct PageState {
matches: PoolMatchList,
} }
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> {
view! { cx, view! { cx,
Layout(title = "Overall Leaderboard") { Layout(title = "Overall Leaderboard") {
// 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 { "leaderboard" } ul {
(View::new_fragment(
state.matches.get()
.pool_matches
.iter()
.rev()
.enumerate()
.map(|(index, item)| {
let game = item.clone();
view! { cx,
li {
(game.winner)
}
}
})
.collect(),
))
}
} }
} }
} }
@@ -26,7 +50,20 @@ async fn get_request_state(
_info: StateGeneratorInfo<()>, _info: StateGeneratorInfo<()>,
req: Request, req: Request,
) -> Result<PageState, BlamedError<std::convert::Infallible>> { ) -> Result<PageState, BlamedError<std::convert::Infallible>> {
Ok(PageState {})
let matches = thread::spawn(move || {
let mut db = DATA.lock().unwrap();
db.matches.pool_matches.push(PoolMatch {
players: vec![],
winner: "lol".to_string(),
});
db.write();
db.matches.clone()
}).join().unwrap();
Ok(PageState {
matches
})
} }
#[engine_only_fn] #[engine_only_fn]