public void Save() { MongoUtils.MongoData.ConnectToDatabase(); MongoDatabase characterDB = MongoUtils.MongoData.GetDatabase("Characters"); if (this.ID == null) { this.ID = new MongoDB.Bson.ObjectId().ToString(); } ; //new character MongoCollection characterCollection = characterDB.GetCollection <BsonDocument>("NPCCharacters"); IMongoQuery search = Query.EQ("_id", ObjectId.Parse(this.ID)); BsonDocument npcCharacter = characterCollection.FindOneAs <BsonDocument>(search); if (npcCharacter == null) { //this is the NPC's first save, create everything from scratch npcCharacter = new BsonDocument(); npcCharacter.Add("FirstName", this.FirstName); npcCharacter.Add("LastName", this.LastName); npcCharacter.Add("Race", this.Race.CamelCaseWord()); npcCharacter.Add("Class", this.Class.CamelCaseWord()); npcCharacter.Add("Gender", this.Gender.CamelCaseWord()); npcCharacter.Add("SkinColor", this.SkinColor.CamelCaseWord()); npcCharacter.Add("SkinType", this.SkinType.CamelCaseWord()); npcCharacter.Add("HairColor", this.HairColor.CamelCaseWord()); npcCharacter.Add("EyeColor", this.EyeColor.CamelCaseWord()); npcCharacter.Add("Weight", this.Weight); npcCharacter.Add("Height", this.Height); npcCharacter.Add("ActionState", this.ActionState.ToString().CamelCaseWord()); npcCharacter.Add("StanceState", this.StanceState.ToString().CamelCaseWord()); npcCharacter.Add("Description", this.Description); npcCharacter.Add("Location", this.Location); npcCharacter.Add("AiState", Fsm.state.ToString()); npcCharacter.Add("previousAiState", Fsm.previousState != null ? Fsm.previousState.ToString() : ""); npcCharacter.Add("AiGlobalState", Fsm.globalState != null ? Fsm.globalState.ToString() : ""); npcCharacter.Add("NextAiAction", this.NextAiAction.ToUniversalTime()); npcCharacter.Add("IsNPC", this.IsNPC); npcCharacter.Add("MobTypeID", this.MobTypeID); npcCharacter.Add("CurrentTarget", this.CurrentTarget != null ? this.CurrentTarget.ToString() : ""); npcCharacter.Add("LastTarget", this.LastTarget != null ? this.LastTarget.ToString() : ""); npcCharacter.Add("InCombat", this.InCombat); npcCharacter.Add("LastCombatTime", this.LastCombatTime); npcCharacter.Add("Experience", this.Experience); npcCharacter.Add("Level", this.Level); npcCharacter.Add("KillerID", this.KillerID); npcCharacter.Add("Title", this.Title); BsonArray attributeList = new BsonArray(); foreach (Attribute a in this.Attributes.Values) { BsonDocument attributes = new BsonDocument(); attributes.Add("Name", ""); attributes.Add("Value", ""); attributes.Add("Max", ""); attributes.Add("RegenRate", ""); attributes["Name"] = a.Name; attributes["Value"] = a.Value; attributes["Max"] = a.Max; attributes["RegenRate"] = a.RegenRate; attributeList.Add(attributes); } npcCharacter.Add("Attributes", attributeList); BsonArray xpTracker = new BsonArray(); foreach (KeyValuePair <string, double> tracker in damageTracker) { BsonDocument track = new BsonDocument(); track.Add("ID", ""); track.Add("Value", 0.0); track["ID"] = tracker.Key; track["Value"] = tracker.Value; xpTracker.Add(track); } npcCharacter.Add("XpTracker", xpTracker); npcCharacter.Add("Bonuses", Bonuses.GetBson()); } else { npcCharacter["FirstName"] = this.FirstName; npcCharacter["LastName"] = this.LastName; npcCharacter["Race"] = this.Race; npcCharacter["Class"] = this.Class; npcCharacter["Gender"] = this.Gender.CamelCaseWord(); npcCharacter["SkinColor"] = this.SkinColor.CamelCaseWord(); npcCharacter["SkinType"] = this.SkinType.CamelCaseWord(); npcCharacter["HairColor"] = this.HairColor.CamelCaseWord(); npcCharacter["EyeColor"] = this.EyeColor.CamelCaseWord(); npcCharacter["Weight"] = this.Weight; npcCharacter["Height"] = this.Height; npcCharacter["Description"] = this.Description; npcCharacter["Location"] = this.Location; npcCharacter["ActionState"] = this.ActionState.ToString().CamelCaseWord(); npcCharacter["StanceState"] = this.StanceState.ToString().CamelCaseWord(); npcCharacter["AiState"] = Fsm.state.ToString(); npcCharacter["previousAiState"] = Fsm.previousState == null ? "" : Fsm.previousState.ToString(); npcCharacter["AiGlobalState"] = Fsm.globalState == null ? "" : Fsm.globalState.ToString(); npcCharacter["NextAiAction"] = this.NextAiAction.ToUniversalTime(); npcCharacter["MobTypeID"] = this.MobTypeID; npcCharacter["IsNPC"] = this.IsNPC; npcCharacter["CurrentTarget"] = this.CurrentTarget == null ? "" : this.CurrentTarget; npcCharacter["LastTarget"] = this.LastTarget == null ? "" : this.LastTarget; npcCharacter["InCombat"] = this.InCombat; npcCharacter["LastCombatTime"] = this.LastCombatTime; npcCharacter["Experience"] = this.Experience; npcCharacter["Level"] = this.Level; npcCharacter["KillerID"] = this.KillerID; npcCharacter["Title"] = this.Title; BsonArray playerAttributes = new BsonArray(); BsonArray xpTracker = new BsonArray(); foreach (KeyValuePair <string, Attribute> attribute in Attributes) { BsonDocument attrib = new BsonDocument(); attrib.Add("Name", ""); attrib.Add("Value", ""); attrib.Add("Max", ""); attrib.Add("RegenRate", ""); attrib["Name"] = attribute.Key; attrib["Value"] = attribute.Value.Value; attrib["Max"] = attribute.Value.Max; attrib["RegenRate"] = attribute.Value.RegenRate; playerAttributes.Add(attrib); } npcCharacter["Attributes"] = playerAttributes; foreach (KeyValuePair <string, double> tracker in damageTracker) { BsonDocument track = new BsonDocument(); track.Add("ID", ""); track.Add("Value", 0.0d); track["ID"] = tracker.Key; track["Value"] = tracker.Value; xpTracker.Add(track); } npcCharacter["XpTracker"] = xpTracker; npcCharacter["Bonuses"] = Bonuses.GetBson(); } characterCollection.Save(npcCharacter); if (this.ID == "000000000000000000000000") { this.ID = npcCharacter["_id"].AsObjectId.ToString(); } }
public void Save() { MongoUtils.MongoData.ConnectToDatabase(); MongoDatabase characterDB = MongoUtils.MongoData.GetDatabase("Characters"); if (this.ID == null) { this.ID = new MongoDB.Bson.ObjectId().ToString(); } ; //new character MongoCollection characterCollection = characterDB.GetCollection <BsonDocument>("PlayerCharacter"); IMongoQuery search = Query.EQ("_id", ObjectId.Parse(this.ID)); BsonDocument playerCharacter = characterCollection.FindOneAs <BsonDocument>(search); if (playerCharacter == null) { //this is the players first save, create everything from scratch playerCharacter = new BsonDocument { //no _id let MongoDB assign it one so we don't have to deal with duplicate values logic { "FirstName", this.FirstName.ToLower() }, { "LastName", this.LastName.ToLower() }, { "Race", this.Race.CamelCaseWord() }, { "Class", this.Class.CamelCaseWord() }, { "Gender", this.Gender.CamelCaseWord() }, { "SkinColor", this.SkinColor.CamelCaseWord() }, { "SkinType", this.SkinType.CamelCaseWord() }, { "HairColor", this.HairColor.CamelCaseWord() }, { "EyeColor", this.EyeColor.CamelCaseWord() }, { "Weight", (double)this.Weight }, { "Height", (double)this.Height }, { "ActionState", this.ActionState.ToString().CamelCaseWord() }, { "StanceState", this.StanceState.ToString().CamelCaseWord() }, { "Description", this.Description }, { "Location", this.Location }, { "Password", this.Password }, { "IsNPC", this.IsNPC }, { "Experience", this.Experience }, { "NextLevelExperience", this.NextLevelExperience }, { "PointsToSpend", this.PointsToSpend }, { "Level", this.Level }, { "Leveled", this.Leveled }, { "MainHand", this.MainHand }, { "KillerID", this.KillerID }, { "Title", this.Title } }; BsonDocument attributes = new BsonDocument { { "Name", "" }, { "Value", "" }, { "Max", "" }, { "RegenRate", "" }, { "Rank", "" } }; BsonArray attributeList = new BsonArray(); foreach (Attribute a in this.Attributes.Values) { attributes["Name"] = a.Name; attributes["Value"] = (double)a.Value; attributes["Max"] = (double)a.Max; attributes["RegenRate"] = (double)a.RegenRate; attributes["Rank"] = (double)a.Rank; attributeList.Add(attributes); } playerCharacter.Add("Attributes", attributeList); BsonArray Inventory = new BsonArray(); playerCharacter.Add("Inventory", Inventory); BsonArray Equipment = new BsonArray(); playerCharacter.Add("Equipment", Equipment); playerCharacter.Add("Bonuses", Bonuses.GetBson()); } else { playerCharacter["FirstName"] = this.FirstName.ToLower(); playerCharacter["LastName"] = this.LastName.ToLower(); playerCharacter["Race"] = this.Race; playerCharacter["Class"] = this.Class; playerCharacter["Gender"] = this.Gender.CamelCaseWord(); playerCharacter["SkinColor"] = this.SkinColor.CamelCaseWord(); playerCharacter["SkinType"] = this.SkinType.CamelCaseWord(); playerCharacter["HairColor"] = this.HairColor.CamelCaseWord(); playerCharacter["EyeColor"] = this.EyeColor.CamelCaseWord(); playerCharacter["Weight"] = this.Weight; playerCharacter["Height"] = this.Height; playerCharacter["Description"] = this.Description; playerCharacter["Location"] = this.Location; playerCharacter["ActionState"] = this.ActionState.ToString().CamelCaseWord(); playerCharacter["StanceState"] = this.StanceState.ToString().CamelCaseWord(); playerCharacter["Password"] = this.Password; playerCharacter["IsNPC"] = this.IsNPC; playerCharacter["Experience"] = this.Experience; playerCharacter["NextLevelExperience"] = this.NextLevelExperience; playerCharacter["PointsToSpend"] = this.PointsToSpend; playerCharacter["Level"] = this.Level; playerCharacter["Leveled"] = this.Leveled; playerCharacter["MainHand"] = this.MainHand; playerCharacter["KillerID"] = this.KillerID; playerCharacter["Title"] = this.Title; BsonArray attributeList = new BsonArray(); BsonArray inventoryList = new BsonArray(); BsonArray equipmentList = new BsonArray(); foreach (Attribute attrib in Attributes.Values) { BsonDocument attributes = new BsonDocument { { "Name", "" }, { "Value", "" }, { "Max", "" }, { "RegenRate", "" }, { "Rank", "" } }; attributes["Name"] = attrib.Name; attributes["Value"] = attrib.Value; attributes["Max"] = attrib.Max; attributes["RegenRate"] = attrib.RegenRate; attributes["Rank"] = attrib.Rank; attributeList.Add(attributes); } playerCharacter["Attributes"] = attributeList; BsonDocument items = new BsonDocument { { "_id", "" } }; //Inventory and equipment foreach (Items.Iitem item in Inventory.inventory) { items["_id"] = item.Id; inventoryList.Add(items); } playerCharacter["Inventory"] = inventoryList; foreach (KeyValuePair <Items.Wearable, Items.Iitem> item in Equipment.equipped) { items["_id"] = item.Value.Id; equipmentList.Add(items); } playerCharacter["Equipment"] = equipmentList; playerCharacter["Bonuses"] = Bonuses.GetBson(); } characterCollection.Save(playerCharacter); if (this.ID == "000000000000000000000000") { this.ID = playerCharacter["_id"].AsObjectId.ToString(); } }