Last active
May 26, 2018 07:03
-
-
Save jinzocode/8794ed52050a8a030a3b933d67a8f1e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////// | |
//Bot username | |
const bot_name = 'utopian-bot'; | |
//Possible scenarios | |
var scenario = []; | |
scenario[0] = '@' + bot_name + ' !utopian'; | |
scenario[1] = '@' + bot_name + ' !utopian @mention1 @mention2 @mention3 @mention4'; | |
scenario[2] = '@' + bot_name + ' @mention1 @mention2 @mention3 @mention4'; | |
scenario[3] = '@' + bot_name + ' @mention1 @mention2 @mention3 @mention4 !utopian'; | |
scenario[4] = '@mention1 @mention2 @mention3 @mention4' + ' @' + bot_name; | |
scenario[5] = '@mention1 @mention2 @mention3 @mention4'; | |
scenario[6] = '@mention1 @mention1 @mention3 @mention4'; | |
scenario[7] = '@' + bot_name + ' !utopian @mention1 @mention2:35% @mention3:25% @mention4:25% @mention10'; | |
scenario[8] = '@' + bot_name + ' !utopian @mention1:10% @mention2:10% @mention3:10% @mention4:25% @mention:55%'; | |
scenario[9] = '@' + bot_name + ' !utopian @mention1:10% @mention2:10% @mention3:10% @mention4:25% @mention:45%'; | |
/////////////////////////////////////// | |
//Start testing some possible scenarios | |
for (let i = 0; i < scenario.length; i++) { | |
let result = checkMentionInput(scenario[i]); | |
console.log(result); | |
console.log('-----------'); | |
} | |
/////////////////////////////////////// | |
//Functions | |
function checkMentionInput(input) { | |
console.log('Input Content: ' + input); //Print all input content for possible scenario. | |
//default object for beneficiaries | |
let result = { | |
beneficiaries: [] | |
}; | |
let mention = input.match(/@[a-zA-Z0-9-]+\:\d[\d|\.]+|@[a-zA-Z0-9-]+/g); //match all mentions in the input | |
//Check if users is less than or equal to 8 | |
if (mention.length <= 8) { | |
//Check if usernames has duplicates | |
if (checkUniqueUsernames(mention)) { | |
switch (true) { | |
case /^\B@[a-zA-Z0-9-]+\s(!utopian)/.test(input): | |
mention.shift(); //remove the bot name from the array. | |
//if there is any mentions return it | |
if (mention.length > 1) { | |
for (let i = 0; i < mention.length; i++) { | |
result.beneficiaries.push({ | |
[mention[i].split(':')[0]]: mention[i].split(':')[1] | |
}); | |
} | |
//Check usernames percentages if exist or not | |
switch (isPercentage(result.beneficiaries)) { | |
case 1: | |
return { | |
error: 'You have some users with mixed weight.' //if noWeight and fixWeight is more than zero. | |
} | |
break; | |
case 2: | |
let len = result.beneficiaries.length; //get length for all beneficiaries | |
let usernames = []; | |
percentage = Math.floor(100 / len) | |
Obj = Object.assign({}, result.beneficiaries); //Convert array to object | |
for (let i = 0; i < len; i++) { | |
usernames.push(Object.keys(Obj[i]).toString()); //Get all usernames from the object | |
} | |
result.beneficiaries = []; | |
// console.log(usernames) | |
for (let i = 0; i < len; i++) { | |
result.beneficiaries.push({ | |
[usernames[i]]: percentage | |
}); | |
} | |
return result; | |
break; | |
case 3: | |
return result; //print result | |
break; | |
case 4: | |
return { | |
error: 'Your total weight must be 100%' //case 4 error is weight doesn't equal 100% | |
}; | |
break; | |
default: | |
return result; //print the default object if no case has been matched | |
} | |
} else { | |
return result; | |
} | |
break; | |
case (mention[0] !== '@' + bot_name): //check if bot name exist | |
return { | |
error: 'The text is not properly formatted, You didn\'t mention ' + bot_name + ' first.' | |
}; | |
break; | |
case (/^\B@[a-zA-Z0-9-]+/.test(input) && input.indexOf('!utopian') >= 0): //check !utopian is right after the bot name | |
return { | |
error: 'The text is not properly formatted, !utopian must be right after the bot name.' | |
} | |
break; | |
case (/^\B@[a-zA-Z0-9-]+/.test(input) && input.indexOf('!utopian') === -1): //check !utopian is missing | |
return { | |
error: 'The text is not properly formatted, !utopian is missing, it must be right after the bot name.' | |
} | |
break; | |
} | |
} else { | |
return { | |
error: 'Some usernames are duplicated.' //print error if usernames are duplicates | |
} | |
} | |
} else { | |
return { | |
error: 'There are more than 8 beneficiaries.' //error handler for more than 8 beneficiaries | |
} | |
} | |
} | |
//Check percentage for every single user | |
function isPercentage(array) { | |
let noWeight = 0, | |
fixedWeight = 0; | |
Obj = Object.assign({}, array) | |
//Check all weight inputs | |
for (let i = 0; i < array.length; i++) { | |
let test = Object.values(Obj[i]).toString(); | |
if (test === '') { | |
noWeight++; //count as no weight exist. | |
} else { | |
fixedWeight += parseFloat(test); //add more weight | |
} | |
} | |
//Check if mixed weight | |
if (noWeight > 0 && fixedWeight > 0) { | |
return 1; | |
} else if (noWeight === array.length) { | |
return 2; | |
} else if (fixedWeight === 100) { | |
return 3; | |
} else { | |
return 4; | |
} | |
} | |
function checkMentions(array) { | |
for (let user of array) { | |
test = /@[a-zA-Z0-9-]+\:\d[\d|\.]+/.test(user); | |
} | |
} | |
function checkUniqueUsernames(array) { | |
let unique = array.filter(onlyUnique); | |
//Return true if no duplicates | |
if (unique.length === array.length) | |
return true; | |
else //else return false | |
return false; | |
} | |
//Get all unique usernames from an array | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment