Can't figure out how to make xp bar - discord.js

const bar_fill_start = "<:filledinstart:966733512428384296>"
const bar_fill_end = "<:filledinend:966733512088649759>"
const bar_fill_middle = "<:filledinmiddle:966733512449355796>"
const bar_empty_start = "<:emptystart:966733512457728000>"
const bar_empty_end = "<:emptyend:966733512499691700>"
const bar_empty_middle = "<:emptymiddle:966733512390615041>"
function generateXpBar(xp, level) {
let bar = bar_fill_start + "";
let barLength = 6;
let xpToNextLevel = Levels.xpFor(level);
let xpToNextLevelPercent = xp / xpToNextLevel;
console.log(xpToNextLevelPercent);
for (let i = 0; i < barLength; i++) {
if (i < Math.floor(barLength * xpToNextLevelPercent) - 1) {
bar += bar_fill_middle;
} else {
if(i <= Math.floor(barLength * xpToNextLevelPercent) - 1) {
bar += bar_empty_middle;
}else {
bar += bar_empty_end;
}
}
}
return bar;
}
On audite it looks good. But on kitten it doesn't
what am i doing wrong ?
Can't figure out where is the error I make since it looks good on audite but not on kitten.

if(i <= Math.floor(barLength * xpToNextLevelPercent) - 1) {
to
if(i < barLength - 1) {
after first else

Related

how do I format my response array in angular?

Need to format my array for chart purpose
myArr=[["6709"],["1949"],["87484"],["12760"],["13326"],["3356"],["98000"],["16949"],["29981"],["7879"],["117640"],["30727"],["122071"],["21325"],["210406"],["65824"],["2744807"],["56664"],["382719"],["134578"],["2440528"],["83819"],["1362744"],["450092"],["2461"],["336"],["166446"],["16363"]]
Below Formatted Array
formatArr= [["6709", "1949", "87484", "12760"], ["13326", "3356", "98000", "16949"], ["29981", "7879", "117640", "30727"], ["122071", "21325", "210406", "65824"], ["2744807", "56664", "382719", "134578"] ["2440528", "83819", "1362744", "450092"], ["2461", "336", "166446", "16363"]]
You could reduce it like this for example:
const formatArr: string[][] = myArr.reduce((prev, item, index) => {
if (index % 4 === 0) {
// every fourth item creates a new array with the current item:
prev.push(item);
} else {
// every other item pushes to the previously added item:
prev[prev.length - 1].push(item[0]);
}
return prev;
}, [] as string[][]);
myArr = [["6709"],["1949"],["87484"],["12760"],["13326"],["3356"],["98000"],["16949"],["29981"],["7879"],["117640"],["30727"],["122071"],["21325"],["210406"],["65824"],["2744807"],["56664"],["382719"],["134578"],["2440528"],["83819"],["1362744"],["450092"],["2461"],["336"],["166446"],["16363"]]
formatArr = []
function makeArray(params) {
let element = [];
for (let i = 0; i < myArr.length; i++) {
element.push(myArr[i][0])
if ((i + 1) % 4 == 0) {
formatArr.push(element);
element = [];
}
}
console.log(formatArr);
}
makeArray();
This is correct if myArr has 4n element, if not use this
function makeArray(params) {
let element = [];
for (let i = 0; i < myArr.length; i++) {
element.push(myArr[i][0])
if ((i + 1) % 4 == 0) {
formatArr.push(element);
element = [];
}
if (i == myArr.length - 1 && (i + 1) % 4 != 0) {
formatArr.push(element);
}
}
console.log(formatArr);
}

Write a function which return any number of hexadecimal colors in an array

I stack, I don't know how to create arrays of hexadecimal colors. If You know, please share.
function concatArray(n){
let arr = []
let hashtag = '#'
let semicolon = ','
let r = Math.floor(Math.random() * 16)
let g = Math.floor(Math.random() * 16)
let b = Math.floor(Math.random() * 16)
for(i = 0; i < n; i++){
arr.push(hashtag,r,g,b,semicolon)
}
console.log(arr.join(''))
}
let arrayOfHexaColors = () => {
let allHexCodes = '123456789abcdef';
let loopCount = parseInt(Math.random() * 50);
let hexColorArr = [];
for(let x=1; x<=loopCount; x++) {
let hexChars = [];
for (let i = 0; i < 6; i++) {
hexChars.push(allHexCodes[parseInt(Math.random() * allHexCodes.length) - 1]);
}
hexColorArr.push('#' + hexChars.join(''));
}
return hexColorArr;
}
console.log(arrayOfHexaColors());

Is there any way to replace text in ChromiumBrowser like Chrome already has?

I'm making a web editor application using CefSharp WinForms library, but I couldn't find a way to replace text from CefSharp API.
There is a find method in WebBrowserExtensions but no replace method.
Question 1:
Does anyone know where replacing text method is in CefSharp?
Or there is no way to replace text in CefSharp? If yes, I need to find a detour for it.
Question 2:
There are yellow blocks marked found words when I try Find method, but those blocks are not a part of selection range of window object in HTML. Are those blocks made by native not web browser?
Answer:
I made "find and replace" function by myself using javascript, so if someone tries to do something like me then you can use below codes:
var lastsearchedNodeIndex = -1;
var lastSearchedTextIndex = -1;
var lastRange = null;
function getTextLengthFromStartTo(targetNodeIndex) {
var childNodes = editor.childNodes;
var textLength = 0;
if (targetNodeIndex >= childNodes.length) {
return editor.textContent.length;
}
for (var i = 0; i < targetNodeIndex; i++) {
if (childNodes[i].textContent != null) {
textLength += childNodes[i].textContent.length;
}
}
return textLength;
}
function getCurrentCaretIndex() {
var currentCaretIndex = 0;
var doc = editor.ownerDocument || editor.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(editor);
preCaretRange.setEnd(range.endContainer, range.endOffset);
currentCaretIndex = preCaretRange.toString().length;
}
} else if ( (sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(editor);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
currentCaretIndex = preCaretTextRange.text.length;
}
return currentCaretIndex;
}
function getCurrentNodeIndexAtCaret(caretIndex) {
if (caretIndex == 0) {
return 0;
}
var currentNodeIndex = -1;
for (var i = 0; i < editor.childNodes.length; i++) {
var frontTextLength = getTextLengthFromStartTo(i);
var backTextLength = getTextLengthFromStartTo(i + 1);
if (caretIndex > frontTextLength && caretIndex <= backTextLength) {
currentNodeIndex = i;
break;
}
}
return currentNodeIndex;
}
function getCurrentTextIndexInNodexAtCaret(nodeIndex, caretIndex) {
var textLength = getTextLengthFromStartTo(nodeIndex);
var textIndex = caretIndex - textLength;
return (textIndex < 0) ? 0 : textIndex;
}
function clearSelection() {
if (window.getSelection().rangeCount > 0) {
if (lastRange != null) {
window.getSelection().removeAllRanges();
lastRange.collapse(true);
window.getSelection().addRange(lastRange);
}
}
}
function getTextNodesIn(node) {
var textNodes = [];
if (node.nodeType == 3) {
textNodes.push(node);
} else {
var children = node.childNodes;
for (var i = 0, len = children.length; i < len; ++i) {
textNodes.push.apply(textNodes, getTextNodesIn(children[i]));
}
}
return textNodes;
}
function setSelectionRange(el, start, end) {
if (document.createRange && window.getSelection) {
var range = document.createRange();
range.selectNodeContents(el);
var textNodes = getTextNodesIn(el);
var foundStart = false;
var charCount = 0, endCharCount;
for (var i = 0, textNode; textNode = textNodes[i++]; ) {
endCharCount = charCount + textNode.length;
if (!foundStart && start >= charCount && (start < endCharCount || (start == endCharCount && i <= textNodes.length))) {
range.setStart(textNode, start - charCount);
foundStart = true;
}
if (foundStart && end <= endCharCount) {
range.setEnd(textNode, end - charCount);
break;
}
charCount = endCharCount;
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
lastRange = range;
sel.anchorNode.parentElement.scrollIntoView();
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
}
}
function findText(text, caseSensitive) {
var currentCaretIndex = getCurrentCaretIndex();
clearSelection();
var regex;
if (caseSensitive) {
regex = text;
} else {
regex = new RegExp(text, "gi");
}
var childNodes = editor.childNodes;
var startNodeIndex = getCurrentNodeIndexAtCaret(currentCaretIndex);
var endNodeIndex = childNodes.length;
var startTextIndex = 0;
if (window.getSelection().focusOffset == 1) {
startTextIndex = lastSearchedTextIndex + 1;
} else {
startTextIndex = getCurrentTextIndexInNodexAtCaret(startNodeIndex, currentCaretIndex);
}
var searchedTextIndex = -1;
var searchedNodeIndex = 0;
var searchTargetSentence = null;
var searchLoopCount = 0;
if (currentCaretIndex == editor.textContent.length) {
startNodeIndex = 0;
startTextIndex = 0;
}
do
{
for (var i = startNodeIndex; i < endNodeIndex; i++) {
if (typeof (childNodes[i].textContent) == undefined || childNodes[i].textContent == null) {
startTextIndex = 0;
continue;
}
if (startTextIndex == childNodes[i].textContent.length) {
startTextIndex = 0;
continue;
}
if (startTextIndex > 0) {
searchTargetSentence = childNodes[i].textContent.substring(startTextIndex, childNodes[i].textContent.length);
} else {
searchTargetSentence = childNodes[i].textContent;
}
searchedTextIndex = searchTargetSentence.search(regex);
if (searchedTextIndex > -1) {
searchedTextIndex += startTextIndex;
searchedNodeIndex = i;
break;
}
startTextIndex = 0;
}
if (searchedTextIndex == -1) {
endNodeIndex = startNodeIndex + 1;
startNodeIndex = 0;
searchLoopCount++;
}
} while (searchLoopCount < 2 && searchedTextIndex == -1);
lastsearchedNodeIndex = searchedNodeIndex;
lastSearchedTextIndex = searchedTextIndex;
if (searchedNodeIndex > -1 && searchedTextIndex > -1) {
var textStartIndex = getTextLengthFromStartTo(searchedNodeIndex) + searchedTextIndex;
setSelectionRange(editor, textStartIndex, textStartIndex + text.length);
return true;
} else {
return false;
}
}
function replaceText(textToFind, textToReplace, caseSensitive) {
if (findText(textToFind, caseSensitive) == true) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(textToReplace));
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.text = textToReplace;
}
return true;
} else {
return false;
}
}
There are no replace text method in CefSharp.
I think you have two options
Implement in javascript/html in the browser (DIY or something
like Tiny)
Manipulating the html from C# using GetSourceAsync and LoadHtml (Documentation)
Second question - I think you may be able to at least read the hits using the FindHandler. Have not tested this myself.

Scrolling through an array

So I have a project in GameMaker, which has a chatbox. The messages for this are stored in an array. I would like to be able to scroll through this array, so I can view earlier chat messages.
This is what I currently have:
Create Event
chatLog[0] = "";
chatIndex = 0;
Step Event
if (chatIndex > 0) {
if (mouse_wheel_down()) {
chatIndex--;
}
}
if (chatIndex < array_length_1d(chatLog) - 1) {
if (mouse_wheel_up()) {
chatIndex++;
}
}
var _maxLines = 5;
for (i = 0; i < _maxLines; i++) {
if (i > (array_length_1d(chatLog) - 1)) { exit; }
var _chatLength = array_length_1d(chatLog) - 1;
draw_text(0, 50 - chatHeight, chatLog[_chatLength - i + chatIndex]);
}
First, for convenience of being able to add messages to front / remove them from the back (once there are too many), let's suppose that the log is a list, item 0 being the newest message,
chatLog = ds_list_create();
chatIndex = 0;
for (var i = 1; i <= 15; i++) {
ds_list_insert(chatLog, 0, "message " + string(i));
}
then, the Step Draw event can use information from the list to clamp scroll offset range and draw items:
var maxLines = 5;
// scrolling:
var dz = (mouse_wheel_up() - mouse_wheel_down()) * 3;
if (dz != 0) {
chatIndex = clamp(chatIndex + dz, 0, ds_list_size(chatLog) - maxLines);
}
// drawing:
var i = chatIndex;
var _x = 40;
var _y = 200;
repeat (maxLines) {
var m = chatLog[|i++];
if (m == undefined) break; // reached the end of the list
draw_text(_x, _y, m);
_y -= string_height(m); // draw the next item above the current one
}
live demo

ActionScript 3 Array Return Runtime Error

All right so I have the following code and all it does is put 3 solid-colour squares on the screen and one rainbow-coloured one in the bottom-right corner. When the user presses on any of the solid-coloured squares, that spot get filled with the rainbow-coloured one and in the original location of the rainbow goes that square that was clicked. The code works almost perfectly except for one thing. When the user tries to click on a square that is UNDER the rainbow square, it returns a run-time error.
My Code:
i
mport flash.display.DisplayObject;
import flash.ui.Mouse;
var t1:DisplayObject = new mc_1;
var t2:DisplayObject = new mc_2;
var t3:DisplayObject = new mc_3;
var t4:DisplayObject = new mc_4;
var tile:Array = [[t1,t2],[t3,t4]];
var r:int;
var c:int;
var a:int = 50;
var b:int = 50;
var aa:int = 1;
var bb:int = 1;
function reDraw() {
a = 50;
b = 50;
for (r=0;r<2;r++) {
for (c=0;c<2;c++) {
tile[r][c].x = a;
tile[r][c].y = b;
trace(tile[r][c]);
stage.addChild(tile[r][c]);
tile[r][c].addEventListener(MouseEvent.CLICK, go);
a += 100;
}
a = 50;
b += 100;
}
}
reDraw();
function go(e:MouseEvent):void {
trace(e.target);
//Right:
if (e.target == tile[aa][bb+1]) {
tile[aa][bb] = e.target;
bb += 1;
tile[aa][bb] = t4;
reDraw();
trace("Right");
}
//Left:
else if (e.target == tile[aa][bb-1]) {
tile[aa][bb] = e.target;
bb -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Left");
}
//Up:
else if (e.target == tile[aa-1][bb]) {
tile[aa][bb] = e.target;
aa -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Up");
}
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
else trace("FAILED!");
trace(aa +" " + bb);
}
The error:
TypeError: Error #1010: A term is undefined and has no properties. at
win_fla::MainTimeline/go()
If you take a look at your code you have this:
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
now you can see here that its looking for tile[aa+1] however aa = 1 in the beginning so aa+1 = 2 and tile[2] doesn't exist or is undefined. You'll need to change your logic there to something like:
var tileFound:Boolean = false;
for(var i:int = 0; i < 2; i++){
for(var j:int = 0; j < 2; j++){
if(tile[i][j] == e.target){
tileFound = true;
tile[aa][bb] = e.target;
tile[i][j] = t4;
if(i > aa) trace ("Right");
else if(i < aa) trace ("Left");
if(j > bb) trace ("Bottom");
else if(j < bb) trace ("Top");
aa = i;
bb = j;
reDraw();
tileFound = true;
break;
}
}
if(tileFound) break;
}

Resources