Welcome, Guest. Please login or register.
Kingdoms of Ahln
Pages: [1]
  Print  
Author Topic: What's wrong with this code?  (Read 597 times)
Saffire
Citizen
***
Posts: 136


View Profile
« on: June 08, 2009, 01:14:47 pm »

Code:
def ahelp_edit(event,player,room)
topic = event[:object].strip.downcase
help_item = "help/#{topic}.help"
if File.exist? help_item
player.editor(IO.read(help_item) || [], 200) do |data|
unless data.nil?
content = data.strip
end
player.output("You finish writing the helpfile.")
end
else
player.output "Help file not found."
end
end

Everything about it works great so far, except that when I type *save after I finish editing a help file, it doesn't actually save the new content!

It's been driving me bonkers for at least a week now.
Logged
Justin
Administrator
Citizen
*****
Posts: 189



View Profile
« Reply #1 on: June 08, 2009, 01:28:52 pm »

You need to write the content back out to the file at some point.

Something like this:
Code:
                                player.editor(IO.read(help_item) || [], 200) do |data|
                                        unless data.nil?
                                             File.open help_item, "w" do |f|
                                                    f.puts data
                                             end
                                         end
                                        player.output("You finish writing the helpfile.")
                                end

Edit: My mistake, you do need to check if data is nil.
« Last Edit: June 08, 2009, 01:31:27 pm by Justin » Logged
Saffire
Citizen
***
Posts: 136


View Profile
« Reply #2 on: June 08, 2009, 01:38:51 pm »

That works perfectly. As you can see I'm still not too great at programming, but I'm getting there Tongue

You can use the code for editing help files online if you like. I was going to put it up in suggestions once I finished it. Here's the complete code:

In components/commandparser.rb:

In the admin commands list, add
Code:
'ahelp',

wherever you like.

In
Code:
def parse_admin(input)

add the following:
Code:
when /^ahelp\s+edit\s+(.*)/i
event[:action] = :ahelp_edit
event[:object] = $1

In events/admin.rb:
Code:
def ahelp_edit(event,player,room)
topic = event[:object].strip.downcase
help_item = "help/#{topic}.help"
if File.exist? help_item
player.editor(IO.read(help_item) || [], 200) do |data|
unless data.nil?
content = data.strip
end
File.open help_item, "w" do |f|
f.puts data
end
player.output("You finish writing the helpfile.")
end
else
player.output "Help file not found."
end
end
Logged
Justin
Administrator
Citizen
*****
Posts: 189



View Profile
« Reply #3 on: June 08, 2009, 01:44:47 pm »

Which version are you using? Because in 0.2.3 there is already an AHELP command for viewing admin help files.

Also, you don't want to write out to the file if data is nil. I should have checked before posting (sorry), but data will be nil if the person quits the editor without saving.
Logged
Justin
Administrator
Citizen
*****
Posts: 189



View Profile
« Reply #4 on: June 08, 2009, 01:45:16 pm »

I need to stop posting before I'm done with my thought. I also wanted to say thanks for contributing code Smiley
Logged
Saffire
Citizen
***
Posts: 136


View Profile
« Reply #5 on: June 08, 2009, 01:55:15 pm »

Thanks for pointing that out. I posted before I saw your edit, so I got a little confused as to where to put the code to make it work. It's fixed now though!

Also, I'm not using any specific version. I started with the first ever version that was released, and after that I took what I wanted from each new version and left out what I didn't want (I took most of what you released in each version, though), and then edited a bunch of it to almost beyond recognition.

I have the ahelp command. But that command is only for viewing admin help files. This ahelp command isn't for viewing admin help files, it's for editing normal help files. I was going to add functionality for editing admin help files with it as well, but I'm incredibly paranoid about letting other admins edit those help files for some reason.

And no problem about the contributing; I figure I have a bunch to post before I catch up to what you've done Cheesy
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.15 | SMF © 2006-2009, Simple Machines
Terra97 design by Bloc