You've already forked AstralRinth
forked from didirus/AstralRinth
Discord and playtime (#462)
* initial * Fixed java thing * fixes * internet check change * some fix/test commit * Fix render issues on windows * bump version --------- Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Jai A <jai@modrinth.com>
This commit is contained in:
@@ -6,7 +6,7 @@ use std::path::PathBuf;
|
||||
use crate::event::emit::{emit_loading, init_loading};
|
||||
use crate::state::CredentialsStore;
|
||||
use crate::util::fetch::{fetch_advanced, fetch_json};
|
||||
use crate::util::io;
|
||||
|
||||
use crate::util::jre::extract_java_majorminor_version;
|
||||
use crate::{
|
||||
state::JavaGlobals,
|
||||
@@ -117,10 +117,6 @@ pub async fn auto_install_java(java_version: u32) -> crate::Result<PathBuf> {
|
||||
|
||||
let path = state.directories.java_versions_dir().await;
|
||||
|
||||
if path.exists() {
|
||||
io::remove_dir_all(&path).await?;
|
||||
}
|
||||
|
||||
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(file))
|
||||
.map_err(|_| {
|
||||
crate::Error::from(crate::ErrorKind::InputError(
|
||||
|
||||
@@ -273,9 +273,7 @@ pub async fn install_zipped_mrpack_files(
|
||||
profile::edit_icon(&profile_path, Some(&potential_icon)).await?;
|
||||
}
|
||||
|
||||
if let Some(profile_val) =
|
||||
crate::api::profile::get(&profile_path, None).await?
|
||||
{
|
||||
if let Some(profile_val) = profile::get(&profile_path, None).await? {
|
||||
crate::launcher::install_minecraft(&profile_val, Some(loading_bar))
|
||||
.await?;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::pack::install_from::{
|
||||
use crate::prelude::{JavaVersion, ProfilePathId, ProjectPathId};
|
||||
use crate::state::ProjectMetadata;
|
||||
|
||||
use crate::util::fetch;
|
||||
use crate::util::io::{self, IOError};
|
||||
use crate::{
|
||||
auth::{self, refresh},
|
||||
@@ -22,6 +23,7 @@ pub use crate::{
|
||||
};
|
||||
use async_zip::tokio::write::ZipFileWriter;
|
||||
use async_zip::{Compression, ZipEntryBuilder};
|
||||
use serde_json::json;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -878,6 +880,65 @@ pub async fn run_credentials(
|
||||
Ok(mc_process)
|
||||
}
|
||||
|
||||
/// Update playtime- sending a request to the server to update the playtime
|
||||
#[tracing::instrument]
|
||||
#[theseus_macros::debug_pin]
|
||||
pub async fn try_update_playtime(path: &ProfilePathId) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
|
||||
let profile = get(path, None).await?.ok_or_else(|| {
|
||||
crate::ErrorKind::OtherError(format!(
|
||||
"Tried to update playtime for a nonexistent or unloaded profile at path {}!",
|
||||
path
|
||||
))
|
||||
})?;
|
||||
let updated_recent_playtime = profile.metadata.recent_time_played;
|
||||
|
||||
let res = if updated_recent_playtime > 0 {
|
||||
// Create update struct to send to Labrinth
|
||||
let modrinth_pack_version_id =
|
||||
profile.metadata.linked_data.and_then(|l| l.version_id);
|
||||
let playtime_update_json = json!({
|
||||
"seconds": updated_recent_playtime,
|
||||
"loader": profile.metadata.loader.to_string(),
|
||||
"game_version": profile.metadata.game_version,
|
||||
"parent": modrinth_pack_version_id,
|
||||
});
|
||||
// Copy this struct for every Modrinth project in the profile
|
||||
let mut hashmap: HashMap<String, serde_json::Value> = HashMap::new();
|
||||
for (_, project) in profile.projects {
|
||||
if let ProjectMetadata::Modrinth { version, .. } = project.metadata
|
||||
{
|
||||
hashmap.insert(version.id, playtime_update_json.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let creds = state.credentials.read().await;
|
||||
fetch::post_json(
|
||||
"https://api.modrinth.com/analytics/playtime",
|
||||
serde_json::to_value(hashmap)?,
|
||||
&state.fetch_semaphore,
|
||||
&creds,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
|
||||
// If successful, update the profile metadata to match submitted
|
||||
if res.is_ok() {
|
||||
let mut profiles = state.profiles.write().await;
|
||||
if let Some(profile) = profiles.0.get_mut(path) {
|
||||
profile.metadata.submitted_time_played += updated_recent_playtime;
|
||||
profile.metadata.recent_time_played = 0;
|
||||
}
|
||||
}
|
||||
// Sync either way
|
||||
State::sync().await?;
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn get_modrinth_pack_list(packfile: &PackFormat) -> Vec<String> {
|
||||
packfile
|
||||
.files
|
||||
|
||||
Reference in New Issue
Block a user