/
build.php
138 lines (100 loc) · 3.19 KB
/
build.php
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
<?php
function switch_village($village)
{
global $server;
if($village == 0) die("village == 0");
$referer = "http://$server/dorf1.php";
$url = "http://$server/dorf1.php?newdid=$village";
// echo $referer . "\n";
echo $url . "\n";
$ch = my_curl_init();
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec ($ch);
curl_close ($ch);
// <a href="?newdid=205861" class="active_vl">
$ret = preg_match('/<a href="\?newdid=([0-9]+)" class="active_vl">/', $result, $matches);
if(!$ret) die("Failed to switch village.");
return $result;
}
// $result is dorf1 page
function get_resource_fields_level($result)
{
global $convert;
$out = array();
// <area href="build.php?id=1" coords="101,33,28" shape="circle" title="‚«‚±‚è ƒŒƒxƒ‹ 4">
if(preg_match_all('#<area href="build.php\?id=([0-9]+)" coords="[0-9]+,[0-9]+,[0-9]+" shape="circle" title="(\S+) \S+ ([0-9]+)">#', $result, $matches, PREG_SET_ORDER)){
foreach($matches as $match){
$type = $convert[$match[2]];
$id = $match[1];
$level = $match[3];
$out[$type][$id] = $level;
}
}else{
die("Can not get resource fields level.");
}
return $out;
}
function get_auto_build_resource_field_id($result)
{
$level_all = get_resource_fields_level($result);
$res = get_res_info($result);
unset($res[4], $res[5], $res[6], $res[7]);
asort($res);
// print_r($res);
foreach($res as $type => $num){
$level_type = $level_all[$type];
asort($level_type);
//print_r($level_type);
list($id, $level) = each($level_type);
if($level >= 10) continue;
return $id;
}
echo "all resource fields level 10.\n";
return false;
}
function build($village, $result)
{
global $server;
// Get a build task
$sql = "select `seq`, `id` from `build` where `village` = $village order by `seq` limit 1";
$res = mysql_query($sql);
if(!$res) die(mysql_error());
$row = mysql_fetch_row($res);
if(!$row){
echo "No build task more. Try to auto build.\n";
$id = get_auto_build_resource_field_id($result);
$seq = -1;
}else{
$seq = $row[0];
$id = $row[1];
}
$referer = "http://$server/dorf" . ( $id <= 18 ? "1" : "2" ) . ".php";
$url = "http://$server/build.php?id=$id";
echo $url . "\n";
$ch = my_curl_init();
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec ($ch);
curl_close ($ch);
// <a href="dorf2.php?a=28&c=23d">
$ret = preg_match('/<a href="(dorf[12]\.php\?a=[0-9]+&c=[0-9a-z]+)">/', $result, $matches);
if(!$ret){
echo "Busy or lacking resources or failed.\n";
return;
}
$url2 = "http://$server/" . $matches[1];
echo $url2 . "\n";
if($seq >= 0){
// Update the target
$sql = "delete from `build` where `seq` = $seq";
if(!mysql_query($sql)) die(mysql_error());
}
$ch = my_curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_REFERER, $url);
$result = curl_exec ($ch);
curl_close ($ch);
//echo $result;
}
?>