You need to install NodeJS and MongoDB
Installing node module
npm i
Setting server config at config.json
"Database": {
"ip": "localhost",
"port": "27017",
"user": "battleship",
"password": "battleship_very_secure_password",
"database": "battleship"
},
"api": {
"port": 3000
}
You can change your game config such as ocean size, ship size, ship amount, etc....
"game": {
"size": 10,
"ship_data": [
{
"name": "Battleship",
"amount": 1,
"size": 4
},
{
"name": "Cruisers",
"amount": 2,
"size": 3
},
{
"name": "Destroyers",
"amount": 3,
"size": 2
},
{
"name": "Submarines",
"amount": 4,
"size": 1
}
]
}
Runing the automated tests
npm run test
In the beginning, you must start your first match by request to /game/new
, after that you can choose a position to shoot in match by request to /game/shoot/:x/:y
.
The response will tell you if it hit or miss or you just sank the ship.
You can check your shoot history of the current match by request to /game/shoot/history
or check match history /game/match/history
.
Also, if you want to check your stats you can request to /stats
. The response will show only win,hit,miss and sunk.
If you want to delete your player data, you must request to /game/deactivate
.
You can't request new match if your last match is still ongoing. If you want to request a new match you must give up on your last match by
request to /game/giveup
to give up your last match.
Do not share your player name to anyone, or another player can play your match or deactivate your data.
Returns result as json data.
-
URL
/game/new
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 201
Content:{ "status": true, "message": "New game succesful !.", "map_size": 10 }
OR
- Code: 200
Content:{ "status": false, "message": "You not finish your last match yet. " }
- Code: 201
-
Sample Call Javascript
$.ajax({ url: "/game/new", dataType: "json", method : "POST", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request POST \ --url http://localhost:3000/game/new \ --data player_name=your_name
Returns result as json data.
-
URL
/game/giveup
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 200
Content:{ "status": true, "message": "You just give up on your last match !." }
OR
- Code: 200
Content:{ "status": false, "message": "You have no playing match." }
- Code: 200
-
Sample Call Javascript
$.ajax({ url: "/game/giveup", dataType: "json", method : "POST", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request POST \ --url http://localhost:3000/game/giveup \ --data player_name=your_name
Delete player data and returns result as json data.
-
URL
/game/deactivate
-
Method:
DELETE
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 200
Content:{ "status": true, "message": "Delete player successful !." }
- Code: 200
-
Error Response:
- Code: 404
Content:{ "status": false, "message": "Player name doesn't exist." }
- Code: 404
-
Sample Call Javascript
$.ajax({ url: "/game/deactivate", dataType: "json", method : "DELETE", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request DELETE \ --url http://localhost:3000/game/deactivate \ --data player_name=your_name
Returns result as json data.
-
URL
/game/shoot/:x/:y
-
Method:
POST
-
URL Params
Required:
x=[integer]
: position of xy=[integer]
: position of y -
Data Params
player_name
: player name -
Success Response:
-
Code: 200
Content:{ "status": true, "message": "Miss" }
OR
-
Code: 200
Content:{ "status": true, "message": "Hit" }
OR
-
Code: 200
Content:{ "status": true, "message": "Win ! You completed the game in 3 moves", "win": { "turns": 3 } }
OR
-
Code: 200
Content:{ "status": false, "message": "Shoot position over map !. You must shoot between (0,0) till (9,9)" }
OR
-
Code: 200
Content:{ "status": false, "message": "The position (8,4) already shooted." }
-
-
Error Response:
-
Code: 404
Content:{ "status": false, "message": "Match playing not found." }
OR
-
Code: 404
Content:{ "status": false, "message": "Player not found." }
-
-
Sample Call Javascript
$.ajax({ url: "/game/shoot/7/1", dataType: "json", method : "POST", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request POST \ --url http://localhost:3000/game/shoot/7/1 \ --data player_name=your_name
Returns result as json data.
-
URL
/game/shoot/history
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 200
Content:{ "status": true, "history": [ { "x": 7, "y": 1, "hit": false, "time": "2018-03-10T14:24:06.724Z" } , ... ] }
- Code: 200
-
Error Response:
-
Code: 404
Content:{ "status": false, "message": "Shoot history not found." }
OR
-
Code: 404
Content:{ "status": false, "message": "Player not found." }
-
-
Sample Call Javascript
$.ajax({ url: "/game/shoot/history?player_name=your_name", dataType: "json", method : "GET", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request GET \ --url http://localhost:3000/game/shoot/history?player_name=your_name \ --data player_name=your_name
Returns result as json data.
-
URL
/game/match/history
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 200
Content:{ "status": true, "history": [{ "map_size":10, "turn": 7, "ship_left": 9, "ending": true, "shooted": [ { "x": 1, "y": 5, "hit": false, "time": "2018-03-10T08:45:49.791Z" } , ...], "ships": [ { "id": 1, "ship_name": "Battleship", "sunk": true } , ...], "ocean": [ { "x": 4, "y": 7, "ship_id": 1, "hit": true } , ...] } , ... ] }
- Code: 200
-
Error Response:
- Code: 404
Content:{ "status": false, "message": "Player not found." }
- Code: 404
-
Sample Call Javascript
$.ajax({ url: "/game/match/history?player_name=your_name", dataType: "json", method : "GET", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request GET \ --url http://localhost:3000/game/match/history?player_name=your_name \ --data player_name=your_name
Returns result as json data.
-
URL
/stats
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
player_name
: player name -
Success Response:
- Code: 200
Content:{ "status": true, "stats": { "game": 0, "hits": 0, "miss": 0, "sunk": 0 } }
- Code: 200
-
Error Response:
- Code: 404
Content:{ "status": false, "message": "Player not found." }
- Code: 404
-
Sample Call Javascript
$.ajax({ url: "/stats?player_name=your_name", dataType: "json", method : "GET", data: { "player_name": "your_name" } success : function(r) { console.log(r); } });
Shell
curl --request GET \ --url http://localhost:3000/stats?player_name=your_name \ --data player_name=your_name
- Wasawat Lertjankhajorn - yacth_Mon