Permalink
Browse files

misc:extract module

  • Loading branch information...
1 parent a1cacf6 commit 057c2d7463901cf3710183197003a52f1a45bcb1 yaopiaohai committed Jul 30, 2012
View
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>robot</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>com.googlecode.jslint4java.eclipse.jsLintBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
- <nature>com.googlecode.jslint4java.eclipse.jsLintNature</nature>
- </natures>
-</projectDescription>
View
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path=""/>
- <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
- <classpathentry kind="output" path=""/>
-</classpath>
@@ -1 +0,0 @@
-org.eclipse.wst.jsdt.launching.baseBrowserLibrary
@@ -1 +0,0 @@
-Window
View
Binary file not shown.
View
@@ -0,0 +1,4 @@
+module.exports.Server = require('./lib/master/server').Server;
+module.exports.Agent = require('./lib/agent/agent').Agent;
+module.exports.HTTP_SERVER = require('./lib/console/http').HTTP_SERVER;
+module.exports.Robot = require('./lib/main').Robot;
View
@@ -20,15 +20,15 @@ var Agent = function(conf) {
this.messages_sent = 0;
this.connected = false;
this.reconnecting = false;
- var agent = this;
this.conf.debug = conf.debug || true;
this.conf.apps = conf.apps;
this.conf.data = conf.data;
this.appLength = conf.apps.length;
this.nodeId = conf.nodeId;
- this.dataCount = __.size(agent.conf.data);
+ this.script = conf.script;
+ this.dataCount = conf.data.length;
this.pageSize = conf.pageSize || 1;
- this.pageCount = this._dataCount/this.pageSize;
+ this.pageCount = this.dataCount/this.pageSize;
this.pageCur = 1;
this.count = 0;
this.machines = [];
@@ -85,14 +85,14 @@ Agent.prototype = {
run:function(){
var agent = this;
agent.runusers = agent.conf.data.slice(0,this.pageCur * this.pageSize);
- this._count = __.size(agent.runusers);
- agent.log.info(this.nodeId + ' run with ' + __(agent.runusers).size() + ' robots,times:' + this.pageCur);
+ this.count = agent.runusers.length;
+ agent.log.info(this.nodeId + ' run ' + agent.runusers.length + ' robots,times:' + this.pageCur);
monitor.clear();
var machines = [];
for (var index in agent.runusers) {
var obj = agent.runusers[index];
var app = agent.conf.apps[index%agent.appLength];
- var machine = new Machine(app,obj,agent.code);
+ var machine = new Machine(app,obj,agent.script);
machine.on('done',function(msg){
agent.check();
});
@@ -104,7 +104,7 @@ Agent.prototype = {
close:function(){
var agent = this;
- this._count = __.size(agent.runusers);
+ this.count = __.size(agent.runusers);
__.each(agent.machines,function(ele){ele.socket.disconnect();});
agent.pageCur++;
//console.log('close ' + agent._nodeId + ' agent.times ' + agent.pageCur + ' ' + agent.pageCount);
@@ -115,9 +115,9 @@ Agent.prototype = {
check: function() {
var agent = this;
- this._count--;
+ this.count--;
//console.error('this._counthis._counthis._coun ' + this._count)
- if (this._count==0){
+ if (this.count==0){
//console.log('machine done report');
//process.exit();
var d = monitor.getData();
View
@@ -7,20 +7,15 @@ var Robot = require('./robot').Robot;
var logging = require('../common/logging').Logger;
-var Machine = function(app,user,code) {
+var Machine = function(app,user,script) {
EventEmitter.call(this);
this.log = logging;
this.conf = {};
this.conf.app = app;
this.conf.encoding = 'utf8';
this.connected = false;
this.reconnecting = false;
- var machine = this;
- if (!!code){
- this.content = code;
- } else {
- this.content = fs.readFileSync(process.cwd() + '/lib/config/lord.js', this.conf.encoding);
- }
+ this.script = script;
this.socket = null;
this.user = user;
this.prepare();
@@ -47,7 +42,7 @@ pro.run = function() {
self.emit('done',msg);
});
var context = vm.createContext(initSandbox);
- vm.runInContext(this.content,context);
+ vm.runInContext(self.script,context);
};
exports.Machine = Machine;
View
@@ -1,94 +0,0 @@
-var _uid = 0;
-var _areaId = 0;
-var _treasures = [];
-var _users = [];
-var _uposX = 0;
-var _uposY = 0;
-var _speed = 50;
-var _isMove = false;
-
-//var robot = new Robot(Iuser, Isocket);
-
-var login = function(){
- var data = {route:'connector.loginHandler.login', username:Iuser.username, password:Iuser.passwd};
- robot.pushMessage(data);
-};
-
-var _paths = [];
-var _stepCount = 10;
-var _index = 0;
-var MAP_WIDTH = 2000;
-var MAP_HEIGHT = 1200;
-
-var genPaths = function() {
- for(var i=0; i<_stepCount; i++) {
- _paths[i] = {x: Math.random() * 2000, y: Math.random() * 1200};
- }
-};
-
-genPaths();
-robot.later(login, 1000);
-
-/**
- * 处理登录请求
- */
-robot.on('connector.loginHandler.login', function(data) {
- var userData = data.userData;
- var uid = userData.uid;
- if (uid <= 0) {
- console.log("用户不存在\n uid:" + uid + " code:" + data.code);
- }else{
- _uid = uid;
- _uposX = userData.x;
- _uposY = userData.y;
- _areaId = userData.sceneId;
- robot.pushMessage({route: "area.userHandler.addUser"});
- robot.later(move, 2000);
- }
-});
-
-//robot.on('area.userHandler.addUser', function(data) {
-// console.error('haha~~~~~~~~~~~~~~');
-//});
-
-/**
- * 处理用户移动请求
- */
-robot.on('area.userHandler.move',function(data){
- //if (_uid == data.body.uid){
- // this.done();
- //}
-});
-
-robot.on('onMove',function(data){
-});
-
-var move = function(){
- if (_uid<=0 || _isMove) {
- return;
- }
-
- if(_index >= _paths.length) {
- robot.done();
- return;
- }
-
- var step = _paths[_index++];
-
- var dx = _uposX - step.x;
- var dy = _uposY - step.y;
- var dis = Math.sqrt(dx * dx + dy * dy);
- var timeNum = dis / _speed * 1000;
- var startX = _uposX;
- var startY = _uposY;
- var path = [{x:startX, y:startY}, {x:step.x, y: step.y}];
- robot.pushMessage({route:"area.userHandler.move", path: path, time: timeNum,uid: _uid});
- _isMove = true;
- robot.later(function(){
- _uposX = step.x;
- _uposY = step.y;
- _isMove = false;
- move();
- }, timeNum);
-};
-
View
@@ -1,135 +0,0 @@
-var _uid = 0;
-var _areaId = 0;
-var _treasures = [];
-var _users = [];
-var _uposX = 0;
-var _uposY = 0;
-var _speed = 50;
-var _isMove = false;
-
-//var robot = new Robot(Iuser, Isocket);
-
-var login = function(){
- var data = {route:'connector.loginHandler.login', username:Iuser.username, password:Iuser.passwd};
- robot.pushMessage(data);
-};
-
-login();
-
-/**
- * 捡宝物回调
- * @param {Object} data
- */
-robot.on('area.treasureHandler.pickItem',function(result){
- if(result.success){
- delete _treasures[treasureId];
- //console.log('success picked up treasure ' + treasureId);
- }
-});
-
-
-/**
- * 处理登录请求
- */
-robot.on('connector.loginHandler.login', function(data){
- var userData = data.userData;
- var uid = userData.uid;
- //console.log(' onLogin userData: '+JSON.stringify(userData));
- if (uid <= 0) {
- console.log("用户不存在\n uid:" + uid + " code:" + data.code);
- }else{
- _uid = uid;
- _uposX = userData.x;
- _uposY = userData.y;
- _areaId = userData.sceneId;
- robot.pushMessage({route:"area.treasureHandler.getTreasures", uid: uid});
- robot.pushMessage({route:"area.userHandler.addUser",uid: uid});
- robot.pushMessage({route:"area.userHandler.getOnlineUsers", uid: uid,areaId:_areaId});
- robot.later(move,2000);
- }
-});
-
-/**
- * 处理自动生成宝物请求
- */
-robot.on('onGenerateTreasures',function(data){
- //console.log('on generateTreasures invoked data type: '+data.type+' code: '+data.code);
- _treasures = data.body.treasures;
-});
-
-
-
-/**
- * 处理宝物请求
- */
-robot.on('area.treasureHandler.getTreasures',function(data){
- _treasures = data.result;
-});
-
-/**
- * 处理在线用户请求
- */
-robot.on('area.userHandler.getOnlineUsers',function(data){
- //console.log(' getOnline Users data ' + JSON.stringify(data));
- _users = data.result;
- for(var key in _users){
- if(_users[key].uid == _uid){
- delete _users[key];
- break;
- }
- }
-});
-
-/**
- * 处理用户离开请求
- */
-robot.on('onUserLeave',function(data){
- //console.log("用户离开: " + JSON.stringify(data.uid));
-});
-/**
- * 处理用户加入请求
- */
-robot.on('onUserJoin',function(data){
- //console.log("新用户加入: " + JSON.stringify(data.user));
-});
-
-/**
- * 处理用户移动请求
- */
-robot.on('area.userHandler.move',function(data){
- if (_uid==data.body.uid){
- this.done();
- }
-});
-
-robot.on('onMove',function(data){
-});
-
-var move = function(){
- if (_uid<=0 || _isMove) return;
- var treas = [];
- for (var id in _treasures){
- treas.push(_treasures[id]);
- }
- var index = Math.round(Math.random()*(treas.length-1) +1);
- var treasure = treas[index];
- if (!treasure) {
- treasure = {};
- treasure.posX = Math.round(Math.random()*(1200-100) +100);
- treasure.posY = Math.round(Math.random()*(700-100) +100);
- }
- var dis = Math.sqrt(treasure.posX * _uposX + treasure.posY * _uposY);
- var timeNum = dis / _speed * 1000;
- //console.log(' uid ' + _uid + ' move to x:= ' + treasure.posX + ' y:= ' + treasure.posY + ' time: ' + timeNum );
- var startX = _uposX;
- var startY = _uposY;
- var path = [{x:startX, y:startY},{x:treasure.posX, y:treasure.posY}];
- robot.pushMessage({route:"area.userHandler.move", path: path, time: timeNum,uid:_uid});
- _isMove = true;
- robot.later(function(){
- _uposX = treasure.posX;
- _uposY = treasure.posY;
- _isMove = false;
- },timeNum);
-};
-
View
@@ -1,16 +0,0 @@
-{
- "dev":
- {
- "master":{"host": "127.0.0.1","port":8888,"webport":8889},
- "apps":[{"host":"192.168.145.18","port":3050}],
- "mysql":{"host":"192.168.145.18","user":"xy","password":"dev","database":"Pomelo"},
- "clients":["127.0.0.1"]
- },
- "prod":
- {
- "master":{"host": "app47v2.photo.163.org","port":8888,"webport":8889},
- "apps":[{"host":"mon2.photo.163.org","port":3050}],
- "mysql":{"host":"app56v1.photo.163.org","user":"xy","password":"dev","database":"Pomelo"},
- "clients":["127.0.0.1","app47v3.photo.163.org","app47v4.photo.163.org"]
- }
-}
Oops, something went wrong.

0 comments on commit 057c2d7

Please sign in to comment.