Skip to content

Instantly share code, notes, and snippets.

@nathan130200
Created February 5, 2018 21:23
Show Gist options
  • Save nathan130200/83b6879da38d81487f0ff0d942610a2e to your computer and use it in GitHub Desktop.
Save nathan130200/83b6879da38d81487f0ff0d942610a2e to your computer and use it in GitHub Desktop.
MongoDB Game Item Serialization
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace WarfaceEmulator.Entities.Game.Profile
{
public class Item
{
[BsonId]
public ObjectId ObjectId {
get;
set;
}
[BsonElement("item_id")]
[XmlAttribute("id")]
[BsonRequired]
public string Id {
get;
set;
}
[BsonElement("profile_id")]
[BsonRequired]
public long ProfileId {
get;
set;
}
[BsonElement("name")]
[XmlAttribute("name")]
public string Name {
get;
set;
}
[BsonElement("equipped")]
[XmlAttribute("equipped")]
[BsonDefaultValue(29)]
public int Equipped {
get;
set;
}
[BsonElement("slot")]
[XmlAttribute("slot")]
public int Slot {
get;
set;
}
[BsonElement("config")]
[XmlAttribute("config")]
public string Config {
get;
set;
}
[BsonElement("attached_to")]
[XmlAttribute("attached_to")]
[BsonDefaultValue(0)]
public int AttachedTo {
get;
set;
}
[BsonElement("default")]
[XmlAttribute("default")]
[BsonDefaultValue(0)]
public int Default {
get;
set;
}
[BsonElement("permanent")]
[XmlAttribute("permanent")]
[BsonDefaultValue(0)]
public int Permanent {
get;
set;
}
[BsonElement("expired_confirmed")]
[XmlAttribute("expired_confirmed")]
public int ExpiredConfirmed {
get;
set;
}
[BsonElement("buy_time_utc")]
[XmlAttribute("buy_time_utc")]
public long BuyTimeUtc {
get;
set;
}
[BsonElement("expiration_time_utc")]
[XmlAttribute("expiration_time_utc")]
public long ExpirationTimeUtc {
get;
set;
}
[BsonElement("seconds_left")]
[XmlAttribute("seconds_left")]
public long SecondsLeft {
get;
set;
}
[BsonElement("total_durability_points")]
[XmlAttribute("total_durability_points")]
public long TotalDurabilityPoints {
get;
set;
}
[BsonElement("durability_points")]
[XmlAttribute("durability_points")]
public long DurabilityPoints {
get;
set;
}
public agsXMPP.Xml.Dom.Element Serialize()
{
var root = new agsXMPP.Xml.Dom.Element("item");
{
foreach (var property in GetType().GetProperties())
{
try
{
var xmlAttributeAttr = property.GetCustomAttribute<XmlAttributeAttribute>();
if (xmlAttributeAttr != null)
{
var value = property.GetValue(this, null).ToString();
if (value != null)
{
root.SetAttribute(xmlAttributeAttr.AttributeName, value);
}
}
}
catch(Exception ex)
{
Log.Error(ex);
continue;
}
}
}
return root;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment