Ejemplo n.º 1
0
        /// <summary>
        /// Saves a key/value pair to a given player's DeviceID with a password.
        /// </summary>
        /// <param name="playerId">the unique ID for the player, expected to be their unique device ID</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="password"></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 SetSecurePlayerData(string playerId, string key, byte[] value, string password, double?expiration = null)
        {
            var encryptedValue = EncryptValue(value, password, out byte[] IVs);

            var db  = new PraxisContext();
            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    = IVs;
            row.DataValue = encryptedValue;
            return(db.SaveChanges() == 1);
        }
Ejemplo n.º 2
0
        /// <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);
        }