public void save() { try { using (Repositories.UserRepository accs = new Repositories.UserRepository()) { Console.WriteLine("Saving Player"); Console.WriteLine(JsonConvert.SerializeObject(entity.items)); foreach (Models.ItemEF item in entity.items) { item.mustSave = false; if (item.id == 0) { accs.Entry(item).State = System.Data.Entity.EntityState.Added; } else { accs.Entry(item).State = System.Data.Entity.EntityState.Modified; //Forces it to update } } //if (mustAttach) accs.users.Attach(this); //adds new items accs.Entry(entity).State = System.Data.Entity.EntityState.Modified; accs.Entry(this).State = System.Data.Entity.EntityState.Modified; accs.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void Register(SharpBridge.Player player, object[] param) { string pass = ((string)param[0]).ToLower(); if (pass.Length < 3 || pass.Length > 22) { player.sendMsg("Your password must contain 3-22 characters", ChatColor.ERROR); return; } try { using (Repositories.UserRepository accs = new Repositories.UserRepository()) { Models.EntityEF uc = new Models.EntityEF { items = new List <Models.ItemEF>(), user = new Models.UserEF { name = player.getNick(), password = Services.Hashing.SHA2(ref pass), clothes = "1", hp = 100, armor = 0, moneyBank = 100 }, // Spawns the player on grass, near the Broker Bus Depot, looking at Montauk Avenue x = 1075.0f, y = 278.0f, z = 30.5f, createDate = DateTime.Now }; uc.user.entity = uc; accs.entities.Add(uc); accs.SaveChanges(); player.sendMsg($"Account created with password '{pass}'. Use /login to proceed", ChatColor.SUCCESS); } } catch (DbUpdateException e) { if (e.ToString().Contains("Duplicate entry")) { player.sendMsg($"Account '{player.getNick()}' already exists. Please use /login [password]", ChatColor.ERROR); return; } Console.WriteLine(e.ToString()); } }