add api, make login a server plugin

This commit is contained in:
Romain Beaumont 2015-08-26 03:49:06 +02:00
parent 034cd43363
commit dde4387536
4 changed files with 75 additions and 24 deletions

View file

@ -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

View file

50
docs/api.md Normal file
View file

@ -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`

View file

@ -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;