This repository has been archived by the owner on Sep 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFactHandler.cs
123 lines (107 loc) · 3.83 KB
/
FactHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Discord;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace SteamDiscordBot
{
class FactHandler
{
private ConcurrentDictionary<ulong, List<string>> facts;
public FactHandler()
{
facts = new ConcurrentDictionary<ulong, List<string>>();
}
public void WriteToGuild(ulong guild, string fact)
{
try
{
using (FileStream stream = new FileStream(Program.BuildPath(guild + "_facts.txt"), FileMode.Append))
{
Byte[] info = new UTF8Encoding(true).GetBytes((fact + "\n"));
stream.Write(info, 0, info.Length);
}
}
catch (Exception e)
{
Program.Instance.Log(new LogMessage(LogSeverity.Info, "FactHandler.WriteToGuild", e.Message));
}
}
public async Task<int> RemoveFromGuild(ulong guild, string fact)
{
// remove from cache
int count = facts[guild].RemoveAll((x) => x.Equals(fact));
if (count == 0)
{
return 0;
}
// remove from file
count = 0;
List<string> array = new List<string>();
using (FileStream fileStream = File.Open(Program.BuildPath(guild + "_facts.txt"), FileMode.Open))
using (BufferedStream bufferedStream = new BufferedStream(fileStream))
using (StreamReader reader = new StreamReader(bufferedStream))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (!line.Contains(fact))
array.Add(line);
else
count++;
}
}
using (FileStream fileStream = File.Open(Program.BuildPath(guild + "_facts.txt"), FileMode.Create))
using (BufferedStream bufferedStream = new BufferedStream(fileStream))
using (StreamWriter reader = new StreamWriter(bufferedStream))
{
foreach (string line in array)
{
await reader.WriteLineAsync(line);
}
}
return count;
}
public async Task AddGuild(ulong guild)
{
if (facts.ContainsKey(guild)) // we must've disconnected briefly, but still have our data
return;
var path = Program.BuildPath(guild + "_facts.txt");
var list = new List<string>();
facts.TryAdd(guild, list);
if (!File.Exists(path))
{
File.CreateText(path); // guild non-existant. Make empty file and move on
return;
}
await LoadList(list, path);
}
public List<string> GetList(ulong id)
{
return facts[id];
}
private static async Task LoadList(List<string> list, string path)
{
try
{
/*
* Unforunately with huge files, using StreamReader alone is too slow,
* so we use BufferedStream to speed things up a bit.
*/
using (FileStream fileStream = File.Open(path, FileMode.Open))
using (BufferedStream bufferedStream = new BufferedStream(fileStream))
using (StreamReader reader = new StreamReader(bufferedStream))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
list.Add(line);
}
}
}
catch { }
}
}
}