-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (100 loc) · 3.62 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require("dotenv").config();
var _ = require("lodash")
var faunadb = require('faunadb'),
q = faunadb.query
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
var cors = require('cors')
const PORT = process.env.PORT || 8000;
const URL = process.env.URL || `https://localhost:8000/`;
console.log(URL)
const app = require('express')()
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors())
var client = new faunadb.Client({secret: process.env.FAUNADB_SECRET})
const topTenSort = function (a, b) { // sorts games by score, with highest score appearing first. Ties go to game with more secondsLeft
return (b.score - a.score || b.secondsLeft - a.secondsLeft)
}
app.get('/', function (req, res) {
const leaderboardQuery = client.query(
q.Get(q.Match(q.Index('leaderboard_topTen')))
)
leaderboardQuery.then((response) => {
const topTen = response.data.topTen
res.json(topTen.sort(topTenSort))
}).catch((error) => {
console.log(error)
})
})
app.get('/country-stats', function (req, res) {
const countryStatsQuery = client.query(
q.Get(q.Ref(q.Collection('country-stats'), '285397108568097293'))
)
countryStatsQuery.then(response => {
res.json(response.data)
}).catch(error => {
console.log(error)
})
})
app.post('/', function (req, res) {
fetch(URL)
.then((response) => {
return response.json()
})
.then(topTen => {
const submittedGame = req.body
const numGames = topTen.push(submittedGame)
topTen.sort(topTenSort)
if (numGames <= 10 || !_.isEqual(topTen.pop(), submittedGame)) { // if the submittedGame becomes part of the new topTen, or there weren't ten games in there
console.log("Updating Top Ten leaderboard for player: ", submittedGame.name)
client.query(
q.Update(
q.Ref(q.Collection('leaderboard'), '283939936047989260'),
{ data: { topTen: topTen } },
)
).then(res => {
console.log("Successfully updated top ten leaderboard for player: ", submittedGame.name)
}).catch((error) => {
console.log(error)
})
}
return {
topTen: topTen.map(game => ({...game, activeGame: _.isEqual(submittedGame, game)})),
submittedGame: submittedGame
}
}).then((results) => {
const {topTen, submittedGame} = results;
fetch(URL + 'country-stats')
.then(response => {
return response.json()
})
.then(countryStats => {
if (submittedGame.score > 10) {
console.log("Updating Country Table for player: ", submittedGame.name)
_.forEach(submittedGame.namedCountryCodes, code => {
countryStats.countryCounts[code] += 1
})
countryStats.totalGames += 1
client.query(
q.Update(
q.Ref(q.Collection('country-stats'), '285397108568097293'),
{ data: countryStats },
)
).then((response) => {
res.json({topTen: topTen, ...response.data})
}).catch((error) => {
console.log(error)
})
} else {
res.json({topTen: topTen, ...countryStats})
}
})
})
.catch((error) => {
console.log(error)
})
})
app.listen(PORT, ()=> {
console.log('countries-game api is running on port ' + PORT);
})