From bd4b3fb8c266ff03188c0336f777ad885a1b3cf6 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 24 Oct 2022 22:02:58 -0700 Subject: [PATCH] ux improvements --- Cargo.lock | 7 +++++ Cargo.toml | 3 ++- src/app.rs | 76 ++++++++++++++++++++++++++++++++++++++++------------ src/main.rs | 1 + src/stats.rs | 59 +++++++++++++++++++++++++++++++--------- 5 files changed, 115 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a560a6..41898d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1148,6 +1148,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "khronos_api" version = "3.1.0" @@ -2867,6 +2873,7 @@ version = "0.1.0" dependencies = [ "eframe", "egui", + "json", "libtor", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 7fe1d92..7b766de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ serde = { version = "1", features = ["derive"] } reqwest = { version = "0.11.12", features = ["blocking", "json", "stream", "socks"] } egui = "0.19.0" rodio = "0.16.0" -regex = "1.6.0" \ No newline at end of file +regex = "1.6.0" +json = "0.12.4" \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 5177728..e047f10 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,6 +5,7 @@ use egui::FontId; use egui::TextStyle::*; use crate::player::Player; use crate::tor::GuiTor; +use crate::stats::Market; use libtor::Error as libtorError; pub struct App { @@ -14,7 +15,9 @@ pub struct App { pub tor_connected: bool, pub to_data: String, pub show_irc: bool, - pub irc_message: String + pub irc_message: String, + pub show_market_data: bool, + pub market: Market } impl Default for App { @@ -23,14 +26,20 @@ impl Default for App { tor_started: false, tor_required: true, tor_connected: false, + show_market_data: false, show_irc: false, to_data: "".to_owned(), player: Player::default(), + market: Market::new(), irc_message: "".to_owned() } } } +fn show_boolmoji(b: bool) -> String { + if b { return "✔".to_owned() } else { return "☠".to_owned() } +} + impl eframe::App for App { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { let mut style = (*ctx.style()).clone(); @@ -54,25 +63,26 @@ impl eframe::App for App { egui::CentralPanel::default().show(ctx, |ui| { - ui.checkbox(&mut self.tor_required, "Require Tor"); + ui.horizontal(|ui| { + ui.checkbox(&mut self.tor_required, "Require Tor"); + if (self.tor_connected && self.tor_required) || (! self.tor_required) { + ui.checkbox(&mut self.show_market_data, "Show Market Data"); + } + + }); ui.separator(); if self.tor_required { if self.tor_started { - ui.label("Tor started."); - if ui.button("Clear Tor logs").clicked() { - let _r: std::io::Result<()> = GuiTor::clear_log(); - } - if ui.button("Check Proxy").clicked() { - if self.player.check_proxy() { - self.tor_connected = true; - } - } - ui.collapsing("View Tor logs:", |ui| { - egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { - let contents = std::fs::read_to_string(crate::TOR_LOG.to_string()).unwrap(); - ui.label(contents); - }); - }); + ui.label( + egui::RichText::new( + format!( + "Tor Started: {}\nTor Connected: {}\nProxy Up: {}", + show_boolmoji(self.tor_started), + show_boolmoji(self.tor_connected), + show_boolmoji(self.tor_connected) + ) + ) + ); } else { if ui.button("Connect to the Tor network").clicked() { let _t: std::thread::JoinHandle> = GuiTor::start_tor(); @@ -81,6 +91,38 @@ impl eframe::App for App { } } } + if self.show_market_data { + if self.market.last_check_time.elapsed().unwrap() > Duration::from_secs(120) { + println!("[+] refreshing WOW market data."); + self.market.last_cg_data = self.market.get_wow_market(self.tor_required); + self.market.last_check_time = SystemTime::now(); + } + let m = &self.market.last_cg_data; + let md = &m["market_data"]; + ui.horizontal_wrapped(|ui| { + ui.vertical(|ui| { + ui.label("CURRENT PRICE"); + ui.heading(egui::RichText::new(md["current_price"]["sats"].to_string()).strong()); + ui.label("SATS"); + }); + ui.vertical(|ui| { + ui.label("ATH"); + ui.heading(egui::RichText::new(md["ath"]["sats"].to_string()).strong()); + ui.label("SATS"); + }); + ui.vertical(|ui| { + ui.label("ATL"); + ui.heading(egui::RichText::new(md["atl"]["sats"].to_string()).strong()); + ui.label("SATS"); + }); + ui.vertical(|ui| { + ui.label("MARKET CAP"); + ui.heading(egui::RichText::new(md["market_cap"]["sats"].to_string()).strong()); + ui.label("SATS"); + }); + }); + } + }); // Show volume controls diff --git a/src/main.rs b/src/main.rs index 0c2a1aa..eeabb09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ pub use crate::app::App; mod app; mod player; mod tor; +mod stats; // "http://wowradiod6mhb4ignjqy5ghf5l42yh2yeumgeq3yi7gn7yqy3efhe5ad.onion" // "http://wowradiof5wx62w4avdk5fwbvcoea2z4ul2q3awffn5vmfaa3vhlgiid.onion" diff --git a/src/stats.rs b/src/stats.rs index 4a49781..ab23e99 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -1,16 +1,49 @@ +extern crate json; +extern crate reqwest; +use std::time::{SystemTime, Duration}; +use json::JsonValue; +use reqwest::{blocking::ClientBuilder, Proxy}; +pub struct Market { + pub last_check_time: SystemTime, + pub last_cg_data: JsonValue +} -fn get_wow_price() -> String { - let url: &str = "https://tradeogre.com/api/v1/ticker/BTC-WOW"; - let client: reqwest::blocking::Client = reqwest::blocking::ClientBuilder::new() - .timeout(std::time::Duration::from_secs(10)) - .build() - .unwrap(); - let res: String = client.get(url) - .send() - .unwrap() - .json() - .unwrap(); - return res -} \ No newline at end of file +impl Market { + pub fn new() -> Self { + Self { + last_check_time: SystemTime::now() - Duration::from_secs(600), + last_cg_data: json::object!{} + } + } + + fn get_client(&self, tor_required: bool) -> ClientBuilder { + let mut client_builder = ClientBuilder::new(); + if tor_required { + let proxy = Proxy::all("socks5://127.0.0.1:19050").unwrap(); + client_builder = client_builder.proxy(proxy); + } + return client_builder; + } + + pub fn get_wow_market(&self, tor_required: bool) -> JsonValue { + let url = "https://api.coingecko.com/api/v3/coins/wownero?community_data=false&developer_data=false"; + let client = self.get_client(tor_required); + let res: String = client + .build() + .unwrap() + .get(url) + .send() + .unwrap() + .text() + .unwrap(); + + let j = json::parse(res.as_str()); + if j.is_ok() { + return j.unwrap(); + } else { + return json::object!{}; + } + } +}