-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatches.cs
278 lines (237 loc) · 8.78 KB
/
Matches.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
namespace CLReader
{
public class Matches
{
public Dictionary<string,Item> matchDict { get; set; }
public DateTime LastScanDate { get; set; }
List<string> currentIgnoreList;
List<string> newIgnoreList;
public Matches()
{
LastScanDate=DateTime.Now;
//Console.WriteLine($"Last Scan date set to: {LastScanDate}");
matchDict = new Dictionary<string, Item>();
currentIgnoreList = JsonConvert.DeserializeObject<List<string>>(File.ReadAllText("TitlesToIgnore.json"));
newIgnoreList = new List<string>();
/*
//one time conversion
foreach(string title in currentIgnoreList)
{
string newtitle = title;
newtitle = System.Net.WebUtility.UrlDecode(newtitle);
newtitle = newtitle.Replace("$","$");
newIgnoreList.Add(newtitle);
}
SaveNewIgnoreList();
*/
}
public void SaveNewIgnoreList()
{
//Let's make sure every ignore entry is given two attempts to work due to the interwebs being flaky
//This way, something has to "miss" twice before falling off
//First, let's load the existing 2nd chance list
string secondChanceFileName="TitlesToIgnore2ndChance.json";
List<string> secondChanceList = JsonConvert.DeserializeObject<List<string>>(File.ReadAllText(secondChanceFileName));
//Time to compare. (entries in 2nd chance, but not in newIgnoreList) In other words, these are the entires which would have fallen off if not not for 2nd chance
secondChanceList = secondChanceList.Except(newIgnoreList).ToList();
//Save 2nd chance as the current (pre add) ignore list. This way, if the entry is STILL not in the current ignore list next time, it WILL fall off
//In other words, we're serializing something that's different than what'll be in memory.
System.IO.File.WriteAllText(secondChanceFileName, JsonConvert.SerializeObject(newIgnoreList, Formatting.Indented));
//Now let's add current 2nd chance list to the new ignore list
newIgnoreList.AddRange(secondChanceList);
//Save the current title to ignore - which now include 2nd chance....
string fileName="TitlesToIgnore.json";
System.IO.File.WriteAllText(fileName, JsonConvert.SerializeObject(newIgnoreList, Formatting.Indented));
}
public Item GetItem(string title)
{
return matchDict[title];
}
public void Save()
{
//Save current matches (these become "last" on startup)
System.IO.File.WriteAllText("LastMatches.json", JsonConvert.SerializeObject(this, Formatting.Indented));
}
public bool AddItem(Item item,Matches lastMatches)
{
//Fix some stuff....
item.Title=item.Title.Replace("'",""); //This is weird, but I couln't find a better way to do this
//make sure we don't need to ignore
string title = item.Title;
if(!currentIgnoreList.Contains(title))
{
//Bring forward from last scan to preserve date (repostings and entries with no date)
Item lastItem;
if(lastMatches != null && lastMatches.matchDict.TryGetValue(title,out lastItem))
{
return matchDict.TryAdd(title,lastItem);
}
else
{
return matchDict.TryAdd(title,item);
}
}
else
{
//Let's save this ignore list entry because it hit a match
newIgnoreList.Add(title);
return false;
}
}
public bool RemoveItem(Item item)
{
return RemoveItem(item);
}
public bool RemoveItem(string title)
{
return matchDict.Remove(title);
}
public void MarkClicked(string titleKey,string user)
{
//See who it is
bool jennieFlag = false;
if(user=="Jennie")
jennieFlag=true;
//Get item
Item item=GetItem(titleKey);
//Set appropriate flag
if(jennieFlag)
item.JennieClicked = true;
else
item.MarkClicked = true;
Save();
}
public void PromoteItem(string titleKey,string user)
{
//See who it is
bool jennieFlag = false;
if(user=="Jennie")
jennieFlag=true;
//Get item
Item item=GetItem(titleKey);
//Set appropriate flag
if(jennieFlag)
{
if(!item.JennieInterested && !item.JennieBuy)
{
item.JennieInterested=true;
}
else if(item.JennieInterested && !item.JennieBuy)
{
item.JennieInterested=false;
item.JennieBuy=true;
}
}
else
{
if(!item.MarkInterested && !item.MarkBuy)
{
item.MarkInterested=true;
}
else if(item.MarkInterested && !item.MarkBuy)
{
item.MarkInterested=false;
item.MarkBuy=true;
}
}
Save();
}
public bool ItemIsPromoted(string titleKey)
{
bool promotedFlag = false;
Item item=GetItem(titleKey);
if(item.MarkBuy || item.MarkInterested || item.JennieBuy || item.JennieInterested)
promotedFlag=true;
return promotedFlag;
}
public void DemoteItem(string titleKey,string user)
{
//See who it is
bool jennieFlag = false;
if(user=="Jennie")
jennieFlag=true;
//Get item
Item item=GetItem(titleKey);
//Set appropriate flag
if(jennieFlag)
{
if(item.JennieBuy)
{
item.JennieInterested=true;
item.JennieBuy=false;
}
else
{
item.JennieInterested=false;
}
}
else
{
if(item.MarkBuy)
{
item.MarkInterested=true;
item.MarkBuy=false;
}
else
{
item.MarkInterested=false;
}
}
Save();
}
public List<Item> GetList()
{
List<Item> matchesList = matchDict.Values.ToList();
ItemPublishDateComparer ic = new ItemPublishDateComparer();
matchesList.Sort(ic);
return matchesList;
}
}
//So we can sort our lists and dictionaries
public class ItemPublishDateComparer : IComparer<Item>
{
public int Compare(Item item1, Item item2)
{
//-1 item1<item2 0-item1=item2, and 1=item1>item2
// our list is in reverse order (newest on top)
//First check promotion level
if(IsItemBuy(item1) && IsItemBuy(item2))
return CompareDates(item1,item2);
else if(IsItemBuy(item1) && !IsItemBuy(item2))
return(-1);
else if(!IsItemBuy(item1) && IsItemBuy(item2))
return(1);
if(IsItemInterested(item1) && IsItemInterested(item2))
return CompareDates(item1,item2);
else if(IsItemInterested(item1) && !IsItemInterested(item2))
return(-1);
else if(!IsItemInterested(item1) && IsItemInterested(item2))
return(1);
//Nothing promoted
return (CompareDates(item1,item2));
}
private int CompareDates(Item item1,Item item2)
{
if(item1.PublishDate < item2.PublishDate)
return(1);
else if(item1.PublishDate == item2.PublishDate)
return(0);
else
return(-1);
}
private bool IsItemBuy(Item item)
{
return(item.MarkBuy || item.JennieBuy);
}
private bool IsItemInterested(Item item)
{
return(item.MarkInterested || item.JennieInterested);
}
}
}