C# anti mutant

Results 1 to 4 of 4
  1. #1
    Freak Mextur is offline
    MemberRank
    Mar 2012 Join Date
    216Posts

    C# anti mutant

    Hello ragezone!

    The other day I saw some RaGEZONER's release Anti-Mutant codes. Now it's my turn!

    What does it filter?
    Parts are valid for gender, and match their color palette.

    What needs to be done
    Check if parts can be colored and membership requirements.

    Here is my code:

    Code:
    using System;
    using System.Collections.Concurrent;
    using System.Linq;
    using System.Xml;
    
    namespace AntiMutant
    {
        /// <summary>
        /// <how_to_use>
        /// Initialize the static class (load figureData.xml):
        ///     AntiMutant.initializeAntiMutant("%hotel_url%/r63/figuredata.xml");
        /// 
        /// Just put a figure in the generator:
        ///     AntiMutant.checkFigure("%figure%", Gender.%type%);
        /// </how_to_use>
        /// <author>Mextur</author>
        /// <date>March 28, 2014 - 19:02</date>
        /// <copyrights>Freely to use, just show my name here.</copyrights>
        /// </summary>
        public static class AntiMutant
        {
            private static string[] requiredFigureParts = new string[2] { "hd", "ch" };
    
            private static ConcurrentDictionary<int, ColorPalette> colorPalettes = new ConcurrentDictionary<int, ColorPalette>();
            private static ConcurrentDictionary<string, FigurePartSet> figurePartSets = new ConcurrentDictionary<string, FigurePartSet>();
    
            public static void initializeAntiMutant(string figureDataPath)
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(figureDataPath);
    
                    XmlNode colorNode = document.GetElementsByTagName("colors").Item(0);
    
                    foreach (XmlElement paletteElement in colorNode)
                    {
                        ColorPalette colorPalette = new ColorPalette();
                        colorPalette.colorPaletteId = Convert.ToInt32(paletteElement.GetAttribute("id"));
                        colorPalette.colors = new ConcurrentDictionary<int, Color>();
    
                        foreach (XmlElement colorElement in paletteElement)
                        {
                            Color color = new Color();
                            color.colorId = Convert.ToInt32(colorElement.GetAttribute("id"));
                            color.membership = Membership.None; // TODO! club always equals 2/0 (doesn't seem to differ)
    
                            colorPalette.colors.TryAdd(color.colorId, color);
                        }
    
                        colorPalettes.TryAdd(colorPalette.colorPaletteId, colorPalette);
                    }
    
                    XmlNode figurePartNode = document.GetElementsByTagName("sets").Item(0);
    
                    foreach (XmlElement figurePartSetElement in figurePartNode)
                    {
                        FigurePartSet figurePartSet = new FigurePartSet();
                        figurePartSet.figurePartSetType = figurePartSetElement.GetAttribute("type");
                        figurePartSet.colorPalletteId = Convert.ToInt32(figurePartSetElement.GetAttribute("paletteid"));
                        figurePartSet.figureParts = new ConcurrentDictionary<int, FigurePart>();
    
                        foreach (XmlElement figurePartElement in figurePartSetElement)
                        {
                            FigurePart figurePart = new FigurePart();
                            figurePart.figurePartId = Convert.ToInt32(figurePartElement.GetAttribute("id"));
                            figurePart.isColorable = figurePartElement.GetAttribute("colorable") == "1";
    
                            string strGender = figurePartElement.GetAttribute("gender");
                            figurePart.gender = (Gender)Enum.Parse(typeof(Gender), strGender);
                            figurePart.membership = Membership.None;
    
                            figurePartSet.figureParts.TryAdd(figurePart.figurePartId, figurePart);
                        }
    
                        figurePartSets.TryAdd(figurePartSet.figurePartSetType, figurePartSet);
                    }
                }
                catch (Exception ex)
                {
                    AntiMutant.handleException(ex);
                }
            }
    
            public static bool checkFigure(string figure, Gender gender)
            {
                try
                {
                    bool containsRequired = AntiMutant.requiredFigureParts.Select(obj => figure.ToLower().Contains(obj)).Where(obj => obj == false).Count() == 0;
                    if (!containsRequired)
                    {
                        return false;
                    }
    
                    string[] parts = figure.Split('.');
    
                    if (parts.Count() < AntiMutant.requiredFigureParts.Count() || parts.Count() > AntiMutant.figurePartSets.Count)
                    {
                        return false; // IT IS TOO SHORT/LONG
                    }
    
                    foreach (string part in parts)
                    {
                        string[] partSplit = part.Split('-');
    
                        if (partSplit.Count() != 3)
                        {
                            return false; // INVALID_DATA
                        }
    
                        string figurePartType = partSplit[0].ToLower();
    
                        FigurePartSet figureSet;
                        bool gainedSet = AntiMutant.figurePartSets.TryGetValue(figurePartType, out figureSet);
    
                        if (!gainedSet)
                        {
                            return false; // INVALID_SET
                        }
    
                        int partId = Convert.ToInt32(partSplit[1]);
    
                        FigurePart figurePart;
                        bool gainedPart = figureSet.figureParts.TryGetValue(partId, out figurePart);
    
                        if (!gainedPart)
                        {
                            return false; // INVALID_PART
                        }
    
                        if (figurePart.gender != Gender.U)
                        {
                            if (figurePart.gender != gender)
                            {
                                return false; // INVALID_GENDER
                            }
                        }
    
                        // TODO: check figurePart.membership ??
                        // TODO: check figurePart.isColorable ??
    
                        ColorPalette colorPalette;
                        bool gainedPalette = AntiMutant.colorPalettes.TryGetValue(figureSet.colorPalletteId, out colorPalette);
    
                        if (!gainedPalette)
                        {
                            return false; // SET_HAS_INVALID_PALETTE
                        }
    
                        int colorId = Convert.ToInt32(partSplit[2]);
    
                        Color color;
                        bool gainedColor = colorPalette.colors.TryGetValue(colorId, out color);
    
                        if (!gainedColor)
                        {
                            return false; // INVALID_COLOR_MATCH
                        }
    
                        // TODO: check color.membership ??
                    }
    
                    return true;
                }
                catch (Exception ex)
                {
                    AntiMutant.handleException(ex);
                }
    
                return false;
            }
    
            private static void handleException(Exception ex)
            {
                try
                {
                    // !! PASS HERE YOUR EXCEPTION TO YOUR GLOBAL EXCEPTION HANDLER !!
                }
                catch { }
            }
        }
    
        public class ColorPalette
        {
            public int colorPaletteId;
            public ConcurrentDictionary<int, Color> colors;
        }
    
        public class Color
        {
            public int colorId;
            public Membership membership;
        }
    
        public class FigurePartSet
        {
            public string figurePartSetType;
            public int colorPalletteId;
            public ConcurrentDictionary<int, FigurePart> figureParts;
        }
    
        public class FigurePart
        {
            public int figurePartId;
            public Gender gender;
            public Membership membership;
            public bool isColorable;
        }
    
        public enum Gender
        {
            M, F, U
        }
    
        public enum Membership
        {
            None, HabboClub, VIP
        }
    }
    NOTE: You're freely to use this code, just read the copyright summary!!

    Regards, Mextur.


  2. #2
    Apprentice Garodin is offline
    MemberRank
    Feb 2014 Join Date
    13Posts

    Re: C# anti mutant

    Great release man! Keep up the good work! :)

  3. #3
    Retired maritnmine is offline
    MemberRank
    May 2007 Join Date
    North KoreaLocation
    1,103Posts

    Re: C# anti mutant

    Code:
        public class ColorPalette
        {
            public int colorPaletteId;
            public ConcurrentDictionary<int, Color> colors;
        }
    
    
        public class Color
        {
            public int colorId;
            public Membership membership;
        }
    
    
        public class FigurePartSet
        {
            public string figurePartSetType;
            public int colorPalletteId;
            public ConcurrentDictionary<int, FigurePart> figureParts;
        }
    
    
        public class FigurePart
        {
            public int figurePartId;
            public Gender gender;
            public Membership membership;
            public bool isColorable;
        }
    These are structs. Not classes. General rule between classes/structs: Structs when all data is public, if you want functions, go with classes (But for gods sake don't put all the data members public, take your time and make getters and setters!).
    Also, make the LINQ thing you do with containsRequired a bit easier. This is probably what would eat most of your CPU.
    I also don't see the point of adding exception handling here, there is no indication to the caller that the call failed and an eventual error message can be displayed to the user.
    You might want to go with a Hashtable instead of ConcurrentDictionary<T, V> as Hashtables are generally faster than ConcurrentDictionary (you also don't have to worry about concurrency issues as long as you don't make a foreach loop), the only drawback is that you have to cast the values if you wanna get an item from it.
    Static constructs is also something you wanna look into.

  4. #4
    Freak Mextur is offline
    MemberRank
    Mar 2012 Join Date
    216Posts

    Re: C# anti mutant

    Quote Originally Posted by maritnmine View Post
    Code:
        public class ColorPalette
        {
            public int colorPaletteId;
            public ConcurrentDictionary<int, Color> colors;
        }
    
    
        public class Color
        {
            public int colorId;
            public Membership membership;
        }
    
    
        public class FigurePartSet
        {
            public string figurePartSetType;
            public int colorPalletteId;
            public ConcurrentDictionary<int, FigurePart> figureParts;
        }
    
    
        public class FigurePart
        {
            public int figurePartId;
            public Gender gender;
            public Membership membership;
            public bool isColorable;
        }
    These are structs. Not classes. General rule between classes/structs: Structs when all data is public, if you want functions, go with classes (But for gods sake don't put all the data members public, take your time and make getters and setters!).
    Also, make the LINQ thing you do with containsRequired a bit easier. This is probably what would eat most of your CPU.
    I also don't see the point of adding exception handling here, there is no indication to the caller that the call failed and an eventual error message can be displayed to the user.
    You might want to go with a Hashtable instead of ConcurrentDictionary<T, V> as Hashtables are generally faster than ConcurrentDictionary (you also don't have to worry about concurrency issues as long as you don't make a foreach loop), the only drawback is that you have to cast the values if you wanna get an item from it.
    Static constructs is also something you wanna look into.
    You're straight to the point.



Advertisement