Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilities for JSON String Array #445

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

skenkerstel
Copy link

Utilities for JSON String Array

What I did

I added 4 functions to handle JSON String Array.

  • splitStringByDelimiter
    Takes two arguments, "string", and "delimiter". Split a string by a delimiter.
    It will greatly help people make a command line.

Use cases

// input
string = "/givePermission skenkerstel simon";
delimiter = " ";

// output
["/givePermission", "skenkerstel", "simon"]

You can extract each element by "getStringArrayElement" function later.

  • joinStringArrayByDelimiter
    Takes two arguments, "string" and "delimiter". Join the JSON String Array by the delimiter. It will still join when it is a JSON Object.

Use Cases

// input
string = '["m0dE", "AetherWindZ", "sloont"]';
delimiter = ",";

// output
m0dE,AetherWindZ,sloont

It will help make an ad-hoc CSV list or destruct the JSON String Array to a readable format.
For example, if you want to show the list of contributors stored in a JSON String Array, you can use it effectively.
Moreover, if you want to look at the content of a JSON Array with high readability, it will help a lot.

  • sortStringArray
    Takes two arguments, "string", and "mode". Sort the JSON String Array according to the mode. It will still return a sorted array as long as it is a JSON object.

There are four modes. Here's the list of how an array will be converted.

  1. alphabetical (["f", "a", "c", "t"] → ["a", "c", "f", "t"])
  2. alphabeticalReverse (["f", "a", "c", "t"] → ["t", "f", "c", "a"])
  3. numericalAscending ([6, 9, 4, 2, 0] → [0, 2, 4, 6, 9])
  4. numericalDescending ([6, 9, 4, 2, 0] → [9, 6, 4, 2, 0])

Use Cases
You will need a sort algorithm to decide the winners, thresholds, and so on. In such cases, it will save time to implement a sort algorithm on the taro engine. Moreover, it will work faster than hand-made ones because of lower overhead.

// input
string = ["100", "59", "24", "115", "502", "694", "582"];
mode = "numericalDescending"

// output
["694", "582", "502", "115", "100", "59", "24"]
  • sortTwoStringArrays
    Takes three arguments, "string", "string2", and "mode". Sort both string1 and string2 according to the mode and string, and return the sorted string2.## Utilities for JSON String Array

What I did

I added 4 functions to handle JSON String Array.

  • splitStringByDelimiter
    Takes two arguments, "string", and "delimiter". Split a string by a delimiter.
    It will greatly help people make a command line.

Use cases

// input
string = "/givePermission skenkerstel simon";
delimiter = " ";

// output
["/givePermission", "skenkerstel", "simon"]

You can extract each element by "getStringArrayElement" function later.

  • joinStringArrayByDelimiter
    Takes two arguments, "string" and "delimiter". Join the JSON String Array by the delimiter. It will still join when it is a JSON Object.

Use Cases

// input
string = '["m0dE", "AetherWindZ", "sloont"]';
delimiter = ",";

// output
m0dE,AetherWindZ,sloont

It will help make an ad-hoc CSV list or destruct the JSON String Array to a readable format.
For example, if you want to show the list of contributors stored in a JSON String Array, you can use it effectively.
Moreover, if you want to look at the content of a JSON Array with high readability, it will help a lot.

  • sortStringArray
    Takes two arguments, "string", and "mode". Sort the JSON String Array according to the mode. It will still return a sorted array as long as it is a JSON object.

There are four modes. Here's the list of how an array will be converted.

  1. alphabetical (["f", "a", "c", "t"] → ["a", "c", "f", "t"])
  2. alphabeticalReverse (["f", "a", "c", "t"] → ["t", "f", "c", "a"])
  3. numericalAscending ([6, 9, 4, 2, 0] → [0, 2, 4, 6, 9])
  4. numericalDescending ([6, 9, 4, 2, 0] → [9, 6, 4, 2, 0])

Use Cases
You will need a sort algorithm to decide the winners, thresholds, and so on. In such cases, it will save time to implement a sort algorithm on the taro engine. Moreover, it will work faster than hand-made ones because of lower overhead.

// input
string = ["100", "59", "24", "115", "502", "694", "582"];
mode = "numericalDescending"

// output
["694", "582", "502", "115", "100", "59", "24"]
  • sortTwoStringArrays
    Takes three arguments, "string", "string2", and "mode". Sort both string1 and string2 according to the mode and string, and return the sorted string2.
    (Sort string1 according to the mode and return string2 based on the sorted order)

Use Cases
You can obtain the rank of each player in a game with this function as follows:

// input
string2 = ["a", "b", "c", "d", "e", "f", "g"]; // each player's ID (or name)
string = ["100", "59", "24", "115", "502", "694", "582"]; // each player's score
mode = "numericalDescending";

// output
["f","g","e","d","a","b","c"]

After this function, you can use "get player from ID" function to access each player to reward them.

(Sort string1 according to the mode and return string2 based on the sorted order)

Use Cases
You can obtain the rank of each player in a game with this function as follows:

// input
string2 = ["a", "b", "c", "d", "e", "f", "g"]; // each player's ID (or name)
string = ["100", "59", "24", "115", "502", "694", "582"]; // each player's score
mode = "numericalDescending";

// output
["f","g","e","d","a","b","c"]

After this function, you can use "get player from ID" function to access each player to reward them.

@skenkerstel skenkerstel changed the title Splitter Utilities for JSON String Array Jun 19, 2023
src/gameClasses/components/script/VariableComponent.js Outdated Show resolved Hide resolved
var string = self.getValue(text.string, vars);
var delimiter = self.getValue(text.delimiter, vars);
var strArr;
if (string && delimiter != undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be the same as (string && delimiter)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it so.

returnValue = JSON.stringify(strArr);
} catch (err) {
console.error(err);
returnValue = "undefined";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"undefined" as a string seems strange. also returnValue is undefined if never set. see the beginning of VariableComponent.getValue(...)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

strArr = string.split(delimiter);
returnValue = JSON.stringify(strArr);
} catch (err) {
console.error(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to not include console messages in production. should use the built in script log that keeps track of script errors while the game is running

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced console.error() with taro.script.errorLog() .

Copy link
Author

@skenkerstel skenkerstel Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, other conventional StringArray functions have the same problem. Shall I fix them for you, as it will not change engine specifications?

if (Array.isArray(array) == true) {
// If the string passed is a real array
returnValue = array.join(delimiter);
} else if (array !== null && typeof array === 'object') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like this behaviour is contrary to what joinStringArrayByDelimiter should be doing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more specifically: if it's not an array, this case should not be handling it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it.

Long explanation Actually, you can read a JSON object with getStringArrayElement by configuring index parameters directly from the raw view. Some games (including mine) are using that feature and it's useful for advanced games (I wish this specification does not change later 🥺 ), so I wanted to list all values for JSON objects. But yes, probably I shouldn't **bother to add** the feature for JSON objects in this function, so I removed it.

}
break;

case 'sortStringArray':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetical and numerical can be merged into the same sort.

there should be two separate cases here. one for asc. one for dec

Copy link
Author

@skenkerstel skenkerstel Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that there should be two cases ( = functions) for this part, one for ascending, and the other for descending, and I should just merge alphabetical and numerical sort? I thought because they work a bit differently, it was better that we had both.
I fixed the behaviors of alphabetical sorts as it wasn't working as I intended.
If you want to integrate numerical and alphabetical anyways, please tell me which to adopt here (I mean, which to remove, numerical or alphabetical)

}
break;

case 'sortTwoStringArrays':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to just have this case merge two string arrays and then use the sort case after. and perhaps merge two string arrays could just be using concatenate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looking a bit weird, this function will be very useful to sort things (such as player name or ID) based on their scores... I think it's not possible only with "sortStringArray" function (note that some player name consists of only numbers). As for the array concatenation, I made a new one, "concatenateTwoStringArrays".

case 'sortStringArray':
var string = self.getValue(text.string, vars);
var mode = self.getValue(text.mode, vars);
var returnValue = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to redefine returnValue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it.

Copy link
Author

@skenkerstel skenkerstel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your quick review. I worked on some points, but still I have several topics to discuss.

returnValue = JSON.stringify(strArr);
} catch (err) {
console.error(err);
returnValue = "undefined";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

strArr = string.split(delimiter);
returnValue = JSON.stringify(strArr);
} catch (err) {
console.error(err);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced console.error() with taro.script.errorLog() .

case 'sortStringArray':
var string = self.getValue(text.string, vars);
var mode = self.getValue(text.mode, vars);
var returnValue = undefined;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it.

var string = self.getValue(text.string, vars);
var delimiter = self.getValue(text.delimiter, vars);
var strArr;
if (string && delimiter != undefined) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it so.

}
break;

case 'sortStringArray':
Copy link
Author

@skenkerstel skenkerstel Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that there should be two cases ( = functions) for this part, one for ascending, and the other for descending, and I should just merge alphabetical and numerical sort? I thought because they work a bit differently, it was better that we had both.
I fixed the behaviors of alphabetical sorts as it wasn't working as I intended.
If you want to integrate numerical and alphabetical anyways, please tell me which to adopt here (I mean, which to remove, numerical or alphabetical)

}
break;

case 'sortTwoStringArrays':
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looking a bit weird, this function will be very useful to sort things (such as player name or ID) based on their scores... I think it's not possible only with "sortStringArray" function (note that some player name consists of only numbers). As for the array concatenation, I made a new one, "concatenateTwoStringArrays".

if (Array.isArray(array) == true) {
// If the string passed is a real array
returnValue = array.join(delimiter);
} else if (array !== null && typeof array === 'object') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it.

Long explanation Actually, you can read a JSON object with getStringArrayElement by configuring index parameters directly from the raw view. Some games (including mine) are using that feature and it's useful for advanced games (I wish this specification does not change later 🥺 ), so I wanted to list all values for JSON objects. But yes, probably I shouldn't **bother to add** the feature for JSON objects in this function, so I removed it.

@skenkerstel skenkerstel requested a review from sloont June 23, 2023 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants