nodebb-plugin-niki-loyalty 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library.js +12 -36
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -2,83 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
const db = require.main.require('./src/database');
|
|
4
4
|
const user = require.main.require('./src/user');
|
|
5
|
-
const routeHelpers = require.main.require('./src/controllers/helpers');
|
|
6
5
|
|
|
7
6
|
const Plugin = {};
|
|
8
7
|
|
|
9
8
|
// --- AYARLAR ---
|
|
10
9
|
const SETTINGS = {
|
|
11
|
-
pointsPerHeartbeat: 5,
|
|
12
|
-
|
|
13
|
-
dailyCap: 250 // Günlük maksimum puan
|
|
10
|
+
pointsPerHeartbeat: 5,
|
|
11
|
+
dailyCap: 250
|
|
14
12
|
};
|
|
15
13
|
|
|
16
14
|
Plugin.init = async function (params) {
|
|
17
15
|
const router = params.router;
|
|
18
16
|
const middleware = params.middleware;
|
|
19
|
-
|
|
20
|
-
// Konsolda bu yazıyı görmelisin, görmüyorsan plugin aktif değildir.
|
|
21
|
-
console.log('✅ Niki Loyalty Plugin: Başlatılıyor...');
|
|
22
17
|
|
|
23
|
-
// API: Kalp Atışı (Puan Kazanma)
|
|
18
|
+
// 1. API: Kalp Atışı (Puan Kazanma - Client.js burayı kullanır)
|
|
24
19
|
router.post('/api/niki-loyalty/heartbeat', middleware.ensureLoggedIn, async (req, res) => {
|
|
25
20
|
const uid = req.uid;
|
|
26
|
-
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
27
|
-
|
|
28
|
-
// 1. Günlük Limiti Kontrol Et
|
|
21
|
+
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
29
22
|
const dailyKey = `niki:daily:${uid}:${today}`;
|
|
23
|
+
|
|
30
24
|
const currentDailyScore = await db.getObjectField(dailyKey, 'score') || 0;
|
|
31
25
|
|
|
32
26
|
if (parseInt(currentDailyScore) >= SETTINGS.dailyCap) {
|
|
33
27
|
return res.json({ earned: false, reason: 'daily_cap' });
|
|
34
28
|
}
|
|
35
29
|
|
|
36
|
-
// 2. Puan Ver
|
|
37
30
|
await user.incrementUserFieldBy(uid, 'niki_points', SETTINGS.pointsPerHeartbeat);
|
|
38
31
|
await db.incrObjectFieldBy(dailyKey, 'score', SETTINGS.pointsPerHeartbeat);
|
|
39
32
|
|
|
40
|
-
// 3. Güncel Bakiyeyi Dön
|
|
41
33
|
const newBalance = await user.getUserField(uid, 'niki_points');
|
|
42
|
-
return res.json({
|
|
43
|
-
earned: true,
|
|
44
|
-
points: SETTINGS.pointsPerHeartbeat,
|
|
45
|
-
total: newBalance,
|
|
46
|
-
daily: parseInt(currentDailyScore) + SETTINGS.pointsPerHeartbeat
|
|
47
|
-
});
|
|
34
|
+
return res.json({ earned: true, points: SETTINGS.pointsPerHeartbeat, total: newBalance });
|
|
48
35
|
});
|
|
49
36
|
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
routeHelpers.setupPageRoute(router, '/niki-wallet', middleware, [], async (req, res) => {
|
|
37
|
+
// 2. YENİ API: Cüzdan Verisi Çekme (Custom Page burayı kullanacak)
|
|
38
|
+
router.get('/api/niki-loyalty/wallet-data', middleware.ensureLoggedIn, async (req, res) => {
|
|
53
39
|
const uid = req.uid;
|
|
54
|
-
|
|
55
|
-
// Giriş yapmamışsa login sayfasına at
|
|
56
|
-
if (!uid) return res.redirect('/login');
|
|
57
|
-
|
|
58
40
|
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
59
41
|
|
|
60
|
-
// Verileri Çek
|
|
61
42
|
const [userData, dailyData] = await Promise.all([
|
|
62
|
-
user.getUserFields(uid, ['
|
|
43
|
+
user.getUserFields(uid, ['niki_points']),
|
|
63
44
|
db.getObject(`niki:daily:${uid}:${today}`)
|
|
64
45
|
]);
|
|
65
46
|
|
|
66
47
|
const currentPoints = parseInt(userData.niki_points) || 0;
|
|
67
48
|
const dailyScore = parseInt(dailyData ? dailyData.score : 0) || 0;
|
|
68
|
-
|
|
69
|
-
// Yüzdelik Hesapla (Bar için)
|
|
70
49
|
let dailyPercent = (dailyScore / SETTINGS.dailyCap) * 100;
|
|
71
50
|
if (dailyPercent > 100) dailyPercent = 100;
|
|
72
51
|
|
|
73
|
-
|
|
74
|
-
res.render('niki-wallet', {
|
|
75
|
-
title: 'Niki Cüzdan',
|
|
52
|
+
res.json({
|
|
76
53
|
points: currentPoints,
|
|
77
54
|
dailyScore: dailyScore,
|
|
78
55
|
dailyCap: SETTINGS.dailyCap,
|
|
79
|
-
dailyPercent: dailyPercent
|
|
80
|
-
user: userData,
|
|
81
|
-
breadcrumbs: routeHelpers.buildBreadcrumbs([{ text: 'Niki Cüzdan' }]) // Breadcrumb ekledim (opsiyonel şıklık)
|
|
56
|
+
dailyPercent: dailyPercent
|
|
82
57
|
});
|
|
83
58
|
});
|
|
84
59
|
};
|
|
@@ -88,6 +63,7 @@ Plugin.addScripts = async function (scripts) {
|
|
|
88
63
|
return scripts;
|
|
89
64
|
};
|
|
90
65
|
|
|
66
|
+
// Menüye eklemeye devam edelim, Custom Page ile aynı linki vereceğiz
|
|
91
67
|
Plugin.addNavigation = async function (nav) {
|
|
92
68
|
nav.push({
|
|
93
69
|
"route": "/niki-wallet",
|