var net = require('net');
var ctype = require('ctype');

module.exports.load = function(eventBus, io, name) {
	// add client scripts; plugin must match this plugin's name, filename is optional and defaults to "client.js"
	// the actual files must be located in plugins/PLUGINNAME/public/
	eventBus.emit('addscripts', [{plugin: name}]);
	
	var rcon = Rcon('nohayclavequevalga', 'acechadores.com');
	
	/* the following events are available:
		procstart(isUnattached)
		procstop()
		userjoin()
		userleave()
	*/
	eventBus.on('procstart', function(isUnattached) {
		rcon.connect(1, 5)
		io.of('/rcon').emit('data', name+': game is started '+(isUnattached?'(unattached)':''));
	});
	eventBus.on('procstop', function() {
		io.of('/rcon').emit('data', name+': game is stopped');
	});
	eventBus.on('userjoin', function(socket) {
		io.of('/rcon').emit('data', name+': User joined');
	});
	eventBus.on('userleave', function() {
		io.of('/rcon').emit('data', name+': User left');
	});
	
	
	
	// must return object with property "unload" being a function with a callback as argument.
	// said callback has an optional "error" argument.
	// eventbus listeners are automatically removed.
	return {
		name: name,
		unload: function(cb) {
			//io.of('/rcon').removeAllListeners('connection');
			cb();
		},
	};
};

var Rcon = function(password, host, port) {
	if (!port) {
		port = 27015;
	}
	
	var packetId = 0;
	var socket;
	
	

	

	
	var getPacket = function(id, type, string1, string2) {
		if (typeof string1 == 'undefined') {
			string1 = '';
			string2 = '';
		} else if(typeof string2 == 'undefined') {
			string2 = '';
		}
		var data = string1 + String.fromCharCode(0) + string2 + String.fromCharCode(0);
		
		// create packet string
		var buffer = new Buffer(2 + Buffer.byteLength(data));
		ctype.wfloat(id, buffer, 'little', 0);
		ctype.wfloat(type, buffer, 'little', 1);
		
		buffer.write(data, 2);
		
		
		// prefix with length
		var bufferWithSize = new Buffer(buffer.length + 1);
		ctype.wfloat(buffer.length, bufferWithSize, 'little', 0);
		buffer.copy(bufferWithSize, 1);
		
		return bufferWithSize;
	};
	
	var createConnection = function(cb) {
		socket = net.createConnection(port, host);
		socket.on('connect', function(e) {
			socket.removeAllListeners('connect');
			socket.removeAllListeners('error');
			cb();
		});
		socket.on('error', function(e) {
			socket.removeAllListeners('connect');
			socket.removeAllListeners('error');
			cb(e);
		});
	};
	
	var authenticate = function() {
		var packet = getPacket(++packetId, 3, password);
		console.log(packet.toString());
		
		socket.write(packet);
		
		socket.on('data', function(data) {
			io.of('/rcon').in('all').emit('data', 'rcon: '+data);
			console.log('data', data);
		});
		socket.on('error', function(e) {
			console.log('error', e);
		})
		socket.on('close', function(e) {
			console.log('close', e);
		})
		socket.on('end', function() {
			console.log('end');
		});
		socket.on('timeout', function() {
			console.log('timeout');
		});
		/*socket.on('drain', function() {
			console.log('drain');
		});*/
	};
	
	var timer = null;
	
	return {
		connect: function(timeout, retries) {
			console.log('connect to '+host+':'+port);
			var connect = function() {
				createConnection(function(err) {
					if (err) {
						//console.log(err);
						if (retries > 0) {
							console.log('connection failure, retrying...');
							retries--;
							timer = setTimeout(connect, timeout * 1000);
						} else {
							console.log('retries exhausted.');
						}
					} else {
						console.log('connected');
						authenticate();
						
					}
				});
			};
			connect();
			
			
			
			
		},
		
		unload: function() {
			clearTimeout(timer);
		}
	};
};




@@ -1,40 +1,13 @@
 var 
-net = require('net');%0D%0Avar ctype
+srcds
  = r
@@ -14,21 +14,21 @@
 equire('
-ctype
+srcds
 ');%0D%0A%0D%0Am
@@ -314,49 +314,71 @@
 ;%0D%0A%09
-%0D%0A%09var rcon = Rcon('nohayclavequevalga', 
+var pluginio = io.of('/'+name);%0D%0A%09%0D%0A%09var rcon = new srcds.RCon(
 'ace
@@ -390,18 +390,28 @@
 res.com'
-);
+, 27015);%0D%0A%09
 %0D%0A%09%0D%0A%09/*
@@ -586,39 +586,82 @@
 con.
-connect(1, 5)%0D%0A%09%09io.of('/rcon')
+auth('nohayclavequevalga', function(err) %7B%0D%0A%09%09%09if (err)%0D%0A%09%09%7D);%0D%0A%09%09pluginio
 .emi
@@ -775,38 +775,32 @@
 tion() %7B%0D%0A%09%09
-io.of('/rcon')
+pluginio
 .emit('data'
@@ -878,38 +878,32 @@
 ocket) %7B%0D%0A%09%09
-io.of('/rcon')
+pluginio
 .emit('data'
@@ -976,30 +976,24 @@
 () %7B%0D%0A%09%09
-io.of('/rcon')
+pluginio
 .emit('d
@@ -1288,63 +1288,8 @@
  %7B%0D%0A
-%09%09%09//io.of('/rcon').removeAllListeners('connection');%0D%0A
 %09%09%09c

use srcds module for source rcon plugin