Skip to content

Instantly share code, notes, and snippets.

@phytal
Created July 13, 2018 02:05
Show Gist options
  • Save phytal/e06a80827a483590d7fe075ccbb9f21a to your computer and use it in GitHub Desktop.
Save phytal/e06a80827a483590d7fe075ccbb9f21a to your computer and use it in GitHub Desktop.
using Wsashi.Configuration;
using System;
using Wsashi.Entities;
using Discord;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Wsashi.Features.GlobalAccounts
{
internal static class GlobalUserAccounts
{
private static readonly ConcurrentDictionary<ulong, GlobalUserAccount> userAccounts = new ConcurrentDictionary<ulong, GlobalUserAccount>();
static GlobalUserAccounts()
{
var info = System.IO.Directory.CreateDirectory(Path.Combine(Constants.ResourceFolder, Constants.UserAccountsFolder));
var files = info.GetFiles("*.json");
if (files.Length > 0)
{
foreach (var file in files)
{
var user = Configuration.DataStorage.RestoreObject<GlobalUserAccount>(Path.Combine(file.Directory.Name, file.Name));
userAccounts.TryAdd(user.Id, user);
}
}
else
{
userAccounts = new ConcurrentDictionary<ulong, GlobalUserAccount>();
}
}
internal static GlobalUserAccount GetUserAccount(ulong id)
{
return userAccounts.GetOrAdd(id, (key) =>
{
var newAccount = new GlobalUserAccount { Id = id };
Configuration.DataStorage.StoreObject(newAccount, Path.Combine(Constants.UserAccountsFolder, $"{id}.json"), useIndentations: true);
return newAccount;
});
}
internal static GlobalUserAccount GetUserAccount(IUser user)
{
return GetUserAccount(user.Id);
}
internal static List<GlobalUserAccount> GetAllAccounts()
{
return userAccounts.Values.ToList();
}
internal static List<GlobalUserAccount> GetFilteredAccounts(Func<GlobalUserAccount, bool> filter)
{
return userAccounts.Values.Where(filter).ToList();
}
internal static void SaveAccounts()
{
foreach (var id in userAccounts.Keys)
{
SaveAccounts(id);
}
}
internal static void SaveAccounts(params ulong[] ids)
{
foreach (var id in ids)
{
Configuration.DataStorage.StoreObject(GetUserAccount(id), Path.Combine(Constants.UserAccountsFolder, $"{id}.json"), useIndentations: true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment