/// <summary> /// Saves a key/value pair to a given map element. Will reject a pair containing a player's deviceId in the database. /// </summary> /// <param name="elementId">the Guid exposed to clients to identify the map element.</param> /// <param name="key">The key to save to the database for the map element.</param> /// <param name="value">The value to save with the key.</param> /// <param name="expiration">If not null, expire the data in this many seconds from now.</param> /// <returns>true if data was saved, false if data was not.</returns> public static bool SetPlaceData(Guid elementId, string key, string value, double?expiration = null) { var db = new PraxisContext(); if (db.PlayerData.Any(p => p.DeviceID == key || p.DeviceID == value)) { return(false); } var row = db.PlaceGameData.Include(p => p.Place).FirstOrDefault(p => p.Place.PrivacyId == elementId && p.DataKey == key); if (row == null) { var sourceItem = db.Places.First(p => p.PrivacyId == elementId); row = new DbTables.PlaceGameData(); row.DataKey = key; row.Place = sourceItem; db.PlaceGameData.Add(row); } if (expiration.HasValue) { row.Expiration = DateTime.Now.AddSeconds(expiration.Value); } else { row.Expiration = null; } row.IvData = null; row.DataValue = value.ToByteArrayUTF8(); return(db.SaveChanges() == 1); }
/// <summary> /// Saves a key/value pair to a given map element with the given password /// </summary> /// <param name="elementId">the Guid exposed to clients to identify the map element.</param> /// <param name="key">The key to save to the database for the map element.</param> /// <param name="value">The value to save with the key.</param> /// <param name="password">The password to encrypt the value with.</param> /// <param name="expiration">If not null, expire this data in this many seconds from now.</param> /// <returns>true if data was saved, false if data was not.</returns> public static bool SetSecurePlaceData(Guid elementId, string key, byte[] value, string password, double?expiration = null) { byte[] encryptedValue = EncryptValue(value, password, out byte[] IVs); var db = new PraxisContext(); var row = db.PlaceGameData.Include(p => p.Place).FirstOrDefault(p => p.Place.PrivacyId == elementId && p.DataKey == key); if (row == null) { var sourceItem = db.Places.First(p => p.PrivacyId == elementId); row = new DbTables.PlaceGameData(); row.DataKey = key; row.Place = sourceItem; db.PlaceGameData.Add(row); } if (expiration.HasValue) { row.Expiration = DateTime.Now.AddSeconds(expiration.Value); } else { row.Expiration = null; } row.IvData = IVs; row.DataValue = encryptedValue; return(db.SaveChanges() == 1); }