Пример #1
0
        private static void AppendRaider(RaidHandle handle, Entry entry)
        {
            //Open the raid file
            using (FileStream fs = File.Open($"./raids/{handle.full_name}/raid.json", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                //Prepare the structure holding the data
                Raid raid;

                //Get UTF-8 encoded text streams
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

                //Deserialise the JSON
                raid = JsonConvert.DeserializeObject <Raid>(sr.ReadToEnd());

                //Reset the stream
                fs.Seek(0, SeekOrigin.Begin);
                fs.SetLength(0);

                //Append the raider
                raid.roster.Add(entry);

                //Serialise the Raid object
                sw.Write(JsonConvert.SerializeObject(raid, Formatting.Indented));
                sw.Flush();
            }
        }
Пример #2
0
        /// <summary>
        /// Removes a raider from the roster.
        /// </summary>
        /// <param name="handle">The handle to the raid.</param>
        /// <param name="entry">The handle to the user.</param>
        public static bool RemoveRaider(RaidHandle handle, Entry entry)
        {
            //Catch any errors
            return(Debug.Try(() =>
            {
                //Open the raid file
                using (FileStream fs = File.Open($"./raids/{handle.full_name}/raid.json", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    //Get UTF-8 encoded text streams
                    StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                    StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

                    //Deserialise the JSON
                    var raid = JsonConvert.DeserializeObject <Raid>(sr.ReadToEnd());

                    //Reset the stream
                    fs.Seek(0, SeekOrigin.Begin);
                    fs.SetLength(0);

                    //Remove the raider
                    raid.roster.RemoveAll(e => e.Equals(entry));

                    //Serialise the Raid object
                    sw.Write(JsonConvert.SerializeObject(raid, Formatting.Indented));
                    sw.Flush();
                }
            }));
        }
Пример #3
0
 /// <summary>
 /// Find the raider that matches the id.
 /// </summary>
 /// <param name="handle">The handle to the raid.</param>
 /// <param name="user_id">The user's unique ID on Discord.</param>
 public static Entry?FindRaider(RaidHandle handle, ulong user_id)
 {
     //Catch any errors
     return(Debug.Try <Entry?>(() =>
     {
         //Find the raider with the same id
         return CoalesceRaiders(handle).First(r => r.user_id == user_id);
     }, null, severity: LOG_LEVEL.INFO));
 }
Пример #4
0
 /// <summary>
 /// Deletes the specified raid.
 /// </summary>
 /// <param name="handle">The handle to the raid.</param>
 public static bool DeleteRaid(RaidHandle handle)
 {
     //Catch any errors
     return(Debug.Try(() =>
     {
         //Delete the raid folder and all content in it
         Directory.Delete($"./raids/{handle.full_name}/", true);
     }));
 }
Пример #5
0
 /// <summary>
 /// Finds any raiders that match a given name.
 /// </summary>
 /// <param name="handle">The handle to the raid.</param>
 /// <param name="user_name">The user's name.</param>
 public static List <Entry> FindRaiders(RaidHandle handle, string user_name)
 {
     //Catch any errors
     return(Debug.Try(() =>
     {
         //Find any that match the name
         return RaidManager.CoalesceRaiders(handle)
         .Where(r => ResolveName(r).ToLower().Contains(user_name.ToLower()))
         .ToList();
     }, new List <Entry>(), severity: LOG_LEVEL.INFO));
 }
Пример #6
0
        private static IEnumerable <Entry> GetRosterHistory(RaidHandle handle)
        {
            //Open the raid file
            using (FileStream fs = File.Open($"./raids/{handle.full_name}/raid.json", FileMode.Open, FileAccess.Read, FileShare.None))
            {
                //Get a UTF-8 encoded text stream
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);

                //Deserialise the JSON and return the roster
                return(JsonConvert.DeserializeObject <Raid>(sr.ReadToEnd()).roster);
            }
        }
Пример #7
0
        /// <summary>
        /// Gets a read-only view of the data for a given raid.
        /// </summary>
        /// <param name="handle">The handle to the raid.</param>
        public static Raid?GetRaidData(RaidHandle handle)
        {
            //Catch any errors
            return(Debug.Try <Raid?>(() =>
            {
                //Open the raid file
                using (FileStream fs = File.Open($"./raids/{handle.full_name}/raid.json", FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    //Get a UTF-8 encoded text stream
                    StreamReader sr = new StreamReader(fs, Encoding.UTF8);

                    //Deserialise the JSON and return the roster
                    return JsonConvert.DeserializeObject <Raid>(sr.ReadToEnd());
                }
            }, null));
        }
Пример #8
0
        /// <summary>
        /// Appends a raider to the raid roster.
        /// </summary>
        /// <param name="handle">The handle to the raid.</param>
        /// <param name="user_name">The user's name.</param>
        /// <param name="roles">A collection of roles the user can take.</param>
        public static bool AppendRaider(RaidHandle handle, string user_name, bool backup, IEnumerable <string> roles)
        {
            //Catch any errors
            return(Debug.Try(() =>
            {
                //Setup the entry
                var entry = new Entry
                {
                    user_id = null,
                    user_name = user_name,
                    backup = backup,
                    roles = roles.ToArray()
                };

                //Append the raider
                AppendRaider(handle, entry);
            }));
        }
Пример #9
0
        /// <summary>
        /// Returns a distinct list of raiders with the most
        /// up-to-date values.
        /// </summary>
        /// <param name="handle">The handle to the raid.</param>
        public static Entry[] CoalesceRaiders(RaidHandle handle)
        {
            //Catch any errors
            return(Debug.Try(() =>
            {
                //Get the roster history
                var roster = GetRosterHistory(handle);

                //Check if it's empty
                if (roster.Count() == 0)
                {
                    return Array.Empty <Entry>();
                }

                //Find the most recent entries for each user
                var entries = roster.Reverse().Distinct();

                //Return the most recent entries while maintaining the correct join order
                return roster.Distinct()
                .Select(e => entries.First(e2 => e2.Equals(e)))
                .ToArray();
            }, Array.Empty <Entry>()));
        }