Skip to content

Commit 6a5ac33

Browse files
committed
award bonus points for each gb uploaded
1 parent 4ec8825 commit 6a5ac33

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ All configuration is provided via a single JavaScript file named `config.js`. Th
6868

6969
A full list of configuration options is below. All are required.
7070

71+
If your configuration is not valid, sqtracker will fail to start.
72+
7173
| Key | Type | Example | Description |
7274
|----------------------------|---------|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7375
| SQ_SITE_NAME | envs | sqtracker demo | The name of your tracker site |
7476
| SQ_SITE_DESCRIPTION | envs | My very own private tracker | A short description of your tracker site |
7577
| SQ_ALLOW_REGISTER | envs | `invite` | Registration mode. Either `open`, `invite` or `closed` |
7678
| SQ_ALLOW_ANONYMOUS_UPLOADS | envs | `false` | Whether or not users can upload torrents anonymously. Either `true` or `false` |
7779
| SQ_MINIMUM_RATIO | envs | 0.75 | Minimum allowed ratio. Below this users will not be able to download |
80+
| SQ_BP_PER_GB | envs | 1 | Number of bonus points awarded to a user for each GB they upload |
7881
| SQ_TORRENT_CATEGORIES | envs | `["Movies", "TV"]` | An array of categories available on your tracker site |
7982
| SQ_BASE_URL | envs | https://demo.sqtracker.dev | The URL of your tracker site |
8083
| SQ_API_URL | envs | https://demo.sqtracker.dev/api | The URL of your API. Under the recommended setup, it should be `${SQ_BASE_URL}/api` |

api/src/middleware/announce.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import Torrent from '../schema/torrent'
55
import Progress from '../schema/progress'
66
import { getUserRatio } from '../utils/ratio'
77

8+
const BYTES_GB = 1.074e9
9+
810
export const binaryToHex = (b) => Buffer.from(b, 'binary').toString('hex')
911
export const hexToBinary = (h) => Buffer.from(h, 'hex').toString('binary')
1012

@@ -71,6 +73,44 @@ const handleAnnounce = async (req, res, next) => {
7173
return
7274
}
7375

76+
/*
77+
Bonus points: determine how much the user has uploaded, and whether or not this announce will take their total
78+
uploaded amount into the next whole GiB. If so, increment the users bonus points by the configured amount.
79+
*/
80+
81+
const [sumUploaded] = await Progress.aggregate([
82+
{
83+
$match: {
84+
userId: user._id,
85+
},
86+
},
87+
{
88+
$group: {
89+
_id: 'uploaded',
90+
bytes: { $sum: '$uploaded' },
91+
},
92+
},
93+
])
94+
95+
const { bytes } = sumUploaded ?? { bytes: 0 }
96+
const nextGb = Math.max(Math.ceil(bytes / BYTES_GB), 1)
97+
98+
const prevProgressRecord = await Progress.findOne({
99+
userId: user._id,
100+
infoHash,
101+
}).lean()
102+
const alreadyUploaded = prevProgressRecord?.uploaded ?? 0
103+
const uploadDelta = params.uploaded - alreadyUploaded
104+
105+
if ((bytes + uploadDelta) / BYTES_GB >= nextGb) {
106+
await User.findOneAndUpdate(
107+
{ _id: user._id },
108+
{ $inc: { bonusPoints: process.env.SQ_BP_PER_GB } }
109+
)
110+
}
111+
112+
// update the progress report for this user/torrent pair
113+
74114
await Progress.findOneAndUpdate(
75115
{ userId: user._id, infoHash },
76116
{
@@ -80,7 +120,7 @@ const handleAnnounce = async (req, res, next) => {
80120
uploaded: params.uploaded,
81121
downloaded:
82122
torrent.freeleech || process.env.SQ_SITE_WIDE_FREELEECH === true
83-
? 0
123+
? prevProgressRecord?.downloaded ?? 0
84124
: params.downloaded,
85125
left: params.left,
86126
},

api/src/utils/validateConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const configSchema = yup
1616
.required(),
1717
SQ_ALLOW_ANONYMOUS_UPLOADS: yup.boolean().required(),
1818
SQ_MINIMUM_RATIO: yup.number().min(0).required(),
19+
SQ_BP_PER_GB: yup.number().min(0).required(),
1920
SQ_TORRENT_CATEGORIES: yup
2021
.array()
2122
.of(yup.string())

config.example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
SQ_ALLOW_REGISTER: 'invite',
1111
SQ_ALLOW_ANONYMOUS_UPLOADS: false,
1212
SQ_MINIMUM_RATIO: 0.75,
13+
SQ_BP_PER_GB: 1,
1314
SQ_TORRENT_CATEGORIES: ['Movies', 'TV', 'Music', 'Books'],
1415
SQ_BASE_URL: 'https://sqtracker.dev',
1516
SQ_API_URL: 'https://sqtracker.dev/api',

0 commit comments

Comments
 (0)