Skip to content

Commit

Permalink
fix: genshin stats
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuskreutz committed Aug 8, 2024
1 parent 6147b73 commit 9c4a6d7
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 8 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ actix = "0.13.5"
actix-cors = "0.7.0"
actix-files = "0.6.6"
actix-multipart = "0.7.2"
actix-session = "0.9.0"
actix-session = "0.10.0"
actix-web = "4.8.0"
anyhow = "1.0.86"
async-process = "2.2.3"
Expand All @@ -34,7 +34,7 @@ reqwest = { version = "0.12.5", default-features = false, features = [
"rustls-tls",
] }
rust-argon2 = "2.1.0"
serde = { version = "1.0.204", features = ["derive"] }
serde = { version = "1.0.205", features = ["derive"] }
serde_json = "1.0.122"
sqlx = { version = "0.8.0", features = [
"postgres",
Expand Down
271 changes: 271 additions & 0 deletions src/api/gi/paimon_wishes_import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,276 @@ async fn post_paimon_warps_import(
database::gi::wishes::weapon::set_all(&set_all_weapon, &pool).await?;
database::gi::wishes::chronicled::set_all(&set_all_chronicled, &pool).await?;

calculate_stats(uid, &pool).await?;

Ok(HttpResponse::Ok().finish())
}

async fn calculate_stats(uid: i32, pool: &PgPool) -> anyhow::Result<()> {
calculate_stats_standard(uid, pool).await?;
calculate_stats_character(uid, pool).await?;
calculate_stats_weapon(uid, pool).await?;
calculate_stats_chronicled(uid, pool).await?;

Ok(())
}

async fn calculate_stats_standard(uid: i32, pool: &PgPool) -> anyhow::Result<()> {
let wishes = database::gi::wishes::standard::get_infos_by_uid(uid, pool).await?;

let mut pull_4 = 0;
let mut sum_4 = 0;
let mut count_4 = 0;

let mut pull_5 = 0;
let mut sum_5 = 0;
let mut count_5 = 0;

for wish in &wishes {
pull_4 += 1;
pull_5 += 1;

match wish.rarity.unwrap() {
4 => {
count_4 += 1;
sum_4 += pull_4;
pull_4 = 0;
}
5 => {
count_5 += 1;
sum_5 += pull_5;
pull_5 = 0;
}
_ => {}
}
}

let luck_4 = sum_4 as f64 / count_4 as f64;
let luck_5 = sum_5 as f64 / count_5 as f64;

let stat = database::gi::wishes_stats::standard::DbWishesStatStandard {
uid,
luck_4,
luck_5,
};
database::gi::wishes_stats::standard::set(&stat, pool).await?;

Ok(())
}

async fn calculate_stats_character(uid: i32, pool: &PgPool) -> anyhow::Result<()> {
let wishes = database::gi::wishes::character::get_infos_by_uid(uid, pool).await?;

let mut pull_4 = 0;
let mut sum_4 = 0;
let mut count_4 = 0;

let mut pull_5 = 0;
let mut sum_5 = 0;
let mut count_5 = 0;

let mut guarantee = false;

let mut sum_win = 0;
let mut count_win = 0;

let mut win_streak = 0;
let mut max_win_streak = 0;

let mut loss_streak = 0;
let mut max_loss_streak = 0;

for wish in &wishes {
pull_4 += 1;
pull_5 += 1;

match wish.rarity.unwrap() {
4 => {
count_4 += 1;
sum_4 += pull_4;
pull_4 = 0;
}
5 => {
count_5 += 1;
sum_5 += pull_5;
pull_5 = 0;

if guarantee {
guarantee = false;
} else {
count_win += 1;

if [
10000042, 10000016, 10000003, 10000035, 10000069, 10000079, 10000041,
]
.contains(&wish.character.unwrap())
{
win_streak = 0;

loss_streak += 1;
max_loss_streak = max_loss_streak.max(loss_streak);

guarantee = true;
} else {
sum_win += 1;

loss_streak = 0;

win_streak += 1;
max_win_streak = max_win_streak.max(win_streak);
}
}
}
_ => {}
}
}

let win_streak = max_win_streak;
let loss_streak = max_loss_streak;

let luck_4 = sum_4 as f64 / count_4 as f64;
let luck_5 = sum_5 as f64 / count_5 as f64;
let win_rate = sum_win as f64 / count_win as f64;

let stat = database::gi::wishes_stats::character::DbWishesStatCharacter {
uid,
luck_4,
luck_5,
win_rate,
win_streak,
loss_streak,
};
database::gi::wishes_stats::character::set(&stat, pool).await?;

Ok(())
}

async fn calculate_stats_weapon(uid: i32, pool: &PgPool) -> anyhow::Result<()> {
let wishes = database::gi::wishes::weapon::get_infos_by_uid(uid, pool).await?;

let mut pull_4 = 0;
let mut sum_4 = 0;
let mut count_4 = 0;

let mut pull_5 = 0;
let mut sum_5 = 0;
let mut count_5 = 0;

let mut guarantee = false;

let mut sum_win = 0;
let mut count_win = 0;

let mut win_streak = 0;
let mut max_win_streak = 0;

let mut loss_streak = 0;
let mut max_loss_streak = 0;

for wish in &wishes {
pull_4 += 1;
pull_5 += 1;

match wish.rarity.unwrap() {
4 => {
count_4 += 1;
sum_4 += pull_4;
pull_4 = 0;
}
5 => {
count_5 += 1;
sum_5 += pull_5;
pull_5 = 0;

if guarantee {
guarantee = false;
} else {
count_win += 1;

if [
15502, 11501, 14502, 13505, 14501, 15501, 12501, 13502, 12502,
]
.contains(&wish.weapon.unwrap())
{
win_streak = 0;

loss_streak += 1;
max_loss_streak = max_loss_streak.max(loss_streak);

guarantee = true;
} else {
sum_win += 1;

loss_streak = 0;

win_streak += 1;
max_win_streak = max_win_streak.max(win_streak);
}
}
}
_ => {}
}
}

let win_streak = max_win_streak;
let loss_streak = max_loss_streak;

let luck_4 = sum_4 as f64 / count_4 as f64;
let luck_5 = sum_5 as f64 / count_5 as f64;
let win_rate = sum_win as f64 / count_win as f64;

let stat = database::gi::wishes_stats::weapon::DbWishesStatWeapon {
uid,
luck_4,
luck_5,
win_rate,
win_streak,
loss_streak,
};
database::gi::wishes_stats::weapon::set(&stat, pool).await?;

Ok(())
}

async fn calculate_stats_chronicled(uid: i32, pool: &PgPool) -> anyhow::Result<()> {
let wishes = database::gi::wishes::chronicled::get_infos_by_uid(uid, pool).await?;

let mut pull_4 = 0;
let mut sum_4 = 0;
let mut count_4 = 0;

let mut pull_5 = 0;
let mut sum_5 = 0;
let mut count_5 = 0;

for wish in &wishes {
pull_4 += 1;
pull_5 += 1;

match wish.rarity.unwrap() {
4 => {
count_4 += 1;
sum_4 += pull_4;
pull_4 = 0;
}
5 => {
count_5 += 1;
sum_5 += pull_5;
pull_5 = 0;
}
_ => {}
}
}

let luck_4 = sum_4 as f64 / count_4 as f64;
let luck_5 = sum_5 as f64 / count_5 as f64;

let stat = database::gi::wishes_stats::chronicled::DbWishesStatChronicled {
uid,
luck_4,
luck_5,
};
database::gi::wishes_stats::chronicled::set(&stat, pool).await?;

Ok(())
}

0 comments on commit 9c4a6d7

Please sign in to comment.