Skip to content

Instantly share code, notes, and snippets.

@boj
Created January 8, 2010 11:01
Show Gist options
  • Save boj/271981 to your computer and use it in GitHub Desktop.
Save boj/271981 to your computer and use it in GitHub Desktop.
class Achievement
include MongoMapper::Document
key :name, String
key :description, String
key :requirements, String
key :lang, String
end
class AchievementSystem
# Scans to see if the character has completed any Achievements of type
def self.scan(player, type)
completed = []
achievements = Achievement.find(:all, :type => type)
for achievement in achievements
found = false
player.character_achievements.each do |pach|
if pach.achievement_id == achievement.id
found = true
end
end
next if found
case type
when "FAMILIAR"
completed << achievement if eval("player.character_familiars.count %s" % achievement.requirements)
when "QUEST"
completed_quests = player.character_quests.find(:all, :is_completed => true)
completed << achievement if eval("completed_quests.size %s" % achievement.requirements)
when "GOLD"
completed << achievement if eval("player.total_gold %s" % achievement.requirements)
end
end
return completed.collect do |ach|
CharacterAchievement.create(:character_id => player.id, :achievement_id => ach.id)
end
end
end
- name: A Familiar!
description: Received first familiar.
requirements: " == 1"
type: FAMILIAR
- name: A Quest!
description: Completed first quest.
requirements: " == 1"
type: QUEST
- name: The Rich Get Richer
description: Received 100 gold.
requirements: " >= 100"
type: GOLD
#######################################
# Achievements
#######################################
puts "Populating Achievements..."
achievements = YAML.load(File.open("#{RAILS_ROOT}/db/load/en/achievements.yml"))
for achievement in achievements
Achievement.create(
:name => achievement['name'],
:description => achievement['description'],
:requirements => achievement['requirements'],
:type => achievement['type'],
:lang => 'en'
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment