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:
NOTE: You're freely to use this code, just read the copyright summary!!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
}
}
Regards, Mextur.

