This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbonus.php
122 lines (97 loc) · 2.8 KB
/
bonus.php
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
112
113
114
115
116
117
118
119
120
121
<?php
require_once 'autoload.php';
use Tracker\Bonus;
use Tracker\User\Details as UserDetails;
use Tracker\Traffic;
require 'include/bittorrent_announce.php';
require 'shutdown.php';
require 'login.php';
function getParam($name) {
return $_REQUEST[$name] ?? '';
}
function calcRatio($n) {
return pow(8, max(1, 20000000000 / $n)) / 2;
}
$user = login();
if ($user == false) {
trigger_error('403', E_USER_ERROR);
exit();
}
$torrent = 0 + intval(getParam('torrent'));
$method = getParam('method') ?: 'query';
$yesterday = strtotime('-1 day');
$lastMonth = $yesterday - Traffic::DefaultTimeGap;
if ($method == 'query') {
$t = new Traffic();
$traffics = $t->getUserAvailableTraffic($user['id'], $torrent, $lastMonth, $yesterday);
print json_encode([
'stt' => $lastMonth,
'end' => $yesterday,
'traffics' => $traffics,
]);
exit();
} else if ($method == 'update') {
$t = new Traffic();
$traffics = $t->getUserAvailableTraffic($user['id'], $torrent, $lastMonth, $yesterday);
$result = $t->updateUserTrafficStatus($user['id'], $torrent, $lastMonth, $yesterday);
if ($result != false) {
print json_encode(['error' => $result]);
die();
}
$b = new Bonus();
$result = $b->updateUserSeedBonus($user['id'], $traffics);
if ($result != 1 && $result != 0) {
// TODO: log error
$result = [$result, 'please connact admin'];
print json_encode(['error' => $result]);
die();
}
$updateResult = $b->result;
print json_encode([
'stt' => $lastMonth,
'end' => $yesterday,
'traffics' => $traffics,
'bonus' => $updateResult['bonus'],
'sum' => $updateResult['sum'],
]);
exit();
} else if ($method == 'exchange') {
$n = 0 + intval(getParam('n'));
$heal = getParam('heal');
if ($n < 100) {
trigger_error('Can\' less than 100', E_USER_ERROR);
exit();
}
$b = new Bonus();
if ($heal) {
if (!$totalBonus = $Cache->get_value("tracker-total-bonus")) {
$totalBonus = $b->getTotalBonus();
$Cache->cache_value("tracker-total-bonus", $totalBonus, 1801);
}
// $totalHP = $b->getTotalHP();
$ratio = calcRatio($totalBonus);
$myBonus = $b->getBonus($user['id']);
$bonusCost = ceil($n * $ratio);
if ($bonusCost > $myBonus) {
$result = ['n' => 0, 'cost' => 0];
} else {
$b->updateBonus($user['id'], -$bonusCost);
$b->updateHP($user['id'], $n);
$result = ['n' => $n, 'cost' => $bonusCost];
}
} else {
$myHP = $b->getHP($user['id']);
$hpCost = ceil($n / 1.414);
if ($hpCost > $myHP) {
$result = ['n' => 0, 'cost' => 0];
} else {
$b->updateHP($user['id'], -$hpCost);
$b->updateBonus($user['id'], $n);
$result = ['n' => $n, 'cost' => $hpCost];
}
}
$d = new UserDetails();
$d->getTrackerInfo($user['id'], true);
print json_encode($result);
exit();
}