Added Random number range

This commit is contained in:
gardient
2015-09-02 21:33:03 +03:00
parent 633862e0bb
commit 4ce42d18eb
5 changed files with 100 additions and 58 deletions

View File

@@ -11,7 +11,7 @@
<applicationSettings> <applicationSettings>
<RedditRandomNumberGiveawayHelper.Properties.Settings> <RedditRandomNumberGiveawayHelper.Properties.Settings>
<setting name="Version" serializeAs="String"> <setting name="Version" serializeAs="String">
<value>V0.2.0</value> <value>V0.3.0</value>
</setting> </setting>
</RedditRandomNumberGiveawayHelper.Properties.Settings> </RedditRandomNumberGiveawayHelper.Properties.Settings>
</applicationSettings> </applicationSettings>

View File

@@ -37,30 +37,32 @@
this.label3 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.checkForUpdatesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.randomMin = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.randomMax)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.randomMax)).BeginInit();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.randomMin)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// label1 // label1
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(11, 41); this.label1.Location = new System.Drawing.Point(33, 41);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 13); this.label1.Size = new System.Drawing.Size(100, 13);
this.label1.TabIndex = 0; this.label1.TabIndex = 0;
this.label1.Text = "Giveaway Post URI"; this.label1.Text = "Giveaway Post URI";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
// //
// textBox1 // textBox1
// //
this.textBox1.Location = new System.Drawing.Point(117, 38); this.textBox1.Location = new System.Drawing.Point(139, 38);
this.textBox1.Name = "textBox1"; this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(463, 20); this.textBox1.Size = new System.Drawing.Size(441, 20);
this.textBox1.TabIndex = 1; this.textBox1.TabIndex = 1;
// //
// randomMax // randomMax
// //
this.randomMax.Location = new System.Drawing.Point(117, 65); this.randomMax.Location = new System.Drawing.Point(220, 64);
this.randomMax.Maximum = new decimal(new int[] { this.randomMax.Maximum = new decimal(new int[] {
100000, 100000,
0, 0,
@@ -72,7 +74,7 @@
0, 0,
0}); 0});
this.randomMax.Name = "randomMax"; this.randomMax.Name = "randomMax";
this.randomMax.Size = new System.Drawing.Size(115, 20); this.randomMax.Size = new System.Drawing.Size(75, 20);
this.randomMax.TabIndex = 2; this.randomMax.TabIndex = 2;
this.randomMax.Value = new decimal(new int[] { this.randomMax.Value = new decimal(new int[] {
5000, 5000,
@@ -85,15 +87,16 @@
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(11, 67); this.label2.Location = new System.Drawing.Point(11, 67);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13); this.label2.Size = new System.Drawing.Size(122, 13);
this.label2.TabIndex = 3; this.label2.TabIndex = 3;
this.label2.Text = "Max Value"; this.label2.Text = "Random Number Range";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
// //
// button1 // button1
// //
this.button1.Location = new System.Drawing.Point(238, 64); this.button1.Location = new System.Drawing.Point(301, 64);
this.button1.Name = "button1"; this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(342, 23); this.button1.Size = new System.Drawing.Size(279, 23);
this.button1.TabIndex = 4; this.button1.TabIndex = 4;
this.button1.Text = "Get Me A Random Winner"; this.button1.Text = "Get Me A Random Winner";
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
@@ -105,7 +108,8 @@
this.textBox2.Multiline = true; this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2"; this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true; this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(568, 140); this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(568, 171);
this.textBox2.TabIndex = 5; this.textBox2.TabIndex = 5;
// //
// label3 // label3
@@ -129,24 +133,39 @@
// //
// fileToolStripMenuItem // fileToolStripMenuItem
// //
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.checkForUpdatesToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); this.fileToolStripMenuItem.Size = new System.Drawing.Size(115, 20);
this.fileToolStripMenuItem.Text = "File"; this.fileToolStripMenuItem.Text = "Check for updates";
this.fileToolStripMenuItem.Click += new System.EventHandler(this.checkForUpdatesToolStripMenuItem_Click);
// //
// checkForUpdatesToolStripMenuItem // randomMin
// //
this.checkForUpdatesToolStripMenuItem.Name = "checkForUpdatesToolStripMenuItem"; this.randomMin.Location = new System.Drawing.Point(139, 64);
this.checkForUpdatesToolStripMenuItem.Size = new System.Drawing.Size(170, 22); this.randomMin.Maximum = new decimal(new int[] {
this.checkForUpdatesToolStripMenuItem.Text = "Check for updates"; 100000,
this.checkForUpdatesToolStripMenuItem.Click += new System.EventHandler(this.checkForUpdatesToolStripMenuItem_Click); 0,
0,
0});
this.randomMin.Minimum = new decimal(new int[] {
1,
0,
0,
0});
this.randomMin.Name = "randomMin";
this.randomMin.Size = new System.Drawing.Size(75, 20);
this.randomMin.TabIndex = 8;
this.randomMin.Value = new decimal(new int[] {
1,
0,
0,
0});
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(593, 273); this.ClientSize = new System.Drawing.Size(593, 304);
this.Controls.Add(this.randomMin);
this.Controls.Add(this.label3); this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1); this.Controls.Add(this.button1);
@@ -162,6 +181,7 @@
((System.ComponentModel.ISupportInitialize)(this.randomMax)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.randomMax)).EndInit();
this.menuStrip1.ResumeLayout(false); this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout(); this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.randomMin)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -178,7 +198,7 @@
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem checkForUpdatesToolStripMenuItem; private System.Windows.Forms.NumericUpDown randomMin;
} }
} }

View File

@@ -19,17 +19,40 @@ namespace RedditRandomNumberGiveawayHelper
public partial class MainForm : Form public partial class MainForm : Form
{ {
//make sure to format with max number //make sure to format with max number
private static string RANDOM_ORG_URI = "https://www.random.org/integers/?num=1&min=1&max={0}&col=1&base=10&format=plain&rnd=new"; private static string RANDOM_ORG_URI = "https://www.random.org/integers/?num=1&min={0}&max={1}&col=1&base=10&format=plain&rnd=new";
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
} }
class DisplayTextbox
{
private TextBox txtBx;
public DisplayTextbox(TextBox x)
{
txtBx = x;
}
public void Write(params object[] strings)
{
txtBx.Text += string.Join(Environment.NewLine, strings) + Environment.NewLine;
}
public void Clear()
{
txtBx.Clear();
}
}
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
//textBox2.Text += string.Format("{0}", Environment.NewLine); DisplayTextbox displayTextBox = new DisplayTextbox(textBox2);
displayTextBox.Clear();
var reddit = new Reddit(); var reddit = new Reddit();
Post giveawayPost = null; Post giveawayPost = null;
int? randomNumber = null; int? randomNumber = null;
@@ -37,31 +60,26 @@ namespace RedditRandomNumberGiveawayHelper
try try
{ {
textBox2.Text += string.Format("Getting giveaway post...{0}", Environment.NewLine); displayTextBox.Write("Getting giveaway post...");
giveawayPost = reddit.GetPost(new Uri(textBox1.Text)); giveawayPost = reddit.GetPost(new Uri(textBox1.Text));
} }
catch (Exception ex) catch (Exception ex)
{ {
textBox2.Text += string.Format("{1}{0}{2}{0}", displayTextBox.Write("Failed getting giveaway post",
Environment.NewLine,
"Failed getting giveaway post",
"You sure that's the right URI (alsomake sure to get the full uri from the address bar)"); "You sure that's the right URI (alsomake sure to get the full uri from the address bar)");
return; return;
} }
textBox2.Text += string.Format("{1}{2}{0}{3}{4}{0}", displayTextBox.Write("Post title: ",
Environment.NewLine,
"Post title: ",
giveawayPost.Title, giveawayPost.Title,
"Comment count: ", "Comment count: ",
giveawayPost.CommentCount); giveawayPost.CommentCount);
try try
{ {
textBox2.Text += string.Format("{1}{0}", displayTextBox.Write("Getting random number from random.org...");
Environment.NewLine,
"Getting random number from random.org..."); WebRequest randomDotOrgRequest = WebRequest.Create(string.Format(RANDOM_ORG_URI, decimal.Round(randomMin.Value, 0), decimal.Round(randomMax.Value, 0)));
WebRequest randomDotOrgRequest = WebRequest.Create(string.Format(RANDOM_ORG_URI, decimal.Round(randomMax.Value, 0)));
using (WebResponse resp = randomDotOrgRequest.GetResponse()) using (WebResponse resp = randomDotOrgRequest.GetResponse())
{ {
using (StreamReader sr = new StreamReader(resp.GetResponseStream())) using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
@@ -72,16 +90,11 @@ namespace RedditRandomNumberGiveawayHelper
} }
catch (Exception ex) catch (Exception ex)
{ {
textBox2.Text += string.Format("{1}{0}{2}{0}", displayTextBox.Write("Failed getting giveaway post", ex);
Environment.NewLine,
"Failed getting giveaway post",
ex);
return; return;
} }
textBox2.Text += string.Format("{1}{2}{0}{3}{4}{0}", displayTextBox.Write("Random Number: ",
Environment.NewLine,
"Random Number: ",
randomNumber, randomNumber,
"Getting winning comment...", "Getting winning comment...",
"This might take a while..."); "This might take a while...");
@@ -96,21 +109,25 @@ namespace RedditRandomNumberGiveawayHelper
foreach (var x in nums) foreach (var x in nums)
{ {
if ((x.Value == randomNumber + i && randomNumber + i < decimal.Round(randomMax.Value, 0)) if ((x.Value == randomNumber + i && randomNumber + i < decimal.Round(randomMax.Value, 0))
|| (x.Value == randomNumber - i && randomNumber - i > 0)) || (x.Value == randomNumber - i && randomNumber - i > decimal.Round(randomMin.Value, 0)))
{ {
winningNumKey = x.Key; winningNumKey = x.Key;
winningNumVal = x.Value; winningNumVal = x.Value;
diff = i; diff = i;
break; break;
} }
//stop if we're out of range
if (randomNumber + i > decimal.Round(randomMax.Value, 0) && randomNumber - i < decimal.Round(randomMin.Value, 0))
break;
} }
} }
if (!string.IsNullOrEmpty(winningNumKey) && winningNumVal.HasValue)
{
Comment winningComment = giveawayPost.Comments.FirstOrDefault(w => w.Shortlink == winningNumKey); Comment winningComment = giveawayPost.Comments.FirstOrDefault(w => w.Shortlink == winningNumKey);
textBox2.Text += string.Format("{1}{2}{0}{3}{4}{0}{5}{6}{0}{7}{8}{0}", displayTextBox.Write("Winning comment (link): ",
Environment.NewLine,
"Winning comment (link): ",
winningNumKey, winningNumKey,
"Winning comment (body): ", "Winning comment (body): ",
winningComment.Body, winningComment.Body,
@@ -120,6 +137,11 @@ namespace RedditRandomNumberGiveawayHelper
diff diff
); );
} }
else
{
displayTextBox.Write("What the heck?? not one post in the range? you sure you have that range right?");
}
}
private async void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) private async void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{ {

View File

@@ -25,7 +25,7 @@ namespace RedditRandomNumberGiveawayHelper.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("V0.2.0")] [global::System.Configuration.DefaultSettingValueAttribute("V0.3.0")]
public string Version { public string Version {
get { get {
return ((string)(this["Version"])); return ((string)(this["Version"]));

View File

@@ -3,7 +3,7 @@
<Profiles /> <Profiles />
<Settings> <Settings>
<Setting Name="Version" Type="System.String" Scope="Application"> <Setting Name="Version" Type="System.String" Scope="Application">
<Value Profile="(Default)">V0.2.0</Value> <Value Profile="(Default)">V0.3.0</Value>
</Setting> </Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>