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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
+
logger = logging.getLogger(__name__)
|
| 5 |
+
|
| 6 |
+
# ============================================================
|
| 7 |
+
# 品质系统
|
| 8 |
+
# ============================================================
|
| 9 |
+
TIERS_LIST = ['gold', 'red', 'pink', 'purple', 'blue']
|
| 10 |
+
# Valve 官方公布概率
|
| 11 |
+
TIERS_WEIGHTS = [0.26, 0.64, 3.20, 15.98, 79.92]
|
| 12 |
+
TIER_INFO = {
|
| 13 |
+
'gold': ("罕见级", "#FFD700"),
|
| 14 |
+
'red': ("隐秘级", "#EB4B4B"),
|
| 15 |
+
'pink': ("保密级", "#D32CE6"),
|
| 16 |
+
'purple': ("受限级", "#8847FF"),
|
| 17 |
+
'blue': ("军规级", "#4B69FF")
|
| 18 |
+
}
|
| 19 |
+
DEFAULT_ITEM_PRICES = {'gold': 2000.0, 'red': 200.0, 'pink': 50.0, 'purple': 15.0, 'blue': 1.0}
|
| 20 |
+
|
| 21 |
+
# ============================================================
|
| 22 |
+
# StatTrak™ 系统
|
| 23 |
+
# ============================================================
|
| 24 |
+
STATTRAK_CHANCE = 0.10 # 10% 出 StatTrak
|
| 25 |
+
STATTRAK_MULTIPLIER = 2.0 # 非金 StatTrak 价格翻倍
|
| 26 |
+
STATTRAK_MULTIPLIER_GOLD = 1.5 # 金色 StatTrak 1.5 倍
|
| 27 |
+
|
| 28 |
+
# ============================================================
|
| 29 |
+
# 磨损度系统
|
| 30 |
+
# ============================================================
|
| 31 |
+
WEAR_LEVELS = [
|
| 32 |
+
("崭新出厂", 0.00, 0.07, 1.40),
|
| 33 |
+
("略有磨损", 0.07, 0.15, 1.15),
|
| 34 |
+
("久经沙场", 0.15, 0.38, 1.00),
|
| 35 |
+
("破损不堪", 0.38, 0.45, 0.82),
|
| 36 |
+
("战痕累累", 0.45, 1.00, 0.65),
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _is_weapon(item):
|
| 41 |
+
"""只有武器皮肤才算"""
|
| 42 |
+
n = str(item.get('name', ''))
|
| 43 |
+
# 黑名单(涂鸦/音乐盒/收藏品——名字可能含 | 但不是武器)
|
| 44 |
+
blacklist = ["柠檬汁", "中华美食", "虎虎生风", "跳跳先生", "功夫少年",
|
| 45 |
+
"卡哇伊杀手", "独角兽", "口袋烧烤", "剃刀大师"]
|
| 46 |
+
for b in blacklist:
|
| 47 |
+
if b in n:
|
| 48 |
+
return False
|
| 49 |
+
return " | " in n or "★" in n
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _roll_wear():
|
| 53 |
+
"""随机生成磨损度,偏向中间值"""
|
| 54 |
+
return round(random.triangular(0.0, 1.0, 0.25), 4)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _get_wear_info(wear_value: float):
|
| 58 |
+
for name, lo, hi, mult in WEAR_LEVELS:
|
| 59 |
+
if lo <= wear_value < hi or (hi == 1.00 and wear_value <= hi):
|
| 60 |
+
return name, wear_value, mult
|
| 61 |
+
return "久经沙场", wear_value, 1.0
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# ============================================================
|
| 65 |
+
# 价格索引缓存(O(1) 查找,替代 O(n) 遍历 10000+ 条目)
|
| 66 |
+
# ============================================================
|
| 67 |
+
_price_index = None # {cn_name: price}
|
| 68 |
+
_gold_items_cache = None # 金色物品列表
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _build_price_index(price_dict: list):
|
| 72 |
+
"""首次调用时构建 {name: price} 索引 + 金色物品列表"""
|
| 73 |
+
global _price_index, _gold_items_cache
|
| 74 |
+
if _price_index is not None:
|
| 75 |
+
return
|
| 76 |
+
_price_index = {}
|
| 77 |
+
_gold_items_cache = []
|
| 78 |
+
for item in price_dict:
|
| 79 |
+
cn = item.get('cn_name', '')
|
| 80 |
+
if cn:
|
| 81 |
+
_price_index[cn] = item['price']
|
| 82 |
+
if any(kw in cn for kw in ["刀", "匕首", "手套", "★"]):
|
| 83 |
+
_gold_items_cache.append(item)
|
| 84 |
+
# 去重
|
| 85 |
+
_gold_items_cache = list({i['cn_name']: i for i in _gold_items_cache}.values())
|
| 86 |
+
logger.info(f"[Index] 价格索引就绪: {len(_price_index)} 条目, {len(_gold_items_cache)} 金色物品")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def generate_random_case_drops(count: int, affordable_cases: list,
|
| 90 |
+
affordable_capsules: list, price_cn_map: dict,
|
| 91 |
+
price_dict: list):
|
| 92 |
+
# 构建索引(仅首次)
|
| 93 |
+
_build_price_index(price_dict)
|
| 94 |
+
opened_items = []
|
| 95 |
+
total_cost = 0.0
|
| 96 |
+
total_earned = 0.0
|
| 97 |
+
|
| 98 |
+
for _ in range(count):
|
| 99 |
+
drop = _simulate_single_drop(affordable_cases, affordable_capsules,
|
| 100 |
+
price_cn_map, price_dict)
|
| 101 |
+
if not drop:
|
| 102 |
+
continue
|
| 103 |
+
opened_items.append(drop)
|
| 104 |
+
total_cost += drop['cost']
|
| 105 |
+
total_earned += drop['price']
|
| 106 |
+
|
| 107 |
+
return opened_items, total_cost, total_earned
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _simulate_single_drop(cases: list, capsules: list, price_cn_map: dict,
|
| 111 |
+
price_dict: list):
|
| 112 |
+
# 只开武器箱
|
| 113 |
+
is_capsule = False
|
| 114 |
+
valid_crates = cases
|
| 115 |
+
if not valid_crates:
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
crate = random.choice(valid_crates)
|
| 119 |
+
cost = _calculate_cost(crate, is_capsule, price_cn_map)
|
| 120 |
+
tiers = crate.get('precomputed_tiers', _categorize_items_by_tier_fallback(crate))
|
| 121 |
+
|
| 122 |
+
selected_tier = random.choices(TIERS_LIST, weights=TIERS_WEIGHTS, k=1)[0]
|
| 123 |
+
|
| 124 |
+
# 从选中的 tier 里剔除涂鸦/收藏品/音乐盒
|
| 125 |
+
weapon_tiers = {}
|
| 126 |
+
for t in TIERS_LIST:
|
| 127 |
+
weapon_tiers[t] = [i for i in tiers.get(t, []) if _is_weapon(i)]
|
| 128 |
+
|
| 129 |
+
# 如果该 tier 被滤空,找最近的非空 tier
|
| 130 |
+
check_tier = selected_tier
|
| 131 |
+
for _ in range(len(TIERS_LIST)):
|
| 132 |
+
if weapon_tiers.get(check_tier):
|
| 133 |
+
selected_tier = check_tier
|
| 134 |
+
break
|
| 135 |
+
idx = TIERS_LIST.index(check_tier)
|
| 136 |
+
check_tier = TIERS_LIST[(idx + 1) % len(TIERS_LIST)]
|
| 137 |
+
|
| 138 |
+
# 万一全空(不太可能),回退原始 tiers
|
| 139 |
+
if not any(weapon_tiers.values()):
|
| 140 |
+
weapon_tiers = tiers
|
| 141 |
+
|
| 142 |
+
raw_item, final_tier = _pick_item(crate, weapon_tiers, selected_tier, is_capsule, price_dict)
|
| 143 |
+
|
| 144 |
+
if not raw_item:
|
| 145 |
+
logger.error(f"[Open] 掉落池为空 - 箱子: {crate.get('name')}")
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
item_name, item_price = _determine_final_item(raw_item, final_tier, price_dict)
|
| 149 |
+
|
| 150 |
+
# 判断是否为武器皮肤(非印花/布章/贴纸)
|
| 151 |
+
is_weapon_skin = (
|
| 152 |
+
" | " in item_name or "刀" in item_name or "匕首" in item_name
|
| 153 |
+
or "手套" in item_name or "★" in item_name
|
| 154 |
+
)
|
| 155 |
+
is_stattrak = False
|
| 156 |
+
wear_name = ""
|
| 157 |
+
wear_float = 0.0
|
| 158 |
+
|
| 159 |
+
if is_weapon_skin:
|
| 160 |
+
# ── StatTrak ──────────────────────────────────
|
| 161 |
+
is_stattrak = random.random() < STATTRAK_CHANCE
|
| 162 |
+
if is_stattrak:
|
| 163 |
+
mult = STATTRAK_MULTIPLIER_GOLD if final_tier == 'gold' else STATTRAK_MULTIPLIER
|
| 164 |
+
item_price *= mult
|
| 165 |
+
item_name = f"StatTrak™ {item_name}"
|
| 166 |
+
|
| 167 |
+
# ── 磨损度 ─────────────────────────────────────
|
| 168 |
+
wear_float = _roll_wear()
|
| 169 |
+
wear_name, _, wear_mult = _get_wear_info(wear_float)
|
| 170 |
+
item_price *= wear_mult
|
| 171 |
+
item_name = f"{item_name} ({wear_name})"
|
| 172 |
+
|
| 173 |
+
item_price = round(item_price, 2)
|
| 174 |
+
rarity_name, color_code = TIER_INFO.get(final_tier, TIER_INFO['blue'])
|
| 175 |
+
|
| 176 |
+
# 如果出金且 StatTrak,品质显示用金色
|
| 177 |
+
if final_tier == 'gold' and is_stattrak:
|
| 178 |
+
rarity_name = "罕见级 ★"
|
| 179 |
+
elif is_stattrak:
|
| 180 |
+
rarity_name = f"{rarity_name} ST"
|
| 181 |
+
|
| 182 |
+
return {
|
| 183 |
+
'crate_name': crate.get('name'),
|
| 184 |
+
'item_name': item_name,
|
| 185 |
+
'price': item_price,
|
| 186 |
+
'cost': cost,
|
| 187 |
+
'tier': final_tier,
|
| 188 |
+
'rarity_name': rarity_name,
|
| 189 |
+
'color': color_code,
|
| 190 |
+
'stattrak': is_stattrak,
|
| 191 |
+
'wear': wear_name,
|
| 192 |
+
'wear_float': wear_float if is_weapon_skin else 0,
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def _calculate_cost(crate: dict, is_capsule: bool, price_cn_map: dict) -> float:
|
| 197 |
+
name = crate.get('name')
|
| 198 |
+
price_data = price_cn_map.get(name)
|
| 199 |
+
market_price = price_data['price'] if price_data else (1.5 if is_capsule else 5.0)
|
| 200 |
+
ctype = (crate.get('type') or '').lower()
|
| 201 |
+
is_key = (ctype == 'case' or '箱' in name or 'case' in name.lower())
|
| 202 |
+
return market_price + (17.5 if is_key else 0.0)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def _categorize_items_by_tier_fallback(crate: dict) -> dict:
|
| 206 |
+
tiers = {t: [] for t in TIERS_LIST}
|
| 207 |
+
for item in crate.get('contains', []):
|
| 208 |
+
rarity = str(item.get('rarity', {}).get('name', '') if isinstance(item.get('rarity'), dict) else item.get('rarity', ''))
|
| 209 |
+
name = str(item.get('name', ''))
|
| 210 |
+
if any(k in rarity for k in ['违禁', 'Contraband']) or any(k in name for k in ['★', '纯金', 'Gold', '金色']):
|
| 211 |
+
tiers['gold'].append(item)
|
| 212 |
+
elif any(k in rarity for k in ['隐秘', 'Covert', '非凡', 'Extraordinary', '大师', 'Master']):
|
| 213 |
+
tiers['red'].append(item)
|
| 214 |
+
elif any(k in rarity for k in ['保密', 'Classified', '奇异', 'Exotic']):
|
| 215 |
+
tiers['pink'].append(item)
|
| 216 |
+
elif any(k in rarity for k in ['受限', 'Restricted', '卓越', 'Remarkable', 'Exceptional']):
|
| 217 |
+
tiers['purple'].append(item)
|
| 218 |
+
else:
|
| 219 |
+
tiers['blue'].append(item)
|
| 220 |
+
return tiers
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def _pick_item(crate: dict, tiers: dict, selected_tier: str, is_capsule: bool,
|
| 224 |
+
price_dict: list):
|
| 225 |
+
if selected_tier == 'gold':
|
| 226 |
+
rare_items = [i for i in crate.get('contains_rare', []) if _is_weapon(i)]
|
| 227 |
+
if rare_items:
|
| 228 |
+
return random.choice(rare_items), 'gold'
|
| 229 |
+
elif tiers['gold']:
|
| 230 |
+
return random.choice(tiers['gold']), 'gold'
|
| 231 |
+
elif not is_capsule and _gold_items_cache:
|
| 232 |
+
return random.choice(_gold_items_cache), 'gold'
|
| 233 |
+
selected_tier = 'red'
|
| 234 |
+
|
| 235 |
+
if not tiers[selected_tier]:
|
| 236 |
+
fallback_order = ['red', 'pink', 'purple', 'blue', 'gold']
|
| 237 |
+
start_idx = fallback_order.index(selected_tier)
|
| 238 |
+
search_list = fallback_order[start_idx:] + fallback_order[:start_idx]
|
| 239 |
+
for t in search_list:
|
| 240 |
+
if tiers[t]:
|
| 241 |
+
return random.choice(tiers[t]), t
|
| 242 |
+
|
| 243 |
+
return random.choice(tiers[selected_tier]) if tiers[selected_tier] else None, selected_tier
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def _determine_final_item(raw_item, tier: str, price_dict: list):
|
| 247 |
+
if isinstance(raw_item, dict) and 'cn_name' in raw_item and 'price' in raw_item:
|
| 248 |
+
return raw_item['cn_name'], raw_item['price']
|
| 249 |
+
|
| 250 |
+
base_name = str(raw_item.get('name', '')) if raw_item else ""
|
| 251 |
+
if not base_name:
|
| 252 |
+
return "未知物品", DEFAULT_ITEM_PRICES.get(tier, 1.0)
|
| 253 |
+
|
| 254 |
+
# ── 仅精确匹配 ──
|
| 255 |
+
if _price_index and base_name in _price_index:
|
| 256 |
+
return base_name, _price_index[base_name]
|
| 257 |
+
|
| 258 |
+
# ── 默认品质价 ──
|
| 259 |
+
fallback = DEFAULT_ITEM_PRICES.get(tier, 1.0) * random.uniform(0.8, 1.5)
|
| 260 |
+
return base_name, round(fallback, 2)
|