-
Notifications
You must be signed in to change notification settings - Fork 2
/
farming.js
88 lines (78 loc) · 3.1 KB
/
farming.js
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
window.addEventListener('DOMContentLoaded', (event) => {
const startFarmingButton = document.getElementById('start-farming-button');
const balanceElement = document.getElementById('balance-amount');
// Retrieve the user's balance from the database on load
const userId = Telegram.WebApp.initDataUnsafe.user.id;
fetch(`get_balance.php?user_id=${userId}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.success) {
balanceElement.textContent = data.balance;
} else {
console.error('Error fetching balance:', data.error);
}
})
.catch(error => console.error('Error fetching balance:', error));
// Check if the user has already started farming within the last 24 hours
const lastFarmingTime = localStorage.getItem('lastFarmingTime');
if (lastFarmingTime && new Date().getTime() - new Date(lastFarmingTime).getTime() < 24 * 60 * 60 * 1000) {
startFarmingButton.disabled = true;
startFarmingButton.textContent = 'Farming in progress...';
}
startFarmingButton.addEventListener('click', () => {
startFarming();
});
});
function startFarming() {
const button = document.getElementById('start-farming-button');
const balanceElement = document.getElementById('balance-amount');
let balance = parseInt(balanceElement.textContent);
let farmedAmount = 0;
button.disabled = true; // Disable the button to prevent multiple clicks
button.textContent = 'Farming in progress...';
const farmingInterval = setInterval(() => {
const reward = 100; // 100 tokens per minute
farmedAmount += reward;
balanceElement.textContent = balance + farmedAmount;
}, 60000); // 1 minute
setTimeout(() => {
clearInterval(farmingInterval);
balance += farmedAmount;
balanceElement.textContent = balance;
uploadBalance(balance);
button.disabled = false; // Enable the button after 24 hours
button.textContent = 'Start Farming';
localStorage.setItem('lastFarmingTime', new Date().toISOString());
}, 12 * 60 * 60 * 1000); // 12 hours
// Update button text to show the farming amount
button.textContent = 'Farming...';
}
function uploadBalance(balance) {
const userId = Telegram.WebApp.initDataUnsafe.user.id;
fetch('update_balance.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ user_id: userId, balance: balance })
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.success) {
alert('Balance updated successfully!');
} else {
console.error('Error updating balance:', data.error);
}
})
.catch(error => console.error('Error uploading balance:', error));
}