Dudicle's First Mafia Script

Dudicle

New member
Hello all.

This is my first post and as a software engineer with seven years of practice and two years of rust, I'm ashamed that I could not get past this hump. Right now I'm in the early stages of farting around, but hopefully soon I'll be able to help out and give advice.

Any tips or pointers I can get on kmail and chat interaction will help me.


And to Zarqon and MrEdge73, sorry if I botch the application of your functions.
 

Attachments

Some explanation of what we're about to download and look at would help and increase how many people will do that. Also, if you want feedback, letting us know what your goal/questions/desires are would help with that.
 
A few things --

First off, welcome to Mafia scripting. Ash isn't quite like most languages you've probably encountered, but syntax should be mostly familiar. The wiki (there's a link at the top of these forums as well as in my signature) should be helpful -- we try to keep it as up-to-date as possible.

Parsing kmails should be doable with visit_url(), but that's a lot of messy parsing -- regexps save lots of time here (click here to learn how to use Mafia's implementation).

Chats, on the other hand, are slightly more complicated. The only way to intercept private chats with Mafia is with a chatBotScript, and the only way to intercept clan chats is via some hacky nonsense (i.e. PMing yourself "update", waiting until you get a message from yourself, then parsing the latest message). You can't access any other chats.

Generally, we try to minimize server hits, since we don't want to generate a whole lot of lag. An added benefit is that the resultant scripts are affected less by lag. Each call to MinutesToRollover() hits the character pane, which doesn't seem entirely necessary -- the charpane code is tied to a certain time of day, so it's unnecessary if you know what time it is already.

Also, you do know that vprint only prints stuff to your CLI, not clan chat, right?
 

As stated, I am not accustomed to posting in open forum when looking for a solution. I'm used to helping others when I know the information and culling for it when I don't. Sorry if I appear to be a total noob.

As for my apologies to Zarqon and MrEdge, it was an attempt at humor since I was calling their functions. I apologized if that alarmed you.


Dependancies: zlib, MrEdge73's Support Script -- Functions (Beta)
Function: This is simply a test script that is intended to check for rollover within a loop. At specified times, I wanted it to PM me, print to KOLMafia, and post to clan chat. By the time I had posted this, I had stripped all code relating to kmail. My only focus right now is how to i/o to chat.


My functions are listed below.
// Let's call this overload. Yeah. That's the ticket...
void chat(string message) {
vprint(message, 1);
}

// Let's call this overload. Yeah. That's the ticket...
void clan_chat(string message) {
chat("/clan " + message);
}

// Let's call this overload. Yeah. That's the ticket...
void slime_chat(string message) {
chat("/slimetube " + message);
}

// Let's call this overload. Yeah. That's the ticket...
void hobo_chat(string message) {
chat("/hobopolis " + message);
}

void main() {
// Initialize your variables. Find out how much time you have left.
int murmur = 0;
int heartbeat = 0;
int rollover = 0;
rollover = MinutesToRollover();
string PM = "";

// As long as rollover is more than 5 minutes away, keep it up.
while ( rollover > 5 ) {
Wait(10);
murmur += 1;
// Find out how much time you have left.
rollover = MinutesToRollover();


# When sixty seconds pass, reset the murmur and make the heart beat
if ( murmur == 6 ) {
murmur = 0;
heartbeat += 1;
print("Thu-thump " + heartbeat + " !","blue");
vprint("Thu-THUMP " + heartbeat + " !","blue");
}

// Annoy myself
PM = "Heartbeat[" + heartbeat + "], Iteration[" + murmur + "] : Rollover is in " + rollover + " minutes.\n";
chat_private("Dudicle123123", PM);

# If it is after 11 pm, increase rollover notifications, otherwise give a half-hourly reminder
int hour = get_hour();
int min = get_minute();

if ( hour == 23 ) {
if ( min == 15 || min == 45 ) {
clan_chat("It is " + rollover + " minutes until rollover.");
}
} else if ( min == 0 || min == 30 ) {
clan_chat("It is " + rollover + " minutes until rollover.");
}
}
}
 
Okay, well, chat_clan(string message) already exists in Mafia.

I'm not entirely sure, but
PHP:
boolean chat_any(string msg) {
    return visit_url("submitnewchat.php?playerid="+my_id()+"&pwd="+my_hash()+"&graf="+url_encode(msg)) != "";
}
might work, unless there's some Mafia code that explicitly disallows visiting submitnewchat.php via visit_url(). (the above function would send whatever message you pass it)

Either way, it's mostly useless without some way to monitor chat.
 
Mafia disallows any access to any KoL url with "chat" in it. Presumably it wouldn't prevent access to an outside url with chat in it, but I haven't honestly tested it.
 
Shoot. It was worth a shot.

So far, I've been able to use the print function only. vprint() and calls to chat via visit_url() haven't worked for me so far -- now I know why chat hasn't been working. I looked at my attachment and found that I'd inadvertently stripped my "visit_url()" when purging and reconfiguring my code.

No wonder I didn't see any scripts working with chat. My dreams of creating a dice roller and a random quote generator seem to be slipping away.

Thanks for all the help so far.


EDIT: I had tried chat_clan() in my early attempts, but I had many errors so it never got through. I was able to successfully loop and output using that. AGAIN, thanks for the help. Sometimes, all you need is someone to compare notes with.
 
Last edited:
Mafia is quite capable of allowing a chat die roller. My clan bot uses mafia to communicate between two clan chats (linking them), and a variety of in-clan functions.

I modified my source code to allow parsing of clan chat, but the rest is using native functions.

Mafia is more accommodating than most people seem to think.
 
Advice on PM incoming chat:
Chat scripts have a priority over all other scripts running so if running multiple scripts in loops you need to make sure they both don't hit the server at the same time. If they do your bot will crash. My best solution is to use a light weight chat script that does not hit the server at all and use a looping bot script to read it in when it desires to:
Code:
## Use this script by placing it in your scripts directory and typing "set chatbotScript=PMlogger.ash" into the gCLI.
void main( string sender , string message )
{
	print("Incoming message from " +sender+ " saying:  "+message,"olive");	
	record note
	{
		string sender;
		string message;
	}[int] PMlog;
	
	File_to_Map("PMlog.txt",PMlog);
	int newPM= count(PMlog);
	PMlog[newPM].sender= sender;
	PMlog[newPM].message= message;
	Map_to_File(PMlog,"PMlog.txt");	
}

You can read it by simply opening file and importing it in as a map whenever your script is ready for it:
Code:
//note record
record note
{
	string sender;
	string message;
};

//Load Chatbot Private message Log from PMlogger.ash
	note[int] PMlog;
	File_to_Map("PMlog.txt",PMlog);
	
	//Erase Log File
	note[int] emptylog;
	Map_to_File(emptylog,"PMlog.txt");	
	
	//Inverses log, Stacks it from LILO to FIFO
	note[int] PMlogInverse;
	for i from 0 upto count(PMlog)-1
		PMlogInverse[count(PMlog)-1-i]=PMlog[i];

for i from 0 upto count(PMlogInverse)-1
	{
		string sender= PMlogInverse[i].sender;
		string message= PMlogInverse[i].message.to_lower_case();
//do something
}

Note that this method will delay response. You will have to let your users know that their commands will not be immediately responded to based on you wait(time). You may have to have a method built in to ignore duplicate input messages. If you need an example of bot Kmail communication you can check out my Gamblebot.ash script. It responds to inputs through Kmail and responds by Kmail and Chat.

Other Notes:
Mafia will not allow you to parse chat messages so you will have to do all inputs using PM or Kmail. You can only spam the clan chat channel and you can only PM clannies. You can Kmail anyone.
 
Thank you for the advice. I was constructing a way to read from the existing chat files, but didn't think to try rediverting the PMs to a separate file. My biggest hurdle was figuring out how to loop through the map. As I'm new to Mafia scripting, it's been kinda fun getting nowhere fast.

I will adapt your example into my logic, but I want to understand it rather than just be a script kiddie. In the below example, I was trying to parse through a snapshot of what was in chat and then print results in Mafia as a test. It sat and hung. Where did I go wrong? :confused:

View attachment test2.ash

For behavior, it produced:
scripts/20101022_dudicle123123_[clan].html
scripts/20101022_dudicle123123_[clan].html

... and persisted. It only exited when I hit "esc."


Thank you for any help you can give while I beat my head on a rock. I'll also analyze what you gave me. I know that the answer lies within.

~ Dudicle
 
--- UPDATE ---
Mr. Edge,

I finally got around to loading up your code. Awesome.

So far, I've only worked with the logger. I have not tried reading in.

Thank you again for the jumpstart,
~Dudicle
 
Last edited:
Map_to_File and File_to_map can only use TAB spaced lists. They cannot read in a HTML file unless you completely strip all of the html out of it using a parser. String parsing is a very advanced lesson, I have many examples out there using regex and Z has many out there using substrings. Take a look in your data folder, the functions can only read/write data in that format.
 

Thanks again for getting me started and the advice.

As mentionned before, I'd made a slight change to the script. In the modified copy the log files are prepended with the date and all entries are dated. I figured I could take the numeric index out, but left it in just in case.

A also replicated the changes to the script that reads the PMlog.

 

Attachments

Back
Top