require "BaseObject.rb" require "Item.rb" require "More.rb" module RubyMud class BookReader < More # A fancier version of more which allows the user to go to specific pages. # Skipping to specific pages is not really suitable for general use of More def initialize(clientconnection, text) super(clientconnection,text,pagesize=25) end def Recieve(line) line = line.strip line = line.downcase if line == 'q' SignOff() return elsif line == 'n' @curpage+=1 DisplayPage(@curpage) elsif # try and convert to a number pagenum = line.to_i() if pagenum >= 1 and pagenum <= @page.length DisplayPage(pagenum) elsif pagenum <= 0 @clientconnection.Send("[red]Books usually have a [bold cyan]non-zero,positive[red] number of pages.\n") else @clientconnection.Send("[red]The book does not have that many pages.\n") end end end def DisplayFooter() @clientconnection.Send("\n"+"--- [white]<q>[] to stop,[white]<n>[] for next page ---\n") @clientconnection.Send("--- [white]<number>[] to jump to a page ---\n") end def DisplayPage(pagenum) # we need to override DisplayPage because More # closes itself when it reaches the last page page = @page[pagenum] if page == nil @clientconnection.Send("[red]Not a valid page.\n") return end page.each { |line| @clientconnection.Send(line+"\n") } DisplayFooter() end end class Book < Item # Overload the baseobject description def description return "The book is titled \"#{@name}\" and has #{@pages} pages."; end # Passes through the text file # Updates the number of lines def updateBookInfo() @numlines = 0 IO.foreach("Books\\"+@filename) do |line| @numlines += 1 end @pages = (@numlines/25)+1 end # Constructor def initialize(parent,name,filename) super(parent,name,"") @filename = filename @numlines = 0 @pages = 0 updateBookInfo() end def filename @filename end def name @name end def numLines @NumLines end def read(clientConnection,page) lines = "" IO.foreach("Books\\"+@filename) do |line| lines += line end bookreader = BookReader.new(clientConnection,lines) clientConnection.addInputHandler(bookreader) end end end #module