//Main constructor public Crypto() { //Set the start time to now StartTime = DateTime.Now; //Form the column headers and save them into ColumnTitlesStrings //Lines of strings that are to be printed into the table format are created as a list/array of ColourStrings. //ColourStrings are a struct having attributes for text, text width, colour and left/right alignment. //When printing a line, go through each ColourString in the array and print it in the designated colour, alignment and column width ColumnTitlesStrings = new ColourString[ColumnTitles.Length]; for (int i = 0; i < ColumnTitles.Length; i++) { ColumnTitlesStrings[i] = new ColourString(ColumnTitles[i], ColumnWidths[i], ColumnAligning[i]); } //Initialize Dictionaries and lists CurrentData = new Dictionary <string, JObject>(); PreviousData = new Dictionary <string, JObject>(); PreviousDisplayStrings = new List <ColourString[]>(); //Try gets coin symbols to be specifically requested from the txt file, throw exception if this fails for some reason if (GetCoinsToBeRequestedFromFile()) { //We got the coin names } else { throw new Exception("Failed getting coin names"); } //Try gets API keys to be specifically requested from the txt file, throw exception if this fails for some reason if (GetAPIKeysFromFile()) { //We got the keys, we good } else { //Error getting keys, throw error throw new Exception("Failed getting API Keys"); } //Set credits used to 0 CreditsUsed = 0; }
/// <summary> /// Main display function to print to screen /// </summary> /// <param name="arg">Display arg on what to show, either new data, old data or an error</param> public void Display(DisplayEnum arg) { //Clear the screen and set the default colour Clear(); ForegroundColor = WHITE; //Write some basic information to the top of the console WriteLine("Monitor started at {0}", StartTime.ToLocalTime().ToString()); //Time program was started WriteLine("Current time is {0}, next update at {1} (in {2})", DateTime.Now.ToLocalTime().ToString(), NextAPICallTime.ToLocalTime().ToString(), NextAPICallTime.Subtract(DateTime.Now).ToString("mm\\:ss")); //Current time and time till next update WriteLine("Credits Used: {0} ({1}/HR, {2}/Day)", CreditsUsed, Math.Round(CreditsUsed / DateTime.Now.Subtract(StartTime).TotalHours, 2), Math.Round(CreditsUsed / DateTime.Now.Subtract(StartTime).TotalDays, 2)); //How many credits we've used so far with approximate rates WriteLine(); //Spacing //If the arg was to display error, write it if (arg == DisplayEnum.Error) { WriteLine("Error while grabbing data."); return; } //If the arg was to repeat the last data that was displayed, re-display the cached ColourStrings so no new calculations need to be made else if (arg == DisplayEnum.Repeat) { //Display headers DisplayColourTextLine(ColumnTitlesStrings); //Display each line as they were before foreach (ColourString[] CSArray in PreviousDisplayStrings) { ForegroundColor = WHITE; DisplayColourTextLine(CSArray); } } //We have new data, calcaulate values and colours to be displayed. First check that data is not empty for some reason else if (CurrentData.Count != 0) { //Data is not empty, lets start displaying. First, write a line for the headers at the top of the table DisplayColourTextLine(ColumnTitlesStrings); //Clear the previous cached strings for the table, we will write the new ones so that the values do not need to be calculated every 5 seconds PreviousDisplayStrings.Clear(); //For each coin in our CurrentData dictionary, display a line in the table for the data on this coin foreach (KeyValuePair <int, string> entry in sortDict) { JObject coinData = CurrentData[entry.Value]; //Check if we have previous data on this coin to make comparisons to bool PreviousDataExists = PreviousData.ContainsKey(entry.Value); //Set a temporary colour to be used throughout this line ConsoleColor tempColor = WHITE; //Create a ColourString array to place the generated ColourStrings into ColourString[] CoinColourStrings = new ColourString[10]; //If we specifically requested this coin to be quoted, make it's rank, name and symbol cyan coloured if (CoinsToBeRequested.Contains((string)coinData["symbol"])) { tempColor = CYAN; } //ColourStrings for rank, name and symbol CoinColourStrings[0] = new ColourString((string)coinData["cmc_rank"], ColumnWidths[0], tempColor, true); CoinColourStrings[1] = new ColourString((string)coinData["name"], ColumnWidths[1], tempColor, true); CoinColourStrings[2] = new ColourString((string)coinData["symbol"], ColumnWidths[2], tempColor, true); //Check if price in currencies is higher than previous data and set colour accordingly tempColor = PreviousDataExists ? GetConsoleColorRatio((decimal)coinData["quote"]["BTC"]["price"] / (decimal)PreviousData[entry.Value]["quote"]["BTC"]["price"]) : WHITE; CoinColourStrings[3] = new ColourString((Math.Round((decimal)coinData["quote"]["BTC"]["price"], 9)).ToString(), ColumnWidths[3], tempColor, true); tempColor = PreviousDataExists ? GetConsoleColorRatio((decimal)coinData["quote"]["AUD"]["price"] / (decimal)PreviousData[entry.Value]["quote"]["AUD"]["price"]) : WHITE; CoinColourStrings[4] = new ColourString("$" + (Math.Round((decimal)coinData["quote"]["AUD"]["price"], 2)).ToString(), ColumnWidths[4], tempColor, true); tempColor = PreviousDataExists ? GetConsoleColorRatio((decimal)coinData["quote"]["USD"]["price"] / (decimal)PreviousData[entry.Value]["quote"]["USD"]["price"]) : WHITE; CoinColourStrings[5] = new ColourString("$" + (Math.Round((decimal)coinData["quote"]["USD"]["price"], 2)).ToString(), ColumnWidths[5], tempColor, true); //Check if percent changes over time are positive or negative and set colour accordingly tempColor = GetConsoleColorPercentages((decimal)coinData["quote"]["USD"]["percent_change_1h"]); CoinColourStrings[6] = new ColourString(Math.Round((decimal)coinData["quote"]["USD"]["percent_change_1h"], 2) + "%", ColumnWidths[6], tempColor, false); tempColor = GetConsoleColorPercentages((decimal)coinData["quote"]["USD"]["percent_change_24h"]); CoinColourStrings[7] = new ColourString(Math.Round((decimal)coinData["quote"]["USD"]["percent_change_24h"], 2) + "%", ColumnWidths[7], tempColor, false); tempColor = GetConsoleColorPercentages((decimal)coinData["quote"]["USD"]["percent_change_7d"]); CoinColourStrings[8] = new ColourString(Math.Round((decimal)coinData["quote"]["USD"]["percent_change_7d"], 2) + "%", ColumnWidths[8], tempColor, false); //Check if market cap in USD is higher than previous data and set colour accordingly tempColor = PreviousDataExists ? GetConsoleColorRatio((decimal)coinData["quote"]["USD"]["market_cap"] - (decimal)PreviousData[entry.Value]["quote"]["USD"]["market_cap"]) : WHITE; CoinColourStrings[9] = new ColourString("$" + string.Format("{0:n}", Math.Round((decimal)coinData["quote"]["USD"]["market_cap"], 0)), ColumnWidths[9], tempColor, false); //Cache this line PreviousDisplayStrings.Add(CoinColourStrings); //Display this line DisplayColourTextLine(CoinColourStrings); } } else { //No data WriteLine("No data to be displayed :("); } }