-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (97 loc) · 3.44 KB
/
Copy pathindex.js
File metadata and controls
109 lines (97 loc) · 3.44 KB
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
var express = require('express');
var exphbs = require('express-handlebars');
var cradle = require('cradle');
var db = new(cradle.Connection)('http://mafia.sockdrawer.io:5984').database('games');
var app = express();
var hbs = exphbs.create({
defaultLayout: "main",
// Specify helpers which are only registered on this instance.
helpers: {
gamePanelClass: (winner) => {
if (winner === 'town') return 'panel-success';
if (winner === 'scum') return 'panel-danger';
if (winner === 'scum2') return 'panel-danger';
if (winner === '3party') return 'panel-warning';
if (winner === 'cult') return 'panel-warning';
return 'panel-info';
},
collapseList: (arr) => {
if (typeof arr === 'object') {
arr = Object.keys(arr);
}
if (arr.length == 0) return "";
if (arr.length == 1) return arr[0];
if (arr.length == 2) return arr[0] + "&" + arr[1];
return arr.reduce((string, val, index) => {
if (index == 0) return val;
if (index != arr.length-1) return string + ", " + val;
return string + ", and " + val;
});
},
getRole: (player) => {
if (player.properties.indexOf('scum') > -1) return "Scum";
if (player.properties.indexOf('3party') > -1) return "3rd Party";
if (player.properties.indexOf('cult') > -1) {
if (player.properties.indexOf('cult-leader') > -1) return "Cult Leader";
return "Cult Recruit";
}
return "Town";
},
getWinner: (win) => {
if (!win) return "Unknown";
if (win === 'town') return "Town";
if (win === 'scum') return "Scum";
if (win === 'scum2') return "Second Scum";
if (win === '3party') return "Third Party";
if (win === 'cult') return "Cult";
return win;
},
makeActionPast: (action) => {
if (action === "vote") return "voted for";
return action + 'ed';
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.get('/', function (req, res) {
res.render('home');
});
app.get('/games', function (req, res) {
db.view('games/listGames', function (err, games) {
res.render('gamesList', {
games: games
});
});
});
app.get('/games/:id', function (req, res) {
db.get(req.params.id, function (err, game) {
res.render('gameView', game);
});
});
app.get('/players', function (req, res) {
db.view('games/byPlayer', {group: true, reduce: true}, function (err, players) {
res.render('playerList', {
count: players.length,
players: players
});
});
});
app.get('/players/:id', function (req, res) {
db.view('games/byPlayer', {group: false, reduce: false, 'key': req.params.id}, function (err, games) {
let numGames = games.length;
let numWins = games.reduce((sum, item) => {
return (item.value.won ? sum + 1 : sum)
}, 0);
console.log(games)
res.render('playerView', {
wins: numWins,
numGames: numGames,
percent: numWins / numGames * 100,
key: games[0].key,
name: games[0].value.username,
games: games
});
});
});
app.listen(process.env.PORT);