react-achievements 1.3.1 → 1.3.2
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/README.md +310 -100
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,161 +1,371 @@
|
|
|
1
|
-
|
|
1
|
+
Certainly! I'll provide you with a revised version of the README that removes the artifacts-related content. Here's the updated version without the artifacts:
|
|
2
|
+
<h1 align="center">🏆 React-Achievements 🏆</h1>
|
|
2
3
|
|
|
3
|
-
A flexible and customizable achievement system for React applications.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A flexible and customizable achievement system for React applications, perfect for adding gamification elements to your projects.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<h2 align="center">🚀 Installation</h2>
|
|
8
8
|
|
|
9
|
-
`
|
|
9
|
+
Install `react-achievements` using npm or yarn:
|
|
10
10
|
|
|
11
|
+
```bash
|
|
12
|
+
npm install react-achievements
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or
|
|
11
16
|
|
|
12
|
-
|
|
17
|
+
```bash
|
|
18
|
+
yarn add react-achievements
|
|
19
|
+
```
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
<h2 align="center">🎮 Usage</h2>
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
Let's walk through setting up a simple RPG-style game with achievements using React-Achievements.
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
<h3 align="center">🛠 Set up the AchievementProvider</h3>
|
|
19
26
|
|
|
20
|
-
|
|
27
|
+
First, wrap your app or a part of your app with the AchievementProvider:
|
|
28
|
+
|
|
29
|
+
```jsx
|
|
30
|
+
import React from 'react';
|
|
21
31
|
import { AchievementProvider } from 'react-achievements';
|
|
22
32
|
import achievementConfig from './achievementConfig';
|
|
33
|
+
import Game from './Game';
|
|
34
|
+
|
|
35
|
+
const initialState = {
|
|
36
|
+
level: 1,
|
|
37
|
+
experience: 0,
|
|
38
|
+
monstersDefeated: 0,
|
|
39
|
+
questsCompleted: 0
|
|
40
|
+
};
|
|
23
41
|
|
|
24
42
|
function App() {
|
|
25
43
|
return (
|
|
26
|
-
<AchievementProvider
|
|
27
|
-
{
|
|
44
|
+
<AchievementProvider
|
|
45
|
+
config={achievementConfig}
|
|
46
|
+
initialState={initialState}
|
|
47
|
+
badgesButtonPosition="top-right"
|
|
48
|
+
>
|
|
49
|
+
<Game />
|
|
28
50
|
</AchievementProvider>
|
|
29
51
|
);
|
|
30
52
|
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
The `initialState` prop should contain the current state of your metrics. For example:
|
|
34
53
|
|
|
35
|
-
|
|
36
|
-
const initialState = {
|
|
37
|
-
transactions: [
|
|
38
|
-
{ id: 1, amount: 100 },
|
|
39
|
-
{ id: 2, amount: 200 }
|
|
40
|
-
]
|
|
41
|
-
};
|
|
54
|
+
export default App;
|
|
42
55
|
```
|
|
43
56
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
Create a file (e.g., `achievementConfig.js`) to define your achievements:
|
|
57
|
+
<h3 align="center">📝 Create an achievement configuration</h3>
|
|
47
58
|
|
|
59
|
+
Create a file (e.g., achievementConfig.js) to define your achievements:
|
|
48
60
|
|
|
49
61
|
```javascript
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
import
|
|
53
|
-
import image2 from './public/path/to/image2.png';
|
|
62
|
+
import levelUpIcon from './icons/level-up.png';
|
|
63
|
+
import monsterSlayerIcon from './icons/monster-slayer.png';
|
|
64
|
+
import questMasterIcon from './icons/quest-master.png';
|
|
54
65
|
|
|
55
66
|
const achievementConfig = {
|
|
56
|
-
|
|
67
|
+
level: [
|
|
57
68
|
{
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
check: (value) => value >= 10,
|
|
70
|
+
data: {
|
|
71
|
+
id: 'level_10',
|
|
72
|
+
title: 'Novice Adventurer',
|
|
73
|
+
description: 'Reached level 10',
|
|
74
|
+
icon: levelUpIcon
|
|
75
|
+
}
|
|
65
76
|
},
|
|
66
77
|
{
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
check: (value) => value >= 50,
|
|
79
|
+
data: {
|
|
80
|
+
id: 'level_50',
|
|
81
|
+
title: 'Seasoned Warrior',
|
|
82
|
+
description: 'Reached level 50',
|
|
83
|
+
icon: levelUpIcon
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
],
|
|
87
|
+
monstersDefeated: [
|
|
88
|
+
{
|
|
89
|
+
check: (value) => value >= 100,
|
|
90
|
+
data: {
|
|
91
|
+
id: 'monster_slayer',
|
|
92
|
+
title: 'Monster Slayer',
|
|
93
|
+
description: 'Defeated 100 monsters',
|
|
94
|
+
icon: monsterSlayerIcon
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
],
|
|
98
|
+
questsCompleted: [
|
|
99
|
+
{
|
|
100
|
+
check: (value) => value >= 50,
|
|
101
|
+
data: {
|
|
102
|
+
id: 'quest_master',
|
|
103
|
+
title: 'Quest Master',
|
|
104
|
+
description: 'Completed 50 quests',
|
|
105
|
+
icon: questMasterIcon
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
]
|
|
77
109
|
};
|
|
78
|
-
```
|
|
79
110
|
|
|
80
111
|
export default achievementConfig;
|
|
81
|
-
|
|
82
|
-
Note: Ensure your icons are located in the public folder of your project.
|
|
83
|
-
|
|
84
|
-
### Customize the badges button location
|
|
85
|
-
|
|
86
|
-
```javascript
|
|
87
|
-
<AchievementProvider
|
|
88
|
-
config={achievementConfig}
|
|
89
|
-
badgesButtonPosition="bottom-right"
|
|
90
|
-
>
|
|
91
|
-
{/* Your app components */}
|
|
92
|
-
</AchievementProvider>
|
|
93
112
|
```
|
|
94
|
-
Specify the position of the badges button:
|
|
95
|
-
Possible values for `badgesButtonPosition` are:
|
|
96
|
-
- 'top-left'
|
|
97
|
-
- 'top-right'
|
|
98
|
-
- 'bottom-left'
|
|
99
|
-
- 'bottom-right'
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
### Use the useAchievement hook
|
|
104
113
|
|
|
105
|
-
|
|
114
|
+
<h3 align="center">🎣 Use the useAchievement hook</h3>
|
|
106
115
|
|
|
107
|
-
|
|
116
|
+
In your game components, use the useAchievement hook to update metrics and trigger achievement checks:
|
|
117
|
+
```jsx
|
|
118
|
+
import React, { useState } from 'react';
|
|
108
119
|
import { useAchievement } from 'react-achievements';
|
|
109
120
|
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
function Game() {
|
|
122
|
+
const { setMetrics, metrics } = useAchievement();
|
|
123
|
+
const [currentQuest, setCurrentQuest] = useState(null);
|
|
124
|
+
|
|
125
|
+
const defeatMonster = () => {
|
|
126
|
+
setMetrics(prevMetrics => {
|
|
127
|
+
const newExperience = prevMetrics.experience + 10;
|
|
128
|
+
const newLevel = Math.floor(newExperience / 100) + 1;
|
|
129
|
+
return {
|
|
130
|
+
...prevMetrics,
|
|
131
|
+
monstersDefeated: prevMetrics.monstersDefeated + 1,
|
|
132
|
+
experience: newExperience,
|
|
133
|
+
level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const completeQuest = () => {
|
|
139
|
+
setMetrics(prevMetrics => {
|
|
140
|
+
const newExperience = prevMetrics.experience + 50;
|
|
141
|
+
const newLevel = Math.floor(newExperience / 100) + 1;
|
|
142
|
+
return {
|
|
143
|
+
...prevMetrics,
|
|
144
|
+
questsCompleted: prevMetrics.questsCompleted + 1,
|
|
145
|
+
experience: newExperience,
|
|
146
|
+
level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
setCurrentQuest(null);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const startQuest = () => {
|
|
153
|
+
setCurrentQuest("Defeat the Dragon");
|
|
154
|
+
};
|
|
123
155
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
156
|
+
return (
|
|
157
|
+
<div>
|
|
158
|
+
<h1>My RPG Game</h1>
|
|
159
|
+
<p>Level: {metrics.level}</p>
|
|
160
|
+
<p>Experience: {metrics.experience}</p>
|
|
161
|
+
<p>Monsters Defeated: {metrics.monstersDefeated}</p>
|
|
162
|
+
<p>Quests Completed: {metrics.questsCompleted}</p>
|
|
163
|
+
|
|
164
|
+
<div>
|
|
165
|
+
<h2>Battle Arena</h2>
|
|
166
|
+
<button onClick={defeatMonster}>Fight a Monster</button>
|
|
167
|
+
</div>
|
|
168
|
+
|
|
169
|
+
<div>
|
|
170
|
+
<h2>Quest Board</h2>
|
|
171
|
+
{currentQuest ? (
|
|
172
|
+
<>
|
|
173
|
+
<p>Current Quest: {currentQuest}</p>
|
|
174
|
+
<button onClick={completeQuest}>Complete Quest</button>
|
|
175
|
+
</>
|
|
176
|
+
) : (
|
|
177
|
+
<button onClick={startQuest}>Start a New Quest</button>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
);
|
|
127
182
|
}
|
|
183
|
+
|
|
184
|
+
export default Game;
|
|
128
185
|
```
|
|
129
|
-
## Features
|
|
130
186
|
|
|
131
|
-
|
|
187
|
+
<h2 align="center">✨ Features</h2>
|
|
188
|
+
|
|
189
|
+
- Flexible Achievement System: Define custom metrics and achievement conditions for your game or app.
|
|
132
190
|
- Automatic Achievement Tracking: Achievements are automatically checked and unlocked when metrics change.
|
|
133
|
-
- Achievement Notifications: A modal pops up when an achievement is unlocked.
|
|
134
|
-
- Persistent Achievements: Unlocked achievements and metrics are stored in local storage.
|
|
135
|
-
- Achievement Gallery:
|
|
136
|
-
- Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked.
|
|
191
|
+
- Achievement Notifications: A modal pops up when an achievement is unlocked, perfect for rewarding players.
|
|
192
|
+
- Persistent Achievements: Unlocked achievements and metrics are stored in local storage, allowing players to keep their progress.
|
|
193
|
+
- Achievement Gallery: Players can view all their unlocked achievements, encouraging completionism.
|
|
194
|
+
- Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked, adding to the excitement.
|
|
137
195
|
|
|
138
|
-
|
|
196
|
+
<h2 align="center">🔧 API</h2>
|
|
139
197
|
|
|
140
|
-
|
|
198
|
+
<h3 align="center">🏗 AchievementProvider</h3>
|
|
141
199
|
|
|
142
200
|
#### Props:
|
|
201
|
+
|
|
143
202
|
- `config`: An object defining your metrics and achievements.
|
|
144
203
|
- `initialState`: The initial state of your metrics.
|
|
145
204
|
- `storageKey` (optional): A string to use as the key for localStorage. Default: 'react-achievements'
|
|
146
205
|
- `badgesButtonPosition` (optional): Position of the badges button. Default: 'top-right'
|
|
147
206
|
- `styles` (optional): Custom styles for the achievement components.
|
|
148
207
|
|
|
149
|
-
|
|
208
|
+
<h3 align="center">🪝 useAchievement Hook</h3>
|
|
209
|
+
|
|
210
|
+
#### Returns an object with:
|
|
150
211
|
|
|
151
|
-
Returns an object with:
|
|
152
212
|
- `setMetrics`: Function to update the metrics.
|
|
153
213
|
- `metrics`: Current metrics object.
|
|
154
214
|
- `unlockedAchievements`: Array of unlocked achievement IDs.
|
|
155
215
|
- `showBadgesModal`: Function to manually show the badges modal.
|
|
156
216
|
|
|
157
|
-
|
|
217
|
+
<h2 align="center">🎨 Customization</h2>
|
|
218
|
+
|
|
219
|
+
React-Achievements allows for extensive customization of its appearance. You can override the default styles by passing a `styles` prop to the `AchievementProvider`:
|
|
220
|
+
|
|
221
|
+
```jsx
|
|
222
|
+
const customStyles = {
|
|
223
|
+
achievementModal: {
|
|
224
|
+
overlay: {
|
|
225
|
+
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
226
|
+
},
|
|
227
|
+
content: {
|
|
228
|
+
backgroundColor: '#2a2a2a',
|
|
229
|
+
color: '#ffffff',
|
|
230
|
+
},
|
|
231
|
+
title: {
|
|
232
|
+
color: '#ffd700',
|
|
233
|
+
},
|
|
234
|
+
button: {
|
|
235
|
+
backgroundColor: '#4CAF50',
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
badgesModal: {
|
|
239
|
+
// Custom styles for the badges modal
|
|
240
|
+
},
|
|
241
|
+
badgesButton: {
|
|
242
|
+
// Custom styles for the badges button
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
function App() {
|
|
247
|
+
return (
|
|
248
|
+
<AchievementProvider
|
|
249
|
+
config={achievementConfig}
|
|
250
|
+
initialState={initialState}
|
|
251
|
+
styles={customStyles}
|
|
252
|
+
>
|
|
253
|
+
<Game />
|
|
254
|
+
</AchievementProvider>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### achievementModal
|
|
260
|
+
|
|
261
|
+
Customizes the modal that appears when an achievement is unlocked.
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
achievementModal: {
|
|
265
|
+
overlay: {
|
|
266
|
+
// Styles for the modal overlay (background)
|
|
267
|
+
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
268
|
+
// You can also customize other overlay properties like zIndex, transition, etc.
|
|
269
|
+
},
|
|
270
|
+
content: {
|
|
271
|
+
// Styles for the modal content container
|
|
272
|
+
backgroundColor: '#2a2a2a',
|
|
273
|
+
color: '#ffffff',
|
|
274
|
+
borderRadius: '10px',
|
|
275
|
+
padding: '20px',
|
|
276
|
+
// Add any other CSS properties for the content container
|
|
277
|
+
},
|
|
278
|
+
title: {
|
|
279
|
+
// Styles for the achievement title
|
|
280
|
+
fontSize: '24px',
|
|
281
|
+
fontWeight: 'bold',
|
|
282
|
+
color: '#ffd700',
|
|
283
|
+
},
|
|
284
|
+
icon: {
|
|
285
|
+
// Styles for the achievement icon
|
|
286
|
+
width: '64px',
|
|
287
|
+
height: '64px',
|
|
288
|
+
marginBottom: '10px',
|
|
289
|
+
},
|
|
290
|
+
description: {
|
|
291
|
+
// Styles for the achievement description
|
|
292
|
+
fontSize: '16px',
|
|
293
|
+
marginTop: '10px',
|
|
294
|
+
},
|
|
295
|
+
button: {
|
|
296
|
+
// Styles for the close button
|
|
297
|
+
backgroundColor: '#4CAF50',
|
|
298
|
+
color: 'white',
|
|
299
|
+
padding: '10px 20px',
|
|
300
|
+
border: 'none',
|
|
301
|
+
borderRadius: '5px',
|
|
302
|
+
cursor: 'pointer',
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### badgesModal
|
|
308
|
+
|
|
309
|
+
```
|
|
310
|
+
badgesModal: {
|
|
311
|
+
overlay: {
|
|
312
|
+
// Similar to achievementModal overlay
|
|
313
|
+
},
|
|
314
|
+
content: {
|
|
315
|
+
// Similar to achievementModal content
|
|
316
|
+
},
|
|
317
|
+
title: {
|
|
318
|
+
// Styles for the modal title
|
|
319
|
+
},
|
|
320
|
+
badgeContainer: {
|
|
321
|
+
// Styles for the container holding all badges
|
|
322
|
+
display: 'flex',
|
|
323
|
+
flexWrap: 'wrap',
|
|
324
|
+
justifyContent: 'center',
|
|
325
|
+
},
|
|
326
|
+
badge: {
|
|
327
|
+
// Styles for individual badge containers
|
|
328
|
+
margin: '10px',
|
|
329
|
+
textAlign: 'center',
|
|
330
|
+
},
|
|
331
|
+
badgeIcon: {
|
|
332
|
+
// Styles for badge icons
|
|
333
|
+
width: '50px',
|
|
334
|
+
height: '50px',
|
|
335
|
+
},
|
|
336
|
+
badgeTitle: {
|
|
337
|
+
// Styles for badge titles
|
|
338
|
+
fontSize: '14px',
|
|
339
|
+
marginTop: '5px',
|
|
340
|
+
},
|
|
341
|
+
button: {
|
|
342
|
+
// Styles for the close button (similar to achievementModal button)
|
|
343
|
+
},
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
### badgesButton
|
|
349
|
+
|
|
350
|
+
```
|
|
351
|
+
badgesButton: {
|
|
352
|
+
// Styles for the floating badges button
|
|
353
|
+
position: 'fixed',
|
|
354
|
+
padding: '10px 20px',
|
|
355
|
+
backgroundColor: '#007bff',
|
|
356
|
+
color: '#ffffff',
|
|
357
|
+
border: 'none',
|
|
358
|
+
borderRadius: '5px',
|
|
359
|
+
cursor: 'pointer',
|
|
360
|
+
zIndex: 1000,
|
|
361
|
+
// You can add more CSS properties as needed. These are just regular CSS
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
This allows you to match the achievement system's look and feel to your game or application's theme.
|
|
158
367
|
|
|
368
|
+
<h2 align="center">📄 License</h2>
|
|
159
369
|
MIT
|
|
160
370
|
|
|
161
|
-
|
|
371
|
+
React-Achievements provides a comprehensive achievement system for React applications, perfect for adding gamification elements to your projects. Whether you're building a game, an educational app, or any interactive experience, this package offers an easy way to implement and manage achievements, enhancing user engagement and retention.
|