0.3.3 • Published 6 years ago
libbellum v0.3.3
libbelum
JavaScript library for interfacing with the Bellum.io game. It contains models which represents a entire game (GameLog) and which represent the game at a specific point in time (Game).
Requirements
Install
Simply add libbellum as a dependency:
npm install --save libbellum
Example usage
See the bellum recorder for a good example on how it is used to record games.
Creating a game state using libbellum.Game and interacting with it:
const libbellum = require('libbellum');
// Initalize a game with server data. Normally you get this from the bellum.io 'serverData' socket.io event
let game = new libbellum.Game(["/#y_lctBx_FCFNFxwGACDk", // id
13, // player count
7200, // match Time
"2017-07-01T11:26:32.917Z", // reset Time
100, // server map size
[15,100,500,3000,10000], // troop cost
[1,5,40,100,400], // click mp gain levels
2.5, // attack cooldown
100]) // max players
// It is possible to call game event functions directly.
game.playerData([{"/#Z7Da4HN8wQaJXnuWAHDR":[64,13,"Darwin\'s_spider(TSW)",9715,"snow"],
"/#h35s75zEBvSCZi44AHET":[67,2,"Darn",6942,"default"],
"/#hjZ0GrQipOUdecYtAHEk":[56,10,"Marxism(OAT)",2513,"red"],
"/#H95sZEPL2_L7L6oUAHFO":["75","26","LifeDeathPanda",3079,"default"],
"/#eoAzP2FuDjDCdgK8AHGf":[1,34,"школиник(Guest)",0,"default"],
"/#1DpUU8V7vadGX7qBAHGl":[35,84,"viola(Oat)",2001,"terminalShield"],
"/#lhIM3LB8-algWnMNAHGn":[38,96,"oat(Oat)",1500,"default"],
"/#8PmSAwnSW0AcdakgAHG1":[2,67,"lol(Guest)",100,"default"],
"/#dcQ3y9gXhqVa_9BzAHG9":["90","33","NaziGermany(Guest)",700], // huh? A number?
}])
// See gameEventNames array in Game.js to see which events can be called and handeled.
// Alternatively you can let the game listen on some socket.io socket.
// This is untested.
game.listen(someSocket)
// At any point you can inspect the state of the game.
game.players; // Is a array of libbellum.Player objects.
game.tiles; // Is a fixed-2d-array object containing libbellum.Tile objects.
game.tiles.forEach(function(tile) {
// Do something with each tile. See Tile.js for all properties.
// Information about defenses, type, owner and x/y location is stored.
});
A game state can also be created from a GameLog like this:
const libbellum = require('libbellum');
let gamelog = new libbellum.GameLog();
fs.readFile('gamelog.json', (err, data) => {
if(!err)
{
gamelog.importFromPlainObject(JSON.parse(data));
let game = new libbellum.Game(gamelog.getServerData());
gamelog.forEach(function(ms_since_start, i, eventName, eventData) {
if(game[eventName])
{
game[eventName].apply(game, eventData);
}
});
}
});