Compare commits
3 Commits
319c77cfa3
...
jak-user
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01eaf059dd | ||
| cb5da9b966 | |||
| cd8b52cd29 |
@@ -6,19 +6,17 @@ use serde::{Deserialize, Serialize};
|
||||
pub type MatchId = u32;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub enum MatchData {
|
||||
Standard8Ball {
|
||||
winner: PlayerId,
|
||||
loser: PlayerId,
|
||||
},
|
||||
Standard9Ball {
|
||||
winner: PlayerId,
|
||||
loser: PlayerId,
|
||||
},
|
||||
CutThroat {
|
||||
winner: PlayerId,
|
||||
losers: [PlayerId; 2],
|
||||
},
|
||||
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)]
|
||||
@@ -28,12 +26,18 @@ pub struct PoolMatch {
|
||||
#[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,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct UserList{
|
||||
pub users: Vec<String>,
|
||||
}
|
||||
|
||||
impl PoolMatch {
|
||||
pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch {
|
||||
PoolMatch { id: 0, data, time }
|
||||
@@ -54,3 +58,17 @@ impl PoolMatchList {
|
||||
self.pool_matches.push(pool_match);
|
||||
}
|
||||
}
|
||||
|
||||
impl UserList {
|
||||
pub fn new() -> UserList {
|
||||
UserList {
|
||||
users: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_user(&mut self, user: String) -> usize {
|
||||
let user_id = self.users.len();
|
||||
self.users.push(user);
|
||||
user_id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// (Server only) In-memory data storage and persistent storage
|
||||
|
||||
use crate::data::pool_match::PoolMatchList;
|
||||
use crate::data::pool_match::UserList;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs, path::Path, sync::Mutex};
|
||||
@@ -8,6 +9,7 @@ use std::{fs, path::Path, sync::Mutex};
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Store {
|
||||
pub matches: PoolMatchList,
|
||||
pub users: UserList,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
@@ -16,6 +18,7 @@ impl Store {
|
||||
match Path::new("data/store.json").exists() {
|
||||
false => Store {
|
||||
matches: PoolMatchList::new(),
|
||||
users: UserList::new(),
|
||||
},
|
||||
true => {
|
||||
let contents = fs::read_to_string("data/store.json").unwrap();
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub const MATCH: &str = "/api/post-match";
|
||||
pub const USER: &str = "/api/post-user";
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
use crate::{
|
||||
data::{
|
||||
pool_match::{PoolMatch, PoolMatchList},
|
||||
pool_match::{PoolMatch, PoolMatchList, UserList},
|
||||
store::DATA,
|
||||
},
|
||||
endpoints::MATCH,
|
||||
endpoints::USER,
|
||||
};
|
||||
use axum::{
|
||||
extract::Json,
|
||||
@@ -15,6 +16,7 @@ use std::thread;
|
||||
|
||||
pub fn register_routes(app: Router) -> Router {
|
||||
let app = app.route(MATCH, post(post_match));
|
||||
let app = app.route(USER, post(post_user));
|
||||
app
|
||||
}
|
||||
|
||||
@@ -32,3 +34,19 @@ async fn post_match(Json(pool_match): Json<PoolMatch>) -> Json<PoolMatchList> {
|
||||
|
||||
Json(matches)
|
||||
}
|
||||
|
||||
async fn post_user(user: String) -> Json<UserList> {
|
||||
// Update the store with the new match
|
||||
let users = thread::spawn(move || {
|
||||
// Get the store
|
||||
let mut data = DATA.lock().unwrap();
|
||||
let user_id = (*data).users.add_user(user);
|
||||
println!("Added new user id: {}\nAll users: {:?}", user_id, (*data).users);
|
||||
(*data).users.clone()
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
|
||||
Json(users)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::data::pool_match::MatchType;
|
||||
use crate::{components::layout::Layout, data::pool_match::MatchData};
|
||||
use perseus::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -6,9 +7,9 @@ use web_sys::Event;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(client)] {
|
||||
use crate::data::pool_match::{PoolMatch, PoolMatchList};
|
||||
use crate::data::pool_match::{PoolMatch, PoolMatchList, UserList};
|
||||
use crate::templates::global_state::AppStateRx;
|
||||
use crate::endpoints::MATCH;
|
||||
use crate::endpoints::{MATCH, USER};
|
||||
use crate::templates::get_api_path;
|
||||
use chrono::Utc;
|
||||
}
|
||||
@@ -19,16 +20,24 @@ cfg_if::cfg_if! {
|
||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||
#[rx(alias = "PageStateRx")]
|
||||
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> {
|
||||
let handle_add_match = move |_event: Event| {
|
||||
#[cfg(client)]
|
||||
{
|
||||
// state.name.get().as_ref().clone()
|
||||
// state.winner.get().as_ref().clone()
|
||||
spawn_local_scoped(cx, async move {
|
||||
let new_match = PoolMatch::new(MatchData::Standard8Ball { winner: 1, loser: 2 }, Utc::now());
|
||||
let new_match = PoolMatch::new(
|
||||
MatchData {
|
||||
type_: MatchType::Standard8Ball,
|
||||
winners: vec![1],
|
||||
losers: vec![2, 3, 4],
|
||||
},
|
||||
Utc::now(),
|
||||
);
|
||||
let client = reqwest::Client::new();
|
||||
let new_matches = client
|
||||
.post(get_api_path(MATCH).as_str())
|
||||
@@ -45,10 +54,37 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
||||
}
|
||||
};
|
||||
|
||||
let handle_add_user = move |_event: Event| {
|
||||
#[cfg(client)]
|
||||
{
|
||||
// state.winner.get().as_ref().clone()
|
||||
spawn_local_scoped(cx, async move {
|
||||
let client = reqwest::Client::new();
|
||||
let new_users = client
|
||||
.post(get_api_path(USER).as_str())
|
||||
.body(state.new_user.get().as_ref().clone())
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.json::<UserList>()
|
||||
.await
|
||||
.unwrap();
|
||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||
global_state.users.set(new_users);
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
Layout(title = "Add Game Results") {
|
||||
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 \
|
||||
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
|
||||
focus:bg-white",)
|
||||
@@ -61,6 +97,20 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +121,8 @@ async fn get_request_state(
|
||||
_req: Request,
|
||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||
Ok(PageState {
|
||||
name: "Ferris".to_string(),
|
||||
winner: "Ferris".to_string(),
|
||||
new_user: "newguy".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Not a page, global state that is shared between all pages
|
||||
|
||||
use crate::data::pool_match::PoolMatchList;
|
||||
use crate::data::pool_match::UserList;
|
||||
|
||||
use perseus::{prelude::*, state::GlobalStateCreator};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -16,6 +18,7 @@ cfg_if::cfg_if! {
|
||||
#[rx(alias = "AppStateRx")]
|
||||
pub struct AppState {
|
||||
pub matches: PoolMatchList,
|
||||
pub users: UserList,
|
||||
}
|
||||
|
||||
pub fn get_global_state_creator() -> GlobalStateCreator {
|
||||
@@ -30,7 +33,11 @@ fn get_state() -> AppState {
|
||||
.join()
|
||||
.unwrap();
|
||||
|
||||
AppState { matches }
|
||||
let users = thread::spawn(move || DATA.lock().unwrap().deref().users.clone())
|
||||
.join()
|
||||
.unwrap();
|
||||
|
||||
AppState { matches, users }
|
||||
}
|
||||
|
||||
#[engine_only_fn]
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
use crate::{components::layout::Layout, templates::global_state::AppStateRx};
|
||||
use crate::{components::layout::Layout, templates::global_state::AppStateRx, data::user::PlayerId};
|
||||
|
||||
use perseus::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sycamore::prelude::*;
|
||||
|
||||
use crate::data::pool_match::PoolMatch;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||
#[rx(alias = "PageStateRx")]
|
||||
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> {
|
||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||
|
||||
@@ -19,12 +28,16 @@ fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageSta
|
||||
.pool_matches
|
||||
.iter()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(|(_index, item)| {
|
||||
.map(|item: &PoolMatch| {
|
||||
let game = item.clone();
|
||||
|
||||
view! { cx,
|
||||
li {
|
||||
li (class = "text-blue-700", id = "ha",) {
|
||||
(game.id)
|
||||
(" ")
|
||||
(format_list_or_single(&game.data.winners))
|
||||
(" ")
|
||||
(format_list_or_single(&game.data.losers))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user