smarter compare.
if one list has something with an oracle id the other doesn't, that means a card was added or removed, update. if the oracle text is updated, update. otherwise, don't.
This commit is contained in:
parent
2630cf799e
commit
10face4c9e
48
Program.cs
48
Program.cs
@ -39,26 +39,19 @@ namespace creatureBirdDwarf
|
||||
string responseFromServer = dataStream.ReadToEnd();
|
||||
OracleIDd = JsonConvert.DeserializeObject<List<Card>>(responseFromServer);
|
||||
}
|
||||
stripData(ref OracleIDd);
|
||||
|
||||
Console.WriteLine("stripped external (price and edhrec rank) data, writing to file");
|
||||
|
||||
File.WriteAllText("./oracle-now.json", JsonConvert.SerializeObject(OracleIDd, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter()));
|
||||
var checksumNow = GetChecksum("./oracle-now.json");
|
||||
Console.WriteLine($"oracled checksum: {checksumNow}");
|
||||
|
||||
if (!File.Exists("./oracled.json"))
|
||||
{
|
||||
File.CreateText("./oracled.json");
|
||||
Console.WriteLine("no oracled.json, can't help but assume checksums will be different");
|
||||
Console.WriteLine("no oracled.json, can't help but assume contents will be different");
|
||||
}
|
||||
var checksumOlder = GetChecksum("./oracled.json");
|
||||
Console.WriteLine($"previous oracled checksum: {checksumOlder}");
|
||||
|
||||
if (checksumOlder != checksumNow)
|
||||
if (needUpdate("./oracled.json", "./oracle-now.json"))
|
||||
{
|
||||
Console.WriteLine("looks different, going to have to update.");
|
||||
Console.WriteLine("scryfall requests 50-100 ms between requests...");
|
||||
Console.WriteLine("scryfall requests 50-100 ms between requests. Granted I can't imagine it hasn't been that long.");
|
||||
await Task.Delay(100);
|
||||
Console.WriteLine("should be good.");
|
||||
File.Delete("./oracled.json");
|
||||
@ -71,7 +64,6 @@ namespace creatureBirdDwarf
|
||||
{
|
||||
string responseFromServer = dataStream.ReadToEnd();
|
||||
defaultList = JsonConvert.DeserializeObject<List<Card>>(responseFromServer);
|
||||
stripData(ref defaultList);
|
||||
File.WriteAllText("./default.json", JsonConvert.SerializeObject(defaultList, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter()));
|
||||
}
|
||||
}
|
||||
@ -81,23 +73,35 @@ namespace creatureBirdDwarf
|
||||
File.Delete("./oracle-now.json");
|
||||
}
|
||||
}
|
||||
|
||||
private static void stripData(ref List<Card> collection)
|
||||
public static bool needUpdate(string path1, string path2)
|
||||
{
|
||||
foreach (var card in collection)
|
||||
if(File.Exists(path1) && File.Exists(path2))
|
||||
{
|
||||
card.prices = null;
|
||||
card.edhrec_rank = null;
|
||||
var lhs = JsonConvert.DeserializeObject<List<Card>>(File.ReadAllText(path1));
|
||||
var rhs = JsonConvert.DeserializeObject<List<Card>>(File.ReadAllText(path2));
|
||||
if(lhs == null || rhs == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
foreach(var lhsCard in lhs)
|
||||
{
|
||||
var rhsCard = rhs.FirstOrDefault(c => c.oracle_id == lhsCard.oracle_id);
|
||||
if(rhsCard == null || lhsCard.oracle_text != rhsCard.oracle_text)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
rhs.Remove(rhsCard);
|
||||
}
|
||||
if(rhs.Count() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string GetChecksum(string filePath)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var stream = new BufferedStream(File.OpenRead(filePath), 1200000))
|
||||
{
|
||||
SHA256Managed sha = new SHA256Managed();
|
||||
byte[] checksum = sha.ComputeHash(stream);
|
||||
return BitConverter.ToString(checksum).Replace("-", String.Empty);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user