Skip to content

Instantly share code, notes, and snippets.

@kmod-midori
Created February 12, 2013 15:29
Show Gist options
  • Save kmod-midori/4770664 to your computer and use it in GitHub Desktop.
Save kmod-midori/4770664 to your computer and use it in GitHub Desktop.
var roll=function(max,min,count){
var i=0;
var ret=[];
while(i<count){
ret.push(parseInt(Math.random()*(max-min+1)+min));
i++;
}
return ret;
}
var makeListResult=function(list,count){
if(list.length==0)return {text:'没有选项让老娘选什么!',type:'TEXT'};
if(list.length==1)return {text:'只有一个选项让老娘选什么!',type:'TEXT'};
if(typeof count =='number'&&count>=list.length)return {text:'全选上不就行了么,还用老娘出马?',type:'TEXT'};
if(typeof count !='number')var count=1;
var r=roll(list.length-1,0,count);
if(count==1){
var ret=[{text:'骰子骨碌骨碌转!选中了 *',type:'TEXT'}];
ret.push(list[r[0]]);
ret.push({text:'* !诶嘿~(ゝω・)☆',type:'TEXT'});
}else{
var ret=[{text:count+'只骰子骨碌骨碌转!分别选中了 *',type:'TEXT'}];
for(var d in r){
ret.push(list[r[d]]);
ret.push({text:'、',type:'TEXT'});
}
ret.pop();
ret.push({text:'* !诶嘿~(ゝω・)☆',type:'TEXT'});
}
return ret;
}
var bulid_content=function(content,bot_id,user_id,user_name){
var command='';
var in_command=false;
var in_list=false;
var in_item=false;
var items=[];
var list_count=1;
for(var i in content){
if(!in_command&&typeof content[i].user !='undefined'&&content[i].user.userId==bot_id){in_command=true;continue;}
//if(in_command&&!in_list&&content[i].type=='LINE_BREAK')in_command=false;break;
if(in_command&&in_list){
if(content[i].type!='LINE_BREAK'){
if(/^endlist/i.exec(content[i].text)!==null){
var r=roll(items.length-1,0,list_count);
var selected=[];
for(var i in r){
selected.push(r[i]);
}
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:''}
];
}
}
}
if(in_command&&!in_list&&content[i].type!='LINE_BREAK'){
//list ...... endlist
var l=/^list\s*(\d+)*$/i.exec(content[i].text);
if(l!==null){
if(typeof l[1] !='undefined'){
if(parseInt(l[1])<=0){
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'没有骰子怎么搞 人家不陪乃玩了!',type:'TEXT'}
];
}else{
list_count=parseInt(l[1]);
in_list=true;
continue;
}
}else{
in_list=true;
continue;
}
}
// B d A
var d=/^\s*(\d+)\s*d\s*(\d+)\s*$/i.exec(content[i].text);
if(d!==null){
if(parseInt(d[1])<=0){
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'没有骰子怎么搞 人家不陪乃玩了!',type:'TEXT'}
];
}
max=parseInt(d[2]);
if(max>256)max=256;
if(max<1)max=1;
var result=roll(max,1,parseInt(d[1])).join('、');
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:parseInt(d[1])+' 只骰子骨碌骨碌转!分别投出了 *'+result+'* 点!诶嘿~(ゝω?)☆',type:'TEXT'}
];
break;
}
//B x A B * A B × A
var x=/^\s*(\d+)[×x\*]\s*(\d+)\s*$/i.exec(content[i].text);
if(x!==null){
if(parseInt(x[1])<=0){
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'没有骰子怎么搞 人家不陪乃玩了!',type:'TEXT'}
];
}
max=parseInt(x[2]);
if(max>256)max=256;
if(max<1)max=1;
var result=roll(max,1,parseInt(x[1])).join('、');
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:parseInt(x[1])+' 只骰子骨碌骨碌转!分别投出了 *'+result+'* 点!诶嘿~(ゝω?)☆',type:'TEXT'}
];
break;
}
//roll roll M roll N M
var r=/^\s*roll\s*(\d*)\s*(\d*)\s*$/i.exec(content[i].text);
if(r!==null){
if(r[1]==""&&r[2]==""){
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'骰子骨碌骨碌转!投出了 *'+roll(100,1,1)[0]+'* 点!诶嘿~(ゝω?)☆',type:'TEXT'}
];
}
if(r[2]==""){
var max=parseInt(r[1]);
if(max<=0)max=1;
if(max>256)max=256;
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'骰子骨碌骨碌转!投出了 *'+roll(max,1,1)[0]+'* 点!诶嘿~(ゝω?)☆',type:'TEXT'}
];
}
var max=parseInt(r[1]);
var min=parseInt(r[2]);
if(min<1)min=1;
if(max>256)max=256;
if(min>max){
var tem=max;
max=min;
min=tem;
}
return [
{text:'+'+user_name,type:'USER_MENTION',user:{name:user_name,userId:user_id}},
{text:'骰子骨碌骨碌转!投出了 *'+roll(max,min,1)[0]+'* 点!诶嘿~(ゝω?)☆',type:'TEXT'}
];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment