390 lines
14 KiB
Rust
390 lines
14 KiB
Rust
use data::gacha;
|
|
use data::gacha::gacha_config::*;
|
|
use proto::*;
|
|
|
|
use chrono::{DateTime, Local};
|
|
use rand::{thread_rng, Rng};
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::hash::{BuildHasher, Hash};
|
|
|
|
#[derive(Default)]
|
|
pub struct GachaModel {
|
|
pub gacha_bin: GachaModelBin,
|
|
}
|
|
|
|
impl GachaModel {
|
|
pub fn from_bin(gacha_bin: GachaModelBin) -> Self {
|
|
let result = Self { gacha_bin };
|
|
result.post_deserialize()
|
|
}
|
|
|
|
pub fn to_bin(&self) -> GachaModelBin {
|
|
self.gacha_bin.clone()
|
|
}
|
|
|
|
pub fn post_deserialize(mut self) -> GachaModel {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
for gacha_pool in gachaconf.character_gacha_pool_list.iter() {
|
|
let gacha_comp_bin = &mut self.gacha_bin;
|
|
let mut gacha_status_map = &mut gacha_comp_bin.gacha_status_map;
|
|
let status_bin = get_or_add(
|
|
&mut gacha_status_map,
|
|
&gacha_pool.sharing_guarantee_info_category,
|
|
);
|
|
for rarity_items in gacha_pool.gacha_items.iter() {
|
|
let progress_bin =
|
|
get_or_add(&mut status_bin.rarity_status_map, &rarity_items.rarity);
|
|
if progress_bin.pity <= 0 {
|
|
progress_bin.pity = 1;
|
|
}
|
|
for category_guarantee_policy_tag in
|
|
rarity_items.category_guarantee_policy_tags.iter()
|
|
{
|
|
get_or_add(
|
|
&mut progress_bin.categories_progress_map,
|
|
&category_guarantee_policy_tag,
|
|
);
|
|
}
|
|
}
|
|
for discount_policy_tag in gacha_pool.discount_policy_tags.iter() {
|
|
get_or_add(&mut status_bin.discount_usage_map, &discount_policy_tag);
|
|
}
|
|
}
|
|
self
|
|
}
|
|
|
|
pub fn perform_pull_pool<'bin, 'conf>(
|
|
&'bin mut self,
|
|
pull_time: &DateTime<Local>,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
) -> GachaRecordBin {
|
|
let mut gacha_bin = &mut self.gacha_bin;
|
|
let (rarity_items, progress_bin, status_bin, probability_model) =
|
|
determine_rarity(&gacha_bin, target_pool);
|
|
let (category_tag, category) = determine_category(rarity_items, progress_bin, target_pool);
|
|
let result =
|
|
determine_gacha_result(pull_time, category, target_pool, status_bin, rarity_items);
|
|
update_pity(&mut gacha_bin, rarity_items, probability_model, target_pool);
|
|
update_category_guarantee_info(&mut gacha_bin, rarity_items, &category_tag, target_pool);
|
|
update_discount(&mut gacha_bin, target_pool, &category_tag, rarity_items);
|
|
result
|
|
}
|
|
}
|
|
|
|
fn get_or_add<'a, K: Eq + PartialEq + Hash + Clone, V: Default, S: BuildHasher>(
|
|
map: &'a mut HashMap<K, V, S>,
|
|
key: &K,
|
|
) -> &'a mut V {
|
|
if !map.contains_key(key) {
|
|
map.insert(key.clone(), V::default());
|
|
}
|
|
map.get_mut(key).unwrap()
|
|
}
|
|
|
|
fn rand_rarity<'bin, 'conf>(
|
|
target_pool: &'conf CharacterGachaPool,
|
|
status_bin: &'bin GachaStatusBin,
|
|
) -> (
|
|
&'conf GachaAvailableItemsInfo,
|
|
&'bin GachaProgressBin,
|
|
&'conf ProbabilityModel,
|
|
) {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
let mut rng = thread_rng();
|
|
let rarity_status_map = &status_bin.rarity_status_map;
|
|
// gacha_items is already sorted by rarity descendingly in its post_configure.
|
|
for rarity_items in target_pool.gacha_items.iter() {
|
|
// Surely any judgement should be made on the current pity.
|
|
let progress_bin = rarity_status_map.get(&rarity_items.rarity).unwrap();
|
|
let pity = progress_bin.pity;
|
|
let probability_model = gachaconf
|
|
.probability_model_map
|
|
.get(&rarity_items.probability_model_tag)
|
|
.unwrap();
|
|
if rng.gen_range(0.0..100.0) <= probability_model.get_chance_percent(&pity) {
|
|
return (rarity_items, progress_bin, probability_model);
|
|
}
|
|
}
|
|
panic!("The user failed to get any items.");
|
|
}
|
|
|
|
fn determine_rarity<'bin, 'conf>(
|
|
gacha_bin: &'bin GachaModelBin,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
) -> (
|
|
&'conf GachaAvailableItemsInfo,
|
|
&'bin GachaProgressBin,
|
|
&'bin GachaStatusBin,
|
|
&'conf ProbabilityModel,
|
|
) {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
let status_bin = gacha_bin
|
|
.gacha_status_map
|
|
.get(&target_pool.sharing_guarantee_info_category)
|
|
.expect(&format!(
|
|
"post_deserialize forgot StatusBin/sharing_guarantee_info_category: {}",
|
|
target_pool.sharing_guarantee_info_category
|
|
));
|
|
let (mut rarity_items, mut progress_bin, mut probability_model) =
|
|
rand_rarity(target_pool, &status_bin);
|
|
|
|
// We should take AdvancedGuarantee discount into consideration.
|
|
for discount_tag in target_pool.discount_policy_tags.iter() {
|
|
if let Some(discount) = gachaconf
|
|
.discount_policies
|
|
.advanced_guarantee_map
|
|
.get(discount_tag)
|
|
{
|
|
if discount.rarity <= rarity_items.rarity {
|
|
continue;
|
|
}
|
|
if status_bin
|
|
.discount_usage_map
|
|
.get(discount_tag)
|
|
.expect(&format!(
|
|
"post_deserialize forgot StatusBin/discount_usage_map: {}",
|
|
discount_tag
|
|
))
|
|
>= &discount.use_limit
|
|
{
|
|
continue;
|
|
}
|
|
let higher_progress_bin =
|
|
status_bin
|
|
.rarity_status_map
|
|
.get(&discount.rarity)
|
|
.expect(&format!(
|
|
"post_deserialize forgot StatusBin/rarity_status_map: {}",
|
|
&discount.rarity
|
|
));
|
|
if higher_progress_bin.pity >= discount.guarantee_pity {
|
|
let mut found_rarity_items = false;
|
|
for gacha_items in target_pool.gacha_items.iter() {
|
|
if gacha_items.rarity == discount.rarity {
|
|
rarity_items = gacha_items;
|
|
probability_model = gachaconf
|
|
.probability_model_map
|
|
.get(&gacha_items.probability_model_tag)
|
|
.unwrap();
|
|
found_rarity_items = true;
|
|
break;
|
|
}
|
|
}
|
|
assert!(found_rarity_items, "Handle AdvancedGuarantee Discount ({discount_tag}) error: The target rarity does not exist in this pool.");
|
|
progress_bin = higher_progress_bin;
|
|
}
|
|
}
|
|
}
|
|
|
|
(rarity_items, progress_bin, status_bin, probability_model)
|
|
}
|
|
|
|
fn determine_category<'bin, 'conf>(
|
|
rarity_items: &'conf GachaAvailableItemsInfo,
|
|
progress_bin: &'bin GachaProgressBin,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
) -> (String, &'conf GachaCategoryInfo) {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
let mut category_tag_inited = false;
|
|
let mut category_tag_result: HashSet<String> = HashSet::new();
|
|
// First of all, if there's a chooseable category and
|
|
// it's can be triggered, then we MUST give that
|
|
// category's item.
|
|
// TODO: Only Genshin can do
|
|
// Then we should take a look at MustGainItem.
|
|
if !category_tag_inited {
|
|
for discount_policy_tag in target_pool.discount_policy_tags.iter() {
|
|
if let Some(discount) = gachaconf
|
|
.discount_policies
|
|
.must_gain_item_map
|
|
.get(discount_policy_tag)
|
|
{
|
|
if discount.rarity != rarity_items.rarity {
|
|
continue;
|
|
}
|
|
category_tag_result.insert(discount.category_tag.clone());
|
|
category_tag_inited = true;
|
|
}
|
|
}
|
|
}
|
|
// Otherwise, just select as normal.
|
|
if !category_tag_inited {
|
|
for tag in rarity_items.categories.keys() {
|
|
category_tag_result.insert(tag.clone());
|
|
}
|
|
for guarantee_policy_tag in rarity_items.category_guarantee_policy_tags.iter() {
|
|
let category_guarantee_policy = gachaconf
|
|
.category_guarantee_policy_map
|
|
.get(guarantee_policy_tag)
|
|
.unwrap();
|
|
let failure_times = progress_bin.categories_progress_map
|
|
.get(guarantee_policy_tag)
|
|
.expect(&format!("post_deserialize forgot StatusBin/rarity_status_map[{}]/categories_progress_map: {}", &rarity_items.rarity, guarantee_policy_tag));
|
|
if failure_times >= &category_guarantee_policy.trigger_on_failure_times {
|
|
category_tag_result = category_tag_result
|
|
.intersection(&category_guarantee_policy.included_category_tags)
|
|
.cloned()
|
|
.collect();
|
|
}
|
|
}
|
|
// category_tag_inited = true;
|
|
}
|
|
|
|
let mut categories: Vec<(String, &GachaCategoryInfo)> = vec![];
|
|
let mut weight_sum = 0;
|
|
for result_tag in category_tag_result {
|
|
let category = rarity_items.categories.get(&result_tag).unwrap();
|
|
categories.push((result_tag, category));
|
|
weight_sum += category.category_weight;
|
|
}
|
|
|
|
let randomnum = rand::thread_rng().gen_range(0..weight_sum);
|
|
let mut enumerated_ranges_end = 0;
|
|
for category in categories.into_iter() {
|
|
if randomnum <= enumerated_ranges_end + category.1.category_weight {
|
|
return (category.0, category.1);
|
|
}
|
|
enumerated_ranges_end += category.1.category_weight;
|
|
}
|
|
panic!("No category is chosen.");
|
|
}
|
|
|
|
fn determine_gacha_result<'bin, 'conf>(
|
|
pull_time: &DateTime<Local>,
|
|
category: &'conf GachaCategoryInfo,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
status_bin: &'bin GachaStatusBin,
|
|
rarity_items: &'conf GachaAvailableItemsInfo,
|
|
) -> GachaRecordBin {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
let item_pool_len = category.item_ids.len() as u32;
|
|
let item_id = category
|
|
.item_ids
|
|
.get(rand::thread_rng().gen_range(0..item_pool_len) as usize)
|
|
.unwrap();
|
|
let mut extra_item_id: u32 = 0;
|
|
let mut extra_item_count: u32 = 0;
|
|
|
|
for extra_items_policy_tag in rarity_items.extra_items_policy_tags.iter() {
|
|
let extra_items_policy = gachaconf
|
|
.extra_items_policy_map
|
|
.get(extra_items_policy_tag)
|
|
.unwrap();
|
|
// TODO: apply_on_owned_count in a context with bag
|
|
if extra_items_policy.apply_on_owned_count == 0 {
|
|
extra_item_id = extra_items_policy.id;
|
|
extra_item_count = extra_items_policy.count;
|
|
}
|
|
}
|
|
let extra_item_bin = GachaExtraItemBin {
|
|
extra_item_id,
|
|
extra_item_count,
|
|
currently_gained: 0,
|
|
};
|
|
GachaRecordBin {
|
|
pull_timestamp: pull_time.timestamp(),
|
|
obtained_item_id: item_id.clone(),
|
|
gacha_id: target_pool.gacha_schedule_id.clone(),
|
|
progress_map: status_bin.rarity_status_map.clone(),
|
|
extra_item_bin: Some(extra_item_bin),
|
|
}
|
|
}
|
|
|
|
fn update_pity<'bin, 'conf>(
|
|
gacha_bin: &'bin mut GachaModelBin,
|
|
rarity_items: &'conf GachaAvailableItemsInfo,
|
|
probability_model: &'conf ProbabilityModel,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
) {
|
|
let status_bin = gacha_bin
|
|
.gacha_status_map
|
|
.get_mut(&target_pool.sharing_guarantee_info_category)
|
|
.unwrap();
|
|
for (rarity, rarity_status) in status_bin.rarity_status_map.iter_mut() {
|
|
if (rarity == &rarity_items.rarity)
|
|
|| (probability_model.clear_status_on_higher_rarity_pulled
|
|
&& rarity < &rarity_items.rarity)
|
|
{
|
|
rarity_status.pity = 1;
|
|
} else {
|
|
rarity_status.pity += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn update_category_guarantee_info<'bin, 'conf>(
|
|
gacha_bin: &'bin mut GachaModelBin,
|
|
rarity_items: &'conf GachaAvailableItemsInfo,
|
|
category_tag: &String,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
) {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
let status_bin = gacha_bin
|
|
.gacha_status_map
|
|
.get_mut(&target_pool.sharing_guarantee_info_category)
|
|
.unwrap();
|
|
let progress_bin = status_bin
|
|
.rarity_status_map
|
|
.get_mut(&rarity_items.rarity)
|
|
.unwrap();
|
|
for policy_tag in rarity_items.category_guarantee_policy_tags.iter() {
|
|
let policy = gachaconf
|
|
.category_guarantee_policy_map
|
|
.get(policy_tag)
|
|
.unwrap();
|
|
// TODO: Chooseable guarantee not implemented
|
|
let prev_failure = progress_bin
|
|
.categories_progress_map
|
|
.get_mut(policy_tag)
|
|
.expect(&format!(
|
|
"post_deserialize forgot StatusBin/rarity_status_map[{}]/categories_progress_map: {}",
|
|
rarity_items.rarity, policy_tag
|
|
));
|
|
if policy.included_category_tags.contains(category_tag) {
|
|
*prev_failure = 0;
|
|
} else {
|
|
*prev_failure += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn update_discount<'bin, 'conf>(
|
|
gacha_bin: &'bin mut GachaModelBin,
|
|
target_pool: &'conf CharacterGachaPool,
|
|
category_tag: &String,
|
|
rarity_items: &GachaAvailableItemsInfo,
|
|
) {
|
|
let gachaconf = gacha::global_gacha_config();
|
|
for (policy_tag, policy) in gachaconf.discount_policies.must_gain_item_map.iter() {
|
|
if *category_tag != policy.category_tag {
|
|
continue;
|
|
}
|
|
if !target_pool.discount_policy_tags.contains(policy_tag) {
|
|
continue;
|
|
}
|
|
let status_bin = gacha_bin
|
|
.gacha_status_map
|
|
.get_mut(&target_pool.sharing_guarantee_info_category)
|
|
.unwrap();
|
|
let usage = status_bin.discount_usage_map.get_mut(policy_tag).unwrap();
|
|
if *usage < policy.use_limit {
|
|
*usage += 1;
|
|
}
|
|
}
|
|
for (policy_tag, policy) in gachaconf.discount_policies.advanced_guarantee_map.iter() {
|
|
if rarity_items.rarity != policy.rarity {
|
|
continue;
|
|
}
|
|
if !target_pool.discount_policy_tags.contains(policy_tag) {
|
|
continue;
|
|
}
|
|
let status_bin = gacha_bin
|
|
.gacha_status_map
|
|
.get_mut(&target_pool.sharing_guarantee_info_category)
|
|
.unwrap();
|
|
let usage = status_bin.discount_usage_map.get_mut(policy_tag).unwrap();
|
|
if *usage < policy.use_limit {
|
|
*usage += 1;
|
|
}
|
|
}
|
|
}
|