﻿// 类库
var GDClass = {
    Version: '1.0.1',
    create: function() {
        return function() { this.initialize.apply(this, arguments); }
    }
};
//var $j = jQuery.noConflict();
/*
武将类
*/
Generals = GDClass.create();
Generals.prototype = {
    initialize: function(general, type) {
        //this = general;
        this.Type = type;
        this.PicAdd = general.picAdd;                                   //图片地址
        this.Pic = parseInt(general.Pic);                                         //图片
        this.Name = general.Name;                                       //名称
        this.Level = parseInt(general.Level);                          //等级
        //        if (isNaN(general.Potential)) { this.Potential = general.Potential; }
        //        else {                                 
        //            this.Potential = parseInt(general.Potential);
        //        }                            //潜力
        this.NewLevel = this.Level;
        if (this.Type == 0) { this.Potential = "—"; }
        else {
            this.Potential = this.NewLevel;
        }
        this.Experience = parseInt(general.Experience);                           //经验
        this.TotalExperience = GetTotalExperience(this.Level);                    //总经验
        this.LevelExperience = GetExperience(this.Level);                         //升级经验
        this.Command = parseInt(general.Command) + parseInt(this.Level);                                 //统率
        this.Affairs = parseInt(general.Affairs);                                 //内政
        this.Chivalrous = parseInt(general.Chivalrous);                           //勇武
        this.Resourcefulness = parseInt(general.Resourcefulness);                 //智谋
        this.NewAffairs = this.Affairs;                                          //更新内政
        this.NewChivalrous = this.Chivalrous;                                   //勇武
        this.NewResourcefulness = this.Resourcefulness;                        //智谋
        this.Description = general.Description;                                 //说明

        this.GEquips = parseInt(general.GEquips);
        this.Attack = this.NewChivalrous * 10;                                   //攻击
        this.Defense = this.NewResourcefulness * 10;                                  //防御
        if (isNaN(general.Support)) {
            this.Support = general.Support;
        }
        else {
            this.Support = parseInt(general.Support);                             //忠诚
        }
        if (isNaN(general.Salary)) {
            this.Salary = general.Salary;
        }
        else {
            this.Salary = parseInt(general.Salary);
        }
        //俸禄
        this.Speed = parseInt(general.Speed);                                     //速度
        this.Power = Math.floor(this.Chivalrous / 3) + Math.floor(this.Level / 5) + 100;                                      //体力
        this.Energy = Math.floor(this.Resourcefulness / 3) + Math.floor(this.Level / 5) + 100;

        this.NewSpeed = this.Speed;                                                   //更新后的速度
        this.NewPower = parseInt(this.Power);                                                   //更新后的体力
        this.NewEnergy = parseInt(this.Energy);                                                  //更新后的精力
        this.NewAttack = this.Attack;                                                  //更新后的攻击
        this.NewDefense = this.Defense;                                                 //更新后的防御
        this.NewSalary = null;                                                  //更新后的俸禄
        this.NewSupport = null;                                                   //更新后的忠诚
        this.NewCommand = parseInt(this.Command);                                                 //更新统率

        //精力
        this.NewPotential = this.Potential;
        this.Equips = new EquipsCollection();                          //装备
    },
    GetEquipsPower: function() {
        var Power = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipPower > 0) {
                Power += Equip.EquipPower;
            }
        }
        return Power;
    },
    //更新勇武
    UpdateChivalrous: function() {
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipForce > 0) {
                this.Chivalrous += Equip.EquipForce;
            }
        }
    },
    //更新体力(要有最新的勇武)
    UpdateNewPower: function() {
        this.NewPower = this.Power + this.GetEquipsPower() + Math.floor((this.NewChivalrous - this.Chivalrous) / 3) + Math.floor((this.NewLevel - this.Level) / 5);
    },

    //更新等级 general 初始武将,
    Upgrade: function(general, level) {
        level = parseInt(level);
        var levelGap = level - parseInt((general.Level));
        if (levelGap < 0) {
            return;
        }
        this.Potential += levelGap;
        this.Affairs = general.Affairs;
        if (general.Type == "1") {
            this.NewCommand = this.NewCommand + (level - parseInt(general.NewLevel));
        }
        general.NewLevel = parseInt(level);
        this.UpdateChivalrous();
        this.UpdateNewPower();
        this.UpdateNewEnergy();
        general.TotalExperience = GetTotalExperience(level);
        general.LevelExperience = GetExperience(level);

    },
    //所有装备的精力数
    GetEquipsEnergy: function() {
        var Energy = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipEnergy > 0) {
                Energy += Equip.EquipEnergy;
            }
        }
        return Energy;
    },
    //更新精力数(要有最新的智谋)
    UpdateNewEnergy: function() {
        this.NewEnergy = this.Energy + this.GetEquipsEnergy() + Math.floor((this.NewResourcefulness - this.Resourcefulness) / 3) + Math.floor((this.NewLevel - this.Level) / 5);
    },
    //所有装备的速度
    GetEquipsSpeed: function() {
        var Speed = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipSpeed > 0) {
                Speed += Equip.EquipSpeed;
            }
        }
        return Speed;
    },
    //更新速度
    UpdateNewSpeed: function(general, speed, type) {
        if (general.Type == "1") {
            this.NewSpeed = this.Speed + this.GetEquipsSpeed();
        } else if (general.Type == "0") {
            this.NewSpeed = this.NewSpeed + speed;
        }
        if (general.Type == "0") {
            if (parseInt(this.NewSpeed) > 9999) {
                alert('速度不能超过9999');
                if (type == 0) {
                    this.NewSpeed = 9999 - speed;
                } else if (type == 1) {
                    this.NewSpeed = 9999;
                }
            }
        }
    },
    //所有装备的攻击
    GetEquipsAttack: function() {
        var Attack = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipAttack > 0) {
                Attack += Equip.EquipAttack;
            }
        }
        return Attack;
    },
    //更新攻击
    UpdateNewAttack: function() {
        this.NewAttack = this.NewAttack + this.GetEquipsAttack();
    },
    //所有装备的防御
    GetEquipsDefense: function() {
        var Defense = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipDefense > 0) {
                Defense += Equip.EquipDefense;
            }
        }
        return Defense;
    },
    //更新防御
    UpdateNewDefense: function() {
        this.NewDefense = this.NewDefense + this.GetEquipsDefense();
    },
    //所有装备的俸禄
    GetEquipsSalary: function() {
        var Salary = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipSalary > 0) {
                Salary += Equip.EquipSalary;
            }
        }
        return Salary;
    },
    //更新俸禄
    UpdateNewSalary: function() {
        this.NewSalary = this.NewSalary + this.GetEquipsSalary();
    },
    //所有装备的统率
    GetEquipsCommand: function() {
        var Command = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipCommand > 0) {
                Command += Equip.EquipCommand;
            }
        }
        return Command;
    },
    //更新统率
    UpdateNewCommand: function() {
        this.NewCommand = this.Command + this.GetEquipsCommand();
    },
    //所有装备的忠诚
    GetEquipsSupport: function() {
        var Support = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipSupport > 0) {
                Support += Equip.EquipSupport;
            }
        }
        return Support;
    },
    //更新忠诚
    UpdateNewSupport: function() {
        this.NewSupport = this.NewSupport + this.GetEquipsSupport();
    },
    //--------------------------------------------------------操作开始
    //加勇武 9.24修改
    AddChivalrous: function(point, general) {
        this.NewChivalrous += point; //更新勇武
        if (general.Type == "1") {
            this.NewAttack += point * 10; //更新攻击
        }
        this.UpdateNewPower(); //更新体力数


    },
    //加智谋 9.24修改
    AddResourcefulness: function(point, general) {
        this.NewResourcefulness += point; //更新智谋
        if (general.Type == "1") {
            this.NewDefense += point * 10; //更新防卫
        }
        this.UpdateNewEnergy(); //更新精力数
    },
    //加内政
    AddAffairs: function(point, general) {

        this.NewAffairs += point;

    },
    //加等级 9.24修改
    AddLevel: function(general) {
        this.NewLevel += 1;
        this.TotalExperience = GetTotalExperience(this.NewLevel);                    //总经验
        this.LevelExperience = GetExperience(this.NewLevel);
        if (general.Type == "1") {
            this.NewCommand = this.NewCommand + 1;
        }

        this.UpdateNewPower();
        this.UpdateNewEnergy();
        if (this.Type != 0) {                    //升级经验
            this.NewPotential += 1;
        }

    },
    //    //按确定
    //    Update: function(AffairNum, ChivalRousNum, ResourcNum) {
    //        var tet = AffairNum - this.NewAffairs;
    //        var tet1 = ChivalRousNum - this.NewChivalrous;
    //        var tet2 = ResourcNum - this.NewResourcefulness;
    //        this.AddAffairs(tet);
    //        this.AddChivalrous(tet1);
    //        this.AddResourcefulness(tet2);
    //    },
    //按确定//9.24修改
    Update: function(AffairNum, ChivalRousNum, ResourcNum, Potential, general) {
        var tet = AffairNum - this.NewAffairs;
        var tet1 = ChivalRousNum - this.NewChivalrous;
        var tet2 = ResourcNum - this.NewResourcefulness;
        this.AddAffairs(tet, general);
        this.AddChivalrous(tet1, general);
        this.AddResourcefulness(tet2, general);
        if (!isNaN(Potential)) {
            this.NewPotential = parseInt(Potential);
        }

    },
    //添加装备//9.24修改
    AddEquip: function(Equips, general) {
        if (this.NewLevel < Equips.Level) {
            alert("名将等级不够");
        }
        else {
            //this.Equips[parseInt(Equips.key)] = Equips;
            this.Equips.add(Equips);
            this.NewAffairs += Equips.EquipAffairs; //内政
            //this.NewAttack += Equips.EquipForce * 10; //勇武转换成攻击
            this.NewAttack += Equips.EquipForce; //勇武转换成攻击
            this.NewAttack += Equips.EquipAttack;
            this.NewChivalrous += Equips.EquipForce; //勇武
            this.UpdateNewPower(); //更新体力
            this.NewCommand += Equips.EquipCommand;
            this.NewDefense += Equips.EquipDefend;
            this.NewResourcefulness += Equips.EquipIntelligence; //智谋
            this.UpdateNewEnergy(); //更新精力
            //this.NewDefense += Equips.EquipIntelligence * 10;
            this.NewDefense += Equips.EquipIntelligence;
            this.UpdateNewSpeed(general, Equips.EquipSpeed, 1);
            //this.UpdateNewAttack();
            //this.UpdateNewDefense();
            if (general.Type == "0") {
                if (parseInt(this.NewCommand) > 9999) {
                    alert("统帅值不能超过9999");
                    this.NewCommand = 9999;
                } else if (parseInt(this.NewAttack) > 99999) {

                    alert("攻击值不能超过99999");
                    this.NewAttack = 99999;
                }
                else if (parseInt(this.NewDefense) > 99999) {

                    alert("防御值不能超过99999");
                    this.NewDefense = 99999;
                }
            }
            if (this.Type != 0) {
                this.UpdateNewSalary();
            }
            //this.UpdateNewCommand();

        }
    },
    //获取所有装备的内证数
    GetEquipsAffairs: function() {
        var Affairs = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipAffairs > 0) {
                Affairs += Equip.EquipAffairs;
            }
        }
        return Affairs;
    },
    //获取所有装备的勇武数
    GetEquipsChivalrous: function() {
        var Chivalrous = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipChivalrous > 0) {
                Chivalrous += Equip.EquipChivalrous;
            }
        }
        return Chivalrous;
    },
    //获取装备的所有智谋术
    GetEquipsResourcefulness: function() {
        var Resourcefulness = 0;
        for (var i = 0; i < this.Equips.size(); i++) {
            var Equip = this.Equips.get(i);
            if (Equip.EquipResourcefulness > 0) {
                Resourcefulness += Equip.EquipResourcefulness;
            }
        }
        return Resourcefulness;

    },
    //洗点
    ClearPoint: function() {
        if (this.NewPotential < this.NewLevel) {
            this.NewAttack = this.NewAttack - (this.NewChivalrous - this.Chivalrous) * 10;
            this.NewDefense = this.NewDefense - (this.NewResourcefulness - this.Resourcefulness) * 10;
            this.NewPotential = this.NewLevel;
            this.NewAffairs = this.Affairs;
            this.NewChivalrous = this.Chivalrous;
            this.NewResourcefulness = this.Resourcefulness;
            this.UpdateNewPower();
            this.UpdateNewEnergy();
            return true;
        }
        else {
            //alert("你不能洗点");
            return false;
        }


    },
    //武将修改名字
    UpdateName: function(name) {
        this.Name = name;
    },
    //道具加勇武
    AddPropChivalrous: function(type) {
        if (type == 1) {
            this.NewPower = this.NewPower + Math.floor((this.NewChivalrous * 0.25) / 3);
            //this.NewAttack = this.NewAttack + Math.floor(this.NewChivalrous * 0.25) * 10;
            this.NewAttack = this.NewAttack + 25;
            this.NewChivalrous = this.NewChivalrous + Math.floor(this.Chivalrous * 0.25);


        }
        else if (type == 0) {
            this.NewChivalrous = this.NewChivalrous - Math.floor(this.Chivalrous * 0.25);
            this.NewPower = this.NewPower - Math.floor((this.NewChivalrous * 0.25) / 3);
            //this.NewAttack = this.NewAttack - Math.floor(this.NewChivalrous * 0.25) * 10;
            this.NewAttack = this.NewAttack - 25;

        }
    },
    //道具加智谋
    AddPropResourcefulness: function(type) {
        if (type == 1) {
            this.NewEnergy = this.NewEnergy + Math.floor((this.NewResourcefulness * 0.25) / 3);
            //this.NewDefense = this.NewDefense + Math.floor(this.NewResourcefulness * 0.25) * 10;
            this.NewDefense = this.NewDefense + 25;
            this.NewResourcefulness = this.NewResourcefulness + Math.floor(this.Resourcefulness * 0.25);
        }
        else if (type == 0) {
            this.NewResourcefulness = this.NewResourcefulness - Math.floor(this.Resourcefulness * 0.25);
            this.NewEnergy = this.NewEnergy - Math.floor((this.NewResourcefulness * 0.25) / 3);
            // this.NewDefense = this.NewDefense - Math.floor(this.NewResourcefulness * 0.25) * 10;
            this.NewDefense = this.NewDefense - 25;

        }
    },
    //道具加统帅
    AddProCommand: function(type) {
        if (type == 1) {
            //this.NewCommand = this.NewCommand + Math.floor(this.Command * 0.5);
            this.NewCommand = Math.ceil(this.NewCommand * (1 + 0.5));
        } else if (type == 0) {
            //this.NewCommand = this.NewCommand - Math.floor(this.Command * 0.5);
            this.NewCommand = Math.ceil((this.NewCommand * 2) / 3);
        }
        if (this.NewCommand <= 0) {
            this.NewCommand = 1;
        }


    },
    //加统帅
    Addcommand: function(command) {
        this.NewCommand = parseInt(command);
    },
    //9.24修改
    //加速度
    AddSpeed: function(speed) {
        this.NewSpeed = parseInt(speed);
    },
    //加攻击
    AddAttack: function(Attack) {
        this.NewAttack = parseInt(Attack);
    },
    //加防御
    AddDefend: function(Defend) {
        this.NewDefense = parseInt(Defend);
    },
    //卸载装备
    RemoveEquip: function(Equip, general) {

        this.Equips.removeOfKey(Equip.key);
        //this.Equips = new EquipsCollection();
        this.NewAffairs -= Equip.EquipAffairs;
        this.NewAttack -= Equip.EquipForce; //勇武转换成攻击
        this.NewAttack -= Equip.EquipAttack;
        this.NewChivalrous -= Equip.EquipForce; //勇武
        this.UpdateNewPower(); //更新体力
        this.NewCommand -= Equip.EquipCommand;
        this.NewDefense -= Equip.EquipDefend;
        this.NewResourcefulness -= Equip.EquipIntelligence; //智谋
        this.UpdateNewEnergy(); //更新精力
        this.NewDefense -= Equip.EquipIntelligence;
        this.UpdateNewSpeed(general, -Equip.EquipSpeed, 0);
        //this.UpdateNewAttack();
        //this.UpdateNewDefense();
        if (this.Type != 0) {
            this.UpdateNewSalary();
        }
        //this.UpdateNewCommand();
    }
    //--------------------------------------------------------操作结束

}


Equip = GDClass.create();
//装备
Equip.prototype = {
    initialize: function(equip, key, StrLevel) {
        this.key = key;     //装备唯一值
        this.EquipStrLevel = StrLevel;//装备强化等级
        this.EquipType = equip.sgdc_GameDataSubTypeIndex; //装备部位 
        this.EquipIndex = equip.sgdc_EquipIndex; //装备序号
        this.EquipName = equip.sgdc_EquipName; //装备名称
        this.PictureAddress = equip.sgdc_PictureAdress; //图片地址
        this.SourceName = equip.sgdc_EquipSourceName; //装备来源
        this.SuitName = equip.sgdc_SuiteName; //套装名称
        this.SuitIndex = equip.sgdc_SuiteIndex//套装序号
        this.QuailtyName = equip.sgdc_EquipQualityName; //品质名称
        this.SubDataTypeName = equip.sgdc_GameDataSubTypeName; //小类名称
        this.EquipCommand = parseInt(equip.sgdc_EquipCommand); //装备统率
        this.EquipAffairs = parseInt(equip.sgdc_EquipAffairs); //装备内政
        this.EquipForce = parseInt(equip.sgdc_EquipForce); //装备勇武

        this.EquipIntelligence = parseInt(equip.sgdc_EquipIntelligence); //装备智谋
        this.EquipPower = parseInt(equip.sgdc_EquipPower); //装备体力
        this.EquipEnergy = parseInt(equip.sgdc_EquipEnergy); //装备精力
        this.EquipLife = parseInt(equip.sgdc_EquipLife); //装备生命
        this.EquipAttack = parseInt(equip.sgdc_EquipAttack); //装备攻击

        this.EquipDefend = parseInt(equip.sgdc_EquipDefend); //装备防御
        this.EquipRange = parseInt(equip.sgdc_EquipRange); //装备射程
        this.EquipSpeed = parseInt(equip.sgdc_EquipSpeed); //装备速度
        this.EquipWeight = parseInt(equip.sgdc_EquipWeight); //装备负重
        this.EquipLevel = parseInt(equip.sgdc_EquipLevel); //装备等级
    },
    equals: function(equip) {
        if (equip.key == this.key) {
            return true;
        }
        else return false;
    },
    equalsofkey: function(key) {
        if (key == this.key) {
            return true;
        }
        else return false;
    }
}

EquipsCollection = GDClass.create();
//准备容器
EquipsCollection.prototype = {
    initialize: function() {
        this.Equips = new ArrayList();
    },
    add: function(equip) {
        this.Equips.add(equip);
    },
    remove: function(equip) {
        var it = this.Equips.iterator();
        while (it.hasNext()) {
            var p = it.next();
            if (p.equals(equip)) {
                this.Equips.remove(p);
                break;
            }
        }
    },
    removeOfKey: function(key) {
        var it = this.Equips.iterator();
        while (it.hasNext()) {
            var p = it.next();
            if (p.equalsofkey(key)) {
                this.Equips.remove(p);
                break;
            }
        }
    },
    update: function(equip) {
        var index = this.indexOf(equip);
        this.Equips.set(index, equip);
    },
    size: function() {
        return this.Equips.size();
    },
    indexOf: function(equip) {
        var number = 0;
        var it = this.Equips.iterator();
        while (it.hasNext()) {
            var p = it.next();
            if (p.equals(equip)) {
                return number;
            }
            number++;
        }
        return -1;
    },
    indexOfKey: function(key) {
        var number = 0;
        var it = this.Equips.iterator();
        while (it.hasNext()) {
            var p = it.next();
            if (p.equalsofkey(key)) {
                return number;
            }
            number++;
        }
        return -1;
    },
    get: function(index) {
        return this.Equips.get(index);
    }
}
