Best JavaScript code snippet using root
generator.ts
Source:generator.ts
1import dotenv from 'dotenv';2import * as fs from 'fs';3import path from 'path';4import * as constants from './constants';5import { createImage } from './draw';6import { getGatewayUrlByToken } from './nftStorage';7import { background, earings, eyes, eyewears, face, hair, lips, nose, Trait } from './traits';8import { NftTobe, TraitTarget } from './types/common';9dotenv.config()10const TARGET_NUM_OF_NFT = 10011const MAX_NUM_OF_FACE_RARITY = 1012const MAX_NUM_OF_HAIR_RARITY = 1013const MAX_NUM_OF_EYES_RARITY = 1014const MAX_NUM_OF_NOSE_RARITY = 1015const MAX_NUM_OF_LIPS_RARITY = 2016const MAX_NUM_OF_EYEWEARS_RARITY = 4017const MAX_NUM_OF_EARINGS_RARITY = 4018const MAX_NUM_OF_BG_RARITY = 10019const ALSs: NftTobe[] = []20let totalFaceRareTraits = 021let totalHairRareTraits = 022let totalEyesRareTraits = 023let totalNoseRareTraits = 024let totalLipsRareTraits = 025let totalEyewearsRareTraits = 026let totalEaringsRareTraits = 027let totalBgRareTraits = 028const FILE_PATH = constants.FILE_PATH29const __dirname = path.resolve()30const basePath = `${__dirname}/${FILE_PATH}`31/**32 * 33 * @param limit 34 * @returns the random index below the limit number35 */36const randIndexBelow = (limit: number): number => Math.floor(Math.random() * limit)37/**38 * 39 * @param traitTarget 40 * @param trait 41 * @returns final NFT tobe42 */43const refineRarity = (traitTarget: TraitTarget, trait: Trait): Trait => {44 if (ALSs.length > 0 && trait.rare) {45 46 switch (traitTarget) {47 case TraitTarget.Face: 48 totalFaceRareTraits += 149 if (totalFaceRareTraits > MAX_NUM_OF_FACE_RARITY) {50 totalFaceRareTraits-= 151 return refineRarity(traitTarget, face[randIndexBelow(face.length)])52 } else return trait53 case TraitTarget.Hair: 54 totalHairRareTraits += 155 if (totalHairRareTraits > MAX_NUM_OF_HAIR_RARITY) {56 totalHairRareTraits-= 157 return refineRarity(traitTarget, hair[randIndexBelow(hair.length)])58 } else return trait59 // refine(hair, totalHairRareTraits, MAX_NUM_OF_HAIR_RARITY)60 case TraitTarget.Eyes: 61 totalEyesRareTraits += 162 if (totalEyesRareTraits > MAX_NUM_OF_EYES_RARITY) {63 totalEyesRareTraits-= 164 return refineRarity(traitTarget, eyes[randIndexBelow(eyes.length)])65 } else return trait66 // refine(eyes, totalEyesRareTraits, MAX_NUM_OF_EYES_RARITY)67 case TraitTarget.Nose: 68 totalNoseRareTraits += 169 if (totalNoseRareTraits > MAX_NUM_OF_NOSE_RARITY) {70 totalNoseRareTraits-= 171 return refineRarity(traitTarget, nose[randIndexBelow(nose.length)])72 } else return trait73 // refine(nose, totalNoseRareTraits, MAX_NUM_OF_NOSE_RARITY)74 case TraitTarget.Lips: 75 totalLipsRareTraits += 176 if (totalLipsRareTraits > MAX_NUM_OF_LIPS_RARITY) {77 totalLipsRareTraits-= 178 return refineRarity(traitTarget, lips[randIndexBelow(lips.length)])79 } else return trait80 // refine(lips, totalLipsRareTraits, MAX_NUM_OF_LIPS_RARITY)81 case TraitTarget.Eyewears: 82 totalEyewearsRareTraits += 183 if (totalEyewearsRareTraits > MAX_NUM_OF_EYEWEARS_RARITY) {84 totalEyewearsRareTraits-= 185 return refineRarity(traitTarget, eyewears[randIndexBelow(eyewears.length)])86 } else return trait87 // refine(eyewears, totalEyewearsRareTraits, MAX_NUM_OF_EYEWEARS_RARITY)88 case TraitTarget.Earings: 89 totalEaringsRareTraits += 190 if (totalEaringsRareTraits > MAX_NUM_OF_EARINGS_RARITY) {91 totalEaringsRareTraits-= 192 return refineRarity(traitTarget, earings[randIndexBelow(earings.length)])93 } else return trait94 case TraitTarget.Background: 95 totalBgRareTraits += 196 if (totalBgRareTraits > MAX_NUM_OF_BG_RARITY) {97 totalBgRareTraits-= 198 return refineRarity(traitTarget, background[randIndexBelow(background.length)])99 } else return trait100 default:101 return trait102 }103 }104 105 return trait106}107/**108 * 109 * @param ALSs 110 * @returns a ALS token generated111 */112const generateALS = (ALSs: NftTobe[]): NftTobe | null => {113 const nftTobe: NftTobe = []114 115 nftTobe.push(refineRarity(TraitTarget.Face, face[randIndexBelow(face.length)]).id)116 nftTobe.push(refineRarity(TraitTarget.Hair, hair[randIndexBelow(hair.length)]).id)117 nftTobe.push(refineRarity(TraitTarget.Eyes, eyes[randIndexBelow(eyes.length)]).id)118 nftTobe.push(refineRarity(TraitTarget.Nose, nose[randIndexBelow(nose.length)]).id)119 nftTobe.push(refineRarity(TraitTarget.Lips, lips[randIndexBelow(lips.length)]).id)120 nftTobe.push(refineRarity(TraitTarget.Eyewears, eyewears[randIndexBelow(eyewears.length)]).id)121 nftTobe.push(refineRarity(TraitTarget.Earings, earings[randIndexBelow(earings.length)]).id)122 nftTobe.push(refineRarity(TraitTarget.Background, background[randIndexBelow(background.length)]).id)123 124 // remove redundancy125 const isRedundant = ALSs.some((item: NftTobe) => JSON.stringify(item) === JSON.stringify(nftTobe))126 return isRedundant? null : nftTobe127}128/**129 * generates 500 ALS tokens130 */131const generateALSs = () => {132 while (ALSs.length < TARGET_NUM_OF_NFT) {133 const newALS = generateALS(ALSs)134 if (newALS !== null) {135 ALSs.push(newALS)136 }137 }138}139const createALSImages = async() => {140 console.log('Creating images...')141 // forEach's callback cannot be used with async await142 for (let i = 0; i < ALSs.length; i += 1) {143 console.log(`Creating an image of ALS-${i + 1}`)144 await createImage(ALSs[i], i)145 }146}147/**148 * convert ipfs uris to https gateway urls in meta.txt file to fetch via web later149 * to see flags available, check out https://nodejs.org/api/fs.html#file-system-flags150 */151const convertMetafileToGatewayUrl = async () => {152 console.log('convertMetafileToGatewayUrl')153 for (let i = 0; i < ALSs.length; i += 1) {154 const gatewayUrl = await getGatewayUrlByToken(i + 1, `${basePath}/${constants.META_FILE_NAME}.txt`)155 const fileName = `${constants.META_FILE_NAME}.href.txt`156 fs.writeFileSync(`${basePath}/${fileName}`, `${gatewayUrl}\r\n`, { flag: 'a+' })157 // a+ flag opens file for reading and appending. The file is created if it does not exist.158 }159}160const start = async () => {161 generateALSs()162 console.log(`TOTAL_NUM_OF_NFT = ${ALSs.length}`)163 console.log(`TOTAL_NUM_OF_FACE_RARITY = ${totalFaceRareTraits}`)164 console.log(`TOTAL_NUM_OF_HAIR_RARITY = ${totalHairRareTraits}`)165 console.log(`TOTAL_NUM_OF_EYES_RARITY = ${totalEyesRareTraits}`)166 console.log(`TOTAL_NUM_OF_NOSE_RARITY = ${totalNoseRareTraits}`)167 console.log(`TOTAL_NUM_OF_LIPS_RARITY = ${totalLipsRareTraits}`)168 console.log(`TOTAL_NUM_OF_EYEWEARS_RARITY = ${totalEyewearsRareTraits}`)169 console.log(`TOTAL_NUM_OF_EARINGS_RARITY = ${totalEaringsRareTraits}`)170 console.log(`TOTAL_NUM_OF_BG_RARITY = ${totalBgRareTraits}`)171 172 await createALSImages()173 await convertMetafileToGatewayUrl()174}...
Koopa.js
Source:Koopa.js
1import Entity from '../Entity.js';2import Trait from '../Trait.js';3import Killable from '../traits/Killable.js';4import PendulumMove from '../traits/PendulumMove.js';5import Physics from '../traits/Physics.js';6import Solid from '../traits/Solid.js';7import Stomper from '../traits/Stomper.js';8import {loadSpriteSheet} from '../loaders/sprite.js';9export function loadKoopaGreen() {10 return loadSpriteSheet('koopa-green')11 .then(createKoopaFactory);12}13export function loadKoopaBlue() {14 return loadSpriteSheet('koopa-blue')15 .then(createKoopaFactory);16 }17const STATE_WALKING = Symbol('walking');18const STATE_HIDING = Symbol('hiding');19const STATE_PANIC = Symbol('panic');20class Behavior extends Trait {21 constructor() {22 super();23 this.hideTime = 0;24 this.hideDuration = 5;25 this.walkSpeed = null;26 this.panicSpeed = 300;27 this.state = STATE_WALKING;28 }29 collides(us, them) {30 if (us.traits.get(Killable).dead) {31 return;32 }33 if (them.traits.has(Stomper)) {34 if (them.vel.y > us.vel.y) {35 this.handleStomp(us, them);36 } else {37 this.handleNudge(us, them);38 }39 }40 }41 handleNudge(us, them) {42 if (this.state === STATE_WALKING) {43 them.traits.get(Killable).kill();44 } else if (this.state === STATE_HIDING) {45 this.panic(us, them);46 } else if (this.state === STATE_PANIC) {47 const travelDir = Math.sign(us.vel.x);48 const impactDir = Math.sign(us.pos.x - them.pos.x);49 if (travelDir !== 0 && travelDir !== impactDir) {50 them.traits.get(Killable).kill();51 }52 }53 }54 handleStomp(us, them) {55 if (this.state === STATE_WALKING) {56 this.hide(us);57 } else if (this.state === STATE_HIDING) {58 us.traits.get(Killable).kill();59 us.vel.set(100, -200);60 us.traits.get(Solid).obstructs = false;61 } else if (this.state === STATE_PANIC) {62 this.hide(us);63 }64 }65 hide(us) {66 us.vel.x = 0;67 us.traits.get(PendulumMove).enabled = false;68 if (this.walkSpeed === null) {69 this.walkSpeed = us.traits.get(PendulumMove).speed;70 }71 this.hideTime = 0;72 this.state = STATE_HIDING73 }74 unhide(us) {75 us.traits.get(PendulumMove).enabled = true;76 us.traits.get(PendulumMove).speed = this.walkSpeed;77 this.state = STATE_WALKING;78 }79 panic(us, them) {80 us.traits.get(PendulumMove).enabled = true;81 us.traits.get(PendulumMove).speed = this.panicSpeed * Math.sign(them.vel.x);82 this.state = STATE_PANIC;83 }84 update(us, gameContext) {85 const deltaTime = gameContext.deltaTime;86 if (this.state === STATE_HIDING) {87 this.hideTime += deltaTime;88 if (this.hideTime > this.hideDuration) {89 this.unhide(us);90 }91 }92 }93}94function createKoopaFactory(sprite) {95 const walkAnim = sprite.animations.get('walk');96 const wakeAnim = sprite.animations.get('wake');97 function routeAnim(koopa) {98 if (koopa.traits.get(Behavior).state === STATE_HIDING) {99 if (koopa.traits.get(Behavior).hideTime > 3) {100 return wakeAnim(koopa.traits.get(Behavior).hideTime);101 }102 return 'hiding';103 }104 if (koopa.traits.get(Behavior).state === STATE_PANIC) {105 return 'hiding';106 }107 return walkAnim(koopa.lifetime);108 }109 function drawKoopa(context) {110 sprite.draw(routeAnim(this), context, 0, 0, this.vel.x < 0);111 }112 return function createKoopa() {113 const koopa = new Entity();114 koopa.size.set(16, 16);115 koopa.offset.y = 8;116 koopa.addTrait(new Physics());117 koopa.addTrait(new Solid());118 koopa.addTrait(new PendulumMove());119 koopa.addTrait(new Killable());120 koopa.addTrait(new Behavior());121 koopa.draw = drawKoopa;122 return koopa;123 };...
device.types.ts
Source:device.types.ts
1export interface IDeviceType {2 code: string;3 name: string;4 attributes?: { [key: string]: string };5 traits: string[];6}7export const deviceTypes: IDeviceType[] = [8 {9 code: 'action.devices.types.CAMERA',10 name: 'Camera',11 attributes: {12 cameraStreamSupportedProtocols: 'array',13 cameraStreamNeedAuthToken: 'boolean',14 cameraStreamNeedDrmEncryption: 'boolean'15 },16 traits: ['action.devices.traits.CameraStream']17 // cameraStreamSupportedProtocols: ['hls', 'dash'],18 // cameraStreamNeedAuthToken: true,19 // cameraStreamNeedDrmEncryption: false20 },21 {22 code: 'action.devices.types.THERMOSTAT',23 name: 'Thermostat',24 traits: ['action.devices.traits.TemperatureSetting']25 // availableThermostatModes: 'off,heat,cool,on',26 // thermostatTemperatureUnit: 'F'27 },28 {29 code: 'action.devices.types.SWITCH',30 name: 'Switch',31 traits: ['action.devices.traits.OnOff']32 },33 {34 code: 'action.devices.types.SPEAKER',35 name: 'Speaker',36 traits: ['action.devices.traits.Volume']37 },38 {39 code: 'action.devices.types.LIGHT',40 name: 'Light',41 traits: ['action.devices.traits.OnOff']42 },43 {44 code: 'action.devices.types.FAN',45 name: 'Fan',46 traits: ['action.devices.traits.OnOff']47 },48 {49 code: 'action.devices.types.MICROWAVE',50 name: 'Microwave',51 traits: ['action.devices.traits.OnOff']52 },53 {54 code: 'action.devices.types.COFFEE_MAKER',55 name: 'Coffee Maker',56 traits: ['action.devices.traits.OnOff']57 },58 {59 code: 'action.devices.types.AC_UNIT',60 name: 'AC Unit',61 traits: []62 }63];64// COFFEE_MAKER65export const deviceTraits: string[] = [66 'action.devices.traits.CameraStream',67 'action.devices.traits.TemperatureSetting',68 'action.devices.traits.OnOff',69 'action.devices.traits.Volume',70 'action.devices.traits.FanSpeed'...
Using AI Code Generation
1var Trait = require('traits');2var t = Trait({3 method1: function() {4 console.log('method1');5 },6 method2: function() {7 console.log('method2');8 }9});10var o = Object.create(Object.prototype, t);11o.method1();12o.method2();13var Trait = require('traits');14var t = Trait({15 method1: function() {16 console.log('method1');17 },18 method2: function() {19 console.log('method2');20 }21});22var o = Object.create(Object.prototype, Trait(t));23o.method1();24o.method2();25> var Trait = require('traits');26> var t = Trait({27> method1: function() {28> console.log('method1');29> },30> method2: function() {31> console.log('method2');32> }33> });34> var o = Object.create(Object.prototype, t);35> o.method1();36> o.method2();37> var Trait = require('traits');38> var t = Trait({39> method1: function() {40> console.log('method1');41> },42> method2: function() {43> console.log('method2
Using AI Code Generation
1var root = require('./root.js');2root.traits();3var root2 = require('./root2.js');4root2.traits();5var root = function(){};6root.prototype.traits = function(){7 console.log('traits method of root');8}9module.exports = new root();10var root2 = function(){};11root2.prototype.traits = function(){12 console.log('traits method of root2');13}14module.exports = new root2();
Using AI Code Generation
1var root = require('root');2var obj = root.traits({a:1, b:2});3console.log(obj.a);4console.log(obj.b);5var root = require('root');6var obj = root.traits({a:1, b:2});7console.log(obj.a);8console.log(obj.b);9var root = require('root');10var obj = root.traits({a:1, b:2});11console.log(obj.a);12console.log(obj.b);13var root = require('root');14var obj = root.traits({a:1, b:2});15console.log(obj.a);16console.log(obj.b);17var root = require('root');18var obj = root.traits({a:1, b:2});19console.log(obj.a);20console.log(obj.b);21var root = require('root');22var obj = root.traits({a:1, b:2});23console.log(obj.a);24console.log(obj.b);25var root = require('root');26var obj = root.traits({a:1, b:2});27console.log(obj.a);28console.log(obj.b);29var root = require('root');30var obj = root.traits({a:1, b:2});31console.log(obj.a);32console.log(obj.b);33var root = require('root');34var obj = root.traits({a:1, b:2});35console.log(obj.a);36console.log(obj.b);37var root = require('root');38var obj = root.traits({a:1, b:2});39console.log(obj.a);40console.log(obj.b);41var root = require('root');42var obj = root.traits({a:1
Using AI Code Generation
1var root = require('root');2root.traits('test');3var test = root.test;4var testObj = new test();5var root = require('root');6root.traits('test');7var test = root.test;8var testObj = new test();9var root = require('root');10root.traits('test');11var test = root.test;12var testObj = new test();13var root = require('root');14root.traits('test');15var test = root.test;16var testObj = new test();17var root = require('root');18root.traits('test');19var test = root.test;20var testObj = new test();21var root = require('root');22root.traits('test');23var test = root.test;24var testObj = new test();25var root = require('root');26root.traits('test');27var test = root.test;28var testObj = new test();29var root = require('root');30root.traits('test');31var test = root.test;32var testObj = new test();33var root = require('root');34root.traits('test');35var test = root.test;36var testObj = new test();37var root = require('root');38root.traits('test');39var test = root.test;40var testObj = new test();41var root = require('root');42root.traits('test');43var test = root.test;44var testObj = new test();45var root = require('root');46root.traits('test');47var test = root.test;48var testObj = new test();49var root = require('root');50root.traits('test');51var test = root.test;52var testObj = new test();53var root = require('root');54root.traits('test');55var test = root.test;56var testObj = new test();
Using AI Code Generation
1var root = require('root');2var newObject = root.traits.create({name: 'newObject'});3var newObject2 = newObject.traits.create({name: 'newObject2'});4var newObject3 = newObject2.traits.create({name: 'newObject3'});5newObject3.traits.add('foo', function() {6 console.log('foo');7});8newObject3.foo();9newObject2.foo();10newObject.foo();11root.foo();12var newObject4 = newObject2.traits.create({name: 'newObject4'});13newObject4.foo();14var newObject5 = newObject2.traits.create({name: 'newObject5'});15newObject5.foo();16var newObject6 = newObject2.traits.create({name: 'newObject6'});17newObject6.foo();18var newObject7 = newObject2.traits.create({name: 'newObject7'});19newObject7.foo();20var newObject8 = newObject2.traits.create({name: 'newObject8'});21newObject8.foo();22var newObject9 = newObject2.traits.create({name: 'newObject9'});23newObject9.foo();24var newObject10 = newObject2.traits.create({name: 'newObject10'});25newObject10.foo();26var newObject11 = newObject2.traits.create({name: 'newObject11'});27newObject11.foo();28var newObject12 = newObject2.traits.create({name: 'newObject12'});29newObject12.foo();
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!!