mobhub
Decentralized model and agent weights distribution network
Brought by drakestapleton
| 1 | //! MobWorth: Pure faction valuation engine in native Rust. |
| 2 | |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | use serde_json::Value; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 7 | pub struct ValuationBreakdown { |
| 8 | pub respect_value: i64, |
| 9 | pub faction_cash_value: i64, |
| 10 | pub armory_value: i64, |
| 11 | pub total_value: i64, |
| 12 | } |
| 13 | |
| 14 | fn coerce_number(v: &Value) -> Option<f64> { |
| 15 | match v { |
| 16 | Value::Number(n) => n.as_f64(), |
| 17 | Value::String(s) => s.trim().parse::<f64>().ok(), |
| 18 | _ => None, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// Parse faction respect from Torn v2 API payload. |
| 23 | pub fn parse_faction_respect(data: &Value) -> Option<i64> { |
| 24 | let raw = data |
| 25 | .get("basic") |
| 26 | .and_then(|b| b.get("respect")) |
| 27 | .or_else(|| data.get("respect")); |
| 28 | |
| 29 | let num = raw.and_then(coerce_number)?; |
| 30 | if num < 0.0 { |
| 31 | return None; |
| 32 | } |
| 33 | Some(num.round() as i64) |
| 34 | } |
| 35 | |
| 36 | /// Parse faction's OWN spendable vault cash. |
| 37 | /// Torn balance endpoint reports as total held in vault, |
| 38 | /// and as member deposits. |
| 39 | /// Spendable faction cash is vault total minus member deposits. |
| 40 | pub fn parse_faction_cash(data: &Value) -> Option<i64> { |
| 41 | let balance = data.get("balance")?; |
| 42 | let faction_money = balance |
| 43 | .get("faction") |
| 44 | .and_then(|f| f.get("money")) |
| 45 | .and_then(coerce_number)?; |
| 46 | |
| 47 | let mut member_deposits = 0.0; |
| 48 | if let Some(members) = balance.get("members").and_then(|m| m.as_array()) { |
| 49 | for m in members { |
| 50 | if let Some(owed) = m.get("money").and_then(coerce_number) { |
| 51 | member_deposits += owed; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | let net = (faction_money - member_deposits).max(0.0); |
| 57 | Some(net.round() as i64) |
| 58 | } |
| 59 | |
| 60 | /// Computes overall faction valuation. |
| 61 | /// Formula: Respect * Rate + Armory + Faction Net Cash |
| 62 | pub fn compute_valuation( |
| 63 | respect: f64, |
| 64 | respect_rate: f64, |
| 65 | armory_value: f64, |
| 66 | faction_cash: f64, |
| 67 | ) -> ValuationBreakdown { |
| 68 | let r = respect.max(0.0); |
| 69 | let rate = respect_rate.max(0.0); |
| 70 | let armory = armory_value.max(0.0).round() as i64; |
| 71 | let cash = faction_cash.max(0.0).round() as i64; |
| 72 | let respect_val = (r * rate).round() as i64; |
| 73 | |
| 74 | ValuationBreakdown { |
| 75 | respect_value: respect_val, |
| 76 | faction_cash_value: cash, |
| 77 | armory_value: armory, |
| 78 | total_value: respect_val + armory + cash, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[cfg(test)] |
| 83 | mod tests { |
| 84 | use super::*; |
| 85 | use serde_json::json; |
| 86 | |
| 87 | #[test] |
| 88 | fn test_compute_valuation() { |
| 89 | let v = compute_valuation(10000.0, 180284.0, 50000000.0, 25000000.0); |
| 90 | assert_eq!(v.respect_value, 1802840000); |
| 91 | assert_eq!(v.armory_value, 50000000); |
| 92 | assert_eq!(v.faction_cash_value, 25000000); |
| 93 | assert_eq!(v.total_value, 1802840000 + 50000000 + 25000000); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_parse_faction_cash() { |
| 98 | let json_data = json!({ |
| 99 | "balance": { |
| 100 | "faction": { "money": 1000000 }, |
| 101 | "members": [ |
| 102 | { "id": 1, "money": 300000 }, |
| 103 | { "id": 2, "money": 200000 } |
| 104 | ] |
| 105 | } |
| 106 | }); |
| 107 | let cash = parse_faction_cash(&json_data).unwrap(); |
| 108 | assert_eq!(cash, 500000); |
| 109 | } |
| 110 | } |