A Node.js library for KNavalBattle/KBattleship.
- Install the library into your Node.js project:
$ npm install knavalbattle.js
- Import the client:
// ESM:
import { NavalClient } from "knavalbattle.js";
// CJS:
const { NavalClient } = require("knavalbattle.js");
- Initialize the client and events:
const client = new NavalClient("NICKNAME");
client.on("connect", (player) => {
client.sendChatMessage(`Hi ${player.nickname}!`);
});
client.on("begin", () => {
// Create your own ships and track them.
});
client.on("move", (hit) => {
client.sendMoveResponse(hit.respond('miss'));
});
client.on("gameOver", (winner) => {
if (winner.me) {
client.sendChatMessage("I win!");
} else {
client.sendChatMessage("You won!");
}
});
- Connect to a peer!
client.connect(54321, "127.0.0.1");
YES PLEASE. I made this in about 7 hours and so there are bound to be bugs. If you discover any bugs or anything, make an issue or a PR on the GitHub repo!
This repository and NPM package are licensed under the MIT License. A version of the license can be viewed in the LICENSE file.