Market price lookup tool
This commit is contained in:
parent
eea653f5c0
commit
c4781e35d4
@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
14
MarketPriceLookup.Common.Models/Prices.cs
Normal file
14
MarketPriceLookup.Common.Models/Prices.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace MarketPriceLookup
|
||||
{
|
||||
public class Prices
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Price { get; set; }
|
||||
public int Average24hPrice { get; set; }
|
||||
public int Average7DaysPrice { get; set; }
|
||||
//public string Trader { get; set; }
|
||||
//public int BuyPackPrice { get; set; }
|
||||
//public string Currency { get; set; }
|
||||
public string TemplateId { get; set; }
|
||||
}
|
||||
}
|
14
MarketPriceLookup.Common.Models/TraderType.cs
Normal file
14
MarketPriceLookup.Common.Models/TraderType.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace MarketPriceLookup.Common.Models
|
||||
{
|
||||
public enum Trader
|
||||
{
|
||||
Unknown = 0,
|
||||
Prapor = 1,
|
||||
Therapist = 2,
|
||||
Skier = 3,
|
||||
Peacekeeper = 4,
|
||||
Mechanic = 5,
|
||||
Ragman = 6,
|
||||
Jaeger = 7
|
||||
}
|
||||
}
|
248814
MarketPriceLookup.Common/Assets/items.json
Normal file
248814
MarketPriceLookup.Common/Assets/items.json
Normal file
File diff suppressed because it is too large
Load Diff
21
MarketPriceLookup.Common/Helpers/DiskHelpers.cs
Normal file
21
MarketPriceLookup.Common/Helpers/DiskHelpers.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.IO;
|
||||
|
||||
namespace MarketPriceLookup.Helpers
|
||||
{
|
||||
public static class DiskHelpers
|
||||
{
|
||||
public static void CreateDirIfDoesntExist(string path)
|
||||
{
|
||||
if (!Directory.Exists($"{path}"))
|
||||
{
|
||||
//create dump dir
|
||||
Directory.CreateDirectory($"{path}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] GetJsonFiles(string path)
|
||||
{
|
||||
return Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
|
||||
}
|
||||
}
|
||||
}
|
78
MarketPriceLookup.Common/Helpers/ItemTemplateHelper.cs
Normal file
78
MarketPriceLookup.Common/Helpers/ItemTemplateHelper.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using MarketPriceLookup.Common.Helpers;
|
||||
using MarketPriceLookup.Common.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MarketPriceLookup.Common
|
||||
{
|
||||
public static class ItemTemplateHelper
|
||||
{
|
||||
private static Dictionary<string, ItemLookup> _itemCache;
|
||||
|
||||
public static Dictionary<string, ItemLookup> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_itemCache == null)
|
||||
{
|
||||
var itemsFilePath = $"{Directory.GetCurrentDirectory()}\\Assets\\items.json";
|
||||
if (!File.Exists(itemsFilePath))
|
||||
{
|
||||
throw new Exception($"Missing items.json under assets ({itemsFilePath})");
|
||||
}
|
||||
|
||||
var itemsJson = File.ReadAllText(itemsFilePath);
|
||||
_itemCache = JsonSerializer.Deserialize<Dictionary<string, ItemLookup>>(itemsJson);
|
||||
}
|
||||
|
||||
return _itemCache;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemLookup GetTemplateById(string templateId)
|
||||
{
|
||||
if (templateId == "5449016a4bdc2d6f028b456f")
|
||||
{
|
||||
return new ItemLookup
|
||||
{
|
||||
_id = templateId,
|
||||
_name = "Roubles",
|
||||
_parent = "543be5dd4bdc2deb348b4569",
|
||||
_type = ""
|
||||
};
|
||||
}
|
||||
|
||||
if (templateId == "569668774bdc2da2298b4568")
|
||||
{
|
||||
return new ItemLookup
|
||||
{
|
||||
_id = templateId,
|
||||
_name = "Euros",
|
||||
_parent = "543be5dd4bdc2deb348b4569",
|
||||
_type = ""
|
||||
};
|
||||
}
|
||||
|
||||
if (templateId == "5696686a4bdc2da3298b456a")
|
||||
{
|
||||
return new ItemLookup
|
||||
{
|
||||
_id = templateId,
|
||||
_name = "Dollars",
|
||||
_parent = "543be5dd4bdc2deb348b4569",
|
||||
_type = ""
|
||||
};
|
||||
}
|
||||
|
||||
if (Items.ContainsKey(templateId))
|
||||
{
|
||||
return Items[templateId];
|
||||
}
|
||||
|
||||
LoggingHelpers.LogToConsole($"Could not locate item template with id {templateId}", ConsoleColor.Red);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
79
MarketPriceLookup.Common/Helpers/LoggingHelpers.cs
Normal file
79
MarketPriceLookup.Common/Helpers/LoggingHelpers.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System;
|
||||
|
||||
namespace MarketPriceLookup.Common.Helpers
|
||||
{
|
||||
public static class LoggingHelpers
|
||||
{
|
||||
public static string LogTimeTaken(double totalSeconds)
|
||||
{
|
||||
return Math.Round(totalSeconds, 2, MidpointRounding.ToEven).ToString();
|
||||
}
|
||||
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.DarkYellow;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
public static void LogInfo(string message)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Gray;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
public static void LogSuccess(string message)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Green;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
public static void LogToConsole(string message, ConsoleColor backgroundColour = ConsoleColor.Green)
|
||||
{
|
||||
Console.BackgroundColor = backgroundColour;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
private static void ResetConsoleColours()
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
|
||||
public static void LogNewLine()
|
||||
{
|
||||
ResetConsoleColours();
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public static void LogHeading(string message)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Cyan;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
private static void LogMessage(string message)
|
||||
{
|
||||
Console.Write(message);
|
||||
ResetConsoleColours();
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
92
MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs
Normal file
92
MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs
Normal file
@ -0,0 +1,92 @@
|
||||
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]);
|
||||
string bsgId = fields[8];
|
||||
|
||||
if (priceFile.ContainsKey(bsgId))
|
||||
{
|
||||
//oh no
|
||||
var existingItem = priceFile[bsgId];
|
||||
LoggingHelpers.LogToConsole($"Item already exists: {bsgId} {name}. item that already exists: {existingItem.TemplateId} {existingItem.Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
priceFile.Add(bsgId, new Prices
|
||||
{
|
||||
Name = name,
|
||||
Price = price,
|
||||
Average24hPrice = avg24hPrice,
|
||||
Average7DaysPrice = avg7daysPrice,
|
||||
//Trader = trader,
|
||||
//BuyPackPrice = buyBackPrice,
|
||||
//Currency = currency,
|
||||
TemplateId = bsgId
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!priceFile.ContainsKey(key))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return priceFile[key];
|
||||
}
|
||||
|
||||
private static string GetCurrencyType(string input)
|
||||
{
|
||||
return input switch
|
||||
{
|
||||
"₽" => "Rouble",
|
||||
"$" => "Dollar",
|
||||
"€" => "Euro",
|
||||
_ => string.Empty,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
29
MarketPriceLookup.Common/JsonWriter.cs
Normal file
29
MarketPriceLookup.Common/JsonWriter.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using MarketPriceLookup.Helpers;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MarketPriceLookup.Common
|
||||
{
|
||||
public static class JsonWriter
|
||||
{
|
||||
public static void WriteJson<T>(T itemToSerialise, string outputFolderName, string workingPath, string fileName)
|
||||
{
|
||||
var outputPath = $"{workingPath}\\output\\{outputFolderName}";
|
||||
DiskHelpers.CreateDirIfDoesntExist(outputPath);
|
||||
|
||||
Console.WriteLine($"Writing json file to {outputPath}");
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
IgnoreNullValues = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
|
||||
};
|
||||
var json = JsonSerializer.Serialize(itemToSerialise, options);
|
||||
File.WriteAllText($"{outputPath}\\{fileName}.json", json);
|
||||
}
|
||||
}
|
||||
}
|
17
MarketPriceLookup.Common/MarketPriceLookup.Common.csproj
Normal file
17
MarketPriceLookup.Common/MarketPriceLookup.Common.csproj
Normal file
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MarketPriceLookup.Common.Models\MarketPriceLookup.Common.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Assets\items.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
55
MarketPriceLookup.Common/Models/Items.cs
Normal file
55
MarketPriceLookup.Common/Models/Items.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MarketPriceLookup.Common.Models
|
||||
{
|
||||
public class ItemsLibRoot
|
||||
{
|
||||
public List<Dictionary<string, ItemLookup>> items { get; set; }
|
||||
}
|
||||
|
||||
public class ItemLookup
|
||||
{
|
||||
public string _id { get; set; }
|
||||
public string _name { get; set; }
|
||||
public string _parent { get; set; }
|
||||
public string _type { get; set; }
|
||||
public Props _props { get; set; }
|
||||
}
|
||||
|
||||
public class Props
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public string Description { get; set; }
|
||||
public List<Chamber> Chambers { get; set; }
|
||||
public List<Slot> Slots { get; set; }
|
||||
public string defAmmo { get; set; }
|
||||
}
|
||||
|
||||
public class Chamber
|
||||
{
|
||||
public string _name { get; set; }
|
||||
public string _id { get; set; }
|
||||
public string _parent { get; set; }
|
||||
public ChamberProps _props { get; set; }
|
||||
public bool _required { get; set; }
|
||||
public bool _mergeSlotWithChildren { get; set; }
|
||||
public string _proto { get; set; }
|
||||
}
|
||||
|
||||
public class Slot
|
||||
{
|
||||
public string _name { get; set; }
|
||||
public bool _required { get; set; }
|
||||
}
|
||||
|
||||
public class ChamberProps
|
||||
{
|
||||
public List<Filter> filters { get; set; }
|
||||
}
|
||||
|
||||
public class Filter
|
||||
{
|
||||
public List<string> filter { get; set; }
|
||||
}
|
||||
}
|
37
MarketPriceLookup.sln
Normal file
37
MarketPriceLookup.sln
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31624.102
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketPriceLookup", "MarketPriceLookup\MarketPriceLookup.csproj", "{8E1A2B84-F63E-45DE-8C27-710A4E85F897}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketPriceLookup.Common", "MarketPriceLookup.Common\MarketPriceLookup.Common.csproj", "{7A93F68F-ACFE-4950-B3DE-2A9DE2DC47FA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketPriceLookup.Common.Models", "MarketPriceLookup.Common.Models\MarketPriceLookup.Common.Models.csproj", "{4F371EC5-29D2-4B99-823D-0866FC51A030}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8E1A2B84-F63E-45DE-8C27-710A4E85F897}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8E1A2B84-F63E-45DE-8C27-710A4E85F897}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8E1A2B84-F63E-45DE-8C27-710A4E85F897}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8E1A2B84-F63E-45DE-8C27-710A4E85F897}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7A93F68F-ACFE-4950-B3DE-2A9DE2DC47FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7A93F68F-ACFE-4950-B3DE-2A9DE2DC47FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7A93F68F-ACFE-4950-B3DE-2A9DE2DC47FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7A93F68F-ACFE-4950-B3DE-2A9DE2DC47FA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4F371EC5-29D2-4B99-823D-0866FC51A030}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4F371EC5-29D2-4B99-823D-0866FC51A030}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4F371EC5-29D2-4B99-823D-0866FC51A030}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4F371EC5-29D2-4B99-823D-0866FC51A030}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4184D14B-CBBB-4B07-9C36-813474039BB4}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
2362
MarketPriceLookup/Examples/marketPrices.csv
Normal file
2362
MarketPriceLookup/Examples/marketPrices.csv
Normal file
File diff suppressed because it is too large
Load Diff
13
MarketPriceLookup/MarketPriceLookup.csproj
Normal file
13
MarketPriceLookup/MarketPriceLookup.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MarketPriceLookup.Common.Models\MarketPriceLookup.Common.Models.csproj" />
|
||||
<ProjectReference Include="..\MarketPriceLookup.Common\MarketPriceLookup.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
27
MarketPriceLookup/Program.cs
Normal file
27
MarketPriceLookup/Program.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using MarketPriceLookup.Common;
|
||||
using MarketPriceLookup.Common.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace MarketPriceLookup
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// loop over all items and get a price for each
|
||||
var priceList = new Dictionary<string, Prices>();
|
||||
foreach (var item in ItemTemplateHelper.Items)
|
||||
{
|
||||
var priceData = MarketPricesHelper.GetItemPrice(item.Key);
|
||||
if (priceData != null && priceData.Price != 0)
|
||||
{
|
||||
priceList.Add(item.Key, priceData);
|
||||
}
|
||||
}
|
||||
|
||||
// save found prices to json
|
||||
JsonWriter.WriteJson(priceList, "output", Directory.GetCurrentDirectory(), "itemPriceSnapshot");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user