State-Driven Skill Prioritization

I know there are ways to use certain prerequisites to control Skills (such as having a Skill do more damage if its target is affected by a specific State), however, what I'm trying to do is a bit different. Rather than choosing between two effects based on whether an applicable target is or isn't affected by a named State, is it possible to use States to cause Skill effects to "prioritize" a State-designated target?

For example, if I were to use a Skill with a Scope of "3 Random Enemies," would it be possible to have all 3 instances of that Skill's effect "look for" a specific State, and then prioritize enemies with that State (in this case, hitting them 3 times)? The closest I've gotten to solving this is having a Skill with a Scope of "All Enemies" only execute its effect on enemies with a certain State, but I'm looking for something a bit less binary (if it's possible).

As for Plugins, I'm using Yanfly's Target Core, Skill Core, Buffs/States Core, and all prerequisites - but if there's anything else I'd need to achieve this effect, just let me know!
Code:
<Custom Target Eval>
var tlist=[];

for (var i = 0; i< foes.aliveMembers().length; ++i) {

var member = foes.aliveMembers()[i];
if (member.isStateAffected(5)) {
tlist.push(member);
}

}

if(tlist.length>3){

tlist = tlist.sort(function(){return Math.random()-0.5});
for(var i=0;i<3;i++){
targets.push(tlist[i]);
}
}else{

targets=tlist;

}
</Custom Target Eval>
This skill will only attack enemies with State 5.
Code:
<Custom Target Eval>
var tlist=[];

for (var i = 0; i< foes.aliveMembers().length; ++i) {

var member = foes.aliveMembers()[i];
if (member.isStateAffected(5)) {
tlist.push(member);
}

}

if(tlist.length>3){

tlist = tlist.sort(function(){return Math.random()-0.5});
for(var i=0;i<3;i++){
targets.push(tlist[i]);
}
}else{

targets=tlist;

}
</Custom Target Eval>
This skill will only attack enemies with State 5.

Ah, this is kind of like what I tried at first, where a Skill that would normally hit all enemies would only hit those indicated by a certain state.

I'm not really fluent in JS, but while this looks like what I'm trying to do (being an if/else statement that prioritizes the State if available, but otherwise hits random enemies), upon implementing it, it ends up hitting 0 enemies if no State is applied, and hitting each State-designated enemy once if any enemies have that State.

I'm not sure if note tags are compatible with code, but maybe I can do something along the lines of using Yanfly's <Target: x Random Foes>, <Repeat: x>, and <Target: Everybody> tags with some sort of if/else statement, where if any enemies in the list have State X, Repeat that Skill's effect 3 times, else Target X Random Foes. Do you think something like this might work?
Use this plugin YEP_X_ActSeqPack1.js
Code:
if target.isStateAffected(5)

action effect

action effect

action effect

else

action effect
If the enemy is in state 5, damage 3 times.
There might be a simpler way of writing it, but I just remember this.

Original thread: https://forums.rpgmakerweb.com/threads/state-driven-skill-prioritization.91996/