}         //end write all jpegs

        /* Write JSON to .stack File  */
        public bool writeJSONFile(int m1, int m5, int m25, int m100, int m250, String tag)
        {
            bool jsonExported = true;
            int  totalSaved   = m1 + (m5 * 5) + (m25 * 25) + (m100 * 100) + (m250 * 250);
            // Track the total coins
            int coinCount = m1 + m5 + m25 + m100 + m250;

            String[] coinsToDelete    = new String[coinCount];
            String[] bankedFileNames  = new DirectoryInfo(this.fileUtils.bankFolder).GetFiles().Select(o => o.Name).ToArray();//Get all names in bank folder
            String[] frackedFileNames = new DirectoryInfo(this.fileUtils.frackedFolder).GetFiles().Select(o => o.Name).ToArray();;
            String[] partialFileNames = new DirectoryInfo(this.fileUtils.partialFolder).GetFiles().Select(o => o.Name).ToArray();
            // Add the two arrays together
            var list = new List <String>();

            list.AddRange(bankedFileNames);
            list.AddRange(frackedFileNames);
            list.AddRange(partialFileNames);

            // Program will spend fracked files like perfect files
            bankedFileNames = list.ToArray();


            // Check to see the denomination by looking at the file start
            int c = 0;
            // c= counter
            String json = "{" + Environment.NewLine;

            json = json + "\t\"cloudcoin\": " + Environment.NewLine;
            json = json + "\t[" + Environment.NewLine;
            String bankFileName;
            String frackedFileName;
            String partialFileName;
            string denomination;

            // Put all the JSON together and add header and footer
            for (int i = 0; (i < bankedFileNames.Length); i++)
            {
                denomination    = bankedFileNames[i].Split('.')[0];
                bankFileName    = this.fileUtils.bankFolder + bankedFileNames[i];    //File name in bank folder
                frackedFileName = this.fileUtils.frackedFolder + bankedFileNames[i]; //File name in fracked folder
                partialFileName = this.fileUtils.partialFolder + bankedFileNames[i];
                if (denomination == "1" && m1 > 0)
                {
                    if (c != 0)//This is the json seperator between each coin. It is not needed on the first coin
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName)) // Is it a bank file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m1--;
                    // Get the clean JSON of the coin
                }// end if coin is a 1

                if (denomination == "5" && m5 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m5--;
                } // end if coin is a 5

                if (denomination == "25" && m25 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m25--;
                }// end if coin is a 25

                if (denomination == "100" && m100 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m100--;
                } // end if coin is a 100

                if (denomination == "250" && m250 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileUtils.loadOneCloudCoinFromJsonFile(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileUtils.loadOneCloudCoinFromJsonFile(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileUtils.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m250--;
                }// end if coin is a 250

                if (m1 == 0 && m5 == 0 && m25 == 0 && m100 == 0 && m250 == 0)
                {
                    break;
                } // Break if all the coins have been called for.
            }     // end for each coin needed

            /*WRITE JSON TO FILE*/
            json  = json + "\t] " + Environment.NewLine;
            json += "}";
            String filename = (this.fileUtils.exportFolder + Path.DirectorySeparatorChar + totalSaved + ".CloudCoins." + tag + ".stack");

            if (File.Exists(filename))
            {
                // tack on a random number if a file already exists with the same tag
                Random rnd     = new Random();
                int    tagrand = rnd.Next(999);
                filename = (this.fileUtils.exportFolder + Path.DirectorySeparatorChar + totalSaved + ".CloudCoins." + tag + tagrand + ".stack");
            }//end if file exists

            File.WriteAllText(filename, json);
            Console.Out.WriteLine("Writing to : ");
            CoreLogger.Log("Writing to : " + filename);
            Console.Out.WriteLine(filename);
            /*DELETE FILES THAT HAVE BEEN EXPORTED*/
            for (int cc = 0; cc < coinsToDelete.Length; cc++)
            {
                // Console.Out.WriteLine("Deleting " + coinsToDelete[cc]);
                if (coinsToDelete[cc] != null)
                {
                    File.Delete(coinsToDelete[cc]);
                }
            }//end for all coins to delete

            // end if write was good
            return(jsonExported);
        }//end write json to file
Beispiel #2
0
        }     //end fix one

        /* PUBLIC METHODS */
        public int[] fixAll(int millisecondsToFixOne)
        {
            int[]     results          = new int[3];
            String[]  frackedFileNames = new DirectoryInfo(this.fileUtils.frackedFolder).GetFiles().Select(o => o.Name).ToArray();
            CloudCoin frackedCC;

            //CoinUtils cu = new CoinUtils(frackedCC);
            if (frackedFileNames.Length < 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Out.WriteLine("You have no fracked coins.");
                CoreLogger.Log("You have no fracked coins.");
                Console.ForegroundColor = ConsoleColor.White;
            }//no coins to unfrack


            for (int i = 0; i < frackedFileNames.Length; i++)
            {
                Console.WriteLine("UnFracking coin " + (i + 1) + " of " + frackedFileNames.Length);
                CoreLogger.Log("UnFracking coin " + (i + 1) + " of " + frackedFileNames.Length);
                try
                {
                    frackedCC = fileUtils.loadOneCloudCoinFromJsonFile(this.fileUtils.frackedFolder + frackedFileNames[i]);
                    CoinUtils cu    = new CoinUtils(frackedCC);
                    String    value = frackedCC.pown;
                    //  Console.WriteLine("Fracked Coin: ");
                    cu.consoleReport();

                    CoinUtils fixedCC = fixCoin(frackedCC, millisecondsToFixOne); // Will attempt to unfrack the coin.

                    cu.consoleReport();
                    switch (fixedCC.getFolder().ToLower())
                    {
                    case "bank":
                        this.totalValueToBank++;
                        this.fileUtils.overWrite(this.fileUtils.bankFolder, fixedCC.cc);
                        this.deleteCoin(this.fileUtils.frackedFolder + frackedFileNames[i]);
                        Console.WriteLine("CloudCoin was moved to Bank.");
                        CoreLogger.Log("CloudCoin was moved to Bank.");
                        break;

                    case "counterfeit":
                        this.totalValueToCounterfeit++;
                        this.fileUtils.overWrite(this.fileUtils.counterfeitFolder, fixedCC.cc);
                        this.deleteCoin(this.fileUtils.frackedFolder + frackedFileNames[i]);
                        Console.WriteLine("CloudCoin was moved to Trash.");
                        CoreLogger.Log("CloudCoin was moved to Trash.");
                        break;

                    default:    //Move back to fracked folder
                        this.totalValueToFractured++;
                        this.deleteCoin(this.fileUtils.frackedFolder + frackedFileNames[i]);
                        this.fileUtils.overWrite(this.fileUtils.frackedFolder, fixedCC.cc);
                        Console.WriteLine("CloudCoin was moved back to Fraked folder.");
                        CoreLogger.Log("CloudCoin was moved back to Fraked folder.");
                        break;
                    }
                    // end switch on the place the coin will go
                    Console.WriteLine("...................................");
                    Console.WriteLine("");
                }
                catch (FileNotFoundException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex);
                    CoreLogger.Log(ex.ToString());
                    Console.ForegroundColor = ConsoleColor.White;
                }
                catch (IOException ioex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ioex);
                    CoreLogger.Log(ioex.ToString());
                    Console.ForegroundColor = ConsoleColor.White;
                } // end try catch
            }     // end for each file name that is fracked

            results[0] = this.totalValueToBank;
            results[1] = this.totalValueToCounterfeit; // System.out.println("Counterfeit and Moved to trash: "+totalValueToCounterfeit);
            results[2] = this.totalValueToFractured;   // System.out.println("Fracked and Moved to Fracked: "+ totalValueToFractured);
            return(results);
        }// end fix all