Example #1
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: hand2xml site inFile outFile [-buckets n]");
                return;
            }

            int buckets = 1;
            #region Parse optional parameters
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i][0] != '-')
                    continue;
                switch (args[i])
                {
                    case "-buckets":
                    case "-b": buckets = int.Parse(args[++i]);
                        break;
                    default: Console.WriteLine("Unknown parameter: {0}", args[i]);
                        return;
                }
            }
            #endregion

            PluginServices host = PluginServices.Instance;
            host.PluginDirectory = AppDomain.CurrentDomain.BaseDirectory;
            host.AddPlugin("HandParserInterface.IHandParser");
            host.FindPlugins();
            AvailablePlugins parsers = host.Plugins["HandParserInterface.IHandParser"];
            IHandParser parser = null;
            foreach (AvailablePlugin plugin in parsers)
            {
                IHandParser curParser = (IHandParser)plugin.Instance;
                if (curParser.Switch == args[0])
                {
                    parser = curParser;
                    break;
                }
            }

            #region Validate parameters
            if (parser == null)
            {
                Console.WriteLine("Unknown parser type: {0}", args[0]);
                return;
            }

            if (!File.Exists(args[1]) && !Directory.Exists(args[1]))
            {
                Console.WriteLine("Unknown file or directory: {0}", args[1]);
                return;
            }

            if (buckets < 1)
            {
                Console.WriteLine("Invalid bucket count. Must have at least one bucket.");
                return;
            }
            #endregion

            PokerHandXML result;

            // get the file attributes for file or directory
            FileAttributes attr = File.GetAttributes(args[1]);

            //detect whether its a directory or file
            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            {
                List<PokerHand> hands = new List<PokerHand>();
                foreach (string filename in Directory.GetFiles(args[1]))
                    hands.AddRange(parser.ParseFile(filename).Hands);
                result = new PokerHandXML() { Hands = hands.ToArray() };
            }
            else
                result = parser.ParseFile(args[1]);

            string outfile = args[2];

            int handsPerFile = result.Hands.Length / buckets;
            for (int i = 0; i < buckets; i++)
            {
                int start = handsPerFile * i;
                int end = start + handsPerFile;
                if (i == (buckets - 1))
                    end = result.Hands.Length;
                var subset = new PokerHandXML() { Hands = result.Hands.Where((h,idx) => idx >= start && idx < end).ToArray() };
                SaveHands(subset, outfile + i + ".xml");
            }
        }
Example #2
0
        public PokerHandXML Parse(TextReader file)
        {
            List<PokerHand> hands = new List<PokerHand>();

            string line;
            bool inHand = false;
            StringBuilder handText = new StringBuilder();
            List<string> lines = new List<string>();
            Console.WriteLine("Starting parse");
            while ((line = file.ReadLine()) != null)
            {
                //skip blank lines
                if (!inHand && line.Length == 0)
                    continue;

                if (inHand && line.Length == 0)
                {
                    Console.WriteLine("Found hand");
                    PokerHand hand = parseHand(lines, handText.ToString());
                    if (hand != null)
                        hands.Add(hand);
                    inHand = false;
                    continue;
                }

                if (!inHand)
                {
                    lines.Clear();
                    handText = new StringBuilder();
                    inHand = true;
                }

                handText.AppendLine(line);
                lines.Add(line);
            }

            if (inHand)
            {
                Console.WriteLine("Found hand");
                PokerHand hand = parseHand(lines, handText.ToString());
                if (hand != null)
                    hands.Add(hand);
            }

            PokerHandXML result = new PokerHandXML();
            result.Hands = hands.ToArray();
            return result;
        }
Example #3
0
 private static void SaveHands(PokerHandXML result, string outfile)
 {
     TextWriter output = new StreamWriter(outfile);
     XmlSerializer serializer = new XmlSerializer(typeof(PokerHandXML));
     serializer.Serialize(output, result);
     output.Flush();
     output.Close();
 }