module RpgGame
Constants
- PLUGIN
- STYLE_TITLE
Public Instance Methods
Source
# File lib/rpg_game.rb, line 170 def choose(data:, choice:) return { error: "Game over — !rpg start" } if data["done"] r = room(data["room"]) opt = r[:options][choice - 1] return { error: "Invalid choice." } unless opt if opt[:needs] && !Array(data["inventory"]).include?(opt[:needs]) return { error: "You need #{opt[:needs]} for that." } end data = data.dup data["moves"] = data["moves"].to_i + 1 inv = Array(data["inventory"]).dup inv << opt[:item] if opt[:item] inv.delete(opt[:remove]) if opt[:remove] data["inventory"] = inv.uniq data["room"] = opt[:next] data["done"] = true if %w[win end].include?(opt[:next]) { data: data, message: format_room(data) } end
Source
# File lib/rpg_game.rb, line 192 def format_header(title) IrcFormat.report_header(tag: "RPG", title: title, style: STYLE_TITLE) end
Source
# File lib/rpg_game.rb, line 155 def format_room(data) r = room(data["room"]) lines = [format_header("One-Line RPG")] lines << r[:text] unless data["done"] || r[:options].empty? r[:options].each_with_index do |opt, i| lines << "#{i + 1}. #{opt[:label]}" end lines << "Reply: !rpg <1-3>" end inv = Array(data["inventory"]) lines << IrcFormat.report_footer("moves #{data['moves']}", inv.empty? ? "no items" : "items: #{inv.join(', ')}") lines.join("\n") end
Source
# File lib/rpg_game.rb, line 137 def load_player(network:, channel:, user:, store: RabbotDb) key = "player:#{GameStore.norm_nick(user)}" GameStore.load(plugin: PLUGIN, network: network, channel: channel, key: key, store: store) end
Source
# File lib/rpg_game.rb, line 147 def new_game { "room" => "start", "inventory" => [], "moves" => 0, "done" => false } end
Source
# File lib/rpg_game.rb, line 119 def parse_command(text) parts = text.to_s.strip.split(/\s+/) return { action: :help } if parts.empty? case parts[0].downcase when "help" { action: :help } when "start", "new" { action: :start } when "status" { action: :status } when /\A[1-3]\z/ { action: :choose, choice: parts[0].to_i } else { error: usage_message } end end
Source
# File lib/rpg_game.rb, line 151 def room(id) RpgRooms::ROOMS[id] || RpgRooms::ROOMS["start"] end
Source
# File lib/rpg_game.rb, line 142 def save_player(network:, channel:, user:, data:, store: RabbotDb) key = "player:#{GameStore.norm_nick(user)}" GameStore.save(plugin: PLUGIN, network: network, channel: channel, key: key, game: data, store: store) end
Source
# File lib/rpg_game.rb, line 115 def usage_message "Usage: !rpg start | !rpg <1-3> | !rpg status" end