//账号和角色相关类
var KBEngine = require("kbengine");


/*-----------------------------------------------------------------------------------------
												entity
-----------------------------------------------------------------------------------------*/
KBEngine.Account = KBEngine.Entity.extend(
	{
		__init__: function () {
			this._super();
			KBEngine.Event.fire("onLoginSuccessfully", KBEngine.app.entity_uuid, this.id, this);

			this.avatars = {};
			this.baseCall("reqAvatarList");
			//this.baseCall('reqCreateAvatarTemp');
		},

		onReqAvatarTemp: function (info) {
			console.log('进入游戏场景:onReqAvatarTemp:',info);
			// this.avatars[info.dbid] = info;
			// this.avatars.values.push(info);
			KBEngine.Event.fire("onReqAvatarTemp");
		},

		reqEnterGame:function(){
			console.log('进入游戏场景，发送服务器请求:reqEnterGame');
			this.baseCall("reqEnterGame");
		},

		onCreateAvatarResult: function (retcode, info) {
			if (retcode == 0) {
				this.avatars[info.dbid] = info;
				this.avatars.values.push(info);
				console.info("KBEAccount::onCreateAvatarResult: name=" + info.name);
			}

			console.info("KBEAccount::onCreateAvatarResult: avatarsize=" + this.avatars.values.length + ", error=" + KBEngine.app.serverErr(retcode));
			KBEngine.Event.fire("onCreateAvatarResult", retcode, info, this.avatars);
		},

		onReqAvatarList: function (infos) {
			this.avatars = infos;
			console.info("KBEAccount::onReqAvatarList: avatarsize=" + this.avatars.values.length);
			for (var i = 0; i < this.avatars.values.length; i++) {
				console.info("KBEAccount::onReqAvatarList: name=" + i + "=" + this.avatars.values[i].name);
			}

			KBEngine.Event.fire("onReqAvatarList", this.avatars);


		},

		onRemoveAvatar: function (dbid) {
			if (this.avatars["values"].length <= 0)
				return;

			var done = false;
			var values = [];
			for (var i = 0; i < this.avatars.values.length; i++) {
				if (this.avatars.values[i].dbid != dbid)
					values.push(this.avatars.values[i]);
				else
					done = true;
			}

			if (done) {
				this.avatars.values = values;
				console.info("Account::onRemoveAvatar: dbid=" + dbid);
				KBEngine.Event.fire("onRemoveAvatar", dbid, this.avatars);
			}
		},

		reqCreateAvatar: function (roleType, name) {
			console.log('reqCreateAvatar==', roleType, name);
			this.baseCall("reqCreateAvatar", roleType, name);
		},

		selectAvatarGame: function (dbid) {
			this.baseCall("selectAvatarGame", dbid);
		},

			//寻找房间
		//匹配对应的房间
		joinRoom:function(){
			console.log('发送请求加入房间');
			this.baseCall("reqJoinRoom");
		},
		onAvatarReady:function(eid,count,name){
			KBEngine.INFO_MSG("onAvatarReady infos:"+eid+',count='+count+',name='+name);
			KBEngine.Event.fire("onAvatarReady", eid,count,name);
        }
	});





