/// <summary> /// Saves a key/value pair to a given player's DeviceID. Will reject a pair containing a PlusCode or map element Id. /// </summary> /// <param name="playerId"></param> /// <param name="key">The key to save to the database. Keys are unique, and you cannot have multiples of the same key.</param> /// <param name="value">The value to save with the key.</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 SetPlayerData(string playerId, string key, string value, double?expiration = null) { if (DataCheck.IsPlusCode(key) || DataCheck.IsPlusCode(value)) { return(false); //Reject attaching a player to a pluscode. } var db = new PraxisContext(); Guid tempCheck = new Guid(); if ((Guid.TryParse(key, out tempCheck) && db.Places.Any(osm => osm.PrivacyId == tempCheck)) || (Guid.TryParse(value, out tempCheck) && db.Places.Any(osm => osm.PrivacyId == tempCheck))) { return(false); //reject attaching a player to an area } var row = db.PlayerData.FirstOrDefault(p => p.DeviceID == playerId && p.DataKey == key); if (row == null) { row = new DbTables.PlayerData(); row.DataKey = key; row.DeviceID = playerId; db.PlayerData.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 the database that isn't attached to anything specific. Wil reject a pair that contains a player's device ID, PlusCode, or a map element ID. Global entries cannot be set to expire. /// </summary> /// <param name="key">The key to save to the database. Keys are unique, and you cannot have multiples of the same key.</param> /// <param name="value">The value to save with the key.</param> /// <returns>true if data was saved, false if data was not.</returns> public static bool SetGlobalData(string key, string value) { bool trackingPlayer = false; bool trackingLocation = false; var db = new PraxisContext(); if (db.PlayerData.Any(p => p.DeviceID == key || p.DeviceID == value)) { trackingPlayer = true; } if (DataCheck.IsPlusCode(key) || DataCheck.IsPlusCode(value)) { trackingLocation = true; } Guid tempCheck = new Guid(); if ((Guid.TryParse(key, out tempCheck) && db.Places.Any(osm => osm.PrivacyId == tempCheck)) || (Guid.TryParse(value, out tempCheck) && db.Places.Any(osm => osm.PrivacyId == tempCheck))) { trackingLocation = true; } if (trackingLocation && trackingPlayer) //Do not allow players and locations to be attached on the global level as a workaround to being blocked on the individual levels. { return(false); } var row = db.GlobalDataEntries.FirstOrDefault(p => p.DataKey == key); if (row == null) { row = new DbTables.GlobalDataEntries(); row.DataKey = key; db.GlobalDataEntries.Add(row); } row.DataValue = value.ToByteArrayUTF8(); return(db.SaveChanges() == 1); }