-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
125 lines (97 loc) · 2.65 KB
/
functions.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
122
123
124
125
<?php
define('SPIN_DELAY', 3);
session_start();
if (!isset($_SESSION['lastSpin'])) {
$_SESSION['balance'] = 0;
$_SESSION['log'] = array();
$_SESSION['lastSpin'] = -1;
}
if (isset($_GET['func'])) {
switch ($_GET['func']) {
case 'spin': {
$date = time();
if ($_SESSION['lastSpin'] !== -1 && $date - $_SESSION['lastSpin'] < SPIN_DELAY)
exit("0");
if (!$_SESSION['balance'])
exit("0");
$bet = min(floor(+$_GET['bet'] / 10) * 10, $_SESSION['balance'], 100);
if ($bet <= 0)
exit("0");
$spinRes = spin($bet);
$_SESSION['balance'] += $spinRes['res'];
$_SESSION['lastSpin'] = $date;
array_push($_SESSION['log'], [
'date' => $date,
'bet' => $bet,
'combination' => $spinRes['combination'],
'res' => $spinRes['res']
]);
echo json_encode(end($_SESSION['log']));
break;
}
case 'getBalance': {
echo json_encode($_SESSION['balance']);
break;
}
case 'getLog': {
echo json_encode($_SESSION['log']);
break;
}
case 'startNewGame': {
if (!$_SESSION['balance'] || $_GET['password'] === "davayponovoy")
startNewGame();
echo json_encode($_SESSION['balance']);
}
}
}
function startNewGame() {
$_SESSION['balance'] = 100;
$_SESSION['log'] = array(); // or unset ?
}
function spin($bet) {
$rand = mt_rand(1, 20);
if ($rand < 11) {
// nothing
$combination[0] = mt_rand(1, 7);
$combination[1] = mt_rand(1, 7);
$combination[2] = mt_rand(1, 7);
while ($combination[0] == $combination[1]) {
$combination[1] = mt_rand(1,7);
}
while ($combination[0] == $combination[2] || $combination[1] == $combination[2]) {
$combination[2] = mt_rand(1,7);
}
} else {
// match
$num = getNum();
$combination = array($num, $num, $num);
if ($rand < 18) {
// match of 2
$differentNum = mt_rand(0, 2);
$combination[$differentNum] = mt_rand(1,7);
while ($combination[$differentNum] == $num) {
$combination[$differentNum] = mt_rand(1,7);
}
}
}
$res = calcResult($combination, $bet);
return array('combination' => (int) implode('', $combination), 'res' => $res);
}
function getNum() {
for ($i = 1; $i < 7; $i++) {
$num = mt_rand(0, 2);
if (!$num)
return $i;
}
return 7;
}
function calcResult($combination, $bet) {
if ($combination[0] == $combination[1] && $combination[0] == $combination[2]) {
$res = $bet * $combination[0] * 2 - $bet; // match of 3
} elseif ($combination[0] == $combination[1] || $combination[0] == $combination[2] || $combination[1] == $combination[2]) {
$num = ($combination[0] == $combination[1]) ? $combination[0] : $combination[2];
$res = round(($bet * $num / 2) / 10) * 10 - $bet; // match of 2
} else
$res = -$bet;
return $res;
}