码迷,mamicode.com
首页 > 编程语言 > 详细

C# 游戏排序算法管理

时间:2015-02-12 18:07:07      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:


using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SGGame
{
    public enum SORT_METHOD
    {
        NONE,

        BY_CARD_SCORE = 1,
        BY_CARD_WORTH,
        BY_CARD_EXP,
        BY_CARD_REFINE,

        BY_EQUIP_SCORE, // use as default
        BY_EQUIP_WORTH,
    }


    public class SortMethod
    {
        private Dictionary<SORT_METHOD, IComparer<SlotInfo>> Methods;

        private static SortMethod Instance;
        public  static IComparer<SlotInfo> Comparer
        {
            get
            {
                if (Instance == null)
                {
                    Instance = new SortMethod();
                    Instance.Methods = new Dictionary<SORT_METHOD, IComparer<SlotInfo>>();
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_SCORE,    new SortMultiple(new SortByFormat(),   new SortByStar(),     new SortByScore(),    new SortByLevel()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_WORTH,    new SortMultiple(new SortByCoinCard(), new SortDECByStar(),  new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_EXP,      new SortMultiple(new SortByExpCard(),  new SortDECByStar(),  new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_REFINE, new SortMultiple(new SortDECByStar(), new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_EQUIP_SCORE,   new SortMultiple(new SortByStar(),     new SortBySlot()));
                    Instance.Methods.Add(SORT_METHOD.BY_EQUIP_WORTH, new SortMultiple(new SortDECByStar(), new SortDECByLevel(), new SortDECByID()));
                    //Instance.Methods.Add(SORT_METHOD.BY_EQUIP_PIECE, new SortMultiple(new SortEquipByEnough(), new SortEquipByStar(), new SortEquipByCan(), new SortEquipPieceCount()));
                }

                return Instance.Methods[GameManager.ProxyManager.MainProxy.SortMethod];
            }
        }
    }


    public class SortBySlot   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Slot - y.Slot; } }
    public class SortByCode   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Code - y.Code; } }
    public class SortByScore  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as CardInfo).Score              - (x as CardInfo).Score; } }
    public class SortByClass  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.attribute - (y as CardInfo).BaseData.attribute; } }
    public class SortByLevel  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (y as EquipInfo).Level - (x as EquipInfo).Level : (y as CardInfo).Level - (x as CardInfo).Level; } }
    public class SortByCost   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as CardInfo).Cost               - (x as CardInfo).Cost;  } }
    public class SortByFormat : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Slot > Global.MAX_FIGHT_POS_COUNT && y.Slot > Global.MAX_FIGHT_POS_COUNT ? 0 : x.Slot - y.Slot; } }
    public class SortByStar   : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            if (x.GetType() == typeof(CardInfo))
                return (y as CardInfo).BaseData.start - (x as CardInfo).BaseData.start;

            if (x.GetType() == typeof(EquipInfo))
                return (y as EquipInfo).BaseData.star - (x as EquipInfo).BaseData.star;

            return 0;
        }
    }

    public class SortDECByStar : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            if (x.GetType() == typeof(CardInfo))
                return (x as CardInfo).BaseData.start-(y as CardInfo).BaseData.start ;

            if (x.GetType() == typeof(EquipInfo))
                return (x as EquipInfo).BaseData.star-(y as EquipInfo).BaseData.star;

            return 0;
        }
    }

    public class SortByCoinCard  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.IsCoinCard() ? (y as CardInfo).BaseData.IsCoinCard() ? 0 : -1 : (y as CardInfo).BaseData.IsCoinCard() ? 1 : 0; } }
    public class SortByExpCard   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.IsExpCard() ? (y as CardInfo).BaseData.IsExpCard() ? 0 : -1 : (y as CardInfo).BaseData.IsExpCard() ? 1 : 0; } }
    public class SortDECByLevel  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (x as EquipInfo).Level-(y as EquipInfo).Level  : (x as CardInfo).Level-(y as CardInfo).Level ; } }
    public class SortDECByID     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (x as EquipInfo).BaseData.id - (y as EquipInfo).BaseData.id : (x as CardInfo).BaseData.ID - (y as CardInfo).BaseData.ID; } }

    public class SortByAttack    : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).Attack - (x as EquipInfo).Attack; } }
    public class SortByHP        : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).HP     - (x as EquipInfo).HP;     } }
    public class SortByPhDef     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).PhDef  - (x as EquipInfo).PhDef;  } }
    public class SortByMgDef     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).MgDef  - (x as EquipInfo).MgDef;  } }
    public class SortByact1Gr100 : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (int)(y as EquipInfo).BaseData.Prop[0].max   - (int)(x as EquipInfo).BaseData.Prop[0].max; } }
    public class SortByact1num   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (int)(y as EquipInfo).BaseData.Prop[0].value - (int)(x as EquipInfo).BaseData.Prop[0].value; } }

    public class SortEquipPieceByCan : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            List<int> sources_X = new List<int>();
            List<int> sources_Y = new List<int>();
            sources_X = TableManager.EquipTable.Find((ushort)infoX.BaseData.id).Source;
            sources_Y = TableManager.EquipTable.Find((ushort)infoY.BaseData.id).Source;
            if (sources_X.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

                    LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoX.BaseData.id && detail.dropType == 6)
                            sources_X.Add((int)floor.ID);
                    }
                }
            }
            if (sources_Y.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

                    LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoY.BaseData.id && detail.dropType == 6)
                            sources_Y.Add((int)floor.ID);
                    }
                }
            }
            if (sources_X.Count == sources_Y.Count && sources_X.Count==0)
            {
                return 0;
            }
            else
            {
                if (sources_X.Count==0)
                {
                    return -1;
                }
                else if (sources_Y.Count==0)
                {
                    return 1;
                }
                else
                {
                    bool floor_available_x = false;
                    bool floor_available_y = false;
                    for (int i = 0; i < sources_X.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_X[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_X[i])
                        {
                            floor_available_x = floor_latest.ID > sources_X[i];
                            break;
                        }
                    }
                    for (int i = 0; i < sources_Y.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_Y[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_Y[i])
                        {
                            floor_available_y = floor_latest.ID > sources_Y[i];
                            break;
                        }
                    }
                    if (floor_available_x == floor_available_y && (floor_available_x == false || floor_available_x == true))
                    {
                        return 0;
                    }
                    else
                    {
                        if (floor_available_x==true)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }

    public class SortCardPieceByCan : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            List<int> sources_X = new List<int>();
            List<int> sources_Y = new List<int>();
            sources_X = TableManager.CardTable.Find((ushort)infoX.BaseData.ID).Source;
            sources_Y = TableManager.CardTable.Find((ushort)infoY.BaseData.ID).Source;
            if (sources_X.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

                    LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoX.BaseData.ID && detail.dropType == 5)
                            sources_X.Add((int)floor.ID);
                    }
                }
            }
            if (sources_Y.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

                    LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoY.BaseData.ID && detail.dropType == 5)
                            sources_Y.Add((int)floor.ID);
                    }
                }
            }
            if (sources_X.Count == sources_Y.Count && sources_X.Count == 0)
            {
                return 0;
            }
            else
            {
                if (sources_X.Count == 0)
                {
                    return -1;
                }
                else if (sources_Y.Count == 0)
                {
                    return 1;
                }
                else
                {
                    bool floor_available_x = false;
                    bool floor_available_y = false;
                    for (int i = 0; i < sources_X.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_X[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_X[i])
                        {
                            floor_available_x = floor_latest.ID > sources_X[i];
                            break;
                        }
                    }
                    for (int i = 0; i < sources_Y.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_Y[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_Y[i])
                        {
                            floor_available_y = floor_latest.ID > sources_Y[i];
                            break;
                        }
                    }
                    if (floor_available_x == floor_available_y && (floor_available_x == false || floor_available_x == true))
                    {
                        return 0;
                    }
                    else
                    {
                        if (floor_available_x == true)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }


    public class SortEquipPieceCount : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            if (infoX.Count == infoY.Count)
            {
               return infoX.BaseData.id-infoY.BaseData.id;
            }
            else
            {
                //return GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoX.BaseData.id) - GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoY.BaseData.id);
                return infoY.Count-infoX.Count;
            }
        }
    }

    public class SortCardPieceCount : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            if (infoX.Count == infoY.Count)
            {
                return infoX.BaseData.ID - infoY.BaseData.ID;
            }
            else
            {
                //return GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoX.BaseData.id) - GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoY.BaseData.id);
                return infoY.Count - infoX.Count;
            }
        }
    }

    public class SortCardPiece : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            if ( infoX.Enough && !infoY.Enough) return -1;
            if (!infoX.Enough &&  infoY.Enough) return 1;

            return infoY.BaseData.start - infoX.BaseData.start;
        }
    }


    public class SortEquipPiece : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            if ( infoX.Enough && !infoY.Enough) return -1;
            if (!infoX.Enough &&  infoY.Enough) return 1;

            return infoY.BaseData.star - infoX.BaseData.star;
        }
    }

    public class SortMultiple : IComparer<SlotInfo>
    {
        private List<IComparer<SlotInfo>> ComparerList;

        public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2) : this(comparer1, comparer2, null, null) {}
        public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2, IComparer<SlotInfo> comparer3) : this(comparer1, comparer2, comparer3, null) {}
        public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2, IComparer<SlotInfo> comparer3, IComparer<SlotInfo> comparer4)
        {
            ComparerList = new List<IComparer<SlotInfo>>();
            if (comparer1 != null) ComparerList.Add(comparer1);
            if (comparer2 != null) ComparerList.Add(comparer2);
            if (comparer3 != null) ComparerList.Add(comparer3);
            if (comparer4 != null) ComparerList.Add(comparer4);

            ComparerList.Add(new SortByCode()); // set code sorter as default sort method
        }

        public int Compare(SlotInfo x, SlotInfo y)
        {
            int result = 0;
            foreach (IComparer<SlotInfo> comparer in ComparerList)
            {
                if (comparer == null) break;

                result = comparer.Compare(x, y);
                if (result != 0) break;
            }

            return result;
        }
    }
}

C# 游戏排序算法管理

标签:

原文地址:http://www.cnblogs.com/mengdi520123/p/4288319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!