开发者

Is polymorphic the best choice?

开发者 https://www.devze.com 2023-02-04 08:21 出处:网络
Hey guys, i\'m creating a browser game where battles between monsters and users(pvp) take place. The Monster model has all the functions that relate to the battle against monsters. I\'m now creating t

Hey guys, i'm creating a browser game where battles between monsters and users(pvp) take place. The Monster model has all the functions that relate to the battle against monsters. I'm now creating the PvP system and the battle system is the same as in Monsters. So, i was thinking of creating some sort of an abstract presentation of the battle, maybe a Combatable Model (?) and make a polymorphic association.开发者_高级运维

Do you think this is the strategy i should follow, or i could do it in a better manner ?


I would say that you should create a Combatant class or module that you can inherit from or include in your Person and Monster classes, but you shouldn't persist a polymorphic association into the database. You should only use two tables: people and monsters.

module Combatant
...
end

class Person < ActiveRecord::Base
  include Combatant
  ...
end

class Monster < ActiveRecord::Base
  include Combatant
  ...
end


Another option is Single Table Inheritance

class Combatant < ActiveRecord::Base
  # Has all the fields we care about and basic damage rules, etc
end

class Person < Combatant
    # Any functions that only players can do
end

class Monster < Combatant
    # any functions that need to be overridden
end
0

精彩评论

暂无评论...
验证码 换一张
取 消