public bool SaveDataToEntityFramework() { using (var ctx = new hybrasylEntities(Constants.ConnectionString)) { var playerquery = ctx.players.Where(player => player.name == Name).SingleOrDefault(); if (playerquery == null) { return false; } var inventory = new JArray(); var equipment = new JArray(); for (byte i = 1; i <= Inventory.Size; ++i) { if (Inventory[i] == null) continue; var obj = new JObject(); obj.Add("slot", (int)i); obj.Add("count", Inventory[i].Count); if (Inventory[i].IsVariant) { obj.Add("item_id", Inventory[i].ParentItem.id); obj.Add("variant_id", Inventory[i].CurrentVariant.id); } else { obj.Add("item_id", Inventory[i].TemplateId); } inventory.Add(obj); } for (byte i = 1; i < Equipment.Size; ++i) { if (Equipment[i] == null) continue; var obj = new JObject(); obj.Add("slot", (int)i); if (Equipment[i].IsVariant) { obj.Add("item_id", Equipment[i].ParentItem.id); obj.Add("variant_id", Equipment[i].CurrentVariant.id); } else { obj.Add("item_id", Equipment[i].TemplateId); } equipment.Add(obj); } foreach (var property in GetType().GetProperties()) { string value; if (User.EntityFrameworkMapping.TryGetValue(property.Name, out value)) { // Nullables on the DB side need special handling to cast correctly. var curType = property.PropertyType; var destType = Nullable.GetUnderlyingType(typeof(player).GetProperty(value).PropertyType) ?? typeof(player).GetProperty(value).PropertyType; object safevalue = (value == null) ? null : Convert.ChangeType(property.GetValue(this), destType); // BEWARE - this will break if passed Enums that aren't handled via EF 5's enum support! ctx.Entry(playerquery).Property(value).CurrentValue = safevalue; } } ctx.Entry(playerquery).Property("inventory").CurrentValue = inventory.ToString(); ctx.Entry(playerquery).Property("equipment").CurrentValue = equipment.ToString(); // Save our current location ctx.Entry(playerquery).Property("map_id").CurrentValue = (int)Map.Id; ctx.Entry(playerquery).Property("map_x").CurrentValue = (int)X; ; ctx.Entry(playerquery).Property("map_y").CurrentValue = (int)Y; if (Citizenship != null) ctx.Entry(playerquery).Property("nation_id").CurrentValue = Citizenship.id; ctx.SaveChanges(); } return true; }
// Chart for all error password-related error codes were provided by kojasou@ on // https://github.com/hybrasyl/server/pull/11. private void PacketHandler_0x26_ChangePassword(Client client, ClientPacket packet) { var name = packet.ReadString8(); var currentPass = packet.ReadString8(); // Clientside validation ensures that the same string is typed twice for the new // password, and the new password is only sent to the server once. We can assume // that they matched if 0x26 request is sent from the client. var newPass = packet.ReadString8(); using (var ctx = new hybrasylEntities(Constants.ConnectionString)) { var player = ctx.players.Where(p => p.name == name).SingleOrDefault(); // Check that `name` exists. If not, return a message indicating that to the user. if (player == null) { client.LoginMessage(GetPasswordError(0x0E), 0x0E); Logger.DebugFormat("Password change attempt on invalid player `{0}`", name); } // If the player does exist, validate the current and new passwords before updating. else { // Check that the current password is correct and the new password is different // than the current password. if (VerifyPassword(currentPass, player)) { // Check if the password is valid. byte err = 0x00; if (ValidPassword(newPass, out err)) { player.password_hash = HashPassword(newPass); ctx.SaveChanges(); // Let the user know the good news. client.LoginMessage("Your password has been changed successfully.", 0x0); Logger.InfoFormat("Password successfully changed for `{0}`", name); } else { client.LoginMessage(GetPasswordError(err), err); Logger.ErrorFormat("Invalid new password proposed during password change attempt for `{0}`", name); } } // The current password is incorrect. Don't allow any changes to happen. else { client.LoginMessage(GetPasswordError(0x0F), 0x0F); Logger.ErrorFormat("Invalid current password during password change attempt for `{0}`", name); } } } }
/// <summary> /// Update a player's last logoff time in the database and the live object. /// </summary> public void UpdateLogoffTime() { using (var ctx = new hybrasylEntities(Constants.ConnectionString)) { var playerquery = ctx.players.Where(player => player.name == Name).SingleOrDefault(); if (playerquery == null) { // This means something very odd is happening; we might want to throw an exception // or do something else here return; } else { var now = DateTime.Now; LoginTime = now; ctx.Entry(playerquery).Property("last_logoff").CurrentValue = now; ctx.SaveChanges(); } } }
private void PacketHandler_0x04_CreateB(Client client, ClientPacket packet) { if (string.IsNullOrEmpty(client.NewCharacterName) || string.IsNullOrEmpty(client.NewCharacterPassword)) return; var hairStyle = packet.ReadByte(); var sex = packet.ReadByte(); var hairColor = packet.ReadByte(); if (hairStyle < 1) hairStyle = 1; if (hairStyle > 17) hairStyle = 17; if (hairColor > 13) hairColor = 13; if (sex < 1) sex = 1; if (sex > 2) sex = 2; if (!Game.World.PlayerExists(client.NewCharacterName)) { using (var ctx = new hybrasylEntities(Constants.ConnectionString)) { player newplayer = new player { name = client.NewCharacterName, password_hash = client.NewCharacterPassword, sex = (Sex) sex, hairstyle = hairStyle, haircolor = hairColor, map_id = 136, map_x = 10, map_y = 10, direction = 1, class_type = 0, level = 1, exp = 0, ab = 0, gold = 0, ab_exp = 0, max_hp = 50, max_mp = 50, cur_hp = 50, cur_mp = 35, str = 3, @int = 3, wis = 3, con = 3, dex = 3, inventory = "[]", equipment = "[]", created_at = DateTime.Now }; try { ctx.players.Add(newplayer); ctx.SaveChanges(); } catch (Exception e) { Logger.ErrorFormat("Error saving new player!"); Logger.ErrorFormat(e.ToString()); client.LoginMessage("Unknown error. Contact [email protected]", 3); } client.LoginMessage("\0", 0); } } }