using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace Ikariam
{
public partial class Form1 : Form
{
int waitFrame = 150000;//it means 2 min 30 sec for wait next pirate attack.
int count = 0, earned;
bool pirateStarted = false;
bool captchaEntered = false;
bool sessionError = false;
public static CookieContainer cookies;//this is important about HTTP REQUESTS
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
string gold, wood, wine, marble,glass, sulfur, ship, shipMax, actionPoints, citizens, population, cityId, actionRequest;
string locale_response;//HTML code after pirate attack done.
public Form1()
{
InitializeComponent();
cookies = new CookieContainer();
pirateLoop();
// This loop starts when application starts. Go to definition to look this method.
}
public static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
return request;
}
public async static Task<HttpWebResponse> MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
{
HttpWebResponse response;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
if (parameters != null)
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string s = "";
foreach (KeyValuePair<string, string> pair in parameters)
{
if (s.Length == 0)
{
s = s + string.Format("{0}={1}", pair.Key, pair.Value);
}
else
{
s = s + string.Format("&{0}={1}", pair.Key, pair.Value);
}
}
byte[] bytes = Encoding.UTF8.GetBytes(s);
using (Stream stream = await request.GetRequestStreamAsync())
{
stream.Write(bytes, 0, bytes.Length);
}
}
request.Method = "GET";
response = await request.GetResponseAsync() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);
while (response.StatusCode == HttpStatusCode.Found)
{
response.Close();
request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
response = await request.GetResponseAsync() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);
}
return response;
}
void fetchData(string html)
{
doc.LoadHtml(html);
gold = doc.DocumentNode.Descendants("a").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_gold").First().InnerText;
wood = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_wood").First().InnerText;
wine = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_wine").First().InnerText;
marble = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_marble").First().InnerText;
glass = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_crystal").First().InnerText;
sulfur = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_sulfur").First().InnerText;
ship = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_freeTransporters").First().InnerText;
shipMax = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_maxTransporters").First().InnerText;
actionPoints = doc.DocumentNode.Descendants("li").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_maxActionPoints").First().InnerText;
citizens = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_citizens").First().InnerText;
population = doc.DocumentNode.Descendants("span").Where(s => s.GetAttributeValue("id", "") == "js_GlobalMenu_population").First().InnerText;
cityId = Regex.Match(html, @"cityId=\s*(.+?)\s*""").Groups[1].Value;
actionRequest = doc.DocumentNode.Descendants("input").Where(s => s.GetAttributeValue("id", "") == "js_ChangeCityActionRequest").First().Attributes["value"].Value;
label8.Text = gold;
label7.Text = ship + " / " + shipMax;
label1.Text = wood;
label2.Text = wine;
label3.Text = marble;
label5.Text = glass;
label4.Text = sulfur;
label6.Text = citizens + " / " + population;
}//Fetch Datas from HTML page with HtmlAgilityPack.
async Task<string> login(string server, string id, string pw)
{
if (!server.Contains("http://"))
{
server = "http://" + server;
}
HttpWebRequest newRequest = GetNewRequest(server + "/index.php?action=loginAvatar&function=login", cookies);
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
dictionary2.Add("name", id);
dictionary2.Add("password", pw);
dictionary2.Add("uni_url", server);
dictionary2.Add("startPageShown", "1");
dictionary2.Add("detectedDevice", "1");
Dictionary<string, string> parameters = dictionary2;
HttpWebResponse gecici = await MakeRequest(newRequest, cookies, parameters);
using (StreamReader reader = new StreamReader(gecici.GetResponseStream()))
{
if (!reader.EndOfStream)
{
return reader.ReadToEnd();
}
}
return "";
}//Returns html codes from successfully or fail login.
async Task<string> pirate_Attack(string server)
{
HttpWebRequest newRequest = GetNewRequest(server + "/index.php?action=PiracyScreen&function=capture&buildingLevel=1&view=pirateFortress&cityId="+cityId+"&position=17&backgroundView=city¤tCityId="+cityId+"&templateView=pirateFortress¤tTab=tabBootyQuest&actionRequest="+actionRequest+"&ajax=1", cookies);
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
Dictionary<string, string> parameters = dictionary2;
HttpWebResponse gecici = await MakeRequest(newRequest, cookies, parameters);
using (StreamReader reader = new StreamReader(gecici.GetResponseStream()))
{
if (!reader.EndOfStream)
{
return reader.ReadToEnd();
}
}
return "";
}//Starts to pirate bot
async Task<string> pirate_Attack_captcha(string captcha, string server)
{
HttpWebRequest newRequest = GetNewRequest(server + "/index.php?action=PiracyScreen&function=capture&buildingLevel=1&captcha=" + captcha + "&view=pirateFortress&cityId=" + cityId + "&position=17&backgroundView=city¤tCityId=" + cityId + "&templateView=pirateFortress¤tTab=tabBootyQuest&actionRequest=" + actionRequest + "&ajax=1", cookies);
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
Dictionary<string, string> parameters = dictionary2;
HttpWebResponse gecici = await MakeRequest(newRequest, cookies, parameters);
using (StreamReader reader = new StreamReader(gecici.GetResponseStream()))
{
if (!reader.EndOfStream)
{
return reader.ReadToEnd();
}
}
return "";
}//If Captcha needed, this method is for pirate bot
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://www.elitepvpers.com/forum/members/3828962-naworia.html");
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://adf.ly/Xb4Nq");//Just view this ad to support me about finance.
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
pirateStarted = true ;
button2.Text = "Loading";
button2.Enabled = false;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
}
async void pirateLoop()
{
while (true)
{
try
{
if (pirateStarted && !captchaEntered)
{
fetchData(await login(textBox1.Text, textBox2.Text, textBox3.Text));
button2.Text = "Started";
locale_response = await pirate_Attack(textBox1.Text);
if (locale_response.Contains("captchaNeeded=1"))
{
button4.Enabled = true;
textBox4.Enabled = true;
listBox1.Items.Add("Captcha Needed");
actionRequest = Regex.Match(locale_response, @"actionRequest"":""\s*(.+?)\s*""").Groups[1].Value;
pirateStarted = false;
string id = Regex.Match(locale_response, @"&rand=\s*(.+?)\s*\\").Groups[1].Value;
string url = textBox1.Text + "/index.php?action=Options&function=createCaptcha&rand=" + id;
HttpWebRequest newRequest = GetNewRequest(url, cookies);
HttpWebResponse gecici = await MakeRequest(newRequest, cookies);
pictureBox9.Image = Image.FromStream(gecici.GetResponseStream());
MessageBox.Show("Enter captcha");
}
else if (locale_response.Contains("error"))
{
listBox1.Items.Add("Error about session");
waitFrame = 3000;
sessionError = true;
}
else
{
waitFrame = 155000;
}
if (pirateStarted)
{
captchaEntered = false;
if (!sessionError)
{
progressing();
}
await Task.Delay(waitFrame);
if (!sessionError)
{
count++;
earned += 40;
listBox1.Items.Add("Earned 40 Gold");
}
linkLabel3.Text = "Completed : " + count;
linkLabel4.Text = "Earned : " + earned;
sessionError = false;
}
}
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message + "\nCheck your credentials.");
button2.Text = "Start";
button2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
pirateStarted = false;
}
await Task.Delay(2000);
}
}//most important to check about this application
private async void button3_Click(object sender, EventArgs e)
{
fetchData(await login(textBox1.Text, textBox2.Text, textBox3.Text));
}
private async void button4_Click(object sender, EventArgs e)
{
try
{
locale_response = await pirate_Attack_captcha(textBox4.Text, textBox1.Text);
if (locale_response.Contains("captchaNeeded=1"))
{
actionRequest = Regex.Match(locale_response, @"actionRequest"":""\s*(.+?)\s*""").Groups[1].Value;
MessageBox.Show("You entered captcha wrong. Please enter true digits in captcha image");
}
else if(locale_response.Contains("captchaNeeded=0"))
{
actionRequest = Regex.Match(locale_response, @"actionRequest"":""\s*(.+?)\s*""").Groups[1].Value;
listBox1.Items.Add("Captcha entered");
waitFrame = 155000;
button4.Enabled = false;
textBox4.Enabled = false;
textBox4.Text = "";
pirateStarted = true;
pictureBox9.Image = Ikariam.Properties.Resources.captc;
}
}
catch(Exception Ex)
{ MessageBox.Show(Ex.Message); }
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show(actionRequest);
}
async void progressing()
{
progressBar1.Value = 0;
for (int i = 0; i < 155; i++)
{
await Task.Delay(1000);
progressBar1.Value++;
this.Text = "Ikariam Pirate Bot v1 - " + Math.Round(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100.00, 1) + "%" ;
}
this.Text = "Ikariam Pirate Bot v1";
}//about timing with progressBar1
}
}