zssn-api v1.0.0
zssn-api
This describes the resources that make up the REST API for ZSSN (Zombie Survival Social Network).
The diagram below shows the architecture of the implementation, which is based on MVC patterns and HTTP request message routing.
The code style follows this JavaScript Style Guide, which uses the ESLint rules for JavaScript. The code organization and configuration also tries to follow the Node.js best practices list.
Table of Contents
Installation
Dependencies
When the dependencies are up and running, install zssn-api via its npm package:
npm install zssn-api
Usage
To start the server, run:
npm start
API
List survivors
List all the survivors.
GET /api/survivors
Request
GET https://zssn.azurewebsites.net/api/survivors
Response
If successful, this method returns a response body with the following structure:
{
"infected": boolean,
"_id": string,
"name": string,
"age": number,
"sex": string,
"reportMessages": array,
"totalInfections": number,
"inventory": {
"water": number,
"food": number,
"medication": number,
"ammunition": number
},
"location": {
"latitude": number,
"longitude": number
}
}
Name | Type | Description |
---|---|---|
infected | boolean | The flag that defines if the survivor is infected or not |
_id | string | The ID of the survivor |
name | string | The name of the survivor |
age | number | The age of the survivor |
sex | string | The gender of the survivor |
inventory | object | The inventory that contains the resources of the survivor |
inventory.water | number | The number of water resources the survivor has |
inventory.food | number | The number of food resources the survivor has |
inventory.medication | number | The number of medication resources the survivor has |
inventory.ammunition | number | The number of ammunition resources the survivor has |
location | object | The last location of the survivor |
location.latitude | number | The latitude of the survivor's location |
location.longitude | number | The longitude of the survivor's location |
reportMessages | array | The messages reported against the survivor |
totalInfections | number | The longitude of the survivor's location |
Add a survivor
Creates a new survivor.
POST /api/survivors
Request
POST https://zssn.azurewebsites.net/api/survivors
Supply a new survivor, with the following properties:
Name | Type | Description |
---|---|---|
name | string | The name of the survivor |
age | number | The age of the survivor |
sex | string | The gender of the survivor |
location | object | The last location of the survivor |
location.latitude | number | The latitude of the survivor's location |
location.longitude | number | The longitude of the survivor's location |
Response
If successful, this method returns a response body with the following structure:
{
"_id": string,
"name": string,
"age": number,
"sex": string,
"infected": boolean,
"inventory": {
"water": number,
"food": number,
"medication": number,
"ammunition": number
},
"location": {
"latitude": number,
"longitude": number
}
}
Name | Type | Description |
---|---|---|
infected | boolean | The flag that defines if the survivor is infected or not |
_id | string | The ID of the survivor |
name | string | The name of the survivor |
age | number | The age of the survivor |
sex | string | The gender of the survivor |
inventory | object | The inventory that contains the resources of the survivor |
inventory.water | number | The number of water resources the survivor has |
inventory.food | number | The number of food resources the survivor has |
inventory.medication | number | The number of medication resources the survivor has |
inventory.ammunition | number | The number of ammunition resources the survivor has |
location | object | The last location of the survivor |
location.latitude | number | The latitude of the survivor's location |
location.longitude | number | The longitude of the survivor's location |
reportMessages | array | The messages reported against the survivor |
totalInfections | number | The longitude of the survivor's location |
Update a survivor location
Update a survivor's location with new latitudes and longitudes.
PATCH /api/survivors/location/:id
Request
PATCH https://zssn.azurewebsites.net/api/survivors/location/:id
Supply the new latitude and the new longitude, according to the rules of patch semantics, with the following properties:
Name | Type | Description |
---|---|---|
latitude | number | The latitude of the survivor |
longitude | number | The longitude of the survivor |
Response
If successful, this method returns a response body with the following structure:
{
"location": {
"latitude": number,
"longitude": number
}
}
Flag survivor as infected
Flag a survivor as infected. A survivor is marked as infected when at least three other survivors report their contamination.
POST /api/survivors/:id/report
Request
POST https://zssn.azurewebsites.net/api/survivors/:id/report
The following report message is optional:
Name | Type | Description |
---|---|---|
report | string | Message of the report. Optional. |
Response
If successful, this method returns a response body with the following structure:
{
"message": "Survivor successfully reported as infected!"
}
Trade items
Exchange items between two survivors. Infected survivors can't trade items among themselves.
Survivors must respect the price table below, where the value of an item is described in terms of points. Both sides of the trade should offer the same amount of points. For example, 1 Water and 1 Medication (1 x 4 + 1 x 2) is worth 6 ammunition (6 x 1) or 2 Food items (2 x 3).
Item | Points |
---|---|
1 Water | 4 points |
1 Food | 3 points |
1 Medication | 2 points |
1 Ammunition | 1 point |
POST /api/:first_id/:second_id/trade
Request
POST https://zssn.azurewebsites.net/api/:first_id/:second_id/trade
Supply the trade items of both survivors, with the following properties:
Name | Type | Description |
---|---|---|
first_survivor_resources | object | The resources of the first survivor |
first_survivor_resources.water | number | The amount of water of the first survivor |
first_survivor_resources.food | number | The amount of food of the first survivor |
first_survivor_resources.medication | number | The amount of medication of the first survivor |
first_survivor_resources.ammunition | number | The amount of ammunition of the first survivor |
second_survivor_resources | object | The resources of the second survivor |
second_survivor_resources.water | number | The amount of water of the second survivor |
second_survivor_resources.food | number | The amount of food of the second survivor |
second_survivor_resources.medication | number | The amount of medication of the second survivor |
second_survivor_resources.ammunition | number | The amount of ammunition of the second survivor |
Response
If successful, this method returns a response body with the following structure:
{
"message": "Trade completed!"
}
Percentage of infected survivors
Reports the percentage of infected survivors.
GET /api/survivors/reports/perc_infected
Request
GET https://zssn.azurewebsites.net/api/survivors/reports/perc_infected
Response
If successful, this method returns a response body with the following structure:
{
"percentage": number
}
Percentage of non-infected survivors
Reports the percentage of non-infected survivors.
GET /api/survivors/reports/perc_non_infected
Request
GET https://zssn.azurewebsites.net/api/survivors/reports/perc_non_infected
Response
If successful, this method returns a response body with the following structure:
{
"percentage": number
}
Averade amount of each kind of resource by survivor
Reports the average amount of each kind of resource by survivor.
GET /api/survivors/reports/avg_resources
Request
GET https://zssn.azurewebsites.net/api/survivors/reports/avg_resources
Response
If successful, this method returns a response body with the following structure:
{
"amount": number
}
Points lost because of infected survivor
Reports the amount of points lost because of an infected survivor.
GET /api/survivors/reports/points_lost
Request
GET https://zssn.azurewebsites.net/api/survivors/reports/points_lost
Response
If successful, this method returns a response body with the following structure:
{
"amount": number
}
Testing
Automated tests were written with Mocha and SuperTest. To run the tests:
npm test
Contributing
PRs accepted.
License
5 years ago