-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgacha_allin.php
More file actions
111 lines (96 loc) · 3.97 KB
/
Copy pathgacha_allin.php
File metadata and controls
111 lines (96 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/init_check.php';
require_once __DIR__ . '/core/Session.php';
require_once __DIR__ . '/core/TokenSystem.php';
require_once __DIR__ . '/core/StockEngine.php';
require_once __DIR__ . '/core/GachaEngine.php';
require_once __DIR__ . '/core/TradingEngine.php';
require_once __DIR__ . '/core/PoolEngine.php';
require_once __DIR__ . '/core/MarketEngine.php';
require_once __DIR__ . '/adapters/manager.php';
Session::start();
require_once __DIR__ . '/core/AutoJob.php';
AutoJob::run();
Session::requireAuth();
header('Content-Type: application/json; charset=utf-8');
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$poolId = (int)($input['pool_id'] ?? 0);
$userId = Session::userId();
$userId = Session::userId();
$db = Database::getInstance();
// Check if all-in is enabled
if (platform_config('system', 'allin_enabled', '1') !== '1') {
echo json_encode(['success' => false, 'message' => '梭哈功能已关闭。']);
exit;
}
// Cooldown check: 30 minutes
$stmt = $db->prepare("SELECT created_at FROM gacha_logs WHERE user_id = ? AND pull_type = 'allin' ORDER BY created_at DESC LIMIT 1");
$stmt->execute([$userId]);
$lastAllin = $stmt->fetch();
if ($lastAllin) {
$lastTime = strtotime($lastAllin['created_at']);
$elapsed = time() - $lastTime;
if ($elapsed < 1800) {
$remaining = ceil((1800 - $elapsed) / 60);
echo json_encode(['success' => false, 'message' => "梭哈冷却中,请 {$remaining} 分钟后再试。"]);
exit;
}
}
$isKs = is_kaleidoscope();
$balance = $isKs ? TokenSystem::getKaleidoscopeBalance($userId) : TokenSystem::getBalance($userId);
$spendable = $balance - ($balance % 10);
$spendable = min($spendable, 5000000);
$cost = GACHA_SINGLE_COST;
$unit = $isKs ? 'SKYT' : '代币';
if ($spendable < $cost) {
echo json_encode(['success' => false, 'message' => "{$unit}不足,至少需要 {$cost} {$unit}才能梭哈。"]);
exit;
}
$count = floor($spendable / $cost);
$totalCost = $count * $cost;
// Deduct tokens directly
if ($isKs) {
$db->prepare("UPDATE users SET kaleidoscope_balance = kaleidoscope_balance - ? WHERE id = ?")->execute([$totalCost, $userId]);
} else {
$db->prepare("UPDATE users SET token_balance = token_balance - ?, total_spent = total_spent + ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?")->execute([$totalCost, $totalCost, $userId]);
}
$results = [];
$legendaryCount = 0;
for ($i = 0; $i < $count; $i++) {
if ($poolId > 0) {
$stock = PoolEngine::getRandomStockFromPool($poolId);
} else {
$weights = GachaEngine::getRarityWeights();
$rarity = GachaEngine::rollFromWeights($weights);
$stock = GachaEngine::selectStockByRarity($rarity);
if (!$stock) $stock = GachaEngine::selectStockByRarity('common');
}
if (!$stock) continue;
$stockId = (int)$stock['id'];
$rarity = $stock['rarity'] ?? 'common';
GachaEngine::creditHolding($userId, $stockId);
$db->prepare("INSERT INTO gacha_logs (user_id, stock_id, rarity, pull_type, cost) VALUES (?, ?, ?, 'allin', ?)")
->execute([$userId, $stockId, $rarity, $cost]);
if ($rarity === 'legendary') $legendaryCount++;
$results[] = [
'id' => $stockId,
'symbol' => $stock['symbol'] ?? '?',
'name' => $stock['name'] ?? '?',
'rarity' => $rarity,
'rarity_name' => GachaEngine::RARITY_NAMES[$rarity] ?? 'Common',
'rarity_color' => GachaEngine::RARITY_COLORS[$rarity] ?? '#aaa',
'price' => (float)($stock['current_price'] ?? 0),
];
}
$newBalance = $isKs ? TokenSystem::getKaleidoscopeBalance($userId) : TokenSystem::getBalance($userId);
$msg = "梭哈完成!共抽 {$count} 次,消耗 {$totalCost} 代币。";
if ($legendaryCount > 0) $msg .= " 🌟 获得 {$legendaryCount} 张传说卡牌!";
echo json_encode([
'success' => true,
'results' => $results,
'balance_remaining' => $newBalance,
'message' => $msg,
]);
exit;