// ==UserScript==
// @name forest_fant
// @namespace http://tampermonkey.net/
// @version 1
// @description try to take over the world!
// @author You
// @match http://newforest.apeha.ru/gates.html*
// @grant GM_addStyle
// @require http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==
var alreadyStarted = false;
function initMod(){
if (top.frames["d_act"].global_data != undefined && top.frames["d_act"].global_data.my_group != undefined) {
if(localStorage.getItem("Start_script") == "true") {
startScript();
}
setTimeout(createControls, 800);
} else {
setTimeout(initMod, 200);
}
}
var reloadId = 0;
setTimeout(initMod, 1000);
var isStarted = false;
var isReallyStarted = false;
var forest_frame = top.frames["d_act"];
var count = 0;
var lastTimeStamp = new Date();
var currentState = "Nothing_forward"
var hitCount = 0;
var currentStoneId = 0;
var timeoutId = 0;
var possibleCopperIdList = [];
var ignoredItems = [];
var timeoutIds = [];
var intervalId = 0;
var canvIntervalId = 0;
function startBtnClicked() {
loadLocalStorage();
log.v("startBtnClicked");
isReallyStarted = true;
startLoop()
startGraph()
}
function startGraph(){
canvIntervalId = setInterval(function() {clearDots(); startCanv()}, 2000);
}
function stopBtnClicked() {
log.v("stopBtnClicked");
clearInterval(intervalId);
do {
clearTimeout(timeoutIds.pop())
} while (timeoutIds.length != 0)
isReallyStarted = false;
clearInterval(canvIntervalId);
clearDots();
isStarted = false
rewriteLocalStorage();
}
function startLoop() {
if(!isStarted){
isStarted = true;
intervalId = setInterval(looper, getRandom(1000, 2000));
} else {
log.v("Already Started")
}
}
function stopLoop() {
if(isStarted) {
clearInterval(intervalId);
do {
clearTimeout(timeoutIds.pop())
} while (timeoutIds.length != 0)
isStarted = false
} else {
log.v("Not started yet")
}
}
function looper() {
if(!isReallyStarted) return;
log.v("START #" + count)
var currTime = new Date()
log.i(currTime - lastTimeStamp + " since last time stamp")
lastTimeStamp = currTime;
var timerCountDown = getSecondsLeft();
var overlayResponse = getResponseIfExists();
setOverlayOff();
log.v("timerCountDown = " + timerCountDown )
if(timerCountDown == "-1") { // Timer is off
log.i("response = " + overlayResponse + ", currentState = " + currentState);
if(overlayResponse == "Вы должны иметь в руках Кирка рудокопа") {
stopLoop();
var promise = new Promise(function (resolve) {
jQuery.get('http://newforest.apeha.ru/bag_type_17.chtml', function (response) {
var re = new RegExp('<img width="75" height="50" src="http:\/\/resources\.apeha\.ru\/upload\/1_963\.gif" title="Кирка рудокопа">.+\\n.+\\n.+\\n<form method=post><input type=hidden name=actUser-Wear value=(\\d+)>', 'gm');
var match = re.exec(response);
if (match && match[1]) {
var itemId = +match[1];
setTimeout(function () {
jQuery.post('http://newforest.apeha.ru/bag_type_17.chtml', { 'actUser-Wear': itemId }).then(function () {
resolve();
});
}, 1000);
}
})
})
promise.then(function() {startLoop()})
} else {
if(overlayResponse == "Вы травмированы. Для работы необходимо вылечить травмы.") {
stopLoop();
var promise = new Promise(function (resolve) {
jQuery.post('http://newforest.apeha.ru/ability_type_common.chtml', { 'actUser-UseCast': '10246747' }).then(function () {
resolve();
});
})
promise.then(function() {startLoop()})
} else {
switch (currentState) {
case ("Copper_forward") : {
hitCount = 0;
if(getLeftForwardAndRight()[1].type == "copper" ||
overlayResponse != "Перед Вами нечего добывать.") {
clickStartDig();
removeFromPossibleLists(currentStoneId, "copper");
} else {
currentState = "Nothing_forward"
stopLoop();
timeoutIds.push( setTimeout(startLoop, getRandom(400, 500)) );
}
break;
}
case ("Nothing_forward"): {
log.v("response = " + overlayResponse);
switch (overlayResponse) {
case ("медь в радиусе 5 шагов от Вас"):
log.i("Copper in 5-cell radius")
increaseCurrentRadiusStones()
addToPossibleListItems(getLeftForwardAndRight()[1], "copper");
stopLoop();
hitCount = 0;
var waitFor = goToTheNearestStone(possibleListItemsMostType(), true)
log.v("Waiting for = " + waitFor)
if(Number.isInteger(waitFor)) {
timeoutIds.push( setTimeout(startLoop, waitFor * 1000 + getRandom(700, 1200)) );
}
break;
case ("Перед Вами нечего добывать."):
log.i("NOTHING TO DIG, GO TO ANOTHER PLACE")
hitCount = 0;
currentStoneId = 0;
stopLoop();
log.i("currentStoneId cleared");
var waitFor = goToTheNearestStone(possibleListItemsMostType(), isThere5Possible())
log.v("Waiting for = " + waitFor)
if(Number.isInteger(waitFor)) {
timeoutIds.push( setTimeout(startLoop, waitFor * 1000 + getRandom(700, 1200)) );
}
break;
case ("Ничего не найдено"):
increaseCurrentRadiusStones()
case ("Not overlayed"):
log.v("hitCount = " + hitCount)
if(hitCount < numberOfSearches() && (isThere5Possible() || getIgnoredItemById(currentStoneId).perc < 100)) {
log.v("SEARCH")
var direction = fixDirection(currentStoneId);
log.i("direction = " + direction)
switch (direction) {
case ("good") : {
clickSearch();
break;
}
case ("turn_left") : {
stopLoop();
CheckKeyDown({keyCode: 37})
timeoutIds.push( setTimeout(startLoop, getRandom(500, 1000)) );
break;
}
case ("turn_right") : {
stopLoop();
CheckKeyDown({keyCode: 39})
timeoutIds.push( setTimeout(startLoop, getRandom(500, 1000)) );
break;
}
case ("need_to_go") : {
stopLoop();
var waitFor = goToTheNearestStone(possibleListItemsMostType(), isThere5Possible())
log.v("Waiting for = " + waitFor)
if(Number.isInteger(waitFor)) {
timeoutIds.push( setTimeout(startLoop, waitFor * 1000 + getRandom(700, 1200)) );
}
break;
}
case ("try_to_turn") : {
stopLoop();
CheckKeyDown({keyCode: 39})
timeoutIds.push( setTimeout(startLoop, getRandom(500, 1000)) );
break;
}
}
} else {
log.i("GO TO ANOTHER PLACE")
hitCount = 0;
currentStoneId = 0;
stopLoop();
log.i("currentStoneId cleared");
var waitFor = goToTheNearestStone(possibleListItemsMostType(), isThere5Possible())
log.v("Waiting for = " + waitFor)
if(Number.isInteger(waitFor)) {
timeoutIds.push( setTimeout(startLoop, waitFor * 1000 + getRandom(700, 1200)) );
}
}
break;
case ("медь прямо перед Вами"): {
log.e("Copper forward")
removeFromPossibleLists(currentStoneId, "copper")
clickStartDig();
hitCount = 0
currentState = "Copper_forward"
break;
}
case ("медь слева от Вас"): {
log.e("Copper on the left, turned and start")
CheckKeyDown({keyCode: 37}) //TurnLeft
removeFromPossibleLists(getLeftForwardAndRight[0], "copper")
clickStartDig();
hitCount = 0
currentState = "Copper_forward"
break;
}
case ("медь справа от Вас"): {
log.e("Copper on the right, turned and start")
CheckKeyDown({keyCode: 39}) //TurnRight
removeFromPossibleLists(getLeftForwardAndRight[2], "copper")
clickStartDig();
hitCount = 0
currentState = "Copper_forward"
break;
}
default: {
log.e("UNEXPECTED BEHAVIOR")
stopLoop()
}
}
}
}
}
}
} else { // Timer is on
stopLoop();
if(Number.isInteger(timerCountDown)) {
//Checks also for Вы неожиданно быстро управились
timeoutIds.push( setTimeout(startLoop,
(timerCountDown > 150 ? (timerCountDown - 150) : timerCountDown) * 1000 + getRandom(700, 1100)) );
}
}
log.v("FINISH #" + count)
count++;
}
var searchCopper = true;
var copperProb = {forward : 20, side : 10, radius : 10}
function numberOfSearches() {
var probability = 0;
if(getLeftForwardAndRight()[1].type == "copper"){
probability = copperProb.forward;
}
if(probability == 0) probability = 20;
log.v("probability = " + Math.ceil(100 / probability))
return Math.ceil(100 / probability);
}
function whatShouldISearch() {
if(!searchCopper) return "undefined";
if(searchCopper) return "copper";
}
function isThere5Possible(){
return possibleCopperIdList.length > 0;
}
function set100PercIfExists(id) {
var tempforwardId = getLeftForwardAndRight()[1];
if(tempforwardId.id = id) {
var tempIgnoredItem = getIgnoredItemById(id);
if(tempIgnoredItem.perc < 100) {
tempIgnoredItem.perc = 100
addOrReplaceIgnoredItem(tempIgnoredItem)
}
}
}
function increaseCurrentRadiusStones() {
var copperAround = getAllItemsInRadius(5, "copper");
var leftForwardRight = getLeftForwardAndRight();
for(var i = 0; i < copperAround.length; i++) {
var ignoredItem = getIgnoredItemById(copperAround[i])
switch(copperAround[i]) {
case leftForwardRight[0].id:
if(leftForwardRight[0].type == "copper") {
ignoredItem.perc += copperProb.side;
}
break;
case leftForwardRight[2].id:
if(leftForwardRight[2].type == "copper") {
ignoredItem.perc += copperProb.side;
}
break;
case leftForwardRight[1].id:
if(leftForwardRight[1].type == "copper") {
ignoredItem.perc += copperProb.forward;
}
break;
default:
ignoredItem.perc += copperProb.radius;
}
addOrReplaceIgnoredItem(ignoredItem)
}
}
function fixDirection(currentStoneId){
var items = getAllItemsInRadius(1, "undefined");
var lfr = getLeftForwardAndRight();
switch (currentStoneId) {
case lfr[0].id: if(lfr[0].type == "copper") return "turn_left";
break;
case lfr[1].id: if(lfr[1].type == "copper") return "good";
break;
case lfr[2].id: if(lfr[2].type == "copper") return "turn_right";
break;
}
if(getAllItemsInRadius(1, "undefined").indexOf(currentStoneId) == -1) return "need_to_go";
return "try_to_turn";
}
function getMyPositionAndDirection() {
return {
x: forest_frame.global_data.my_group.posx,
y: forest_frame.global_data.my_group.posy,
direction: forest_frame.global_data.my_group.napr
};
}
function isInIgnoredItemsByObj(item) {
if(item != null && item.hasOwnProperty("id")) {
return ignoredItems.find(item => item.id == id) !== undefined
}
}
function getIgnoredItemById(_id) {
return isInIgnoredItemById(_id) ? ignoredItems.find(item => item.id == _id) : {id: _id, perc: 0}
}
function isInIgnoredItemById(id) {
return ignoredItems.find(item => item.id == id) !== undefined
}
function addOrReplaceIgnoredItem(item) {
if(item != null && item.hasOwnProperty("id") && item.hasOwnProperty("perc")) {
var tempIndex = ignoredItems.findIndex(it => it.id == item.id);
if(tempIndex != -1) {
ignoredItems[tempIndex] = {id: item.id, perc: item.perc}
} else {
ignoredItems.push({id: item.id, perc: item.perc})
}
}
}
function getLeftForwardAndRight(){
var myPos = getMyPositionAndDirection();
var absY = myPos.y;
var absX = myPos.x;
var tempDirection = parseInt(myPos.direction);
var f = function(napr) {
switch(napr) {
case 1:
tempY--;
break;
case 2:
tempY--;
tempX++;
break;
case 3:
tempX++;
break;
case 4:
tempY++;
tempX++;
break;
case 5:
tempY++;
break;
case 6:
tempY++;
tempX--;
break;
case 7:
tempX--;
break;
case 8:
tempY--;
tempX--;
break;
}
}
var tempY = absY; var tempX = absX;
f(tempDirection);
var forwardId = (tempY - 1) * 6000 + tempX;
tempY = absY; tempX = absX;
f(tempDirection - 1 == 0 ? 8 : tempDirection - 1);
var leftId = (tempY - 1) * 6000 + tempX;
tempY = absY; tempX = absX;
f(tempDirection + 1 == 9 ? 1 : tempDirection + 1);
var rightId = (tempY - 1) * 6000 + tempX;
return [ {id: leftId, type: getStoneTypeById(leftId)},
{id: forwardId, type: getStoneTypeById(forwardId)},
{id: rightId, type: getStoneTypeById(rightId)} ];
}
function getAllItemsInRadius(radius, stoneType) {
var allItemsOnTheScreen = forest_frame.global_data.abs_poses
var itemsInRadius = [];
var currentPosition = {};
currentPosition.x = forest_frame.global_data.my_group.posx
currentPosition.y = forest_frame.global_data.my_group.posy
for(var index = 0; index < allItemsOnTheScreen.length; index++) {
var item = allItemsOnTheScreen[index];
if(item != null && item.hasOwnProperty("type") &&
item.hasOwnProperty("id") && item.id != 0 &&
item.hasOwnProperty("posx") && Math.abs(currentPosition.x - item.posx) <= radius &&
item.hasOwnProperty("posy") && Math.abs(currentPosition.y - item.posy) <= radius) {
switch(stoneType) {
case "copper":
case "undefined":
if(item.type >= 74 && item.type <= 75 || item.type >= 104 && item.type <= 106) {
itemsInRadius.push(parseInt(item.id));
}
break;
}
}
}
return itemsInRadius;
}
function getStoneTypeById(id) {
var typeNum = 0;
forest_frame.global_data.abs_poses.forEach(item => {
if(item !== undefined && item.id == id) {
typeNum = parseInt(item.type)
}
});
if(typeNum >= 74 && typeNum <= 75 || typeNum >= 104 && typeNum <= 106) {
return "copper";
}
return "undefined";
}
function clickSearch() {
hitCount++;
top.frames["d_act"].Client.send('actNewMaps-StartSearch=1')
}
function isInPossibleListItems(id, stoneType) {
if(id != 0) {
switch(stoneType) {
case "undefined":
case "copper":
return possibleCopperIdList.forEach(item => {if(item.indexOf(id) != -1) return true})
return false;
break;
}
}
}
function removeFromPossibleLists(obj, stoneType) {
var id = 0;
if(obj != null && obj.hasOwnProperty("id")) {
id = obj.id;
} else {
id = obj;
}
if(id != 0) {
switch(stoneType) {
case "copper":
case "undefined":
possibleCopperIdList = possibleCopperIdList.filter(item => {item.indexOf(id) == -1})
break;
}
}
}
function possibleListItemsMostType() {
if(possibleCopperIdList.length > 0) return "copper";
return whatShouldISearch();
}
function addToPossibleListItems(id, stoneType) {
var stoneIds = getAllItemsInRadius(5, stoneType).filter(item => item != id);
if(id != 0) {
switch(stoneType) {
case "copper":
var copy = true;
for(var ind = 0; ind < possibleCopperIdList.length; ind++) {
if(possibleCopperIdList[ind].join(",").localeCompare(stoneIds.join(",")) == 0) {
copy = false
}
}
if(copy) {
possibleCopperIdList.push(stoneIds)
}
break;
}
}
}
function getAllPossibleItemsByType(stoneType) {
if(stoneType == "copper") {
return possibleCopperIdList;
}
}
function goToTheNearestStone(stoneType, goTo5Possible) {
if(goTo5Possible) {
var itemsArr = getAllPossibleItemsByType(stoneType);
var resultArr = [];
itemsArr.forEach(item => {item.forEach(id => {
if(getDistanceToId(id) <= 13) {
resultArr.push(id)
}
})})
var leastProb = Number.MAX_VALUE;
var leastProbId = 0;
resultArr.forEach(id => {
var tempIgnoredItem = getIgnoredItemById(id);
if(tempIgnoredItem.perc < leastProb) {leastProb = tempIgnoredItem.perc; leastProbId = tempIgnoredItem.id}
})
goToPosition(leastProbId);
currentStoneId = parseInt(leastProbId)
return getDistanceToId(currentStoneId)
} else {
currentPosition = {
x: forest_frame.global_data.my_group.posx,
y: forest_frame.global_data.my_group.posy}
var allItemsOnTheScreen = forest_frame.global_data.abs_poses
var stoneItems = [];
var typedStoneIds = getAllItemsInRadius(13, stoneType)
for(var index = 0; index < allItemsOnTheScreen.length; index++) {
var item = allItemsOnTheScreen[index];
if(item !== undefined && item.hasOwnProperty("id")) {
for(var idsInd = 0; idsInd < typedStoneIds.length; idsInd++) {
if(typedStoneIds[idsInd] == item.id) {
stoneItems.push(item)
}
}
}
}
var stoneItemsRadius = [[],[],[],[],[],[],[],[],[],[],[],[],[]]
for(var radius = 1; radius <= 13; radius++) {
for(var index = 0; index < stoneItems.length; index++) {
var dx = Math.abs(stoneItems[index].posx - currentPosition.x);
var dy = Math.abs(stoneItems[index].posy - currentPosition.y);
if((dx == radius && dy <= radius) || (dx <= radius && dy == radius)) {
stoneItemsRadius[radius-1].push(stoneItems[index])
}
}
}
for(var index = 0; index < stoneItemsRadius.length; index++) {
if(stoneItemsRadius[index].length > 0) {
var leastProb = Number.MAX_VALUE;
var leastProbId = 0;
stoneItemsRadius[index].forEach(item => {
var tempIgnoredItem = getIgnoredItemById(item.id);
if(tempIgnoredItem.perc < leastProb) {leastProb = tempIgnoredItem.perc; leastProbId = tempIgnoredItem.id}
})
if(leastProb < 100) {
currentStoneId = parseInt(leastProbId);
goToPosition(currentStoneId)
return getDistanceToId(currentStoneId);
}
}
}
//TODO ADD IF NOTHING WHERE FOUND
}
}
function getDistanceToId(id) {
var num = Number(id)
if(!isNaN(num)) {
var result = {}
result.x = num % 6000
result.y = Math.floor(num / 6000) + 1
var myPos = getMyPositionAndDirection()
return Math.max(Math.abs(result.x - myPos.x), Math.abs(result.y - myPos.y));
} else {
return "isNan"
}
}
function goToPosition(id) {
var tempId = parseInt(id);
if(Number.isInteger(tempId)) {
log.i("trying to go to " + tempId)
top.frames["d_act"].Client.send('actNewMaps-GotoKletka=' + tempId)
return tempId;
} else {
log.e("cannot go to" + tempId)
}
}
function createNewButton(targetframe, id, style, onclick, inner, parstyle){
var navbutton = createMyElement(pers_f, "b", "parent-"+id, "button", parstyle, "", "");
var innernavbutton = createMyElement(pers_f, "b", "", "", "width: 100%;", "", "");
navbutton.appendChild(innernavbutton);
var end_button = createMyElement(targetframe, "button", id, "", style+"outline: none;", onclick, inner);
innernavbutton.appendChild(end_button);
return navbutton;
}
function createMyElement(targetframe, elname, elid, elclass, elstyle, elonclick, innertext) {
var NewElem = targetframe.createElement(elname);
NewElem.setAttribute("id", elid);
NewElem.setAttribute("style", elstyle);
NewElem.setAttribute("class", elclass);
NewElem.setAttribute("onclick", elonclick);
NewElem.innerHTML = innertext;
return NewElem;
}
function createControls(){
if (top.frames["d_pers"].document.getElementsByTagName('body')[0]!=null) {
pers_f = top.frames["d_pers"].document;
var bod = pers_f.getElementsByTagName('body')[0];
var controlsdiv = createMyElement(pers_f, "div", "controlsdiv", "", "padding:0px 5px 0px 5px;", "", "<p style='text-align:center; font-weight:bold; margin: 5px 0px 0px 0px;'>Сontrol</p>");
var startScript = createNewButton(pers_f, "framecontrolstart", "width:100%!important;", "top.frames[\"d_act\"].startScript()", "Start", "width:49%;");
controlsdiv.appendChild(startScript);
var stopScript = createNewButton(pers_f, "framecontrolstop", "width:100%!important;", "top.frames[\"d_act\"].stopScript()", "Stop", "width:49%;");
controlsdiv.appendChild(stopScript);
bod.appendChild(controlsdiv);
createNavSelector();
startShowCoordinates();
} else {
setTimeout(createControls, 800);
}
}
top.frames["d_act"].startScript = function startScript(){
if(!alreadyStarted) {
alreadyStarted = true;
setTimeout(startBtnClicked, 1000);
reloadId = setTimeout(function(){
localStorage.setItem("Start_script", "true");
stopBtnClicked()
top.location.reload()
}, 60*60*1000)
}
}
top.frames["d_act"].stopScript = function stopScript(){
localStorage.setItem("Start_script", "false");
setTimeout(stopBtnClicked, 1000);
clearTimeout(reloadId);
alreadyStarted = false;
}
//------------------------------------------------------------------NAV CONTROL
function byIdFr(dframe, did) {
return top.frames[dframe].document.getElementById(did);
}
function createNavSelector(){
if (top.frames["d_pers"].document.getElementsByTagName('body')[0]!=null) {
pers_f = top.frames["d_pers"].document;
var bod = pers_f.getElementsByTagName('body')[0];
var selectdiv = createMyElement(pers_f, "div", "navdiv", "", "padding:0px 5px 0px 5px;", "", "");
var titlediv = createMyElement(pers_f, "div", "navtitle", "", "", "", "<b>Навигация:</b> ");
selectdiv.appendChild(titlediv);
var perscords = createMyElement(pers_f, "span", "perscords", "", "", "", "");
titlediv.appendChild(perscords);
var standartobjects = createMyElement(pers_f, "div", "standartobjects", "", "", "", "Объекты ");
selectdiv.appendChild(standartobjects);
var navkords = createMyElement(pers_f, "div", "navkords", "", "", "", "");
selectdiv.appendChild(navkords);
var nbutt = createNewButton(pers_f, "navcontrol", "width:100%!important;", "top.frames[\"d_act\"].startNavigation()", "Запустить навигатор", "width:100%;"); //TODO
var nbutt2 = createNewButton(pers_f, "nav2control", "width:100%!important;", "top.frames[\"d_act\"].stopNavigation()", "Остановить навигатор", "width:100%;"); //TODO
navkords.innerHTML = "<label id='navxcord' style='line-height: 25px;float: left;display: block;max-width: 50%;' for='xnavcord'>X - <input type='text' name='xnavcord' id='xnavcord' value='' style='width: 75%;' placeholder='координата'/></label><label id='navycord' style='line-height: 25px;float: left;display: block;max-width: 50%;' for='ynavcord'>Y - <input type='text' name='ynavcord' id='ynavcord' value='' style='width: 75%;' placeholder='координата'/></label><br />";
navkords.appendChild(nbutt);
navkords.appendChild(nbutt2);
var selecttag = createMyElement(pers_f, "select", "NavSelect", "", "width:72%;", "", "");
selecttag.setAttribute("name", "NavSelect");
selecttag.setAttribute("onchange", "top.frames['d_act'].changeNavTarget(this.value)"); //TODO
for (var i = 0; i<NavObjects.length; i++) {
var navoption = pers_f.createElement("option");
navoption.setAttribute("value", i);
navoption.innerHTML = NavObjects[i].name;
selecttag.appendChild(navoption);
}
standartobjects.appendChild(selecttag);
bod.appendChild(selectdiv);
}
}
top.frames["d_act"].startNavigation = function startNavigation(){
var xval = parseInt(byIdFr("d_pers", "xnavcord").value);
var yval = parseInt(byIdFr("d_pers", "ynavcord").value);
if(xval !== undefined && yval !== undefined && Number.isInteger(xval) && Number.isInteger(yval) && xval != 0 && yval != 0) {
start(xval, yval);
}
}
top.frames["d_act"].stopNavigation = function stopNavigation(){
stop()
}
var NavObjects = [
{name :"Не выбрано", latname : "тщту", cordx : "", cordy : "", ofsetx: 0, ofsety: 0, obglocation : ""},
{name :"Альпийский Одуванчик", latname : "Fort", cordx : 795, cordy : 2771, ofsetx: 6, ofsety: 6, obglocation : "Альпийские луга"},
{name :"Заначка Аутлавс", latname : "Fort", cordx : 2041, cordy : 3689, ofsetx: 6, ofsety: 6, obglocation : "Заводь среднеморская"},
{name :"Среднеморье", latname : "Smorye", cordx : 3755, cordy : 2965, ofsetx: 6, ofsety: 6, obglocation : "Окрестности Сморья"},
{name :"Утес дракона", latname : "Utes", cordx : 749, cordy : 1129, ofsetx: 6, ofsety: 6, obglocation : "Окрестности Утеса"},
{name :"Ковчег", latname : "Kovcheg", cordx : 3755, cordy : 1128, ofsetx: 6, ofsety: 6, obglocation : "Окрестности Ковчега"}
];
top.frames["d_act"].changeNavTarget = function changeNavTarget(val) {
byIdFr("d_pers", "xnavcord").value = NavObjects[val].cordx;
byIdFr("d_pers", "ynavcord").value = NavObjects[val].cordy;
}
function startShowCoordinates(){
setInterval(function() {
byIdFr("d_pers", "perscords").innerHTML = "x-"+top.frames["d_act"].global_data.my_group.posx+" y-"+top.frames["d_act"].global_data.my_group.posy;
}, 1000)
}
//------------------------------------------------------------------NAV CONTROL END
//------------------------------------------------------------------NAVIGATION
function goTo(item) {
log.i("trying to go to " + item)
if(item != null && item.hasOwnProperty("id") && item.id != 0) {
if(getApprovanceById(item.id)) {
log.i(item.id)
Client.send('actNewMaps-GotoKletka=' + item.id)
return getAbs(getMyCurrentCellId(), item.id)
}
return 0;
}
}
function getMyCurrentCellId() {
var x = global_data.my_group.posx
var y = global_data.my_group.posy
if(!isNaN(x) && !isNaN(y)) {
return (y-1)*6000 + x;
}
}
function getApprovanceById(id) {
var x = global_data.my_group.posx
var y = global_data.my_group.posy
if(!isNaN(x) && !isNaN(y)) {
if(!isNaN(id)) {
var result = getCoordinates(id);
if(Math.abs(result.x - x) < 13 && Math.abs(result.y - y) < 13) {
return true;
}
}
}
return false;
}
function getCoordinates (e) {
var result = {}
var num = Number(e)
if(!isNaN(num)) {
result.x = num % 6000
result.y = Math.floor(num / 6000) + 1
return result;
} else {
result = "isNan"
}
};
function getId (x, y) {
if(isNaN(x) || isNaN(y)) return "0"
return (y-1) * 6000 + x;
};
function getAbs(id1, id2) {
if(isNaN(id1) || isNaN(id2)) return "0";
var res1 = {}
res1.x = Math.abs((id1 % 6000) - (id2 % 6000))
res1.y = Math.abs((Math.floor(id1 / 6000) + 1) - (Math.floor(id2 / 6000) + 1))
if(res1.x > res1.y) return Math.round(res1.x * 2 / 3)
else { return Math.round(res1.y * 2 / 3) }
}
function chooseDirection(x, y) {
result = {}
result.x = -1;
result.y = -1;
var x_my = global_data.my_group.posx
var y_my = global_data.my_group.posy
var dx = x_my - x
var dy = y_my - y
if(Math.abs(dx) < 13 && Math.abs(dy) < 13) {
result.x = x;
result.y = y;
result.visible = true;
return result;
}
if(dx < 0) {
if(dy < 0) { //Done
if(Math.abs(dx)) {
result.x = 12 + x_my;
result.y = 12 + y_my;
} else {
if(Math.abs(dx) > Math.abs(dy)) {
result.x = 12 + x_my
result.y = (((y - y_my) * 12) / (x - x_my)) + y_my
}
if(Math.abs(dy) > Math.abs(dx)) {
result.y = 12 + y_my
result.x = (((x - x_my) * 12) / (y - y_my)) + x_my
}
}
}
if(dy > 0) {
if(Math.abs(dx) == Math.abs(dy)) {
result.x = 12 + x_my;
result.y = -12 + y_my
} else {
if(Math.abs(dx) > Math.abs(dy)) {
result.x = 12 + x_my
result.y = (((y - y_my) * 12) / (x - x_my)) + y_my
}
if(Math.abs(dy) > Math.abs(dx)) {
result.y = -12 + y_my
result.x = -((x - x_my) * 12) / (y - y_my) + x_my
}
}
}
if(dy == 0) {
result.y = y_my;
result.x = 12 + x_my;
}
}
if(dx > 0) {
if(dy < 0) {//Done
if(Math.abs(dx) == Math.abs(dy)) {
result.x = -12 + x_my;
result.y = 12 + y_my;
} else {
if(Math.abs(dx) > Math.abs(dy)) {
result.x = -12 + x_my
result.y = -(((y - y_my) * 12) / (x - x_my)) + y_my
}
if(Math.abs(dy) > Math.abs(dx)) {
result.y = 12 + y_my
result.x = (((x - x_my) * 12) / (y - y_my)) + x_my
}
}
}
if(dy > 0) {//Done
if(Math.abs(dx) == Math.abs(dy)) {
result.x = -12 + x_my;
result.y = -12 + y_my
} else {
if(Math.abs(dx) > Math.abs(dy)) {
result.x = -12 + x_my
result.y = -(((y - y_my) * 12) / (x - x_my)) + y_my
}
if(Math.abs(dy) > Math.abs(dx)) {
result.y = -12 + y_my
result.x = -((x - x_my) * 12) / (y - y_my) + x_my
}
}
}
if(dy == 0) {
result.y = y_my;
result.x = -12 + x_my;
}
}
if(dx == 0) {
result.x = x_my;
if(dy < 0) {
result.y = 12 + y_my
}
if(dy > 0) {
result.y = -12 + y_my
}
if(dy == 0) {
result.y = y_my;
}
}
result.x = Math.round(result.x)
result.y = Math.round(result.y)
return result
}
var interval = 1000;
var timeoutId;
function goToGlobalCoordinates(x, y) {
if(isNaN(x) || isNaN(y)) return;
var result = chooseDirection(x, y)
var id = 0
if(result != null && result.hasOwnProperty("x") && result.x != -1 &&
result.hasOwnProperty("y") && result.y != -1) {
if(result.hasOwnProperty("visible") && result.visible == true) {
id = 0;
goTo({"id":getId(result.x, result.y)});
} else {
id = getId(result.x, result.y)
}
}
if(id != 0) {
interval = goTo({"id":id}) * 1000;
log.i("interval = " + interval )
timeoutId = setTimeout(function() {
start(x, y);
}, interval)
}
}
function start(x, y) {
goToGlobalCoordinates(x, y)
}
function getCoordinatesAndStart(e) {
var result = {}
var num = Number(e)
if(!isNaN(num)) {
result.x = num % 6000
result.y = Math.floor(num / 6000) + 1
start(result.x, result.y)
return result;
} else {
result = "isNan"
}
}
function stop() {
clearTimeout(timeoutId);
}
//------------------------------------------------------------------NAVIGATION END
//-------------------Helper functions
function log2(prefix, str) {
var d = new Date();
console.log(prefix + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " -->" + str)
}
function clickStartDig() {
top.frames["d_act"].Client.send('actNewMaps-StartDobycha=1')
}
var log = {
e:function (str) {log2("_________ ", str)},
i:function (str) {log2("______ ", str)},
v:function (str) {log2("___ ", str)}
}
//Returns random number in range of @min and @max
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
// Возвращает случайное целое число между min (включительно) и max (не включая max)
// Использование метода Math.round() даст вам неравномерное распределение!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
//Returns timer value
function getSecondsLeft() {
var secondsLeft = parseInt(forest_frame.global_data.wait_time) -
(parseInt(forest_frame.global_data.timestamp) + parseInt(Math.floor(new Date().getTime() / 1000)) - parseInt(forest_frame.Realtime))
if(secondsLeft >= 0) {
return secondsLeft
} else {
return "-1";
}
}
//Returns text of overlayed window otherwise "Not overlayed"
function getResponseIfExists() {
if(isOverlayOn()) {
return jQuery('#modal_form').text().slice()
} else {
return "Not overlayed"
}
}
//Returns true or false
function isOverlayOn() {
return jQuery('#overlay').css("display") == "block"
}
//Clicks the overlay to hide
function setOverlayOff() {
if(isOverlayOn()) {
jQuery('#overlay').click()
}
}
var forest_f = top.frames["d_act"].document;
var dotsArr = [];
function createDot(id, text) {
var startCellId = forest_frame.global_data.my_group.id - 72012;
var dx = (id - startCellId) % 6000;
var dy = Math.abs(Math.floor((id - startCellId) / 6000));
var rrr = createMyElement(forest_f, "div", "dot-" + id, "", "display:block;position: absolute; z-index:2; width:35px; height:35px; top:0%;left:0%;margin-top:" + (dy * 35) + "px; margin-left:" + (dx * 35) + "px;background-color: #ffffff45;color: red;", "", text);
byIdFr("d_act", "canvas").parentNode.appendChild(rrr);
dotsArr.push(rrr)
}
function clearDots() {
dotsArr.forEach(function(item, i, arr) {
item.remove();
})
dotsArr = [];
}
function byIdFr(dframe, did) {
return top.frames[dframe].document.getElementById(did);
}
function createMyElement(targetframe, elname, elid, elclass, elstyle, elonclick, innertext) {
var NewElem = targetframe.createElement(elname);
NewElem.setAttribute("id", elid);
NewElem.setAttribute("style", elstyle);
NewElem.setAttribute("class", elclass);
NewElem.setAttribute("onclick", elonclick);
NewElem.innerHTML = innertext;
return NewElem;
}
function startCanv() {
if(searchCopper) {
var allitems = getAllItemsInRadius(12, "copper");
byIdFr("d_act", "canvas").parentNode.style.overflow = "hidden";
for (var index = 0; index < allitems.length; index++) {
createDot(allitems[index], getIgnoredItemById(allitems[index]).perc + "")
}
}
}
function rewriteLocalStorage() {
localStorage.setItem("last_state",
JSON.stringify({"currentState": currentState,
"hitCount": hitCount,
"currentStoneId": currentStoneId}))
localStorage.setItem("possibleLists",
JSON.stringify({"possibleCopperIdList": possibleCopperIdList}))
localStorage.setItem("ignoredItems",
JSON.stringify({"ignoredItems": ignoredItems}))
}
function loadLocalStorage() {
var lastState = JSON.parse(localStorage.getItem("last_state"))
if(lastState != null) {
if(lastState.hasOwnProperty("currentState")) currentState = lastState.currentState;
if(lastState.hasOwnProperty("hitCount")) hitCount = lastState.hitCount;
if(lastState.hasOwnProperty("currentStoneId")) currentStoneId = parseInt(lastState.currentStoneId);
}
var possibleLists = JSON.parse(localStorage.getItem("possibleLists"))
if(possibleLists != null) {
if(possibleLists.hasOwnProperty("possibleCopperIdList")) possibleCopperIdList = possibleLists.possibleCopperIdList;
}
var _ignoredItems = JSON.parse(localStorage.getItem("ignoredItems"))
if(_ignoredItems != null) {
if(_ignoredItems.hasOwnProperty("ignoredItems")) ignoredItems = _ignoredItems.ignoredItems;
}
}
forest_frame.OpenCapcha = function OpenCapcha(data) {
fetch("http://newforest.apeha.ru/interface/codeimage.fpl/" + data.ci).then(data1 => data1.blob()).then(res => {
Tesseract.recognize(res)
.then(function(result) {
lang: "equ" // Язык текста
})
.then(function(result) {
var r = result.text.replace(/\D/g,'')
if(r.length == 4) {
setTimeout(function() {forest_frame.Client.send('actNewMaps-StartDobycha='+ data.ci + '.' + r)}, getRandom(500, 700));
log.i("sent = " + r);
} else {
log.i("NOT sent = " + r);
}
})
})}
document.onkeydown = function (e) {
var code = e.keyCode ? e.keyCode : e.which;
if(code === 106) { //*
startBtnClicked()
}
if(code === 109) { //-
stopBtnClicked()
}
};
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Tesseract = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
// cached from whatever global is present so that test runners that stub it
// don't break things. But we need to wrap it in a try catch in case it is
// wrapped in strict mode code which doesn't define any globals. It's inside a
// function because try/catches deoptimize in certain engines.
var cachedSetTimeout;
var cachedClearTimeout;
function defaultSetTimout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}
(function () {
try {
if (typeof setTimeout === 'function') {
cachedSetTimeout = setTimeout;
} else {
cachedSetTimeout = defaultSetTimout;
}
} catch (e) {
cachedSetTimeout = defaultSetTimout;
}
try {
if (typeof clearTimeout === 'function') {
cachedClearTimeout = clearTimeout;
} else {
cachedClearTimeout = defaultClearTimeout;
}
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
} ())
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
//normal enviroments in sane situations
return setTimeout(fun, 0);
}
// if setTimeout wasn't available but was latter defined
if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
cachedSetTimeout = setTimeout;
return setTimeout(fun, 0);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedSetTimeout(fun, 0);
} catch(e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedSetTimeout.call(null, fun, 0);
} catch(e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
return cachedSetTimeout.call(this, fun, 0);
}
}
}
function runClearTimeout(marker) {
if (cachedClearTimeout === clearTimeout) {
//normal enviroments in sane situations
return clearTimeout(marker);
}
// if clearTimeout wasn't available but was latter defined
if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
cachedClearTimeout = clearTimeout;
return clearTimeout(marker);
}
try {
// when when somebody has screwed with setTimeout but no I.E. maddness
return cachedClearTimeout(marker);
} catch (e){
try {
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
return cachedClearTimeout.call(null, marker);
} catch (e){
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
return cachedClearTimeout.call(this, marker);
}
}
}
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = runTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
runClearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
runTimeout(drainQueue);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.prependListener = noop;
process.prependOnceListener = noop;
process.listeners = function (name) { return [] }
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
},{}],2:[function(require,module,exports){
module.exports={
"name": "tesseract.js",
"version": "1.0.10",
"description": "Pure Javascript Multilingual OCR",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" & exit 1",
"start": "watchify src/index.js -t [ envify --NODE_ENV development ] -t [ babelify --presets [ es2015 ] ] -o dist/tesseract.dev.js --standalone Tesseract & watchify src/browser/worker.js -t [ envify --NODE_ENV development ] -t [ babelify --presets [ es2015 ] ] -o dist/worker.dev.js & http-server -p 7355",
"build": "browserify src/index.js -t [ babelify --presets [ es2015 ] ] -o dist/tesseract.js --standalone Tesseract && browserify src/browser/worker.js -t [ babelify --presets [ es2015 ] ] -o dist/worker.js",
"release": "npm run build && git commit -am 'new release' && git push && git tag `jq -r '.version' package.json` && git push origin --tags && npm publish"
},
"browser": {
"./src/node/index.js": "./src/browser/index.js"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"babel-preset-es2015": "^6.16.0",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"envify": "^3.4.1",
"http-server": "^0.9.0",
"isomorphic-fetch": "^2.2.1",
"pako": "^1.0.3",
"watchify": "^3.7.0"
},
"dependencies": {
"file-type": "^3.8.0",
"is-url": "^1.2.2",
"jpeg-js": "^0.2.0",
"level-js": "^2.2.4",
"node-fetch": "^1.6.3",
"object-assign": "^4.1.0",
"png.js": "^0.2.1",
"tesseract.js-core": "^1.0.2"
},
"repository": {
"type": "git",
"url": "https://github.com/naptha/tesseract.js.git"
},
"bugs": {
"url": "https://github.com/naptha/tesseract.js/issues"
},
"homepage": "https://github.com/naptha/tesseract.js"
}
},{}],3:[function(require,module,exports){
(function (process){
'use strict';
var defaultOptions = {
// workerPath: 'https://cdn.rawgit.com/naptha/tesseract.js/0.2.0/dist/worker.js',
corePath: 'https://cdn.rawgit.com/naptha/tesseract.js-core/0.1.0/index.js',
langPath: 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'
};
if (process.env.NODE_ENV === "development") {
console.debug('Using Development Configuration');
defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3);
} else {
var version = require('../../package.json').version;
defaultOptions.workerPath = 'https://cdn.rawgit.com/naptha/tesseract.js/' + version + '/dist/worker.js';
}
exports.defaultOptions = defaultOptions;
exports.spawnWorker = function spawnWorker(instance, workerOptions) {
if (window.Blob && window.URL) {
var blob = new Blob(['importScripts("' + workerOptions.workerPath + '");']);
var worker = new Worker(window.URL.createObjectURL(blob));
} else {
var worker = new Worker(workerOptions.workerPath);
}
worker.onmessage = function (e) {
var packet = e.data;
instance._recv(packet);
};
return worker;
};
exports.terminateWorker = function (instance) {
instance.worker.terminate();
};
exports.sendPacket = function sendPacket(instance, packet) {
loadImage(packet.payload.image, function (img) {
packet.payload.image = img;
instance.worker.postMessage(packet);
});
};
function loadImage(image, cb) {
if (typeof image === 'string') {
if (/^\#/.test(image)) {
// element css selector
return loadImage(document.querySelector(image), cb);
} else if (/(blob|data)\:/.test(image)) {
// data url
var im = new Image();
im.src = image;
im.onload = function (e) {
return loadImage(im, cb);
};
return;
} else {
var xhr = new XMLHttpRequest();
xhr.open('GET', image, true);
xhr.responseType = "blob";
xhr.onload = function (e) {
return loadImage(xhr.response, cb);
};
xhr.onerror = function (e) {
if (/^https?:\/\//.test(image) && !/^https:\/\/crossorigin.me/.test(image)) {
console.debug('Attempting to load image with CORS proxy');
loadImage('https://crossorigin.me/' + image, cb);
}
};
xhr.send(null);
return;
}
} else if (image instanceof File) {
// files
var fr = new FileReader();
fr.onload = function (e) {
return loadImage(fr.result, cb);
};
fr.readAsDataURL(image);
return;
} else if (image instanceof Blob) {
return loadImage(URL.createObjectURL(image), cb);
} else if (image.getContext) {
// canvas element
return loadImage(image.getContext('2d'), cb);
} else if (image.tagName == "IMG" || image.tagName == "VIDEO") {
// image element or video element
var c = document.createElement('canvas');
c.width = image.naturalWidth || image.videoWidth;
c.height = image.naturalHeight || image.videoHeight;
var ctx = c.getContext('2d');
ctx.drawImage(image, 0, 0);
return loadImage(ctx, cb);
} else if (image.getImageData) {
// canvas context
var data = image.getImageData(0, 0, image.canvas.width, image.canvas.height);
return loadImage(data, cb);
} else {
return cb(image);
}
throw new Error('Missing return in loadImage cascade');
}
}).call(this,require('_process'))
},{"../../package.json":2,"_process":1}],4:[function(require,module,exports){
"use strict";
// The result of dump.js is a big JSON tree
// which can be easily serialized (for instance
// to be sent from a webworker to the main app
// or through Node's IPC), but we want
// a (circular) DOM-like interface for walking
// through the data.
module.exports = function circularize(page) {
page.paragraphs = [];
page.lines = [];
page.words = [];
page.symbols = [];
page.blocks.forEach(function (block) {
block.page = page;
block.lines = [];
block.words = [];
block.symbols = [];
block.paragraphs.forEach(function (para) {
para.block = block;
para.page = page;
para.words = [];
para.symbols = [];
para.lines.forEach(function (line) {
line.paragraph = para;
line.block = block;
line.page = page;
line.symbols = [];
line.words.forEach(function (word) {
word.line = line;
word.paragraph = para;
word.block = block;
word.page = page;
word.symbols.forEach(function (sym) {
sym.word = word;
sym.line = line;
sym.paragraph = para;
sym.block = block;
sym.page = page;
sym.line.symbols.push(sym);
sym.paragraph.symbols.push(sym);
sym.block.symbols.push(sym);
sym.page.symbols.push(sym);
});
word.paragraph.words.push(word);
word.block.words.push(word);
word.page.words.push(word);
});
line.block.lines.push(line);
line.page.lines.push(line);
});
para.page.paragraphs.push(para);
});
});
return page;
};
},{}],5:[function(require,module,exports){
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var adapter = require('../node/index.js');
var jobCounter = 0;
module.exports = function () {
function TesseractJob(instance) {
_classCallCheck(this, TesseractJob);
this.id = 'Job-' + ++jobCounter + '-' + Math.random().toString(16).slice(3, 8);
this._instance = instance;
this._resolve = [];
this._reject = [];
this._progress = [];
this._finally = [];
}
_createClass(TesseractJob, [{
key: 'then',
value: function then(resolve, reject) {
if (this._resolve.push) {
this._resolve.push(resolve);
} else {
resolve(this._resolve);
}
if (reject) this.catch(reject);
return this;
}
}, {
key: 'catch',
value: function _catch(reject) {
if (this._reject.push) {
this._reject.push(reject);
} else {
reject(this._reject);
}
return this;
}
}, {
key: 'progress',
value: function progress(fn) {
this._progress.push(fn);
return this;
}
}, {
key: 'finally',
value: function _finally(fn) {
this._finally.push(fn);
return this;
}
}, {
key: '_send',
value: function _send(action, payload) {
adapter.sendPacket(this._instance, {
jobId: this.id,
action: action,
payload: payload
});
}
}, {
key: '_handle',
value: function _handle(packet) {
var data = packet.data;
var runFinallyCbs = false;
if (packet.status === 'resolve') {
if (this._resolve.length === 0) console.log(data);
this._resolve.forEach(function (fn) {
var ret = fn(data);
if (ret && typeof ret.then == 'function') {
console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.');
}
});
this._resolve = data;
this._instance._dequeue();
runFinallyCbs = true;
} else if (packet.status === 'reject') {
if (this._reject.length === 0) console.error(data);
this._reject.forEach(function (fn) {
return fn(data);
});
this._reject = data;
this._instance._dequeue();
runFinallyCbs = true;
} else if (packet.status === 'progress') {
this._progress.forEach(function (fn) {
return fn(data);
});
} else {
console.warn('Message type unknown', packet.status);
}
if (runFinallyCbs) {
this._finally.forEach(function (fn) {
return fn(data);
});
}
}
}]);
return TesseractJob;
}();
},{"../node/index.js":3}],6:[function(require,module,exports){
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var adapter = require('./node/index.js');
var circularize = require('./common/circularize.js');
var TesseractJob = require('./common/job');
var version = require('../package.json').version;
function create() {
var workerOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var worker = new TesseractWorker(Object.assign({}, adapter.defaultOptions, workerOptions));
worker.create = create;
worker.version = version;
return worker;
}
var TesseractWorker = function () {
function TesseractWorker(workerOptions) {
_classCallCheck(this, TesseractWorker);
this.worker = null;
this.workerOptions = workerOptions;
this._currentJob = null;
this._queue = [];
}
_createClass(TesseractWorker, [{
key: 'recognize',
value: function recognize(image) {
var _this = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this._delay(function (job) {
if (typeof options === 'string') options = { lang: options };
options.lang = options.lang || 'eng';
job._send('recognize', { image: image, options: options, workerOptions: _this.workerOptions });
});
}
}, {
key: 'detect',
value: function detect(image) {
var _this2 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this._delay(function (job) {
job._send('detect', { image: image, options: options, workerOptions: _this2.workerOptions });
});
}
}, {
key: 'terminate',
value: function terminate() {
if (this.worker) adapter.terminateWorker(this);
this.worker = null;
this._currentJob = null;
this._queue = [];
}
}, {
key: '_delay',
value: function _delay(fn) {
var _this3 = this;
if (!this.worker) this.worker = adapter.spawnWorker(this, this.workerOptions);
var job = new TesseractJob(this);
this._queue.push(function (e) {
_this3._queue.shift();
_this3._currentJob = job;
fn(job);
});
if (!this._currentJob) this._dequeue();
return job;
}
}, {
key: '_dequeue',
value: function _dequeue() {
this._currentJob = null;
if (this._queue.length) {
this._queue[0]();
}
}
}, {
key: '_recv',
value: function _recv(packet) {
if (packet.status === 'resolve' && packet.action === 'recognize') {
packet.data = circularize(packet.data);
}
if (this._currentJob.id === packet.jobId) {
this._currentJob._handle(packet);
} else {
console.warn('Job ID ' + packet.jobId + ' not known.');
}
}
}]);
return TesseractWorker;
}();
module.exports = create();
},{"../package.json":2,"./common/circularize.js":4,"./common/job":5,"./node/index.js":3}]},{},[6])(6)
});