// Loads stats from stat sheet public static async Task Load() { // Create stat sheet if it doesn't exist if (File.Exists(Constants.StatSheet)) { // Load values JObject StatSheet = JObject.Parse(File.ReadAllText(Constants.StatSheet)); GlobalStats = StatSheet["globalStats"].ToObject <StatTracker>(); ChannelStats = StatSheet["channelStats"].ToObject <Dictionary <ulong, StatTracker> >(); UserStats = StatSheet["userStats"].ToObject <Dictionary <ulong, StatTracker> >(); } else { await Update(); } }
// Update user tips public static Task Tip(DateTime Date, ulong Channel, ulong Id, decimal Amount) { // Create a database connection using (SqliteConnection Connection = new SqliteConnection("Data Source=" + RainBorg.databaseFile)) { // Open connection to database Connection.Open(); // Update global stats StatTracker GlobalStats = new StatTracker(); SqliteCommand Command = new SqliteCommand("SELECT totaltips, totalamount FROM global", Connection); Command.Parameters.AddWithValue("id", Id); using (SqliteDataReader Reader = Command.ExecuteReader()) if (Reader.Read()) { GlobalStats = new StatTracker { TotalTips = Reader.GetInt32(0), TotalAmount = Reader.GetDecimal(1) } } ; GlobalStats.TotalTips++; GlobalStats.TotalAmount += Amount; Command = new SqliteCommand(@"UPDATE global SET totaltips = @totaltips, totalamount = @totalamount", Connection); Command.Parameters.AddWithValue("id", Channel); Command.Parameters.AddWithValue("totaltips", GlobalStats.TotalTips); Command.Parameters.AddWithValue("totalamount", GlobalStats.TotalAmount); Command.ExecuteNonQuery(); // Update channel stats StatTracker ChannelStats = new StatTracker(); Command = new SqliteCommand("SELECT totaltips, totalamount FROM channels WHERE id = @id", Connection); Command.Parameters.AddWithValue("id", Id); using (SqliteDataReader Reader = Command.ExecuteReader()) if (Reader.Read()) { ChannelStats = new StatTracker { TotalTips = Reader.GetInt32(0), TotalAmount = Reader.GetDecimal(1) } } ; ChannelStats.TotalTips++; ChannelStats.TotalAmount += Amount; Command = new SqliteCommand(@"INSERT OR REPLACE INTO channels (id, totaltips, totalamount) values (@id, @totaltips, @totalamount)", Connection); Command.Parameters.AddWithValue("id", Channel); Command.Parameters.AddWithValue("totaltips", ChannelStats.TotalTips); Command.Parameters.AddWithValue("totalamount", ChannelStats.TotalAmount); Command.ExecuteNonQuery(); // Update user stats StatTracker UserStats = new StatTracker(); Command = new SqliteCommand("SELECT totaltips, totalamount FROM users WHERE id = @id", Connection); Command.Parameters.AddWithValue("id", Id); using (SqliteDataReader Reader = Command.ExecuteReader()) if (Reader.Read()) { UserStats = new StatTracker { TotalTips = Reader.GetInt32(0), TotalAmount = Reader.GetDecimal(1) } } ; UserStats.TotalTips++; UserStats.TotalAmount += Amount; Command = new SqliteCommand(@"INSERT OR REPLACE INTO users (id, totaltips, totalamount) values (@id, @totaltips, @totalamount)", Connection); Command.Parameters.AddWithValue("id", Id); Command.Parameters.AddWithValue("totaltips", UserStats.TotalTips); Command.Parameters.AddWithValue("totalamount", UserStats.TotalAmount); Command.ExecuteNonQuery(); // Add tip to database Command = new SqliteCommand(@"INSERT INTO tips (user, channel, date, amount) values (@user, @channel, @date, @amount)", Connection); Command.Parameters.AddWithValue("user", Id); Command.Parameters.AddWithValue("channel", Channel); Command.Parameters.AddWithValue("date", Date); Command.Parameters.AddWithValue("amount", Amount); Command.ExecuteNonQuery(); } // Completed return(Task.CompletedTask); }