diff options
author | emkael <emkael@tlen.pl> | 2012-11-14 22:44:39 +0000 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2012-11-14 22:44:39 +0000 |
commit | bf0f197187b2d4403d4e80da5d36048fab3834b3 (patch) | |
tree | d07e7bc75b7ddf653416ffbc74d08d1101a529ae /Analizator9000 | |
parent | 258f973cf4e3f46f6983c2d6a1218f87b8fa3d0d (diff) |
* deal generating and writing to file
git-svn-id: https://svn.emkael.info/an9k@6 05ec0d5d-773b-4d93-9e23-c81a7ac79feb
Diffstat (limited to 'Analizator9000')
-rw-r--r-- | Analizator9000/Analizator9000/Analizator9000.csproj | 1 | ||||
-rw-r--r-- | Analizator9000/Analizator9000/DealerParser.cs | 4 | ||||
-rw-r--r-- | Analizator9000/Analizator9000/DealerWrapper.cs | 106 | ||||
-rw-r--r-- | Analizator9000/Analizator9000/Form1.Designer.cs | 156 | ||||
-rw-r--r-- | Analizator9000/Analizator9000/Form1.cs | 79 | ||||
-rw-r--r-- | Analizator9000/Analizator9000/Form1.resx | 2 |
6 files changed, 267 insertions, 81 deletions
diff --git a/Analizator9000/Analizator9000/Analizator9000.csproj b/Analizator9000/Analizator9000/Analizator9000.csproj index a54c6a5..aca4d4e 100644 --- a/Analizator9000/Analizator9000/Analizator9000.csproj +++ b/Analizator9000/Analizator9000/Analizator9000.csproj @@ -65,6 +65,7 @@ </ItemGroup>
<ItemGroup>
<Compile Include="DealerParser.cs" />
+ <Compile Include="DealerWrapper.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
diff --git a/Analizator9000/Analizator9000/DealerParser.cs b/Analizator9000/Analizator9000/DealerParser.cs index ee3ea17..f638aa3 100644 --- a/Analizator9000/Analizator9000/DealerParser.cs +++ b/Analizator9000/Analizator9000/DealerParser.cs @@ -111,7 +111,7 @@ namespace Analizator9000 public String saveFile()
{
String filename = "an9k-" + DateTime.Now.ToString("yyyyMMddHHmmssFFF") + ".dealer";
- StreamWriter file = new StreamWriter("files\\"+filename);
+ StreamWriter file = new StreamWriter("files\\" + filename);
String predealStr = "";
String suitLetters = "SHDC";
foreach (KeyValuePair<String, String[]> pre in this.predeal)
@@ -137,7 +137,7 @@ namespace Analizator9000 if (this.condition.Trim().Length > 0)
{
file.WriteLine("condition");
- file.Write(this.condition);
+ file.WriteLine(this.condition);
}
file.WriteLine("generate");
file.WriteLine(this.generate);
diff --git a/Analizator9000/Analizator9000/DealerWrapper.cs b/Analizator9000/Analizator9000/DealerWrapper.cs new file mode 100644 index 0000000..b17c9ab --- /dev/null +++ b/Analizator9000/Analizator9000/DealerWrapper.cs @@ -0,0 +1,106 @@ +using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Forms;
+using System.Diagnostics;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace Analizator9000
+{
+ class DealerWrapper
+ {
+ private Form1 debugForm;
+ private String scriptname;
+ private StreamWriter outputFile;
+ private bool running = false;
+ private long produce = 0;
+ private long lineCount = 0;
+
+ public DealerWrapper(String scriptname, Form1 debugForm, long produce)
+ {
+ if (!File.Exists(scriptname))
+ {
+ throw new Exception("Nie znaleziono pliku: " + scriptname);
+ }
+ if (produce < 1)
+ {
+ throw new Exception("Nieprawidłowa liczba rozdań do wyprodukowania");
+ }
+ this.scriptname = scriptname;
+ this.debugForm = debugForm;
+ this.produce = produce;
+ }
+
+ private void debugWriteLine(String line) {
+ this.debugForm.addStatusLine(line);
+ }
+
+ private bool handleData(string data)
+ {
+ if (data != null)
+ {
+ String[] dataLines = data.Split('\n');
+ foreach (String line in dataLines)
+ {
+ Match lineMatch = Regex.Match(line.Trim(), "^n\\s*(\\S*)\\s*e\\s*(\\S*)\\s*s\\s*(\\S*)\\s*w\\s*(\\S*)$");
+ if (lineMatch.Success)
+ {
+ this.outputFile.WriteLine(lineMatch.Result("${1} ${2} ${3} ${4}"));
+ this.lineCount++;
+ }
+ }
+ int progress = ((int)(Convert.ToDouble(this.lineCount) / Convert.ToDouble(this.produce) * 100));
+ this.debugForm.setProgress(progress);
+ this.debugWriteLine(data);
+ return true;
+ }
+ return false;
+ }
+
+ public void closeFile()
+ {
+ this.outputFile.Close();
+ }
+
+ private delegate void onEndDelegate(String filename);
+ public void run(Action<String> onEnd)
+ {
+ if (!this.running)
+ {
+ this.running = true;
+ this.lineCount = 0;
+ String filename = "an9k-" + DateTime.Now.ToString("yyyyMMddHHmmssFFF") + ".deals";
+ this.outputFile = new StreamWriter("files\\"+filename);
+ ProcessStartInfo pInfo = new ProcessStartInfo();
+ pInfo.FileName = "bin\\dealer.exe";
+ pInfo.WindowStyle = ProcessWindowStyle.Hidden;
+ pInfo.CreateNoWindow = true;
+ pInfo.Arguments = "\"" + this.scriptname + "\"";
+ pInfo.UseShellExecute = false;
+ pInfo.RedirectStandardOutput = true;
+ pInfo.RedirectStandardError = true;
+ this.debugWriteLine(pInfo.FileName + " " + pInfo.Arguments);
+ Process dealerProc = new Process();
+ dealerProc.StartInfo = pInfo;
+ dealerProc.OutputDataReceived += (sender, output) =>
+ {
+ if (output != null)
+ {
+ if (!this.handleData(output.Data))
+ {
+ this.closeFile();
+ (new onEndDelegate(onEnd)).Invoke(filename);
+ }
+ }
+ };
+ dealerProc.ErrorDataReceived += (sender, error) => { if (error != null) { this.debugWriteLine(error.Data); } };
+ dealerProc.Start();
+ dealerProc.BeginErrorReadLine();
+ dealerProc.BeginOutputReadLine();
+ }
+ }
+
+ }
+}
diff --git a/Analizator9000/Analizator9000/Form1.Designer.cs b/Analizator9000/Analizator9000/Form1.Designer.cs index 5c086d5..3b8c459 100644 --- a/Analizator9000/Analizator9000/Form1.Designer.cs +++ b/Analizator9000/Analizator9000/Form1.Designer.cs @@ -29,7 +29,7 @@ private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
- this.groupBox1 = new System.Windows.Forms.GroupBox();
+ this.generateGroup = new System.Windows.Forms.GroupBox();
this.conditionBox = new System.Windows.Forms.RichTextBox();
this.generateButton = new System.Windows.Forms.Button();
this.produceBox = new System.Windows.Forms.TextBox();
@@ -67,42 +67,42 @@ this.generateFileNameTextBox = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
- this.comboBox2 = new System.Windows.Forms.ComboBox();
- this.comboBox1 = new System.Windows.Forms.ComboBox();
+ this.contractList = new System.Windows.Forms.ComboBox();
+ this.declarerList = new System.Windows.Forms.ComboBox();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
- this.textBox21 = new System.Windows.Forms.TextBox();
+ this.analyzeFileNameTexBox = new System.Windows.Forms.TextBox();
this.generateFileDialog = new System.Windows.Forms.OpenFileDialog();
- this.progressBar1 = new System.Windows.Forms.ProgressBar();
- this.listBox1 = new System.Windows.Forms.ListBox();
- this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
+ this.progressBar = new System.Windows.Forms.ProgressBar();
+ this.statusListBox = new System.Windows.Forms.ListBox();
+ this.analyzeFileDialog = new System.Windows.Forms.OpenFileDialog();
this.textBox22 = new System.Windows.Forms.TextBox();
- this.groupBox1.SuspendLayout();
+ this.generateGroup.SuspendLayout();
this.tableLayoutPanel1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.conditionBox);
- this.groupBox1.Controls.Add(this.generateButton);
- this.groupBox1.Controls.Add(this.produceBox);
- this.groupBox1.Controls.Add(this.label12);
- this.groupBox1.Controls.Add(this.generateBox);
- this.groupBox1.Controls.Add(this.label11);
- this.groupBox1.Controls.Add(this.label10);
- this.groupBox1.Controls.Add(this.tableLayoutPanel1);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.button1);
- this.groupBox1.Controls.Add(this.generateFileNameTextBox);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(513, 444);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "Generowanie";
+ // generateGroup
+ //
+ this.generateGroup.Controls.Add(this.conditionBox);
+ this.generateGroup.Controls.Add(this.generateButton);
+ this.generateGroup.Controls.Add(this.produceBox);
+ this.generateGroup.Controls.Add(this.label12);
+ this.generateGroup.Controls.Add(this.generateBox);
+ this.generateGroup.Controls.Add(this.label11);
+ this.generateGroup.Controls.Add(this.label10);
+ this.generateGroup.Controls.Add(this.tableLayoutPanel1);
+ this.generateGroup.Controls.Add(this.label1);
+ this.generateGroup.Controls.Add(this.button1);
+ this.generateGroup.Controls.Add(this.generateFileNameTextBox);
+ this.generateGroup.Location = new System.Drawing.Point(12, 12);
+ this.generateGroup.Name = "generateGroup";
+ this.generateGroup.Size = new System.Drawing.Size(513, 444);
+ this.generateGroup.TabIndex = 0;
+ this.generateGroup.TabStop = false;
+ this.generateGroup.Text = "Generowanie";
//
// conditionBox
//
@@ -499,13 +499,13 @@ // groupBox2
//
this.groupBox2.Controls.Add(this.button4);
- this.groupBox2.Controls.Add(this.comboBox2);
- this.groupBox2.Controls.Add(this.comboBox1);
+ this.groupBox2.Controls.Add(this.contractList);
+ this.groupBox2.Controls.Add(this.declarerList);
this.groupBox2.Controls.Add(this.label15);
this.groupBox2.Controls.Add(this.label14);
this.groupBox2.Controls.Add(this.label13);
this.groupBox2.Controls.Add(this.button3);
- this.groupBox2.Controls.Add(this.textBox21);
+ this.groupBox2.Controls.Add(this.analyzeFileNameTexBox);
this.groupBox2.Location = new System.Drawing.Point(531, 12);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(513, 179);
@@ -522,36 +522,36 @@ this.button4.Text = "ANALIZUJ";
this.button4.UseVisualStyleBackColor = true;
//
- // comboBox2
+ // contractList
//
- this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this.comboBox2.FormattingEnabled = true;
- this.comboBox2.Items.AddRange(new object[] {
+ this.contractList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
+ this.contractList.FormattingEnabled = true;
+ this.contractList.Items.AddRange(new object[] {
"Wszystkie",
"Bez atu",
"Piki",
"Kiery",
"Kara",
"Trefle"});
- this.comboBox2.Location = new System.Drawing.Point(89, 101);
- this.comboBox2.Name = "comboBox2";
- this.comboBox2.Size = new System.Drawing.Size(121, 21);
- this.comboBox2.TabIndex = 6;
+ this.contractList.Location = new System.Drawing.Point(89, 101);
+ this.contractList.Name = "contractList";
+ this.contractList.Size = new System.Drawing.Size(121, 21);
+ this.contractList.TabIndex = 6;
//
- // comboBox1
+ // declarerList
//
- this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
- this.comboBox1.FormattingEnabled = true;
- this.comboBox1.Items.AddRange(new object[] {
+ this.declarerList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
+ this.declarerList.FormattingEnabled = true;
+ this.declarerList.Items.AddRange(new object[] {
"Wszyscy",
"N",
"E",
"S",
"W"});
- this.comboBox1.Location = new System.Drawing.Point(89, 73);
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.Size = new System.Drawing.Size(121, 21);
- this.comboBox1.TabIndex = 5;
+ this.declarerList.Location = new System.Drawing.Point(89, 73);
+ this.declarerList.Name = "declarerList";
+ this.declarerList.Size = new System.Drawing.Size(121, 21);
+ this.declarerList.TabIndex = 5;
//
// label15
//
@@ -589,36 +589,37 @@ this.button3.Text = "Wybierz plik";
this.button3.UseVisualStyleBackColor = true;
//
- // textBox21
+ // analyzeFileNameTexBox
//
- this.textBox21.Location = new System.Drawing.Point(7, 20);
- this.textBox21.Name = "textBox21";
- this.textBox21.ReadOnly = true;
- this.textBox21.Size = new System.Drawing.Size(419, 20);
- this.textBox21.TabIndex = 0;
+ this.analyzeFileNameTexBox.Location = new System.Drawing.Point(7, 20);
+ this.analyzeFileNameTexBox.Name = "analyzeFileNameTexBox";
+ this.analyzeFileNameTexBox.ReadOnly = true;
+ this.analyzeFileNameTexBox.Size = new System.Drawing.Size(419, 20);
+ this.analyzeFileNameTexBox.TabIndex = 0;
//
// generateFileDialog
//
this.generateFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
- // progressBar1
+ // progressBar
//
- this.progressBar1.Location = new System.Drawing.Point(531, 312);
- this.progressBar1.Name = "progressBar1";
- this.progressBar1.Size = new System.Drawing.Size(513, 23);
- this.progressBar1.TabIndex = 2;
+ this.progressBar.Location = new System.Drawing.Point(531, 312);
+ this.progressBar.Name = "progressBar";
+ this.progressBar.Size = new System.Drawing.Size(513, 23);
+ this.progressBar.TabIndex = 2;
//
- // listBox1
+ // statusListBox
//
- this.listBox1.FormattingEnabled = true;
- this.listBox1.Location = new System.Drawing.Point(531, 343);
- this.listBox1.Name = "listBox1";
- this.listBox1.Size = new System.Drawing.Size(513, 108);
- this.listBox1.TabIndex = 3;
+ this.statusListBox.FormattingEnabled = true;
+ this.statusListBox.Location = new System.Drawing.Point(531, 343);
+ this.statusListBox.Name = "statusListBox";
+ this.statusListBox.Size = new System.Drawing.Size(513, 108);
+ this.statusListBox.TabIndex = 3;
//
- // openFileDialog2
+ // analyzeFileDialog
//
- this.openFileDialog2.FileName = "openFileDialog2";
+ this.analyzeFileDialog.FileName = "openFileDialog2";
+ this.analyzeFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.analyzeFileDialog_FileOk);
//
// textBox22
//
@@ -634,18 +635,19 @@ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1052, 463);
this.Controls.Add(this.textBox22);
- this.Controls.Add(this.listBox1);
- this.Controls.Add(this.progressBar1);
+ this.Controls.Add(this.statusListBox);
+ this.Controls.Add(this.progressBar);
this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
+ this.Controls.Add(this.generateGroup);
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Analizator9000";
- this.groupBox1.ResumeLayout(false);
- this.groupBox1.PerformLayout();
+ this.Load += new System.EventHandler(this.Form1_Load);
+ this.generateGroup.ResumeLayout(false);
+ this.generateGroup.PerformLayout();
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.groupBox2.ResumeLayout(false);
@@ -657,7 +659,7 @@ #endregion
- private System.Windows.Forms.GroupBox groupBox1;
+ private System.Windows.Forms.GroupBox generateGroup;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.OpenFileDialog generateFileDialog;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
@@ -694,16 +696,16 @@ private System.Windows.Forms.Label label12;
private System.Windows.Forms.TextBox generateBox;
private System.Windows.Forms.Label label11;
- private System.Windows.Forms.ProgressBar progressBar1;
- private System.Windows.Forms.ListBox listBox1;
- private System.Windows.Forms.ComboBox comboBox2;
- private System.Windows.Forms.ComboBox comboBox1;
+ private System.Windows.Forms.ProgressBar progressBar;
+ private System.Windows.Forms.ListBox statusListBox;
+ private System.Windows.Forms.ComboBox contractList;
+ private System.Windows.Forms.ComboBox declarerList;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Button button3;
- private System.Windows.Forms.TextBox textBox21;
- private System.Windows.Forms.OpenFileDialog openFileDialog2;
+ private System.Windows.Forms.TextBox analyzeFileNameTexBox;
+ private System.Windows.Forms.OpenFileDialog analyzeFileDialog;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox22;
private System.Windows.Forms.RichTextBox conditionBox;
diff --git a/Analizator9000/Analizator9000/Form1.cs b/Analizator9000/Analizator9000/Form1.cs index 7c090e2..f277e4a 100644 --- a/Analizator9000/Analizator9000/Form1.cs +++ b/Analizator9000/Analizator9000/Form1.cs @@ -6,6 +6,7 @@ using System.Drawing; using System.Linq;
using System.Text;
using System.Windows.Forms;
+using System.IO;
namespace Analizator9000
{
@@ -71,6 +72,8 @@ namespace Analizator9000 private void generateButton_Click(object sender, EventArgs e)
{
+ generateGroup.Enabled = false;
+ progressBar.Value = 0;
try
{
this.parser.generate = Convert.ToInt64(generateBox.Text);
@@ -92,7 +95,81 @@ namespace Analizator9000 {
MessageBox.Show("Błąd wprowadzania danych: " + ex.Message, "Błąd wprowadzania danych", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
- String outputFileName = parser.saveFile();
+ try
+ {
+ String outputFileName = parser.saveFile();
+ DealerWrapper dealer = new DealerWrapper("files/" + outputFileName, this, this.parser.produce);
+ dealer.run(this.generateEnd);
+ }
+ catch (FileNotFoundException)
+ {
+ MessageBox.Show("Nie można utworzyć pliku. Sprawdź, czy w katalogu programu istnieje katalog 'files'", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Błąd generatora: " + ex.Message, "Błąd generatora", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
}
+
+ private delegate void endDelegate(String filename);
+ private void generateEnd(String filename)
+ {
+ if (this.InvokeRequired)
+ {
+ this.Invoke(new endDelegate(generateEnd), new object[] { filename });
+ }
+ else
+ {
+ progressBar.Value = 100;
+ if (filename != null)
+ {
+ this.addStatusLine("Zapisano do pliku: " + filename);
+ }
+ analyzeFileNameTexBox.Text = Path.GetFullPath(filename);
+ generateGroup.Enabled = true;
+ }
+ }
+
+ private delegate void AddStatusDelegate(String line);
+ public void addStatusLine(String line)
+ {
+ if (line != null)
+ {
+ if (statusListBox.InvokeRequired)
+ {
+ this.Invoke(new AddStatusDelegate(addStatusLine), new object[] { line });
+ }
+ else
+ {
+ statusListBox.Items.Add(line);
+ statusListBox.SelectedIndex = statusListBox.Items.Count - 1;
+ }
+ }
+ }
+
+ private delegate void SetProgressDelegate(int progress);
+ public void setProgress(int progress)
+ {
+ if (progressBar.InvokeRequired)
+ {
+ this.Invoke(new SetProgressDelegate(setProgress), new object[]{ progress });
+ }
+ else
+ {
+ progressBar.Value = progress;
+ }
+ }
+
+ private void Form1_Load(object sender, EventArgs e)
+ {
+ declarerList.SelectedIndex = 0;
+ contractList.SelectedIndex = 0;
+ }
+
+ private void analyzeFileDialog_FileOk(object sender, CancelEventArgs e)
+ {
+
+ }
+
}
}
diff --git a/Analizator9000/Analizator9000/Form1.resx b/Analizator9000/Analizator9000/Form1.resx index a06519b..b153991 100644 --- a/Analizator9000/Analizator9000/Form1.resx +++ b/Analizator9000/Analizator9000/Form1.resx @@ -120,7 +120,7 @@ <metadata name="generateFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
- <metadata name="openFileDialog2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+ <metadata name="analyzeFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>171, 17</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|