Best JavaScript code snippet using devicefarmer-stf
solves.js
Source:solves.js
1(function() {2 // LocalSolves manages solves which the user does offline. It uses3 // LocalPuzzles to get the solves of interest.4 //5 // The puzzles argument must be a LocalPuzzles which has already been loaded6 // with the user's current puzzles.7 //8 // This is an EventEmitter which will emit the following events:9 // - delete: either the deleteSolve or the moveSolve method was used.10 // - modify: the modifySolve method was used.11 // - modifyUnindexed: modifySolveById was used and the ID was not in a cursor.12 // - move: the moveSolve method was used.13 // - loadingStats14 // - computedStats15 function LocalSolves(puzzles) {16 window.app.EventEmitter.call(this);17 this._puzzles = puzzles;18 this._stats = null;19 this._averages = null;20 this._cursors = [];21 this._fillInMissingFields();22 this._computeStats();23 }24 LocalSolves.prototype = Object.create(window.app.EventEmitter.prototype);25 // addSolve pushes a solve to the end of the list.26 LocalSolves.prototype.addSolve = function(solve) {27 var solves = this.getSolves();28 solve.id = window.app.generateId();29 solves.push(solve);30 // All the cursors at the end of the data get the extra solve.31 for (var i = 0, len = this._cursors.length; i < len; ++i) {32 var cursor = this._cursors[i];33 if (cursor._start+cursor._length === this.getSolves().length-1) {34 ++cursor._length;35 }36 }37 recomputeLastPBsAndPWs(solves, solves.length-1);38 if (this._averages !== null) {39 this._averages.pushSolve(solve);40 }41 this._emitStats();42 };43 // createCursor generates a ticket for getting a LocalCursor.44 LocalSolves.prototype.createCursor = function(start, length, cb) {45 return new window.app.LocalCursorTicket(cb, this, start, length);46 };47 // cursorClosed is called by a LocalCursor when it is closed by a consumer.48 LocalSolves.prototype.cursorClosed = function(cursor) {49 var idx = this._cursors.indexOf(cursor);50 if (idx < 0) {51 throw new Error('cursor was not registered');52 }53 this._cursors.splice(idx, 1);54 };55 // cursorCreated is called by each cursor to register itself with the56 // LocalSolves.57 LocalSolves.prototype.cursorCreated = function(cursor) {58 this._cursors.push(cursor);59 };60 // deleteSolve deletes a solve at a given index.61 LocalSolves.prototype.deleteSolve = function(index) {62 var solves = this.getSolves();63 var id = solves[index].id;64 solves.splice(index, 1);65 for (var i = 0, len = this._cursors.length; i < len; ++i) {66 var cursor = this._cursors[i];67 if (index < cursor._start) {68 --cursor._start;69 } else if (index < cursor._start+cursor._length) {70 --cursor._length;71 }72 }73 recomputeLastPBsAndPWs(solves, index);74 this._resetStats();75 this.emit('delete', id, index);76 };77 // getLatestSolve returns the latest solve or null if no solves exist.78 LocalSolves.prototype.getLatestSolve = function() {79 var solves = this.getSolves();80 if (solves.length === 0) {81 return null;82 }83 return solves[solves.length - 1];84 };85 // getSolves returns the current array of solves.86 LocalSolves.prototype.getSolves = function() {87 return this._puzzles.getActivePuzzle().solves;88 };89 // getStats returns the current stats or null if no stats are cached.90 LocalSolves.prototype.getStats = function() {91 return this._stats;92 };93 // modifySolve changes the attributes of a given solve.94 LocalSolves.prototype.modifySolve = function(index, attrs) {95 this._modifySolveNoEmit(index, attrs);96 this.emit('modify', this.getSolves()[index].id, attrs, index);97 };98 // modifySolveById changes the attributes of a solve by looking up its ID.99 // This will emit 'modifyUnindexed' instead of 'modify' if the solve is not100 // within any cursor.101 LocalSolves.prototype.modifySolveById = function(id, attrs) {102 var index = -1;103 var solves = this.getSolves();104 for (var i = 0, len = solves.length; i < len; ++i) {105 if (solves[i].id === id) {106 index = i;107 break;108 }109 }110 if (index < 0) {111 return;112 }113 this._modifySolveNoEmit(index, attrs);114 var containedByCursor = false;115 for (var i = 0, len = this._cursors.length; i < len; ++i) {116 var cursor = this._cursors[i];117 if (index < cursor._start+cursor._length && index >= cursor._start) {118 containedByCursor = true;119 break;120 }121 }122 if (containedByCursor) {123 this.emit('modify', id, attrs, index);124 } else {125 this.emit('modifyUnindexed', id, attrs);126 }127 };128 // moveSolve moves a solve to a different puzzle.129 LocalSolves.prototype.moveSolve = function(index, puzzleId) {130 var puzzle = this._puzzles.findById(puzzleId);131 if (puzzle === null) {132 return;133 }134 var solve = this.getSolves()[index];135 this.deleteSolve(index);136 insertSolveUsingTimestamp(solve, puzzle.solves);137 this.emit('move', solve.id, puzzle);138 };139 // reset should be called whenever the solves were changed in a way that140 // cannot be easily broken down into deletions, additions and modifications.141 LocalSolves.prototype.reset = function() {142 while (this._cursors.length > 0) {143 this._cursors[this._cursors.length-1].close();144 }145 this._resetStats();146 };147 // _computeStats generates this._stats.148 LocalSolves.prototype._computeStats = function() {149 if (this._averages === null) {150 this._averages = new window.app.OfflineAverages();151 var solves = this.getSolves();152 for (var i = 0, len = solves.length; i < len; ++i) {153 this._averages.pushSolve(solves[i]);154 }155 }156 this._stats = this._averages.stats();157 };158 // _emitStats uses this._averages to emit new stats asynchronously.159 // It will create this._averages if it is null.160 LocalSolves.prototype._emitStats = function() {161 this._stats = null;162 this.emit('loadingStats');163 setTimeout(function() {164 if (this._stats === null) {165 this._computeStats();166 this.emit('computedStats', this._stats);167 }168 }.bind(this), 1);169 };170 // _fillInMissingFields fills in missing fields for all solve objects on all171 // puzzles.172 LocalSolves.prototype._fillInMissingFields = function() {173 var puzzles = this._puzzles.getPuzzles();174 for (var i = 0, len = puzzles.length; i < len; ++i) {175 var puzzle = puzzles[i];176 var solves = puzzle.solves;177 recomputeLastPBsAndPWs(solves, 0);178 for (var j = 0, len1 = solves.length; j < len1; ++j) {179 var solve = solves[j];180 if (!solve.scrambler) {181 solve.scrambler = puzzle.scrambler;182 solve.scrambleType = puzzle.scrambleType;183 }184 }185 }186 };187 // _modifySolveNoEmit modifies a solve without emitting a modify event.188 LocalSolves.prototype._modifySolveNoEmit = function(index, attrs) {189 var solve = this.getSolves()[index];190 if ('undefined' === typeof solve) {191 throw new Error('no solve at index: ' + index);192 }193 var newSolve = window.app.copySolve(solve);194 this.getSolves()[index] = newSolve;195 var keys = Object.keys(attrs);196 for (var i = 0, len = keys.length; i < len; ++i) {197 var key = keys[i];198 newSolve[key] = attrs[key];199 }200 // TODO: do not recompute this stuff if neither the time nor the penalties201 // were modified.202 recomputeLastPBsAndPWs(this.getSolves(), index+1);203 this._resetStats();204 };205 // _resetStats deletes all pre-existing knowledge about the stats and206 // recomputes them asynchronously.207 LocalSolves.prototype._resetStats = function() {208 this._averages = null;209 this._emitStats();210 };211 function insertSolveUsingTimestamp(solve, solves) {212 // TODO: use a binary search here.213 for (var i = solves.length-1; i >= 0; --i) {214 if (solves[i].date <= solve.date) {215 solves.splice(i+1, 0, solve);216 recomputeLastPBsAndPWs(solves, i+1);217 return;218 }219 }220 solves.unshift(solve);221 recomputeLastPBsAndPWs(solves, 0);222 }223 function recomputeLastPBsAndPWs(solves, startIndex) {224 recomputeLastPBs(solves, startIndex);225 recomputeLastPWs(solves, startIndex);226 }227 function recomputeLastPBs(solves, startIndex) {228 if (startIndex >= solves.length) {229 return;230 }231 var lastPB = -1;232 if (startIndex > 0) {233 var previousSolve = solves[startIndex - 1];234 if (previousSolve.dnf) {235 lastPB = previousSolve.lastPB;236 } else if (previousSolve.lastPB === -1) {237 lastPB = window.app.solveTime(previousSolve);238 } else {239 lastPB = Math.min(window.app.solveTime(previousSolve),240 previousSolve.lastPB);241 }242 }243 for (var i = startIndex, len = solves.length; i < len; ++i) {244 var solve = solves[i];245 solve.lastPB = lastPB;246 if (!solve.dnf) {247 if (lastPB < 0) {248 lastPB = window.app.solveTime(solve);249 } else {250 lastPB = Math.min(lastPB, window.app.solveTime(solve));251 }252 }253 }254 };255 function recomputeLastPWs(solves, startIndex) {256 if (startIndex >= solves.length) {257 return;258 }259 var lastPW = -1;260 if (startIndex > 0) {261 var previousSolve = solves[startIndex - 1];262 if (previousSolve.dnf) {263 lastPW = previousSolve.lastPW;264 } else if (previousSolve.lastPW === -1) {265 lastPW = window.app.solveTime(previousSolve);266 } else {267 lastPW = Math.max(window.app.solveTime(previousSolve),268 previousSolve.lastPW);269 }270 }271 for (var i = startIndex, len = solves.length; i < len; ++i) {272 var solve = solves[i];273 solve.lastPW = lastPW;274 if (!solve.dnf) {275 if (lastPW < 0) {276 lastPW = window.app.solveTime(solve);277 } else {278 lastPW = Math.max(lastPW, window.app.solveTime(solve));279 }280 }281 }282 };283 window.app.LocalSolves = LocalSolves;...
index.js
Source:index.js
...77 cube.checkForMove();78 TWEEN.update();79 renderer.render(scene, camera);80}81function solve() {82 if (cube.animating) {83 solveMessage.innerHTML = "Wait until Cube is Still";84 return;85 }86 const xhr = new XMLHttpRequest();87 const url = "http://127.0.0.1:5000/";88 xhr.open("POST", url, true);89 xhr.setRequestHeader("Content-type", "application/json");90 let data = JSON.stringify({ cubeSize: cubeSize, scramble: cube.state() });91 console.log(data);92 xhr.send(data);93 solveMessage.innerHTML = "Solving...";94 cube.sequencing = true;95 solveButton.disabled = true;...
heuristica.js
Source:heuristica.js
1(function () {2 var tiles = [],3 goal = [];4 var startScreen = document.querySelector("#startScreen");5 startScreen.addEventListener("click", startGame, false);6 var endScreen = document.querySelector("#endScreen");7 /**********8 Solução e initBoard9 **********/10 var initBoard = $(".initBoard").attr('data-value');11 var arrayInitBoard = initBoard.split(" ");12 var indexSolve = 0;13 var solveBoard = $(".solveBoard").attr('data-value');14 var arraySolveBoard = solveBoard.split(" ");15 arraySolveBoard.reverse();16 function setStepSolve(){17 indexSolve++;18 if(arraySolveBoard[indexSolve] == 'UP'){19 $('.showSolve').html("PASSO "+indexSolve+": CIMA");20 }else if(arraySolveBoard[indexSolve] == 'DOWN'){21 $('.showSolve').html("PASSO "+indexSolve+": BAIXO");22 }else if(arraySolveBoard[indexSolve] == 'RIGHT'){23 $('.showSolve').html("PASSO "+indexSolve+": DIREITA");24 }else if(arraySolveBoard[indexSolve] == 'LEFT'){25 $('.showSolve').html("PASSO "+indexSolve+": ESQUERDA");26 }else{27 $('.showSolve').html("FIM!!!");28 }29 }30 function init() {31 for(var i = 1; i < 9; i++){32 var tile = document.querySelector('#n' + i);33 tile.style.background = "url('/../img/n"+i+".png')";34 tile.addEventListener("click", moveTile, false);35 tiles.push(tile);36 goal.push(tile);37 }38 tiles.push(null);39 goal.push(null);40 render();41 }42 function render(){43 for(var i in tiles){44 var tile = tiles[i];45 if(tile != null){46 tile.style.left = (i%3) *100 + 5 + "px";47 if(i < 3){48 tile.style.top = "5px";49 }else if(i < 6){50 tile.style.top = "105px";51 }else{52 tile.style.top = "205px";53 }54 }55 }56 }57 function startGame(){58 /*remove screen start*/59 this.style.opacity = "0";60 this.style.zIndex = "-1";61 this.removeEventListener("click", startGame, false);62 /*coloca o estado init no board*/63 var aux = [];64 aux[0] = tiles[arrayInitBoard[0]-1];65 aux[1] = tiles[arrayInitBoard[1]-1];66 aux[2] = tiles[arrayInitBoard[2]-1];67 aux[3] = tiles[arrayInitBoard[3]-1];68 aux[4] = tiles[arrayInitBoard[4]-1];69 aux[5] = tiles[arrayInitBoard[5]-1];70 aux[6] = tiles[arrayInitBoard[6]-1];71 aux[7] = tiles[arrayInitBoard[7]-1];72 aux[8] = tiles[arrayInitBoard[8]-1];73 tiles = aux;74 render();75 setStepSolve();76 }77 function moveTile(){78 var index = tiles.indexOf(this);79 if(index % 3 !== 0 && arraySolveBoard[indexSolve] === "RIGHT"){80 if(!tiles[index-1]){81 tiles[index-1] = this;82 tiles[index] = null;83 setStepSolve();84 }85 }86 if(index % 3 !== 2 && arraySolveBoard[indexSolve] === "LEFT"){87 if(!tiles[index+1]){88 tiles[index+1] = this;89 tiles[index] = null;90 setStepSolve();91 }92 }93 if(index > 2 && arraySolveBoard[indexSolve] === "DOWN"){94 if(!tiles[index-3]){95 tiles[index-3] = this;96 tiles[index] = null;97 setStepSolve();98 }99 }100 if(index < 6 && arraySolveBoard[indexSolve] === "UP"){101 if(!tiles[index+3]){102 tiles[index+3] = this;103 tiles[index] = null;104 setStepSolve();105 }106 }107 render();108 if(checkGoal()){109 showEndScreen();110 }111 }112 function showEndScreen() {113 endScreen.style.opacity = "1";114 endScreen.style.zIndex = "1";115 setTimeout(function functionName() {116 endScreen.addEventListener("click", goIndex, false);117 })118 }119 120 function goIndex() {121 var url = window.location.href;122 var arrayUrl = url.split("/");123 console.log(arrayUrl);124 window.location="http://"+arrayUrl[2]+"/servlets/standalone.rkt;";125 }126 function setScore(){127 var inputScore = $('.scoreHidden').val();128 inputScore++;129 $('.score').html("SCORE "+inputScore);130 $('.scoreHidden').attr('value', inputScore);131 }132 function checkGoal(){133 for(var i in tiles){134 if(tiles[i] !== goal[i]){135 return false;136 }137 }138 return true;139 }140 init();...
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2var device = stf.use('HT4A2SK00033');3device.solve('com.android.calculator2', 'com.android.calculator2.Calculator', function(err, result) {4 if (err) {5 console.error(err);6 } else {7 console.log(result);8 }9});10{11 "dependencies": {12 }13}
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var device = new stf.Device();3device.solve('9b9d5b5c', 'com.android.settings', 'com.android.settings.Settings$WifiSettingsActivity');4var stf = require('devicefarmer-stf');5var device = new stf.Device();6device.solve('9b9d5b5c', 'com.android.settings', 'com.android.settings.Settings$WifiSettingsActivity', function(err, res){7 if(err){8 console.log("Error occurred: "+err);9 }else{10 console.log("Solve response: "+res);11 }12});13{14 "data": {15 }16}17{18 "data": {19 }20}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!