remove expects, warnings, code duplication

This commit is contained in:
Songtronix 2019-06-20 08:16:08 +02:00
parent 2338084af0
commit 6fa086b964
6 changed files with 49 additions and 45 deletions

View File

@ -1,6 +1,8 @@
use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC};
use std::time::SystemTime;
use crate::DISCORD_INSTANCE;
use std::sync::mpsc::Sender;
use std::sync::{mpsc, Mutex, MutexGuard};
use std::thread;
@ -33,8 +35,13 @@ pub fn run() -> Mutex<DiscordState> {
Mutex::new(DiscordState {
tx,
thread: Some(thread::spawn(move || {
let rpc = RPC::init::<Handlers>(DISCORD_APPLICATION_ID, true, None)
.expect("failed to initiate discord_game_sdk");
let mut rpc: RPC = match RPC::init::<Handlers>(DISCORD_APPLICATION_ID, true, None) {
Ok(rpc) => rpc,
Err(e) => {
log::error!("failed to initiate discord_game_sdk: {}", e);
return;
}
};
//Set initial Status
let mut current_presence = RichPresence {
@ -91,6 +98,8 @@ pub fn run() -> Mutex<DiscordState> {
Err(_) => {}
}
}
rpc.clear_presence();
})),
})
}
@ -103,7 +112,7 @@ impl EventHandlers for Handlers {
}
fn errored(errcode: i32, message: &str) {
log::debug!("Error {}: {}", errcode, message);
log::warn!("Error {}: {}", errcode, message);
}
fn disconnected(errcode: i32, message: &str) {
@ -123,13 +132,13 @@ impl EventHandlers for Handlers {
}
}
/* Some helpers */
pub fn send_menu(disc: &mut MutexGuard<DiscordState>) {
disc.tx.send(DiscordUpdate::Details("Menu".into()));
disc.tx.send(DiscordUpdate::State("Idling".into()));
disc.tx.send(DiscordUpdate::LargeImg("bg_main".into()));
}
pub fn send_singleplayer(disc: &mut MutexGuard<DiscordState>) {
disc.tx.send(DiscordUpdate::Details("Singleplayer".into()));
disc.tx.send(DiscordUpdate::State("Playing...".into()));
pub fn send_all(updates: Vec<DiscordUpdate>) {
match DISCORD_INSTANCE.lock() {
Ok(mut disc) => {
for update in updates {
let _ = disc.tx.send(update);
}
}
Err(e) => log::error!("couldn't send Update to discord: {}", e),
}
}

View File

@ -6,7 +6,7 @@ use conrod_core::{
use super::{img_ids::Imgs, settings_window::SettingsTab, Fonts, TEXT_COLOR};
#[cfg(feature = "discord")]
use crate::{discord, discord_instance};
use crate::{discord, discord::DiscordUpdate};
widget_ids! {
struct Ids {
@ -155,10 +155,11 @@ impl<'a> Widget for EscMenu<'a> {
{
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
Ok(mut disc) => discord::send_menu(&mut disc),
Err(e) => log::error!("couldn't send Update to discord: {}", e),
}
discord::send_all(vec![
DiscordUpdate::Details("Menu".into()),
DiscordUpdate::State("Idling".into()),
DiscordUpdate::LargeImg("bg_main".into()),
]);
}
return Some(Event::Logout);

View File

@ -42,7 +42,7 @@ use std::collections::VecDeque;
use vek::*;
#[cfg(feature = "discord")]
use crate::{discord, discord_instance};
use crate::{discord, discord::DiscordUpdate};
const XP_COLOR: Color = Color::Rgba(0.59, 0.41, 0.67, 1.0);
const TEXT_COLOR: Color = Color::Rgba(1.0, 1.0, 1.0, 1.0);
@ -689,10 +689,11 @@ impl Hud {
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
Ok(mut disc) => discord::send_menu(&mut disc),
Err(e) => log::error!("couldn't send Update to discord: {}", e),
}
discord::send_all(vec![
DiscordUpdate::Details("Menu".into()),
DiscordUpdate::State("Idling".into()),
DiscordUpdate::LargeImg("bg_main".into()),
]);
}
}
Some(esc_menu::Event::Quit) => events.push(Event::Quit),

View File

@ -89,7 +89,7 @@ pub trait PlayState {
#[cfg(feature = "discord")]
lazy_static! {
//Set up discord rich presence
static ref discord_instance: Mutex<discord::DiscordState> = {
static ref DISCORD_INSTANCE: Mutex<discord::DiscordState> = {
discord::run()
};
}
@ -127,7 +127,7 @@ fn main() {
// Initialize discord. (lazy_static initalise lazily...)
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
match DISCORD_INSTANCE.lock() {
Ok(disc) => {
//great
}
@ -267,12 +267,12 @@ fn main() {
//Properly shutdown discord thread
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
match DISCORD_INSTANCE.lock() {
Ok(mut disc) => {
disc.tx.send(discord::DiscordUpdate::Shutdown);
let _ = disc.tx.send(discord::DiscordUpdate::Shutdown);
match disc.thread.take() {
Some(th) => {
th.join();
let _ = th.join();
}
None => {
error!("couldn't gracefully shutdown discord thread");

View File

@ -9,7 +9,7 @@ use std::{
};
#[cfg(feature = "discord")]
use crate::{discord, discord_instance};
use crate::{discord, discord::DiscordUpdate};
#[derive(Debug)]
pub enum Error {
@ -60,18 +60,11 @@ impl ClientInit {
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
Ok(mut disc) => {
if !server_address.eq("127.0.0.1") {
discord::send_singleplayer(&mut disc);
disc.tx.send(discord::DiscordUpdate::Details(
server_address,
));
}
}
Err(e) => {
log::error!("couldn't send Update to discord: {}", e)
}
if !server_address.eq("127.0.0.1") {
discord::send_all(vec![
DiscordUpdate::Details(server_address),
DiscordUpdate::State("Playing...".into()),
]);
}
}

View File

@ -11,7 +11,7 @@ use std::{
};
#[cfg(feature = "discord")]
use crate::{discord, discord_instance};
use crate::{discord, discord::DiscordUpdate};
const TPS: u64 = 30;
@ -72,10 +72,10 @@ fn run_server(mut server: Server, rec: Receiver<Msg>) {
#[cfg(feature = "discord")]
{
match discord_instance.lock() {
Ok(mut disc) => discord::send_singleplayer(&mut disc),
Err(e) => log::error!("couldn't send Update to discord: {}", e),
}
discord::send_all(vec![
DiscordUpdate::Details("Singleplayer".into()),
DiscordUpdate::State("Playing...".into()),
]);
}
loop {