610 lines
19 KiB
ActionScript
610 lines
19 KiB
ActionScript
package net.pixvi.myr.center
|
||
{
|
||
import net.pixvi.common.system.language.lang;
|
||
import net.pixvi.common.utils.Alert;
|
||
import net.pixvi.myr.CP;
|
||
import net.pixvi.myr.GameMath;
|
||
import net.pixvi.myr.element.achi.AchiType;
|
||
import net.pixvi.myr.element.bag.EquipVo;
|
||
import net.pixvi.myr.element.bag.ItemType;
|
||
import net.pixvi.myr.element.bag.ItemVo;
|
||
import net.pixvi.myr.element.cost.CostVo;
|
||
import net.pixvi.myr.element.guide.GSub;
|
||
import net.pixvi.myr.element.mon.EggVo;
|
||
import net.pixvi.myr.element.mon.MonsterVo;
|
||
import net.pixvi.myr.event.MYREvent;
|
||
import net.pixvi.myr.net.NetItem;
|
||
import net.pixvi.myrane.MyrAne;
|
||
import net.pixvi.myrloader.center.MyrCenter;
|
||
import net.pixvi.pfw.utils.ArrayUtil;
|
||
import net.pixvi.pfw.utils.Paster;
|
||
import net.pixvi.pfw.utils.broadcast.GlobalBroadcast;
|
||
import net.pixvi.pfw.utils.core.Singleton;
|
||
import net.pixvi.pfw.utils.json.Json;
|
||
|
||
public class BagCenter
|
||
{
|
||
|
||
public var nearstUseMethod:int = 0;
|
||
|
||
public var nearstGetMethod:int = 0;
|
||
|
||
private var sell_para:Object;
|
||
|
||
private var useItem_para:Object;
|
||
|
||
private var onUseItemComplete:Function;
|
||
|
||
private var onGiftCodeComplete:Function;
|
||
|
||
public function BagCenter()
|
||
{
|
||
super();
|
||
}
|
||
|
||
public static function getName(type:int) : String
|
||
{
|
||
return Table.item_info[type].name;
|
||
}
|
||
|
||
public static function getIns() : BagCenter
|
||
{
|
||
return Singleton.getInstanceOrCreate(BagCenter) as BagCenter;
|
||
}
|
||
|
||
public static function eggSpeedItemNum() : int
|
||
{
|
||
return getIns().getNum(GameMath.speedItemId);
|
||
}
|
||
|
||
public static function alchemySpeedItemNum() : int
|
||
{
|
||
return getIns().getNum(GameMath.alchemySpeedItemId);
|
||
}
|
||
|
||
public static function getItemByItemData(itemData:Object) : ItemVo
|
||
{
|
||
var item:ItemVo = null;
|
||
var eq:EquipVo = null;
|
||
switch(ItemVo.getItemType(itemData.typeId))
|
||
{
|
||
case ItemType.TYPE_EGG:
|
||
item = new EggVo();
|
||
break;
|
||
case ItemType.TYPE_EQUIP:
|
||
item = new EquipVo();
|
||
eq = item as EquipVo;
|
||
eq.isUsed = itemData.isUsed > 0;
|
||
if(itemData.equipParams == null || itemData.itemClass != 3)
|
||
{
|
||
itemData.equipPara = {
|
||
"apCostSave":0,
|
||
"atk":0,
|
||
"atkPoi":0,
|
||
"atkPoiDmg":0,
|
||
"atkSpd":0,
|
||
"atkStun":0,
|
||
"atkVim":0,
|
||
"crit":0,
|
||
"deadBoom":0,
|
||
"def":0,
|
||
"defStun":0,
|
||
"dex":0,
|
||
"hp":0,
|
||
"moonHeal":0,
|
||
"selfCure":0,
|
||
"skillCooldown":0,
|
||
"sunFire":0,
|
||
"thorns":0
|
||
};
|
||
Alert.showMsg(Paster.font("equip error:" + itemData.id + ",pls contact customer service to fix。",null,14,true));
|
||
}
|
||
else
|
||
{
|
||
itemData.equipPara = Json.decode(itemData.equipParams);
|
||
}
|
||
eq.atk.val = itemData.equipPara.atk;
|
||
eq.def.val = itemData.equipPara.def;
|
||
eq.hp.val = itemData.equipPara.hp;
|
||
eq.dex.val = itemData.equipPara.dex;
|
||
eq.crit.val = itemData.equipPara.crit;
|
||
eq.atkSpd.val = itemData.equipPara.atkSpd;
|
||
eq.atkVim.val = itemData.equipPara.atkVim;
|
||
eq.selfCure.val = itemData.equipPara.selfCure;
|
||
eq.deadBoom.val = itemData.equipPara.deadBoom;
|
||
eq.atkStun.val = itemData.equipPara.atkStun;
|
||
eq.atkStunTime.val = 2;
|
||
eq.atkStunType = Table.item_info[itemData.typeId].atkStunType;
|
||
eq.defStun.val = itemData.equipPara.defStun;
|
||
eq.defStunTime.val = 2;
|
||
eq.defStunType = Table.item_info[itemData.typeId].defStunType;
|
||
eq.atkPoi.val = itemData.equipPara.atkPoi;
|
||
eq.atkPoiDmg.val = itemData.equipPara.atkPoiDmg;
|
||
eq.atkPoiType = itemData.equipPara.atkPoiType;
|
||
eq.apCostSave.val = itemData.equipPara.apCostSave;
|
||
eq.skillCooldown.val = itemData.equipPara.skillCooldown;
|
||
eq.sunFire.val = itemData.equipPara.sunFire;
|
||
eq.moonHeal.val = itemData.equipPara.moonHeal;
|
||
eq.thorns.val = itemData.equipPara.thorns;
|
||
break;
|
||
default:
|
||
item = new ItemVo();
|
||
}
|
||
item.id = itemData.id;
|
||
item.type = itemData.typeId;
|
||
item.num = itemData.num;
|
||
return item;
|
||
}
|
||
|
||
public function subNum(itemId:Number, num:int, show:Boolean = false, keyword:String = "") : ItemVo
|
||
{
|
||
var str:String = null;
|
||
if(keyword == null || keyword == "")
|
||
{
|
||
keyword = lang("消耗了 {0} 个 {1}");
|
||
}
|
||
var itemvo:ItemVo = getById(itemId);
|
||
itemvo.num -= num;
|
||
if(show)
|
||
{
|
||
str = lang(keyword,CP.item(num.toString()),CP.item(itemvo.name));
|
||
Alert.showMsg(str);
|
||
}
|
||
if(itemvo.num == 0)
|
||
{
|
||
removeItem(itemId);
|
||
}
|
||
else if(itemvo.num < 0)
|
||
{
|
||
throw new Error("err less than zero");
|
||
}
|
||
return itemvo;
|
||
}
|
||
|
||
public function subNumByType(itemTypeId:int, num:int, show:Boolean = false, keyword:String = "", sendEvent:Boolean = false) : ItemVo
|
||
{
|
||
var vo:ItemVo = subNum(getByType(itemTypeId).id,num,show,keyword);
|
||
if(sendEvent && vo != null)
|
||
{
|
||
GlobalBroadcast.dispatchEvent(new MYREvent(MYREvent.ITEM_NUM_CHANGE,vo));
|
||
}
|
||
return vo;
|
||
}
|
||
|
||
public function removeItem(itemid:Number) : void
|
||
{
|
||
ArrayUtil.removeByValue(Master.bag.items,"id",itemid);
|
||
}
|
||
|
||
public function checkNeed(needs:Array, money:int = 0, ke:int = 0) : Boolean
|
||
{
|
||
if(Master.money < money || Master.ke < ke)
|
||
{
|
||
return false;
|
||
}
|
||
for each(var o:Object in needs)
|
||
{
|
||
if(getNum(o.type) < o.num)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public function subLocalCost(cost:CostVo, show:Boolean = false, num:int = 1, sendEvent:Boolean = false) : void
|
||
{
|
||
var i:int = 0;
|
||
var itemdata:Object = null;
|
||
if(cost.ke > 0)
|
||
{
|
||
MasterCenter.getIns().addKe(-cost.ke * num,show);
|
||
}
|
||
if(cost.money > 0)
|
||
{
|
||
MasterCenter.getIns().addMoney(-cost.money * num,show);
|
||
}
|
||
if(cost.ti > 0)
|
||
{
|
||
MasterCenter.getIns().addTi(-cost.ti * num,show);
|
||
}
|
||
if(cost.items.length > 0)
|
||
{
|
||
for(i = 0; i < cost.items.length; )
|
||
{
|
||
itemdata = cost.items[i];
|
||
subNumByType(itemdata.typeId,itemdata.num * num,show,"",sendEvent);
|
||
i++;
|
||
}
|
||
}
|
||
}
|
||
|
||
public function checkCost(cost:CostVo, exceptEquipOn:Boolean = false, exceptTiKeMoney:Boolean = false) : Boolean
|
||
{
|
||
var i:int = 0;
|
||
var needi:Object = null;
|
||
var hasnum:int = 0;
|
||
if(!exceptTiKeMoney && (Master.ke < cost.ke || Master.money < cost.money || Master.ti < cost.ti))
|
||
{
|
||
return false;
|
||
}
|
||
i = 0;
|
||
while(i < cost.items.length)
|
||
{
|
||
needi = cost.items[i];
|
||
hasnum = BagCenter.getIns().getNum(needi.typeId,exceptEquipOn);
|
||
if(hasnum < needi.num)
|
||
{
|
||
return false;
|
||
}
|
||
i++;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public function addItem(itemvo:ItemVo) : void
|
||
{
|
||
var newItemVo:EggVo = null;
|
||
var oldvo:ItemVo = getById(itemvo.id);
|
||
if(oldvo == null)
|
||
{
|
||
if(itemvo.itemType == ItemType.TYPE_EGG)
|
||
{
|
||
newItemVo = new EggVo();
|
||
newItemVo.id = itemvo.id;
|
||
newItemVo.num = itemvo.num;
|
||
newItemVo.type = itemvo.type;
|
||
itemvo = newItemVo;
|
||
}
|
||
Master.bag.items.push(itemvo);
|
||
if(itemvo.itemType == ItemType.TYPE_EQUIP)
|
||
{
|
||
if(Master.history.equipLib.indexOf(itemvo.type.toString()) == -1)
|
||
{
|
||
Master.history.equipLib.push(itemvo.type.toString());
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
oldvo.num += itemvo.num;
|
||
}
|
||
TaskCenter.getIns().activityItemNum(itemvo.type);
|
||
}
|
||
|
||
public function addItems(arr:Array) : void
|
||
{
|
||
var i:int = 0;
|
||
for(i = 0; i < arr.length; )
|
||
{
|
||
addItem(arr[i]);
|
||
i++;
|
||
}
|
||
}
|
||
|
||
public function isSingleItem(type:int) : Boolean
|
||
{
|
||
return Table.item_info[type].maxNum == 1;
|
||
}
|
||
|
||
public function isEquipItem(type:int) : Boolean
|
||
{
|
||
return Table.item_info[type].itemType == ItemType.TYPE_EQUIP;
|
||
}
|
||
|
||
public function getNum(type:int, exceptEquipOn:Boolean = false) : int
|
||
{
|
||
var j:int = 0;
|
||
var eq:EquipVo = null;
|
||
var i:int = 0;
|
||
var num:int = 0;
|
||
var arr:Array = getAllItemvo(type);
|
||
if(arr.length > 0 && exceptEquipOn && arr[0] is EquipVo)
|
||
{
|
||
for(j = 0; j < arr.length; )
|
||
{
|
||
eq = arr[j] as EquipVo;
|
||
if(!eq.isUsed)
|
||
{
|
||
num++;
|
||
}
|
||
j++;
|
||
}
|
||
return num;
|
||
}
|
||
i = 0;
|
||
while(i < arr.length)
|
||
{
|
||
num += arr[i].num;
|
||
i++;
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public function getAllItemvo(type:int) : Array
|
||
{
|
||
var i:int = 0;
|
||
var a:Array = [];
|
||
var len:int = int(Master.bag.items.length);
|
||
for(i = 0; i < len; )
|
||
{
|
||
if((Master.bag.items[i] as ItemVo).type == type)
|
||
{
|
||
a.push(Master.bag.items[i]);
|
||
}
|
||
i++;
|
||
}
|
||
return a;
|
||
}
|
||
|
||
public function getIdsByType(type:int, num:int) : Array
|
||
{
|
||
var ids:Array = [];
|
||
var hasNum:int = 0;
|
||
var items:Array = getAllItemvo(type);
|
||
for each(var item:ItemVo in items)
|
||
{
|
||
if(hasNum >= num)
|
||
{
|
||
break;
|
||
}
|
||
ids.push(item.id);
|
||
hasNum += item.num;
|
||
}
|
||
return ids;
|
||
}
|
||
|
||
public function getById(id:Number) : ItemVo
|
||
{
|
||
var i:int = 0;
|
||
for(i = 0; i < Master.bag.items.length; )
|
||
{
|
||
if(Master.bag.items[i].id == id)
|
||
{
|
||
return Master.bag.items[i];
|
||
}
|
||
i++;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public function getByType(type:int) : ItemVo
|
||
{
|
||
var i:int = 0;
|
||
for(i = 0; i < Master.bag.items.length; )
|
||
{
|
||
if(Master.bag.items[i].type == type)
|
||
{
|
||
return Master.bag.items[i];
|
||
}
|
||
i++;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public function sell(itemId:Number, num:int = 1, show:Boolean = false) : void
|
||
{
|
||
var i:int = 0;
|
||
var mon:MonsterVo = null;
|
||
var sellitem:ItemVo = getById(itemId);
|
||
if(num < 0 || num > 10000)
|
||
{
|
||
Alert.showMsg("error number.");
|
||
return;
|
||
}
|
||
if(sellitem.itemType == ItemType.TYPE_EQUIP)
|
||
{
|
||
if((sellitem as EquipVo).isUsed)
|
||
{
|
||
Alert.showMsg(lang("装备【{0}】正在使用中。").replace("{0}",sellitem.name));
|
||
return;
|
||
}
|
||
i = 0;
|
||
while(i < Master.monsters.length)
|
||
{
|
||
mon = Master.monsters[i];
|
||
if(mon.equip1 == sellitem.id || mon.equip2 == sellitem.id || mon.equip3 == sellitem.id || mon.equip4 == sellitem.id || mon.equip5 == sellitem.id)
|
||
{
|
||
Alert.yes("sell item error","equip status:" + sellitem["isUsed"] + ",conflict:m" + mon.id + ",pls contact customer service。");
|
||
return;
|
||
}
|
||
i++;
|
||
}
|
||
}
|
||
sell_para = {
|
||
"itemId":itemId,
|
||
"num":num,
|
||
"show":show
|
||
};
|
||
NetItem.getIns().sellItem(itemId,num,sellHandler);
|
||
}
|
||
|
||
private function sellHandler(money:int) : void
|
||
{
|
||
var item:ItemVo = getById(sell_para.itemId);
|
||
item.num -= sell_para.num;
|
||
if(item.num == 0)
|
||
{
|
||
Master.bag.removeItem(sell_para.itemId);
|
||
}
|
||
MasterCenter.getIns().addMoney(money,sell_para.show);
|
||
GlobalBroadcast.dispatchEvent(new MYREvent(MYREvent.ITEM_NUM_CHANGE,item));
|
||
sell_para = null;
|
||
}
|
||
|
||
public function subLocalNeed(arr:Array, show:Boolean = false) : void
|
||
{
|
||
var i:int = 0;
|
||
var o:Object = null;
|
||
for(i = 0; i < arr.length; )
|
||
{
|
||
o = arr[i];
|
||
subNumByType(o.type,o.num,show);
|
||
i++;
|
||
}
|
||
}
|
||
|
||
public function useItem(id:Number, num:int, onComplete:Function) : void
|
||
{
|
||
onUseItemComplete = onComplete;
|
||
useItem_para = {
|
||
"id":id,
|
||
"num":num
|
||
};
|
||
var item:ItemVo = getById(id);
|
||
NetItem.getIns().useItem(id,num,useItemHandler);
|
||
}
|
||
|
||
private function useItemHandler(rewardData:Object, param:Object) : void
|
||
{
|
||
var vipstr:String = null;
|
||
var item:ItemVo = subNum(useItem_para.id,useItem_para.num);
|
||
switch(item.para.action)
|
||
{
|
||
case "vipCard":
|
||
Master.vipTime = param.vipTime;
|
||
vipstr = lang("获得了{0}天的【特别驯养师】资格!剩余天数:{1}",item.para.day * useItem_para.num,MasterCenter.getIns().getVipDay());
|
||
Alert.yes(lang("恭喜!"),vipstr);
|
||
if(!GuideCenter.getIns().checkFin(GSub.ACHI_VIP))
|
||
{
|
||
GuideCenter.getIns().fin(GSub.ACHI_VIP);
|
||
AchiCenter.getIns().autoCheck(AchiType.NONE,true);
|
||
if(MyrCenter.channel == "XDG")
|
||
{
|
||
MyrAne.track("monthlycard");
|
||
}
|
||
}
|
||
GlobalBroadcast.dispatchEvent(new MYREvent(MYREvent.VIP_STATUS_CHANGE));
|
||
break;
|
||
case "getAvatar":
|
||
if(Master.avatars != null)
|
||
{
|
||
Master.avatars.push(item.para.avatarType);
|
||
}
|
||
if(Master.newAvatars == null)
|
||
{
|
||
Master.newAvatars = [];
|
||
}
|
||
Master.newAvatars.push(item.para.avatarType);
|
||
Alert.showMsg(lang("解锁了新头像") + "【" + Table.avatar_info[item.para.avatarType].des + "】");
|
||
break;
|
||
case "getSkin":
|
||
Master.history.unlockSkins.push(int(item.para.skinType));
|
||
Alert.showMsg(lang("解锁了魔物时装") + "【" + Table.skin_info[item.para.skinType].name + "】");
|
||
break;
|
||
default:
|
||
RewardCenter.getIns().getRewardByData(rewardData,true,"panel");
|
||
}
|
||
GlobalBroadcast.dispatchEvent(new MYREvent(MYREvent.ITEM_NUM_CHANGE,item));
|
||
useItem_para = null;
|
||
if(onUseItemComplete != null)
|
||
{
|
||
onUseItemComplete();
|
||
onUseItemComplete = null;
|
||
}
|
||
}
|
||
|
||
public function resetItemUsingNum() : void
|
||
{
|
||
var j:int = 0;
|
||
var len:int = int(Master.bag.items.length);
|
||
for(j = 0; j < len; )
|
||
{
|
||
Master.bag.items[j].usingNum = 0;
|
||
j++;
|
||
}
|
||
}
|
||
|
||
public function giftCode(giftCode:String, onComplete:Function) : void
|
||
{
|
||
onGiftCodeComplete = onComplete;
|
||
NetItem.getIns().giftCode(giftCode,giftCodeHandler);
|
||
}
|
||
|
||
private function giftCodeHandler(rewardData:Object, resCode:int, resMsg:String) : void
|
||
{
|
||
if(resCode != 0)
|
||
{
|
||
Alert.yes("giftcode失效",resMsg);
|
||
return;
|
||
}
|
||
RewardCenter.getIns().getRewardByData(rewardData,true,"panel",null);
|
||
if(onGiftCodeComplete != null)
|
||
{
|
||
onGiftCodeComplete();
|
||
onGiftCodeComplete = null;
|
||
}
|
||
}
|
||
|
||
public function equipTypeByTypes(valueName:String, type:String, typeIds:Array) : int
|
||
{
|
||
var equip:EquipVo = null;
|
||
var i:int = 0;
|
||
var num:int = 0;
|
||
for(i = 0; i < typeIds.length; )
|
||
{
|
||
equip = new EquipVo();
|
||
equip.type = typeIds[i];
|
||
equip.buildAuto();
|
||
if(equip != null)
|
||
{
|
||
if(equip[valueName] != null && equip[valueName].val > 0)
|
||
{
|
||
return equip[type];
|
||
}
|
||
}
|
||
i++;
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
public function getAllSkinItem() : Array
|
||
{
|
||
var itemIds:Array = [];
|
||
if(Table.item_info == null || Master.history == null || Master.history.unlockSkins == null)
|
||
{
|
||
return itemIds;
|
||
}
|
||
for each(var itemData:Object in Table.item_info)
|
||
{
|
||
if(!(itemData == null || itemData.para == null || itemData.para.action != "getSkin"))
|
||
{
|
||
if(Master.history.unlockSkins.indexOf(int(itemData.para.skinType)) != -1)
|
||
{
|
||
itemIds.push(itemData.type.toString());
|
||
}
|
||
}
|
||
}
|
||
itemIds.sort(sortBySkinLevel);
|
||
return itemIds;
|
||
}
|
||
|
||
private function sortBySkinLevel(a:Object, b:Object) : int
|
||
{
|
||
var la:int = getSkinLevelByItem(int(a));
|
||
var lb:int = getSkinLevelByItem(int(b));
|
||
if(la != lb)
|
||
{
|
||
return lb - la;
|
||
}
|
||
return int(a) - int(b);
|
||
}
|
||
|
||
private function getSkinLevelByItem(type:int) : int
|
||
{
|
||
var item:Object = Table.item_info[type];
|
||
if(item == null || item.para == null)
|
||
{
|
||
return 0;
|
||
}
|
||
var skin:Object = Table.skin_info[item.para.skinType.toString()];
|
||
if(skin == null || skin.level == null)
|
||
{
|
||
return 0;
|
||
}
|
||
return int(skin.level);
|
||
}
|
||
}
|
||
}
|
||
|