Skip to content

Commit 6ca5581

Browse files
committed
Added get player by title, id and badges
1 parent 3e7917f commit 6ca5581

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Gamification Server
22

3-
Gamification-server provides a framework for providing awards/points to users or teams and can be operated either standalone or integrated with other web-based applications. Based on the notion of badges used within other gamification systems. The gamification-server is implemented as an Express.js web application, built by [DevLeague](http://www.devleague.com/).
3+
Gamification-server provides a framework for providing awards/points to users or teams and can be operated either standalone or integrated with other web-based applications. Based on the notion of badges used within other gamification systems. The gamification-server is implemented as an Express.js web application. This gamification server was built by [DevLeague](http://www.devleague.com/).
44

55

66
## APIs and remote usage
77

88
Gamification-server is designed so that other sites can send in "signals" that are parsed through a rules engine and generate points and badges. Also, other sites and apps can pull in JSON to list badges that a user has.
99

10+
1011
### End Points
1112

1213
* GET `/api/player`
@@ -15,13 +16,16 @@ Returns an array of all the players
1516
* POST `/api/player/new`
1617
Creates a new player and returns the new player
1718

18-
* GET `/api/player/<PLAYER_ID>`
19+
* GET `/api/player/id/<PLAYER_ID>`
20+
Returns the player's current stats, badges and achievements
21+
22+
* GET `/api/player/name/<PLAYER_NAME>`
1923
Returns the player's current stats, badges and achievements
2024

21-
* GET `/api/player/<PLAYER_ID>/badges`
25+
* GET `/api/player/<PLAYER_NAME>/badges`
2226
Returns all the badges
2327

24-
* POST `/api/player/<PLAYER_ID>/assign_badge`
28+
* POST `/api/player/<PLAYER_NAME>/assign_badge`
2529
Creates a new badge for the player and returns an array of all the players badges.
2630

2731

model/Player.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ const mongoose = Promise.promisifyAll(require('mongoose'));
66
mongoose.Promise = require('bluebird');
77

88
var playerSchema = mongoose.Schema({
9-
name: String
9+
name: String,
10+
createdAt: { type: Date, default: Date.now },
11+
rank: { type: Number, default: 0 },
12+
reputation: { type: Number, default: 0 },
13+
badges: [{
14+
name: { type: String, default: 'New Badge' },
15+
points: { type: Number, default: 0 },
16+
createdAt: { type: Date, default: Date.now },
17+
}],
1018
});
1119

1220
var Player = mongoose.model('Player', playerSchema);

routes/player.js

+69-13
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,83 @@
33
const express = require('express');
44
const router = express.Router();
55
const mongoose = require('mongoose');
6-
// console.log(mongoose)
76
const Player = require('../model/Player');
8-
// console.log(Player)
9-
// const Player = mongoose.model('Player');
10-
// console.log(Player)
117

12-
router.route('/')
8+
router.route('/new')
9+
// Creates a new player and returns the new player
10+
.post((req, res) => {
11+
const newPlayer = new Player({
12+
name: req.body.name,
13+
rank: 0,
14+
reputation: 0,
15+
badges: {
16+
name: 'New User',
17+
points: 10
18+
},
19+
});
20+
newPlayer.save()
21+
.then((result) => {
22+
res.send(result);
23+
})
24+
.catch((err) => {
25+
res.send(err);
26+
});
27+
})
28+
29+
router.route('/id/:id')
30+
// Returns array of all the players
1331
.get((req, res) => {
14-
Player.find()
15-
.then((players) => {
16-
res.send(players);
32+
Player.find({ _id: req.params.id }).exec()
33+
.then((player) => {
34+
res.send(player);
1735
})
1836
.catch((err) => {
1937
res.send(err);
2038
});
2139
})
22-
.post((req, res) => {
23-
const newPlayer = new Player({ name: req.body.name});
24-
newPlayer.save()
25-
.then((result) => {
26-
res.send(result);
40+
41+
router.route('/name/:name')
42+
// Returns array of all the players
43+
.get((req, res) => {
44+
Player.find({ name: req.params.name }).exec()
45+
.then((player) => {
46+
res.send(player);
47+
})
48+
.catch((err) => {
49+
res.send(err);
50+
});
51+
})
52+
53+
router.route('/name/:name/badges')
54+
// Returns array of all the players
55+
.get((req, res) => {
56+
Player.find({ name: req.params.name }).exec()
57+
.then((player) => {
58+
res.send(player[0].badges);
59+
})
60+
.catch((err) => {
61+
res.send(err);
62+
});
63+
})
64+
65+
router.route('/name/:name/assign_badges')
66+
// Returns array of all the players
67+
.get((req, res) => {
68+
Player.find({ name: req.params.name }).exec()
69+
.then((player) => {
70+
res.send(player[0].badges);
71+
})
72+
.catch((err) => {
73+
res.send(err);
74+
});
75+
})
76+
77+
router.route('/')
78+
// Returns array of all the players
79+
.get((req, res) => {
80+
Player.find()
81+
.then((players) => {
82+
res.send(players);
2783
})
2884
.catch((err) => {
2985
res.send(err);

0 commit comments

Comments
 (0)