update data => list with date, name, score

This commit is contained in:
CoCo_Sol 2024-01-07 14:50:38 +01:00
parent b9d892a989
commit 4cbaeb391d
5 changed files with 173 additions and 28 deletions

146
Cargo.lock generated
View file

@ -26,6 +26,21 @@ dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "async-stream"
version = "0.3.5"
@ -113,6 +128,12 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "bytemuck"
version = "1.14.0"
@ -140,6 +161,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets 0.48.5",
]
[[package]]
name = "cookie"
version = "0.18.0"
@ -151,6 +186,12 @@ dependencies = [
"version_check",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "deranged"
version = "0.3.11"
@ -440,6 +481,29 @@ dependencies = [
"want",
]
[[package]]
name = "iana-time-zone"
version = "0.1.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "indexmap"
version = "2.1.0"
@ -474,6 +538,15 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -594,6 +667,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
@ -693,6 +775,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
name = "ponguito-serv"
version = "0.1.0"
dependencies = [
"chrono",
"rocket",
"serde",
"serde_json",
@ -1358,6 +1441,60 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
[[package]]
name = "winapi"
version = "0.3.9"
@ -1389,6 +1526,15 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-sys"
version = "0.48.0"

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.31"
rocket = { version = "0.5.0", features = ["json"] }
serde = "1.0.195"
serde_json = "1.0.111"

View file

@ -1 +1 @@
{}
[]

10
exemple.py Normal file
View file

@ -0,0 +1,10 @@
import requests as rq
IP = "127.0.0.1"
post = {"name": "coco_sol", "score": 10}
rq.post(f"http://{IP}/new_score", post)
rq.post(f"http://{IP}/new_score", post)
print(rq.get(f"http://{IP}/data").json())

View file

@ -1,12 +1,12 @@
use chrono::Utc;
use rocket::form::Form;
use rocket::serde::json::Json;
use rocket::{get, launch, Config, FromForm};
use rocket::{post, routes};
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::io::{Read, Write};
use std::net::IpAddr;
#[derive(Deserialize, Serialize, FromForm, Clone, Debug)]
@ -19,20 +19,15 @@ struct NewScore {
fn new_score(score: Form<NewScore>) -> Option<()> {
let name = score.name.clone();
let score = score.score.clone();
if load_data().contains_key(&name) {
if score < *load_data().get(&name).unwrap() {
return Some(());
}
save_data(name, score);
} else {
save_data(name, score);
}
save_data(name, score, Utc::now().to_string());
Some(())
}
#[get("/data")]
fn data() -> Json<Vec<(u32, String)>> {
let mut best_scores: Vec<_> = load_data().into_iter().map(|(k, v)| (v, k)).collect();
fn data() -> Json<Vec<(u32, String, String)>> {
let mut best_scores: Vec<_> = load_data().into_iter().map(|(k, v, d)| (v, k, d)).collect();
let nb = 5.min(best_scores.len());
@ -42,26 +37,19 @@ fn data() -> Json<Vec<(u32, String)>> {
Json(best_scores)
}
fn save_data(name: String, score: u32) {
fn save_data(name: String, score: u32, date: String) {
let mut data = load_data();
data.insert(name, score);
data.push((name, score, date));
let json = serde_json::to_string(&data).unwrap();
let mut file = File::create("data.json").unwrap();
file.write_all(json.as_bytes()).unwrap();
}
fn load_data() -> HashMap<String, u32> {
let file = File::open("data.json");
if let Ok(file) = file {
// read the file and put the data into ARTICLES if it didn't work then create an empty hashmap
let articles: HashMap<String, u32> =
serde_json::from_reader(file).unwrap_or_else(|_| HashMap::new());
return articles;
} else {
println!("Failed to open articles.json : {:?}", file);
panic!("Failed to open articles.json : {:?}", file);
}
fn load_data() -> Vec<(String, u32, String)> {
let mut file = File::open("data.json").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap()
}
// The main function of the website.
@ -72,7 +60,7 @@ async fn rocket() -> _ {
.merge(("port", 80))
.merge(("worker_count", 4))
.merge(("log_level", rocket::config::LogLevel::Critical))
.merge(("address", IpAddr::from([0, 0, 0, 0])));
.merge(("address", IpAddr::from([127, 0, 0, 1])));
// create a config
let config = Config::from(figment);