-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (103 loc) · 3.26 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
var express = require('express');
var app = express();
// Setting up creature here, because databases are hard
creatures = [
{
"id": "0",
"name": "Bill",
"type": "Ieki",
"color": "blue",
"stats": {
"defense": 11,
"intelligence": 15,
"skills": 12
},
"hunger": 100,
"fatigue": 100,
"status": "idle",
"time": 0
},
{
"id": "1",
"name": "Ted",
"type": "Ekana",
"color": "red",
"stats": {
"defense": 15,
"intelligence": 12,
"skills": 13
},
"hunger": 50,
"fatigue": 100,
"status": "sleep",
"time": 4
}
];
app.get('/creatures/:creature_id', function(req, res) {
res.status(200).send(creatures[req.params.creature_id]);
});
app.put('/creatures/:creature_id/rest', function(req, res) {
creatures[req.params.creature_id].fatigue = 100; // pet is no longer fatigued
creatures[req.params.creature_id].status = "sleeping"; // the pet is sleeping for 9 visits
creatures[req.params.creature_id].time = 9;
res.status(204).send(creatures[req.params.creature_id]);
});
app.put('/creatures/:creature_id/feed', function(req, res) {
creatures[req.params.creature_id].hunger = 100; // pet is no longer hungry
creatures[req.params.creature_id].status = "eating"; // the pet is eating for 9 visits
creatures[req.params.creature_id].time = 9; // the pet is eating for 9 visits
res.status(204).send(creatures[req.params.creature_id]);
});
app.put('/creatures/:creature_id/visit/:page_url', function(req, res) {
if (creatures[req.params.creature_id].time > 0) {
creatures[req.params.creature_id].time -= 1;
}
// get category of the page
// hard-coding it to "games"
var category = {
"espn.go.com": "sports",
"www.wikipedia.org": "reference",
"www.reddit.com": "home"
}[req.params.page_url];
var skills = ["games", "kids and teens", "recreation", "sports", "shopping"];
var defense = ["health", "regional", "world", "society", "home"];
var intelligence = ["arts", "buisness", "computers", "news", "science", "reference"];
var stat;
if (skills.indexOf(category) != -1) {
creatures[req.params.creature_id].hunger -= 2;
creatures[req.params.creature_id].fatigue -= 2;
stat = "skills";
}
else if(defense.indexOf(category) != -1) {
creatures[req.params.creature_id].fatigue -= 2;
stat = "defense";
}
else if(intelligence.indexOf(category) != -1) {
creatures[req.params.creature_id].hunger -= 2;
stat = "intelligence";
}
if (creatures[req.params.creature_id].time < 1) { // we can go to the stat
if (creatures[req.params.creature_id].fatigue < 25) {
creatures[req.params.creature_id].status = "tired";
creatures[req.params.creature_id].time = 99;
}
else if (creatures[req.params.creature_id].hunger < 25) {
creatures[req.params.creature_id].status = "hungry";
creatures[req.params.creature_id].time = 99;
}
else {
if (stat != undefined) {
creatures[req.params.creature_id].status = stat;
creatures[req.params.creature_id].time = 9;
creatures[req.params.creature_id].stats[stat] += 1;
}
else {
creatures[req.params.creature_id].status = "idle";
}
}
}
res.status(204).send(creatures[req.params.creature_id]);
});
var server = app.listen(3000, function() {
console.log("running");
});