Python Plugins / File Handling

Posted by LupusFatalis on Thu 16 Apr 2009 01:41 AM — 11 posts, 40,587 views.

#0
Ok, so its been a couple years since I've played a mud/programmed/etc...

I just coded a script in python for the mud I'm playing. Works great, etc... I'd like to make it into a plugin, and I'd like the plugin to read/write variables to another file that is character specific. This way I can use the same plugin with every character, share it with other players, etc...

I have no idea where to start with this, and would appreciate any suggestions/assistance that could be offered to this end.
Australia Forum Administrator #1
See: http://www.gammon.com.au/mushclient/plugins/

There is a plugin wizard that converts your triggers etc. plus your script file into a plugin.
#2
Heya, thanks Nick...

The Plugin Wizard is great, but in that case I need the plugin have two capacities that I can't seem to figure out.

1) Other plugins, etc... must be able to read the variable values.

2) The plugin must be able to create new variables within itself.

Are these doable?
USA #3
1)
GetPluginVariable: http://www.gammon.com.au/scripts/doc.php?function=GetPluginVariable";
GetPluginVariableList: http://www.gammon.com.au/scripts/doc.php?function=GetPluginVariableList";

2)
SetVariable: http://www.gammon.com.au/scripts/doc.php?function=SetVariable";

Plugins save their state separately per world file as far as I'm aware; you don't need to do anything special if you use separate worlds for each character.

To Nick: Any chance you can write/have written a forum code for hyperlinks?
Amended on Thu 16 Apr 2009 03:57 PM by Twisol
#4
Alrighty, I made it into a plugin. I made an alias to check the variable listing function, that works great. The problem I am having is still that it doesn't store the variables from session to session.

It stores them when it is a script, as the world variables. But once I make it into a plugin it doesn't. Once I close the sessions, they are all lost, etc...

I'll post the things here so you can have a look at it, thanks again! All I really have in there is 2 triggers and a script for each of them. When I query the mud for my skills it gives me them in a descriptive manner, I need to track the actual numbers though.


<triggers>
  <trigger
   enabled="y"
   match="^(.*)\:(\s+)(Unskilled|A Tyro|A Novice|A Beginner|Poor|Not Very Good|Below Average|Average|Above Average|Able|Fair|Proficient|Good|Adroit|Very Good|Excellent|An Expert|Superb|A Master|Eminent|An Adept|Renowned|A High Master|Consummate|A Virtuoso|A Grand Master|Legendary)\.$"
   omit_from_output="y"
   regexp="y"
   script="SkillCheck"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   match="^\* You think your (.*) skill has improved\. \*$"
   regexp="y"
   script="OnSkillGain"
   sequence="100"
  >
  </trigger>
</triggers>

<script>
<![CDATA[
def OnSkillGain(TriggerName, trig_line, wildcards):

	VariableName = ''.join([wildcards[x] for x in range(len(wildcards)-1)])
	VariableName = VariableName.replace(' ', '_').replace('\'', '').replace('-', '_').replace('#', '_')

	if not world.GetVariable(VariableName):
		VariableValue = 1
	else:
		VariableValue = int(world.GetVariable(VariableName)) + 1

	world.SetVariable(VariableName, str(VariableValue))
	world.note(VariableName + ': ' + str(VariableValue))


def SkillCheck(TriggerName, trig_line, wildcards):

	VariableStringName = wildcards[0]
	TempSpace = wildcards[1]
	VariableDesc = wildcards[2]

	VariableName = VariableStringName.replace(' ', '_').replace('\'', '').replace('-', '_').replace('#', '_')

	if not world.GetVariable(VariableName):
		VariableValue = 1
	else:
		VariableValue = int(world.GetVariable(VariableName))

	if VariableDesc == "Unskilled": 
		if VariableValue > 3: world.SetVariable(VariableName, '3')
		else: world.SetVariable(VariableName, '1')
	elif VariableDesc == "A Tyro": 
		if VariableValue < 3: world.SetVariable(VariableName, '4')
	elif VariableDesc == "A Novice": 
		if VariableValue < 9: world.SetVariable(VariableName, '10')
	elif VariableDesc == "A Beginner": 
		if VariableValue < 17: world.SetVariable(VariableName, '18')
	elif VariableDesc == "Poor": 
		if VariableValue < 29: world.SetVariable(VariableName, '30')
	elif VariableDesc == "Not Very Good": 
		if VariableValue < 49: world.SetVariable(VariableName, '50')
	elif VariableDesc == "Below Average": 
		if VariableValue < 69: world.SetVariable(VariableName, '70')
	elif VariableDesc == "Average": 
		if VariableValue < 89: world.SetVariable(VariableName, '90')
	elif VariableDesc == "Above Average": 
		if VariableValue < 109: world.SetVariable(VariableName, '110')
	elif VariableDesc == "Able": 
		if VariableValue < 134: world.SetVariable(VariableName, '135')
	elif VariableDesc == "Fair": 
		if VariableValue < 164: world.SetVariable(VariableName, '165')
	elif VariableDesc == "Proficient": 
		if VariableValue < 199: world.SetVariable(VariableName, '200')
	elif VariableDesc == "Good": 
		if VariableValue < 239: world.SetVariable(VariableName, '240')
	elif VariableDesc == "Adroit": 
		if VariableValue < 284: world.SetVariable(VariableName, '285')
	elif VariableDesc == "Very Good": 
		if VariableValue < 334: world.SetVariable(VariableName, '335')
	elif VariableDesc == "Excellent": 
		if VariableValue < 389: world.SetVariable(VariableName, '390')
	elif VariableDesc == "An Expert": 
		if VariableValue < 449: world.SetVariable(VariableName, '450')
	elif VariableDesc == "Superb": 
		if VariableValue < 514: world.SetVariable(VariableName, '515')
	elif VariableDesc == "A Master": 
		if VariableValue < 584: world.SetVariable(VariableName, '585')
	elif VariableDesc == "Eminent": 
		if VariableValue < 659: world.SetVariable(VariableName, '660')
	elif VariableDesc == "An Adept": 
		if VariableValue < 739: world.SetVariable(VariableName, '740')
	elif VariableDesc == "Renowned": 
		if VariableValue < 824: world.SetVariable(VariableName, '825')
	elif VariableDesc == "A High Master": 
		if VariableValue < 919: world.SetVariable(VariableName, '920')
	elif VariableDesc == "Consummate": 
		if VariableValue < 1049: world.SetVariable(VariableName, '1050')
	elif VariableDesc == "A Virtuoso": 
		if VariableValue < 1199: world.SetVariable(VariableName, '1200')
	elif VariableDesc == "A Grand Master": 
		if VariableValue < 1399: world.SetVariable(VariableName, '1400')
	elif VariableDesc == "Legendary": 
		if VariableValue <1700: world.SetVariable(VariableName, '1700')

	world.ColourTell ('#CCCCCC', '', VariableStringName + ':' + TempSpace + VariableDesc)
	world.ColourTell('#FFFFFF', '', ' [')
	world.ColourTell('#FF0000', '', world.GetVariable(VariableName))
	world.ColourNote('#FFFFFF', '', ']')


]]>
</script>


USA #5
I'm pretty sure you just need to add the save_state="y" flag in the <plugin> XML tag near the top. MUSH automatically saves variables set via SetVariable as a statefile.
#6
Wow, I feel like an idiot. I just saw that option in there. Of course after I changed it, I somehow messed something up and my default plugin and world directories gave me some trouble. But I'm up and running now, thank you.
USA #7
Sure! Glad to help.
Amended on Thu 16 Apr 2009 06:55 PM by Twisol
#8
An update, everything seems to be working now. But just incase you thought of doing it... be careful if you want to just copy those .mcl files... trying to save time and copy characters I didn't realize I was shooting myself in the foot, as it also copied the id used for the save state variables ;)

On another note, do you happen to know if my fix of just generating new id's and copying them in is going to hold up, or will I have to redo everything?
USA #9
Oh, you can just do File -> New World, which will pop up a dialog asking if you want to load a pre-existing world to base the new one off of. :) But I think your ID-changing thing should work OK, though that's mostly just an educated guess.
Australia Forum Administrator #10
Quote:

Any chance you can write/have written a forum code for hyperlinks?


Without more sophisticated parsing of forum codes than I currently do, it could make a mess if you have the opening tag but not the closing one.

Quote:

But just incase you thought of doing it... be careful if you want to just copy those .mcl files... trying to save time and copy characters I didn't realize I was shooting myself in the foot, as it also copied the id used for the save state variables ;)


Just open a world file and do File -> Save As - that makes a copy and the copy will (should?) get a new ID automatically.