· 7 years ago · Jan 25, 2019, 02:02 AM
1// ==UserScript==
2// @name Ressource Forecast
3// @version 0.1
4// @author dh
5// @match *.die-staemme.de/game.php?village=*
6// ==/UserScript==
7
8var menuIsExtendend = false;
9var position;
10
11(function() {
12 'use strict';
13
14 //FORECAST INPUT VARIABLES
15
16 /////////////////////////////////////////
17 // PUT IN DEFAULT AMOUNT FOR FORECAST //
18 var amount = 10; //
19 /////////////////////////////////////////
20 // PUT IN DEFAULT INTERVAL IN SECONDS //
21 var interval = 30*60; //
22 /////////////////////////////////////////
23
24 //CURRENT VALUES OF VILLAGE
25 var wood;
26 var stone;
27 var iron;
28 var max_storage;
29 var wood_prod;
30 var stone_prod;
31 var iron_prod;
32
33 //SEARCH INPUT VARIABLES
34 var wood_lookup;
35 var stone_lookup;
36 var iron_lookup;
37 var time_lookup;
38
39 //CHECK IF RESSOURCE BAR EXISTS
40 var ressource_bar = checkForToolbar(); //returns ressource toolbar
41
42
43 if(ressource_bar){ //TODO check if bar exists
44
45 position = $(ressource_bar).offset();
46 position.inherited_width = $(ressource_bar).width();
47 position.inherited_height = $(ressource_bar).height() - 8;
48 window.onresize = function(event) {
49 position = $(ressource_bar).offset();
50 draw();
51 };
52 draw();
53
54 console.log(position);
55
56 //TODO get ressource values via jquery
57 //TODO get html spacing info
58
59 //create button for extending table
60 createIcon();
61
62 //TODO create event handler for clicking on this button
63 // -> toggle hide of table
64 //TODO create <div> with table
65 //TODO create menu buttons event handler
66 // -> lookup (dedects current field type/maybe refresh in realtime)
67 // -> lookup-clear
68 // -> forecast (inputs amount and interval)
69 // -> forecast-refresh
70 }else{
71 //could not locate ressource toolbar
72 console.log("##### FORECAST: unable to dock onto toolbar #####");
73 }
74
75})();
76
77
78//////////////////////////////////FUNCTIONS///////////////////////////
79
80function createForecastTable(amount, interval_s){ //gets executed on opening of tab, or manual refresh
81 var table = {};
82
83 for(var i = 1; i <= amount; i++){ //start at 1
84 table[i] = calcValuesFromOffset(interval_s*i); //calc lines for each offset
85 }
86
87 //get line entries for each time a ressource hits max
88 table["H"] = lookup("wood", max_storage);
89 table["L"] = lookup("stone", max_storage);
90 table["E"] = lookup("iron", max_storage);
91
92 table.sort(function (a, b) { //sort in theese maximum lines
93 return b.time - a.time;
94 });
95
96 return table;
97}
98
99
100function lookup(fieldtype, value){ //gets executed when manual lookup is used
101 var lookup_value = value;
102
103 //check input range
104 if ((fieldtype == "wood" || fieldtype == "stone" || fieldtype == "iron") && (value > max_storage)){
105 lookup_value = max_storage;
106 }else if ((fieldtype == "wood" || fieldtype == "stone" || fieldtype == "iron") && (value < 0)){
107 lookup_value = 0;
108 }
109
110 var diff_time = 0; //time difference until desired value is reached
111
112 //calc time
113 switch(true){
114 case fieldtype == "time":
115 diff_time = value; //TODO time reformating calculation
116 break;
117
118 case fieldtype == "wood":
119 var wood_diff = value - wood;
120 if (wood_diff < 0){
121 break;
122 }
123 diff_time = (wood_diff*3600)/wood_prod;
124 break;
125
126 case fieldtype == "stone":
127 var stone_diff = value - stone;
128 if (stone_diff < 0){
129 break;
130 }
131 diff_time = (stone_diff*3600)/stone_prod;
132 break;
133
134 case fieldtype == "iron":
135 var iron_diff = value - iron;
136 if (iron_diff < 0){
137 break;
138 }
139 diff_time = (iron_diff*3600)/iron_prod;
140 break;
141 }
142
143 diff_time = diff_time.toFixed(0); //throw away possible float number
144
145 return calcValuesFromOffset(diff_time);
146}
147
148
149function calcValuesFromOffset(offset_s){ //calculates value set of certain time offset
150 var set = {};
151 set.wood = wood + ((wood_prod*offset_s)/60);
152 set.stone = stone + ((stone_prod*offset_s)/60);
153 set.iron = iron + ((iron_prod*offset_s)/60);
154
155 var curr_time = new Date();
156 curr_time = curr_time/1000;
157
158 set.time = curr_time + offset_s;
159
160 return set; //returns one line set of values
161}
162
163function createIcon(){
164 //convert image to bytecode https://dopiaza.org/tools/datauri/index.php <--- PROPS AN DIESE SEITE
165 var icon_id = "forecast_mainbutton";
166 var icon_image = "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAB3RJTUUH4wEYEx4aw4pOwAAAAZVJREFUOMuN081LVWEQBvCf96MgLIsorgS1qIUkbiQLImgj0UIoDIp2bd3UPxC0aZO0VbcR0cIWLYKoIEgEF30ZfkCLEKHECAtK8NpVs80ceLseb/eBwznMmXlm5pkZtsdmzrN7O+dSA6IPqAZBC9qwHv924hi+4KdwyMNeVHAYu7CKBSxiCT14gUe4iW95JFfwNALStn7hFQYiwWjY72NHSlDGbayEwwj6cRp9uIsa1nAP7RgMn0JGUsCNIHiDI0Fcr+c+PA+/oajkH3k6sYzJyJThZAQNJLZi6FPDhbSSEq6iFbdC0Ax/4r2e2DZwPQgvY09GVMYlTOC15rCAh+jFgYyoiA7M42uTRMuYwsHoRCER63dOQDFZwHpUE3mUQodqTKQc483wNpZzJWeClfiuSbI9wyd0NdlaBeN4j0NZWWt4gKO42OBsUpzFGTypP4/9GIubuvYfkvP4jo8xpC04gbkgG46M2Q21oBt38CPurq9RtuN4Gdu8hFm8w0ysxiamcaoZIYs4F7p9jgoX8TgONG8V/AXyyGHjsPkFdgAAAABJRU5ErkJggg==)";
167 var icon = '<td align="right" class="topAlign"><table id="forecast_icon_background1" class="header-border menu_block_right" style="border-collapse: collapse;"><tbody><tr><td><table id="forecast_icon_background2" class="box" cellspacing="0"><tbody><tr><td id="forecast_icon_background3" class="box-item icon-box firstcell" style="white-space:nowrap; text-align:center;"><a href="#" id="'+icon_id+'" title="Forecast" style="vertical-align:middle;"><span class="icon header" style="background-image:'+icon_image+';margin:0;"></span></a></td></tr></tbody></table></td></tr><tr class="newStyleOnly"><td class="shadow"><div class="leftshadow"> </div><div class="rightshadow"> </div></td></tr></tbody></table></td>';
168 $('#header_info > tbody > tr > td:nth-child(4)').before(icon); //TODO - get proper position
169 //$(icon_id).css("background-color", "yellow");
170
171 $('#ds_body').prepend('<div id="forecast_container" style="visibility:hidden; position:absolute;marginLeft: 0; marginTop: 0;z-index: 9999;border: solid; border-width: 1px;border-top-style: none;"></div>'); //create container for actual content
172
173 $('#'+ icon_id).click(function(){ toggleButton(); return false; });
174}
175
176function toggleButton(){
177 menuIsExtendend = !menuIsExtendend;
178 draw();
179}
180
181function checkForToolbar(){
182 var temp; //temp variable containing html element that is getting checked
183
184 if ($('#wood').length == 0){ //ressource bar not existing
185 return false;
186 }
187
188 if ($('#wood').parentsUntil(".topAlign").is('html')){ //unable to find toolbar
189 return false;
190 }
191
192 $('#wood').parentsUntil(".topAlign").css("margin-left", -1); //anpassung ressourcenbar
193 return $('#wood').parentsUntil(".topAlign").parent();
194
195}
196
197
198
199function draw(){
200
201 if(!menuIsExtendend){
202 $("#forecast_container").css("visibility", "hidden");
203
204 $("#forecast_icon_background1").css("background", "transparent url(https://dsde.innogamescdn.com/asset/76fbc28978/graphic/index/statusbar-center.png) scroll left top repeat-x");
205 $("#forecast_icon_background2").css("background", "transparent url(https://dsde.innogamescdn.com/asset/76fbc28978/graphic/index/statusbar-right.png) scroll right top no-repeat");
206 $("#forecast_icon_background3").css("background", "transparent url(https://dsde.innogamescdn.com/asset/76fbc28978/graphic/index/statusbar-left.png) scroll left top no-repeat");
207
208 $("#forecast_icon_background3").css("border-top-style", "none");
209 $("#forecast_icon_background3").css("border-left-style", "none");
210 $("#forecast_icon_background3").css("padding-left", 3);
211 }
212 if(menuIsExtendend){
213
214
215 $("#forecast_icon_background1").css("background-color", "white");
216
217
218 $("#forecast_icon_background2").css("background-color", "white");
219
220
221 $("#forecast_icon_background3").css("background-image", "none");
222 $("#forecast_icon_background3").css("background-color", "white");
223 $("#forecast_icon_background3").css("border-width", 1);
224 $("#forecast_icon_background3").css("background-size:", 15);
225 $("#forecast_icon_background3").css("border-top-style", "solid");
226 $("#forecast_icon_background3").css("border-left-style", "solid");
227 $("#forecast_icon_background3").css("padding-left", 2);
228
229 var offset_top = position.top + position.inherited_height;
230 var offset_left = position.left - 24;
231 var width = position.inherited_width + 22;
232
233 $("#forecast_container").css("width", width);
234 $("#forecast_container").css("top", offset_top);
235 $("#forecast_container").css("left", offset_left);
236 $("#forecast_container").css("height", 400);
237 $("#forecast_container").css("background-color", "white");
238
239 $("#forecast_container").css("visibility", "visible");
240 }
241}