Best JavaScript code snippet using redwood
ais.js
Source:ais.js
1//@flow 2import _ from 'lodash'3import type { UnitAction } from "./lib/GameTypes"4import Unit from "./lib/Unit"5import Board from "./lib/Board"6import { unitTypes, actionTypes, directions, distances } from "./lib/gameInfo"7import Victor from "./lib/Victor"8function moveFromAverage(unit: Board, board: Board, averageFunction: Func): Object {9 let mediumRangeUnits = board.units.slice().filter(u => u !== unit && 10 u.team !== unit.team && 11 (u.distance(unit) === distances.medium || u.distance(unit) === distances.short))12 let targetUnit; 13 let currentAction;14 if (mediumRangeUnits.length > 0) {15 targetUnit = averageFunction(mediumRangeUnits, unit)16 } else {17 let longRangeUnits = board.units.slice().filter(u => u !== unit && 18 u.team !== unit.team && 19 u.distance(unit) === distances.long)20 if (longRangeUnits.length > 0) {21 targetUnit = averageFunction(longRangeUnits, unit)22 } else {23 return {24 actionType: actionTypes.idle25 }26 }27 }28 return {29 target_unit: targetUnit,30 actionType: actionTypes.move,31 direction: directions.away32 }33}34const doNothing = {35 id: 1,36 name: "Idle",37 findNextAction: function () {38 return {39 actionType: actionTypes.idle40 }41 }42}43const justAttack = {44 id: 2,45 name: "Attack closest in-range enemy",46 findNextAction: function (unit: Board, board: Board): Object {47 let closestUnit = _.minBy(board.units.slice().filter(u => u !== unit && u.team !== unit.team),48 u => unit.absoluteDistance(u))49 let currentAction;50 if (closestUnit && (closestUnit.distance(unit) !== distances.outOfRange)) {51 currentAction = {52 target_unit: closestUnit,53 actionType: actionTypes.attack,54 }55 } else {56 currentAction = {57 actionType: actionTypes.idle58 }59 }60 return currentAction61 }62}63function averageCoordinate(units, sourceUnit) {64 let sumX = 0;65 let sumY = 0;66 units.forEach(u => {sumX += u.x ; sumY += u.y})67 return {68 x: sumX / units.length,69 y: sumY / units.length,70 averageCoordinate: true71 }72}73function averageAngle(units, sourceUnit) {74 let sumAngle = 0;75 let sourceCoordinate = {76 x: sourceUnit.x,77 y: sourceUnit.y78 }79 units.forEach(u => {80 sumAngle += Math.atan2(u.y - sourceUnit.y, sourceUnit.x - u.x);81 })82 let angleRadians = sumAngle / units.length83 let one = new Victor(200, 0);84 one.rotate(angleRadians)85 return {86 x: sourceUnit.x - one.x,87 y: sourceUnit.y + one.y,88 averageCoordinate: true89 }90}91function weightedAngle(units, sourceUnit) {92 let sumAngle = 0;93 let sumCoordinate = {94 x: 0, 95 y: 0,96 averageCoordinate: true97 }98 units.forEach(u => {99 let factor = 1.0 / ( new Victor(sourceUnit.x, sourceUnit.y)).distance(new Victor(u.x, u.y))100 sumCoordinate.x += factor * (u.x - sourceUnit.x)101 sumCoordinate.y += factor * (u.y - sourceUnit.y)102 })103 sumCoordinate.x *= 100104 sumCoordinate.y *= 100105 sumCoordinate.x += sourceUnit.x106 sumCoordinate.y += sourceUnit.y107 return sumCoordinate;108}109const fleeFromAll = {110 id: 3,111 name: "Flee from All (Medium then Long)",112 findNextAction: function (unit: Board, board: Board): Object {113 return moveFromAverage(unit, board, averageCoordinate);114 }115}116const fleeFromAverageAngle = {117 id: 6,118 name: "Flee from Average Angle",119 findNextAction: function (unit: Board, board: Board): Object {120 return moveFromAverage(unit, board, averageAngle);121 }122}123const fleeFromWeightedAngle = {124 id: 7,125 name: "Flee from Weighted Angle",126 findNextAction: function (unit: Board, board: Board): Object {127 return moveFromAverage(unit, board, weightedAngle);128 }129}130const justFlee = {131 id: 5,132 name: "Flee closest in-range enemy",133 findNextAction: function (unit: Board, board: Board): Object {134 let closestUnit = _.minBy(board.units.slice().filter(u => u !== unit && u.team !== unit.team),135 u => unit.absoluteDistance(u))136 let currentAction;137 if (closestUnit && closestUnit.distance(unit) !== distances.outOfRange) {138 currentAction = {139 target_unit: closestUnit,140 actionType: actionTypes.move,141 direction: directions.away,142 }143 } else {144 currentAction = {145 actionType: actionTypes.idle146 }147 }148 return currentAction149 }150}151const complexAi = {152 id: 4,153 name: "Complex AI",154 findNextAction: function (unit: Unit, board: Board) {155 const oldAction = unit.currentAction156 let otherUnits = board.units.slice().filter(u => u !== unit && u.team !== unit.team)157 let targetUnitExists = oldAction && oldAction.target_unit && !oldAction.target_unit.dead158 let distanceToTargetUnit = unit.distance(oldAction.target_unit)159 let closestAttackingMe160 let distanceToClosestAttacking161 let unitsAttackingMe = otherUnits.filter(162 u => u.currentAction && u.currentAction.target_unit === unit && u.currentAction.actionType === actionTypes.attack)163 if (unitsAttackingMe.length > 0) {164 closestAttackingMe = _.minBy(unitsAttackingMe, u => u.absoluteDistance(unit))165 distanceToClosestAttacking = unit.distance(closestAttackingMe)166 }167 let closestTarget = _.minBy(otherUnits, u => unit.absoluteDistance(u))168 let closestTargetDistance = unit.distance(closestTarget)169 let currentAction: UnitAction;170 if (distanceToClosestAttacking && distanceToClosestAttacking !== distances.outOfRange && unit.shield / unit.unitType.shield <= 0.25) {171 currentAction = {172 target_unit: closestAttackingMe,173 actionType: actionTypes.move,174 direction: directions.away175 }176 }177 else if (targetUnitExists && oldAction.actionType === actionTypes.attack &&178 distanceToTargetUnit !== distances.outOfRange &&179 (unit.unitType === unitTypes.sniper || unit.unitType === unitTypes.machineGun || distanceToTargetUnit !== distances.long)) {180 if (distanceToTargetUnit === distances.short || distanceToTargetUnit === distances.medium) {181 // Keep attacking selfAttacking if at short/medium182 currentAction = oldAction183 }184 else if ((unit.unitType === unitTypes.sniper || unit.unitType === unitTypes.machineGun) && distanceToTargetUnit === distances.long) {185 currentAction = oldAction186 } else if (closestTargetDistance === distances.short || closestTargetDistance === distances.medium) {187 currentAction = {188 target_unit: closestTarget,189 actionType: actionTypes.attack,190 }191 } else if (distanceToTargetUnit === distances.long) {192 currentAction = oldAction193 }194 } else {195 if (closestTargetDistance === distances.short || closestTargetDistance === distances.medium) {196 currentAction = {197 target_unit: closestTarget,198 actionType: actionTypes.attack,199 }200 } else if ((unit.unitType === unitTypes.sniper || unit.unitType === unitTypes.machineGun) && closestTargetDistance === distances.long) {201 currentAction = {202 target_unit: closestTarget,203 actionType: actionTypes.attack,204 }205 } else {206 currentAction = {207 target_unit: closestTarget,208 actionType: actionTypes.move,209 direction: directions.towards,210 }211 }212 }213 return currentAction214 }215}216export function findAi(id) { 217 return allAis.find(ai => ai.id === id)218}219export const allAis = [ doNothing, justAttack, justFlee, complexAi, fleeFromAll, fleeFromAverageAngle, fleeFromWeightedAngle ]220export const ais = {221 doNothing: 1,222 justAttack: 2,223 justFlee: 5,224 simpleAi: 4,225 fleeFromAll: 3,226 fleeFromAverageAngle: 6,227 fleeFromWeightedAngle: 7228}229// export const ais = {230// doNothing: doNothing,231// justAttack: justAttack,232// justFlee: justFlee,233// simpleAi: complexAi...
queue.ts
Source:queue.ts
...70 // TODO RingBuffer71 this.queue.length > 250 && this.queue.shift();72 if ( !this.waiting ) {73 this.waiting = true;74 this.write( this.findNextAction(), () => {75 this.waiting = false;76 this.execute(this.findNextAction());77 m.redraw();78 });79 }80 }81 /** Write a command on a action characteristic */82 async write ( action: IAction, callback: any ) {83 try {84 Logger.action(this.bolt, action);85 await action.charac.writeValue(action.bytes);86 } catch(error) {87 console.log('Queue.write.error', error.message);88 } finally {89 action.written = true;90 callback();...
actions.js
Source:actions.js
...27$(function () {28 run()29});30function run() {31 var id = findNextAction();32 if (id != undefined) {33 executeAction(id);34 }35}36function findNextAction() {37 return $(".row[state='unchecked']").first().attr('id');38}39function executeAction(id) {40 var start = new Date().getTime();41 $.post('../actions.php', {action: id, url: window.location.pathname}).done(function (status) {42 if(status == 1) {43 status = true;44 } else {45 status = false;46 }47 var time = determineWaitTime(determineElapsedTime(start));48 if (time > 0) {49 setTimeout(function () {50 setStatus(id, status);...
Using AI Code Generation
1import { findNextAction } from '@redwoodjs/api'2import { findNextAction } from '@redwoodjs/router'3import { findNextAction } from '@redwoodjs/router'4import { findNextAction } from '@redwoodjs/api'5import { findNextAction } from '@redwoodjs/router'6import { findNextAction } from '@redwoodjs/api'7import { findNextAction } from '@redwoodjs/router'8import { findNextAction } from '@redwoodjs/api'9import { findNextAction } from '@redwoodjs/router'10import { findNextAction } from '@redwoodjs/api'11import { findNextAction } from '@redwoodjs/router'12import { findNextAction } from '@redwoodjs/api'13import { findNextAction } from '@redwoodjs/router'14import { findNextAction } from '@redwoodjs/api'15import
Using AI Code Generation
1var redwood = require('redwood');2redwood.findNextAction("test", function(err, result) {3 if (err) {4 console.log("Error: " + err);5 } else {6 console.log("Result: " + result);7 }8});9var redwood = require('redwood');10redwood.findNextAction("test", function(err, result) {11 if (err) {12 console.log("Error: " + err);13 } else {14 console.log("Result: " + result);15 }16});17var redwood = require('redwood');18redwood.findNextAction("test", function(err, result) {19 if (err) {20 console.log("Error: " + err);21 } else {22 console.log("Result: " + result);23 }24});25var redwood = require('redwood');26redwood.findNextAction("test", function(err, result) {27 if (err) {28 console.log("Error: " + err);29 } else {30 console.log("Result: " + result);31 }32});33var redwood = require('redwood');34redwood.findNextAction("test", function(err, result) {35 if (err) {36 console.log("Error: " + err);37 } else {38 console.log("Result: " + result);39 }40});41var redwood = require('redwood');42redwood.findNextAction("test", function(err, result) {43 if (err) {44 console.log("Error: " + err);45 } else {46 console.log("Result: " + result);47 }48});49var redwood = require('redwood');50redwood.findNextAction("test", function(err, result) {51 if (err) {52 console.log("Error: " + err);53 } else {54 console.log("Result: " + result);
Using AI Code Generation
1var redwood = require('redwood');2var actions = redwood.actions;3var context = redwood.context;4var findNextAction = redwood.findNextAction;5var nextAction = findNextAction(actions, context);6var redwood = require('redwood');7var actions = redwood.actions;8var context = redwood.context;9var findNextAction = redwood.findNextAction;10var nextAction = findNextAction(actions, context);11var redwood = require('redwood');12var actions = redwood.actions;13var context = redwood.context;14var findNextAction = redwood.findNextAction;15var nextAction = findNextAction(actions, context);16var redwood = require('redwood');17var actions = redwood.actions;18var context = redwood.context;19var findNextAction = redwood.findNextAction;20var nextAction = findNextAction(actions, context);21var redwood = require('redwood');22var actions = redwood.actions;23var context = redwood.context;24var findNextAction = redwood.findNextAction;25var nextAction = findNextAction(actions, context);26var redwood = require('redwood');27var actions = redwood.actions;28var context = redwood.context;29var findNextAction = redwood.findNextAction;30var nextAction = findNextAction(actions, context);31var redwood = require('redwood');32var actions = redwood.actions;33var context = redwood.context;34var findNextAction = redwood.findNextAction;35var nextAction = findNextAction(actions, context);36var redwood = require('redwood');37var actions = redwood.actions;38var context = redwood.context;39var findNextAction = redwood.findNextAction;40var nextAction = findNextAction(actions, context);41var redwood = require('redwood');42var actions = redwood.actions;43var context = redwood.context;44var findNextAction = redwood.findNextAction;45var nextAction = findNextAction(actions
Using AI Code Generation
1var redwood = require("redwood");2var findNextAction = redwood.findNextAction;3var result = findNextAction("test1");4console.log(result);5{ 6 [ { name: 'test2', action: 'test2', type: 'action', next: [Object] },7 { name: 'test3', action: 'test3', type: 'action', next: [Object] } 8}9var redwood = require("redwood");10var findNextAction = redwood.findNextAction;11var result = findNextAction("test2");12console.log(result);13{ 14 [ { name: 'test4', action: 'test4', type: 'action', next: [Object] },15 { name: 'test5', action: 'test5', type: 'action', next: [Object] } 16}17var redwood = require("redwood");18var findNextAction = redwood.findNextAction;19var result = findNextAction("test3");20console.log(result);
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!!