From dde4387536346393deb8617092d42dfa37f02081 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 26 Aug 2015 03:49:06 +0200 Subject: [PATCH] add api, make login a server plugin --- README.md | 3 ++ docs/API.md | 0 docs/api.md | 50 +++++++++++++++++++ lib/{clientPlugins => serverPlugins}/login.js | 46 ++++++++--------- 4 files changed, 75 insertions(+), 24 deletions(-) delete mode 100644 docs/API.md create mode 100644 docs/api.md rename lib/{clientPlugins => serverPlugins}/login.js (88%) diff --git a/README.md b/README.md index fad84b8..256b0af 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,9 @@ Before running or building it is recommended that you configure the server in co ## Documentation Documentation for how to operate and how to customize your server are coming soon! +## Dev Documentation +For development see [api.md](docs/api.md) + ## Contributors - @roblabla for helping out with the protocols diff --git a/docs/API.md b/docs/API.md deleted file mode 100644 index e69de29..0000000 diff --git a/docs/api.md b/docs/api.md new file mode 100644 index 0000000..2d7b15e --- /dev/null +++ b/docs/api.md @@ -0,0 +1,50 @@ +# API + +## MCServer + +### CraftyJS.createMCServer(options) + +Create and return an instance of the class MCServer. + +options is an object containing the settings + +### Properties + +#### serv.entityMaxId + +Current maximum entity id + +#### serv.playersConnected + +Array of connected players + +#### serv.uuidToPlayer + +Object uuid to players + +#### serv.world + +The map + +### Methods + +### serv.login(client) + +login `client` + +### serv.createLog() + +create the log file + +### serv.log(message) + +logs a `message` + +### serv.otherClients(client) + +return the other clients than `client` + +### serv.writeOthers(client,packetName, packetFields) + +write to other clients than `client` the packet `packetName` with fields `packetFields` + diff --git a/lib/clientPlugins/login.js b/lib/serverPlugins/login.js similarity index 88% rename from lib/clientPlugins/login.js rename to lib/serverPlugins/login.js index 31b5ae6..1cc4764 100644 --- a/lib/clientPlugins/login.js +++ b/lib/serverPlugins/login.js @@ -1,5 +1,3 @@ -var log = require("../serverPlugins/log").log; - module.exports=inject; function transformUuid(s) @@ -8,11 +6,11 @@ function transformUuid(s) } -function inject(serv,client) +function inject(serv) { serv.login=login; - function addPlayer() + function addPlayer(client) { serv.entityMaxId++; client.id = serv.entityMaxId; @@ -21,7 +19,7 @@ function inject(serv,client) serv.uuidToPlayer[client.uuid] = client; } - function sendLogin() + function sendLogin(client) { // send init data so client will start rendering world client.write('login', { @@ -34,7 +32,7 @@ function inject(serv,client) maxPlayers: serv._server.maxPlayers }); } - function sendMap() + function sendMap(client) { client.write('map_chunk', { x: 0, @@ -45,7 +43,7 @@ function inject(serv,client) }); } - function sendInitialPosition() + function sendInitialPosition(client) { client.write('position', { x: 6, @@ -57,7 +55,7 @@ function inject(serv,client) }); } - function updateTime() + function updateTime(client) { client.write('update_time', { age: [0, 0], @@ -65,7 +63,7 @@ function inject(serv,client) }); } - function updateGameState() + function updateGameState(client) { client.write('game_state_change', { reason: 3, @@ -73,7 +71,7 @@ function inject(serv,client) }); } - function announceLogin() + function announceLogin(client) { client.on('chat', function (data) { var message = '<' + client.username + '>' + ' ' + data.message; @@ -83,7 +81,7 @@ function inject(serv,client) }); } - function fillTabList() + function fillTabList(client) { serv.otherClients(client).forEach(function (otherClient) { otherClient.write('player_info', { @@ -117,7 +115,7 @@ function inject(serv,client) }); } - function spawn() + function spawn(client) { serv.otherClients(client).forEach(function (otherClient) { var pos = serv.uuidToPlayer[otherClient.uuid].position; @@ -146,7 +144,7 @@ function inject(serv,client) }); } - function announceJoin() + function announceJoin(client) { broadcast(client.username + ' joined the game.', "yellow"); var addr = client.socket.remoteAddress + ':' + client.socket.remotePort; @@ -190,27 +188,27 @@ function inject(serv,client) } - function login() + function login(client) { if (serv.uuidToPlayer[client.uuid]) { client.end("You are already connected"); return; } - addPlayer(); - sendLogin(); - sendMap(); - sendInitialPosition(); + addPlayer(client); + sendLogin(client); + sendMap(client); + sendInitialPosition(client); console.log("[INFO]: position written, player spawning..."); serv.log("[INFO]: position written, player spawning..."); - updateTime(); - updateGameState(); - announceLogin(); - fillTabList(); - spawn(); + updateTime(client); + updateGameState(client); + announceLogin(client); + fillTabList(client); + spawn(client); - announceJoin(); + announceJoin(client); var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;