1.0.0 • Published 5 years ago

zssn v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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.

npm.io

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
    }
}
NameTypeDescription
infectedbooleanThe flag that defines if the survivor is infected or not
_idstringThe ID of the survivor
namestringThe name of the survivor
agenumberThe age of the survivor
sexstringThe gender of the survivor
inventoryobjectThe inventory that contains the resources of the survivor
inventory.waternumberThe number of water resources the survivor has
inventory.foodnumberThe number of food resources the survivor has
inventory.medicationnumberThe number of medication resources the survivor has
inventory.ammunitionnumberThe number of ammunition resources the survivor has
locationobjectThe last location of the survivor
location.latitudenumberThe latitude of the survivor's location
location.longitudenumberThe longitude of the survivor's location
reportMessagesarrayThe messages reported against the survivor
totalInfectionsnumberThe 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:

NameTypeDescription
namestringThe name of the survivor
agenumberThe age of the survivor
sexstringThe gender of the survivor
locationobjectThe last location of the survivor
location.latitudenumberThe latitude of the survivor's location
location.longitudenumberThe 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
    }
}
NameTypeDescription
infectedbooleanThe flag that defines if the survivor is infected or not
_idstringThe ID of the survivor
namestringThe name of the survivor
agenumberThe age of the survivor
sexstringThe gender of the survivor
inventoryobjectThe inventory that contains the resources of the survivor
inventory.waternumberThe number of water resources the survivor has
inventory.foodnumberThe number of food resources the survivor has
inventory.medicationnumberThe number of medication resources the survivor has
inventory.ammunitionnumberThe number of ammunition resources the survivor has
locationobjectThe last location of the survivor
location.latitudenumberThe latitude of the survivor's location
location.longitudenumberThe longitude of the survivor's location
reportMessagesarrayThe messages reported against the survivor
totalInfectionsnumberThe 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:

NameTypeDescription
latitudenumberThe latitude of the survivor
longitudenumberThe 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:

NameTypeDescription
reportstringMessage 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).

ItemPoints
1 Water4 points
1 Food3 points
1 Medication2 points
1 Ammunition1 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:

NameTypeDescription
first_survivor_resourcesobjectThe resources of the first survivor
first_survivor_resources.waternumberThe amount of water of the first survivor
first_survivor_resources.foodnumberThe amount of food of the first survivor
first_survivor_resources.medicationnumberThe amount of medication of the first survivor
first_survivor_resources.ammunitionnumberThe amount of ammunition of the first survivor
second_survivor_resourcesobjectThe resources of the second survivor
second_survivor_resources.waternumberThe amount of water of the second survivor
second_survivor_resources.foodnumberThe amount of food of the second survivor
second_survivor_resources.medicationnumberThe amount of medication of the second survivor
second_survivor_resources.ammunitionnumberThe 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

MIT © Richard McRichface.

1.0.0

5 years ago