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;
|
pub type MatchId = u32;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub enum MatchData {
|
pub enum MatchType {
|
||||||
Standard8Ball {
|
Standard8Ball,
|
||||||
winner: PlayerId,
|
Standard9Ball,
|
||||||
loser: PlayerId,
|
CutThroat,
|
||||||
},
|
}
|
||||||
Standard9Ball {
|
|
||||||
winner: PlayerId,
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
loser: PlayerId,
|
pub struct MatchData {
|
||||||
},
|
pub type_: MatchType,
|
||||||
CutThroat {
|
pub winners: Vec<PlayerId>,
|
||||||
winner: PlayerId,
|
pub losers: Vec<PlayerId>,
|
||||||
losers: [PlayerId; 2],
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
@@ -28,12 +26,18 @@ pub struct PoolMatch {
|
|||||||
#[serde(with = "ts_seconds")]
|
#[serde(with = "ts_seconds")]
|
||||||
pub time: DateTime<Utc>,
|
pub time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub struct PoolMatchList {
|
pub struct PoolMatchList {
|
||||||
pub pool_matches: Vec<PoolMatch>,
|
pub pool_matches: Vec<PoolMatch>,
|
||||||
pub max_id: MatchId,
|
pub max_id: MatchId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct UserList{
|
||||||
|
pub users: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl PoolMatch {
|
impl PoolMatch {
|
||||||
pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch {
|
pub fn new(data: MatchData, time: DateTime<Utc>) -> PoolMatch {
|
||||||
PoolMatch { id: 0, data, time }
|
PoolMatch { id: 0, data, time }
|
||||||
@@ -54,3 +58,17 @@ impl PoolMatchList {
|
|||||||
self.pool_matches.push(pool_match);
|
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
|
// (Server only) In-memory data storage and persistent storage
|
||||||
|
|
||||||
use crate::data::pool_match::PoolMatchList;
|
use crate::data::pool_match::PoolMatchList;
|
||||||
|
use crate::data::pool_match::UserList;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{fs, path::Path, sync::Mutex};
|
use std::{fs, path::Path, sync::Mutex};
|
||||||
@@ -8,6 +9,7 @@ use std::{fs, path::Path, sync::Mutex};
|
|||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
pub matches: PoolMatchList,
|
pub matches: PoolMatchList,
|
||||||
|
pub users: UserList,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Store {
|
impl Store {
|
||||||
@@ -16,6 +18,7 @@ impl Store {
|
|||||||
match Path::new("data/store.json").exists() {
|
match Path::new("data/store.json").exists() {
|
||||||
false => Store {
|
false => Store {
|
||||||
matches: PoolMatchList::new(),
|
matches: PoolMatchList::new(),
|
||||||
|
users: UserList::new(),
|
||||||
},
|
},
|
||||||
true => {
|
true => {
|
||||||
let contents = fs::read_to_string("data/store.json").unwrap();
|
let contents = fs::read_to_string("data/store.json").unwrap();
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
pub const MATCH: &str = "/api/post-match";
|
pub const MATCH: &str = "/api/post-match";
|
||||||
|
pub const USER: &str = "/api/post-user";
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
data::{
|
data::{
|
||||||
pool_match::{PoolMatch, PoolMatchList},
|
pool_match::{PoolMatch, PoolMatchList, UserList},
|
||||||
store::DATA,
|
store::DATA,
|
||||||
},
|
},
|
||||||
endpoints::MATCH,
|
endpoints::MATCH,
|
||||||
|
endpoints::USER,
|
||||||
};
|
};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Json,
|
extract::Json,
|
||||||
@@ -15,6 +16,7 @@ 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(MATCH, post(post_match));
|
||||||
|
let app = app.route(USER, post(post_user));
|
||||||
app
|
app
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,3 +34,19 @@ async fn post_match(Json(pool_match): Json<PoolMatch>) -> Json<PoolMatchList> {
|
|||||||
|
|
||||||
Json(matches)
|
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 crate::{components::layout::Layout, data::pool_match::MatchData};
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -6,9 +7,9 @@ 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::data::pool_match::{PoolMatch, PoolMatchList, UserList};
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -19,16 +20,24 @@ 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::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 client = reqwest::Client::new();
|
||||||
let new_matches = client
|
let new_matches = client
|
||||||
.post(get_api_path(MATCH).as_str())
|
.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,
|
view! { cx,
|
||||||
Layout(title = "Add Game Results") {
|
Layout(title = "Add Game Results") {
|
||||||
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",)
|
||||||
@@ -61,6 +97,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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,7 +121,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,6 +1,8 @@
|
|||||||
// 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 crate::data::pool_match::PoolMatchList;
|
||||||
|
use crate::data::pool_match::UserList;
|
||||||
|
|
||||||
use perseus::{prelude::*, state::GlobalStateCreator};
|
use perseus::{prelude::*, state::GlobalStateCreator};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -16,6 +18,7 @@ cfg_if::cfg_if! {
|
|||||||
#[rx(alias = "AppStateRx")]
|
#[rx(alias = "AppStateRx")]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub matches: PoolMatchList,
|
pub matches: PoolMatchList,
|
||||||
|
pub users: UserList,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_global_state_creator() -> GlobalStateCreator {
|
pub fn get_global_state_creator() -> GlobalStateCreator {
|
||||||
@@ -30,7 +33,11 @@ fn get_state() -> AppState {
|
|||||||
.join()
|
.join()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
AppState { matches }
|
let users = thread::spawn(move || DATA.lock().unwrap().deref().users.clone())
|
||||||
|
.join()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
AppState { matches, users }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[engine_only_fn]
|
#[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 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);
|
||||||
|
|
||||||
@@ -19,12 +28,16 @@ fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageSta
|
|||||||
.pool_matches
|
.pool_matches
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.enumerate()
|
.map(|item: &PoolMatch| {
|
||||||
.map(|(_index, item)| {
|
|
||||||
let game = item.clone();
|
let game = item.clone();
|
||||||
|
|
||||||
view! { cx,
|
view! { cx,
|
||||||
li {
|
li (class = "text-blue-700", id = "ha",) {
|
||||||
(game.id)
|
(game.id)
|
||||||
|
(" ")
|
||||||
|
(format_list_or_single(&game.data.winners))
|
||||||
|
(" ")
|
||||||
|
(format_list_or_single(&game.data.losers))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user