summaryrefslogtreecommitdiff
path: root/Analizator9000/Analizator9000/DealerParser.cs
blob: f638aa3b8182a280161d26db6fc6ae09c31778e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Analizator9000
{
    class DealerParser
    {
        public String condition = "";
        public long generate = -1;
        public long produce = -1;
        public Dictionary<String, String[]> predeal = new Dictionary<String, String[]>();

        public void loadFile(String file)
        {
            String[] contents = File.ReadAllLines(file);
            Dictionary<String, int> keyLines = new Dictionary<String, int>();
            String[] keywords = {"predeal", "condition", "generate", "produce", "action"};
            for (int i = 0; i < contents.Length; i++)
            {
                String line = contents[i];
                int lineNo = Array.IndexOf(keywords, line.Trim());
                if (lineNo >= 0)
                {
                    keyLines.Add(keywords[lineNo], i);
                }
            }
            keyLines = keyLines.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            for (int i = 0; i < keyLines.Count; i++)
            {
                KeyValuePair<String, int> keyline = keyLines.ElementAt(i);
                String[] section = new String[contents.Length - keyline.Value];
                if (i + 1 < keyLines.Count)
                {
                    Array.Copy(contents, keyline.Value, section, 0, keyLines.ElementAt(i + 1).Value - keyline.Value);
                }
                else
                {
                    Array.Copy(contents, keyline.Value, section, 0, contents.Length - keyline.Value);
                }
                section[0] = Regex.Replace(section[0], "^"+keyline.Key, "").Trim();
                section = section.Where(str => str != null && str.Trim().Length > 0).ToArray();
                switch (keyline.Key)
                {
                    case "predeal":
                        String[] players = { "north", "east", "south", "west" };
                        char[] suits = { 'S', 'H', 'D', 'C' };
                        foreach (String l in section)
                        {
                            String line = l.Trim();
                            int player = Array.IndexOf(players, line.Substring(0, line.IndexOf(' ')));
                            if (player >= 0) {
                                String[] chunks = Regex.Replace(line, "^" + players[player], "").Split(',');
                                String[] hand = new String[4];
                                foreach (String chunk in chunks)
                                {
                                    int suit = Array.IndexOf(suits, chunk.Trim().ToUpper()[0]);
                                    if (suit >= 0)
                                    {
                                        hand[suit] = chunk.Trim().Substring(1);
                                    }
                                }
                                this.predeal.Add(players[player], hand);
                            }
                        }
                        break;
                    case "condition":
                        this.condition = section.Aggregate((a, b) => a + "\n" + b);
                        break;
                    case "generate":
                        if (section.Length > 1)
                        {
                            throw new Exception("Zbyt duża liczba wartości 'generate' w skrypcie");
                        }
                        if (section.Length == 1)
                        {
                            try
                            {
                                this.generate = Convert.ToInt64(section[0]);
                            }
                            catch (OverflowException)
                            {
                                throw new Exception("Za duża wartość 'generate'");
                            }
                        }
                        break;
                    case "produce":
                        if (section.Length > 1)
                        {
                            throw new Exception("Zbyt duża liczba linii 'produce' w skrypcie");
                        }
                        if (section.Length == 1)
                        {
                            try
                            {
                                this.produce = Convert.ToInt64(section[0]);
                            }
                            catch (OverflowException)
                            {
                                throw new Exception("Za duża wartość 'produce'");
                            }
                        }
                        break;
                }
            }
        }
        public String saveFile()
        {
            String filename = "an9k-" + DateTime.Now.ToString("yyyyMMddHHmmssFFF") + ".dealer";
            StreamWriter file = new StreamWriter("files\\" + filename);
            String predealStr = "";
            String suitLetters = "SHDC";
            foreach (KeyValuePair<String, String[]> pre in this.predeal)
            {
                List<String> hand = new List<String>();
                for (int i = 0; i < pre.Value.Length; i++)
                {
                    if (pre.Value[i].Trim().Length > 0)
                    {
                        hand.Add(suitLetters[i] + pre.Value[i].Trim());
                    }
                }
                if (hand.Count > 0)
                {
                    predealStr += pre.Key + " " + hand.Aggregate((a, b) => a + ", " + b) + "\n";
                }
            }
            if (predealStr.Length > 0)
            {
                file.WriteLine("predeal");
                file.Write(predealStr);
            }
            if (this.condition.Trim().Length > 0)
            {
                file.WriteLine("condition");
                file.WriteLine(this.condition);
            }
            file.WriteLine("generate");
            file.WriteLine(this.generate);
            file.WriteLine("produce");
            file.WriteLine(this.produce);
            file.WriteLine("action");
            file.WriteLine("printoneline");
            file.Close();
            return filename;
        }
    }
}