Javascript code:
var psm = "Cups: %cups | Drunk: %drank | Funds: %money";
var payment = 10;
var price = 300;
var your_money = 500;
var show_in_psm = true;
var cups_drank = 0;
var cups_owned = 0;
var toast = true;
var notify_contact = true;
var Wnd;
var total_length = 0;
var total_messages = 0;
var flood = false;
var cups_bought = 0;
function OnEvent_Initialize(){
if(Messenger.MyStatus > 1){
OnEvent_SigninReady(Messenger.MyEmail);
}
}
function OnEvent_SigninReady(email){
if(get("!")===-1){
_save();
}
_load();
MsgPlus.AddTimer("Save_Statistics",180000);
MsgPlus.AddTimer("Update_PSM",10000);
if(show_in_psm===-1||show_in_psm===true){
Messenger.MyPersonalMessage = "The Kyozo Café - " + _parse(psm);
}
}
function OnEvent_Uninitialize(){
_save();
}
function parse_txt(){
var s = "The Kyozo Café - Statistics\n\n";
s += "Sent Messages: " + get("total_messages") + "\n";
s += "Total Length Of Messages: " + get("total_length") + "\n";
s += "Average Length Of Messages: " + Math.floor((get("total_length")*1)/(get("total_messages")*1)) + "\n";
s += "Funds: $" + _calc(get("your_money")) + "\n";
s += "Cups Bought: " + ((get("cups_owned")*1)+(get("cups_drank")*1)) + "\n";
s += "Cups Drank: " + get("cups_drank") + "\n";
s += "Statistics Collected Since: " + new Date(get("!")).toGMTString() + "\n";
return s;
}
function parse_xml(){
var s = "<Statistics>\n\n";
s += " <Sent_Messages>" + get("total_messages") + "</Sent_Messages>\n";
s += " <Total_Length_Of_Messages>" + get("total_length") + "</Total_Length_Of_Messages>\n";
s += " <Average_Length_Of_Messages>" + Math.floor((get("total_length")*1)/(get("total_messages")*1)) + "</Average_Length_Of_Messages>\n";
s += " <Cups_Owned>" + ((get("cups_owned")*1)+(get("cups_drank")*1)) + "</Cups_Owned>\n";
s += " <Cups_Drank>" + get("cups_drank") + "</Cups_Drank>\n";
s += " <Installed>" + new Date(get("!")).toGMTString() + "</Installed>\n</Statistics>";
return s;
}
function SaveToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile(MsgPlus.ScriptFilesPath+"\\Stats.txt", 2, true,0);
FileObject.write(sText);
FileObject.close();
return true;
}
function SaveToXMLFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile(MsgPlus.ScriptFilesPath+"\\Stats.xml", 2, true,0);
FileObject.write(sText);
FileObject.close();
return true;
}
function OnEvent_Timer(TimerId){
if(TimerId==="Save_Statistics"){
_save();
MsgPlus.AddTimer("Save_Statistics",180000);
}
if(TimerId==="Flood"){
flood = !flood;
}
if(TimerId==="Update_PSM"){
if(show_in_psm===-1||show_in_psm===true){
Messenger.MyPersonalMessage = "The Kyozo Café - " + _parse(psm);
MsgPlus.AddTimer("Update_PSM",10000);
}
}
}
function OpenSettings(){
Wnd = MsgPlus.CreateWnd("gui.xml","WndSettings");
Wnd.SetControlText("txt_string",psm);
Wnd.Button_SetCheckState("chk_psm",new Boolean(show_in_psm*-1));
Wnd.Button_SetCheckState("chk_toast",new Boolean(toast*-1));
Wnd.Button_SetCheckState("chk_notify",new Boolean(notify_contact*-1));
}
function OnWndSettingsEvent_CtrlClicked(PlusWnd, ControlId){
if(ControlId==="BtnOk"){
show_in_psm = PlusWnd.Button_IsChecked("chk_psm");
toast = PlusWnd.Button_IsChecked("chk_toast");
notify_contact = PlusWnd.Button_IsChecked("chk_notify");
psm = PlusWnd.GetControlText("txt_string");
_save();
PlusWnd.Close(1);
if(toast===-1||toast===true){
MsgPlus.DisplayToastContact("The Kyozo Café","[b]Saved![/b]","Your settings have been saved");
}
}
}
function _save(){
set("your_money",your_money);
set("show_in_psm",show_in_psm);
set("cups_drank",cups_drank);
set("cups_owned",cups_owned);
set("cups_bought",cups_bought);
set("toast",toast);
set("notify_contact",notify_contact);
set("psm",psm);
set("total_length",total_length);
set("total_messages",total_messages);
var d = new Date();
set("last_saved",d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
}
function _load(){
your_money = get("your_money")*1;
show_in_psm = (get("show_in_psm")*-1)>0;
cups_drank = get("cups_drank")*1;
cups_owned = get("cups_owned")*1;
toast = (get("toast")*-1)>0;
cups_bought = get("cups_bought")*1;
notify_contact = (get("notify_contact")*-1)>0;
psm = get("psm");
total_length = get("total_length")*1;
total_messages = get("total_messages")*1;
}
function _calc(x){
x = x/100;
x = x.toString();
try{
x = x.split(".");
if(x[1].length<2){
x[1] = x[1]+"0";
}
if(x.length>2){
x[1] = x[1].substr(0,2);
}
return x[0]+"."+x[1];
}catch(e){
return x;
}
}
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MsgKind){
if(Origin!==Messenger.MyName){
if(Message.search("!cafestats_msg")!==-1&&Message.search("_txt")===-1&&Message.search("_xml")===-1){
OnEvent_ChatWndSendMessage(ChatWnd, "/cafestats_msg");
return "Sending Stats";
}
if(Message.search("!cafestats_txt")!==-1){
OnEvent_ChatWndSendMessage(ChatWnd, "/cafestats_txt");
return "Sending Stats";
}
if(Message.search("!cafestats_xml")!==-1){
OnEvent_ChatWndSendMessage(ChatWnd, "/cafestats_xml");
return "Sending Stats";
}
}
}
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(Message) !== null) {
var command = RegExp.$1.toLowerCase();
var parameter = RegExp.$2;
switch (command) {
case 'cafestats_msg':
ChatWnd.SendMessage(parse_txt());
return "";
case 'cafestats_txt':
if(SaveToFile(parse_txt())===true){ChatWnd.SendMessage("/sendfile "+MsgPlus.ScriptFilesPath + "\\" + "stats.txt");}
return "";
case 'cafestats_xml':
if(SaveToXMLFile(parse_xml())===true){ChatWnd.SendMessage("/sendfile "+MsgPlus.ScriptFilesPath + "\\" + "stats.xml");}
return "";
case 'dchocolate':
if(parameter.search(/max/i)!==-1){paramater=1000;}
if((parameter*1)>=1){
b_toast = toast;
b_notify_contact = notify_contact;
for(var i = 0; i < (parameter*1); i++){
DrinkCoffee(ChatWnd,(parameter*1));
toast = false;
notify_contact = false;
}
toast = b_toast;
notify_contact = b_notify_contact;
}else{
DrinkCoffee(ChatWnd);
}
return "";
case 'bchocolate':
if(parameter.search(/max/i)!==-1){paramater=1000;}
if((parameter*1)>=1){
b_toast = toast;
b_notify_contact = notify_contact;
for(var i = 0; i < (parameter*1); i++){
BuyCoffee(ChatWnd,(parameter*1));
toast = false;
notify_contact = false;
}
toast = b_toast;
notify_contact = b_notify_contact;
}else{
BuyCoffee(ChatWnd);
}
return "";
case 'cafestats':
OnEvent_MenuClicked("stats");
return "";
case 'cafeabout':
OnEvent_MenuClicked("about");
return "";
}
}else{
if(flood===false){
your_money += payment;
total_length += Message.length;
total_messages++;
flood = !flood;
MsgPlus.AddTimer("Flood",5000);
}
}
if(show_in_psm===-1||show_in_psm===true){
Messenger.MyPersonalMessage = "The Kyozo Café - " + _parse(psm);
}
return _parse(Message);
}
function _parse(x){
x = x.replace(/%cups/gi,cups_owned).replace(/%drank/gi,cups_drank).replace(/%money/gi,"$"+_calc(your_money));
return x;
}
function DrinkCoffee(x,y){
if(cups_owned>0){
cups_drank++;
cups_owned--;
if(notify_contact===true||notify_contact===-1){
if(typeof(y)!=="undefined"){
x.SendMessage("/me has drank "+y+" cups of hot chocolate!");
}else{
x.SendMessage("/me has drank a cup of hot chocolate!");
}
}
if(typeof(y)!=="undefined"){
x.DisplayInfoMessage(Messenger.MyName + " has drank "+y+" cups of hot chocolate!");
}else{
x.DisplayInfoMessage(Messenger.MyName + " has drank a cup of hot chocolate!");
}
if(toast===-1||toast===true){
if(typeof(y)!=="undefined"){
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4"+MsgPlus.RemoveFormatCodes(Messenger.MyName),"just drank "+y+" cups of hot chocolate!");
}else{
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4"+MsgPlus.RemoveFormatCodes(Messenger.MyName),"just drank a cup of hot chocolate!");
}
}
}else{
if(toast===-1||toast===true){
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4Can't do that!","You need to buy more cups!");
}
}
if(show_in_psm===-1||show_in_psm===true){
Messenger.MyPersonalMessage = "The Kyozo Café - " + _parse(psm);
}
}
function BuyCoffee(x,y){
if((your_money-price)>=0){
cups_owned++;
your_money = your_money-price;
if(notify_contact===true||notify_contact===-1){
if(typeof(y)!=="undefined"){
x.SendMessage("/me has bought "+y+" cups of hot chocolate!");
}else{
x.SendMessage("/me has bought a cup of hot chocolate!");
}
}
if(typeof(y)!=="undefined"){
x.DisplayInfoMessage(Messenger.MyName + " has bought "+y+" cups of hot chocolate!");
}else{
x.DisplayInfoMessage(Messenger.MyName + " has bought a cup of hot chocolate!");
}
if(toast===-1||toast===true){
if(typeof(y)!=="undefined"){
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4"+MsgPlus.RemoveFormatCodes(Messenger.MyName),"just bought "+y+" cups of hot chocolate!");
}else{
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4"+MsgPlus.RemoveFormatCodes(Messenger.MyName),"just bought a cup of hot chocolate!");
}
}
}else{
if(toast===-1||toast===true){
MsgPlus.DisplayToastContact("The Kyozo Café","ˇ$4Can't do that!","You don't have the money!");
}
}
if(show_in_psm===-1||show_in_psm===true){
Messenger.MyPersonalMessage = "The Kyozo Café - " + _parse(psm);
}
}
function set(key, value){
try{
return new ActiveXObject("WScript.Shell").RegWrite(MsgPlus.ScriptRegPath + Messenger.MyUserID + "\\" + key, value, "REG_SZ");
}catch(e){}
}
function get(key){
try{
return new ActiveXObject("WScript.Shell").RegRead(MsgPlus.ScriptRegPath + Messenger.MyUserID + "\\" + key);
}catch(e){
if(key==="!"){
MsgPlus.DisplayToastContact("The Kyozo Café","[b]Thank you![/b]", "for downloading The Kyozo Café\nplease enjoy");
set("!",new Date());
return -1
}else{
return false;
}
}
}
function OnGetScriptMenu(nLocation) {
var s = "<ScriptMenu>";
s += "<MenuEntry Id='settings'>Settings</MenuEntry>";
s += "<MenuEntry Id='stats'>Statistics</MenuEntry>";
s += "<Separator/>";
s += "<MenuEntry Id='about'>About</MenuEntry>";
s += "</ScriptMenu>";
return s;
}
function OnEvent_MenuClicked(sMenuItemId, nLocation, OriginWnd) {
if(sMenuItemId === "settings") {
OpenSettings();
}
if(sMenuItemId === "stats"){
var stats = MsgPlus.CreateWnd("gui.xml", "WndStats");
stats.SetControlText("txt_1","[c=4]"+total_messages+"[/c]");
stats.SetControlText("txt_2","[c=4]"+total_length+"[/c]");
stats.SetControlText("txt_3","[c=4]"+Math.floor(total_length/total_messages)+"[/c]");
stats.SetControlText("txt_4","[c=4]"+cups_owned+"[/c]");
stats.SetControlText("txt_5","[c=4]"+cups_drank+"[/c]");
stats.SetControlText("txt_6","[c=4]$"+_calc(your_money)+"[/c]");
var dat = get("!");
dat = dat.split(" ");
dat = dat[2]+"/"+dat[1]+"/"+dat[5]
stats.SetControlText("imp","[c=4]Since: "+dat+"[/c]");
}
if(sMenuItemId === "about") {
MsgPlus.CreateWnd("gui.xml", "WndAbout");
}
}
function OnGetScriptCommands(){
var commands = '<ScriptCommands>';
commands+='<Command>';
commands+='<Name>bchocolate</Name>';
commands+='<Description>Buy a cup of hot chocolate</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>dchocolate</Name>';
commands+='<Description>Drink a cup of hot chocolate</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>cafestats</Name>';
commands+='<Description>Stats from The Kyozo Café</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>cafeabout</Name>';
commands+='<Description>About The Kyozo Café</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>cafestats_msg</Name>';
commands+='<Description>Send The Kyozo Café statistics to a contact as a MESSAGE</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>cafestats_txt</Name>';
commands+='<Description>Send The Kyozo Café statistics to a contact as a TXT FILE</Description>';
commands+='</Command>';
commands+='<Command>';
commands+='<Name>cafestats_xml</Name>';
commands+='<Description>Send The Kyozo Café statistics to a contact as an XML FILE</Description>';
commands+='</Command>';
commands+='</ScriptCommands>';
return commands;
}