108 lines
4.3 KiB
C#
Raw Normal View History

2021-09-19 18:29:16 +01:00
using MarketPriceLookup.Helpers;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace MarketPriceLookup.Common.Helpers
{
public static class MarketPricesHelper
{
private static readonly Dictionary<string, Prices> priceFile = new Dictionary<string, Prices>();
public static Prices GetItemPrice(string key)
{
// parse csv if dictionary is empty
if (priceFile.Count == 0)
{
var workingPath = Directory.GetCurrentDirectory();
var inputPath = $"{workingPath}//input";
var filePath = $"{inputPath}//marketPrices.csv";
DiskHelpers.CreateDirIfDoesntExist(inputPath);
using (TextFieldParser csvParser = new TextFieldParser(filePath))
{
csvParser.CommentTokens = new string[] { "#" };
csvParser.SetDelimiters(new string[] { "," });
csvParser.HasFieldsEnclosedInQuotes = true;
// Skip the row with the column names
csvParser.ReadLine();
csvParser.ReadLine();
while (!csvParser.EndOfData)
{
// Read current line fields, pointer moves to the next line.
string[] fields = csvParser.ReadFields();
//string uid = fields[0];
string name = fields[1];
int price = int.Parse(fields[2]);
int avg24hPrice = int.Parse(fields[3]);
int avg7daysPrice = int.Parse(fields[4]);
//string trader = fields[5];
//int buyBackPrice = int.Parse(fields[6]);
string currency = GetCurrencyType(fields[7]);
2021-09-19 18:29:16 +01:00
string bsgId = fields[8];
int traderPrice = int.Parse(fields[9]);
2021-09-19 18:29:16 +01:00
//if (avg7daysPrice == 0)
//{
// LoggingHelpers.LogError($"unable to add bad item with price average of 0, ignoring: {bsgId} {name}");
// continue;
//}
2021-09-19 18:29:16 +01:00
if (priceFile.ContainsKey(bsgId))
{
//oh no, item already exists in the csv
2021-09-19 18:29:16 +01:00
var existingItem = priceFile[bsgId];
LoggingHelpers.LogError($"Unable to add item: {bsgId} {name}. existing item: {existingItem.TemplateId} {existingItem.Name}");
if (existingItem.Average7DaysPrice != avg7daysPrice)
{
LoggingHelpers.LogError($"Price diff found: already existing item price: {existingItem.Average7DaysPrice} new item price: {avg7daysPrice}");
continue;
}
2021-09-19 18:29:16 +01:00
}
if (!priceFile.ContainsKey(bsgId))
2021-09-19 18:29:16 +01:00
{
priceFile.Add(bsgId, new Prices
{
Name = name,
Price = price,
Average24hPrice = avg24hPrice,
Average7DaysPrice = avg7daysPrice,
//Trader = trader,
//BuyPackPrice = buyBackPrice,
Currency = currency,
TemplateId = bsgId,
TraderPrice = traderPrice,
2021-09-19 18:29:16 +01:00
});
}
LoggingHelpers.LogSuccess($"Adding item: {bsgId} {name}");
2021-09-19 18:29:16 +01:00
}
}
}
if (!priceFile.ContainsKey(key))
{
return null;
}
return priceFile[key];
}
private static string GetCurrencyType(string input)
{
return input switch
{
"₽" => "Rouble",
"$" => "Dollar",
"€" => "Euro",
_ => string.Empty,
};
}
}
}