Class: MobCreator
- Inherits:
-
Object
- Object
- MobCreator
- Defined in:
- app/models/mob_creator.rb
Overview
Factory class of Mob. noinspection ALL
Class Method Summary (collapse)
-
+ (Object) assign_new_mobs(num, user_id = nil, specie_id = nil)
Assign new (first genartion) mobs to user.
-
+ (Object) mate_mob(father_id, mother_id)
Create new mob by mating.
-
+ (Object) random_mate_mob(num, user_id = nil, specie_id = nil)
Create new mob by mating.
-
+ (Object) random_split_mob(num, user_id = nil, specie_id = nil)
Create new mob by splitting.
-
+ (Object) seed_mobs(specie_id, num)
Create seed mobs.
-
+ (Boolean) specie_able_to_mate?(user_id, specie_id)
Check the specie whether it is able to mate or not.
-
+ (Object) split_mob(mother_id)
Create new mob by splitting.
Class Method Details
+ (Object) assign_new_mobs(num, user_id = nil, specie_id = nil)
Assign new (first genartion) mobs to user. The first genartion mob is a mob which is created from seed mobs.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/mob_creator.rb', line 68 def self.assign_new_mobs(num, user_id = nil, specie_id = nil) # Validation return false if num < 1 if specie_id.blank? specie_id = Specie.all.sample.id end num.times do mob = Mob.new mob.specie_id = specie_id mob.houdd_user_id = user_id if mob.specie.family.asexual_flg mob.mother_id = Mob.find_all_by_specie_id_and_generation(specie_id, nil).sample.id else mob.father_id = Mob.find_all_by_specie_id_and_generation_and_gender_sym(specie_id, nil, Params::GENDER_MALE.to_s).sample.id mob.mother_id = Mob.find_all_by_specie_id_and_generation_and_gender_sym(specie_id, nil, Params::GENDER_FEMALE.to_s).sample.id end # Create status modifying gene. mod_gene = Gene.new gene_a = nil gene_b = nil if mob.specie.family.asexual_flg mod_gene = mob.mother.dnas.first.gene else gene_a = mob.mother.dnas.sample.gene gene_b = mob.father.dnas.sample.gene if gene_a.dominance_flg and not gene_b.dominance_flg mod_gene = gene_a elsif not gene_a.dominance_flg and gene_b.dominance_flg mod_gene = gene_b else mod_gene = gene_a + gene_b end end # Effect of miss print mod_gene = shake_gene(mod_gene, mob.specie.mutation_rate) # Create starting status. mob.squad_id = nil mob.generation = 1 mob = new_mob(mob, mod_gene) # Set DNA. if mob.specie.family.asexual_flg dna = Dna.new dna.mob_id = mob.id dna.gene_id = mod_gene.id dna.save mob.dnas << dna else dna_a = Dna.new dna_a.mob_id = mob.id dna_a.gene_id = gene_a.id dna_a.save mob.dnas << dna_a dna_b = Dna.new dna_b.mob_id = mob.id dna_b.gene_id = gene_b.id dna_b.save mob.dnas << dna_b end end end |
+ (Object) mate_mob(father_id, mother_id)
This method is only for mobs of sexual reproduction.
Create new mob by mating.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'app/models/mob_creator.rb', line 227 def self.mate_mob(father_id, mother_id) mob = Mob.new mob.father_id = father_id mob.mother_id = mother_id mob.specie_id = mob.mother.specie.id mob.houdd_user_id = mob.mother.houdd_user_id mob.generation = mob.mother.generation + 1 gene_a = mob.mother.dnas.sample.gene gene_b = mob.father.dnas.sample.gene if gene_a.dominance_flg and not gene_b.dominance_flg mod_gene = gene_a elsif not gene_a.dominance_flg and gene_b.dominance_flg mod_gene = gene_b else mod_gene = gene_a + gene_b end # Effect of miss print mod_gene = shake_gene(mod_gene, mob.specie.mutation_rate) # Create starting status. mob.squad_id = nil mob = new_mob(mob, mod_gene) # Set DNA. dna_a = Dna.new dna_a.mob_id = mob.id dna_a.gene_id = gene_a.id dna_a.save mob.dnas << dna_a dna_b = Dna.new dna_b.mob_id = mob.id dna_b.gene_id = gene_b.id dna_b.save mob.dnas << dna_b end |
+ (Object) random_mate_mob(num, user_id = nil, specie_id = nil)
This method calls mate_mob function after selected mating pair in this function.
Create new mob by mating.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/mob_creator.rb', line 161 def self.random_mate_mob(num, user_id = nil, specie_id = nil) # Validation return false if num < 1 if user_id.blank? user_id = HouddUser.all.sample.id end user = HouddUser.find(user_id) if specie_id.blank? specie_id = user.sexual_species.sample.id end return false if specie_id.blank? num.times do father = Mob.find_all_by_houdd_user_id_and_specie_id_and_gender_sym(user_id, specie_id, Params::GENDER_MALE.to_s).sample mother = Mob.find_all_by_houdd_user_id_and_specie_id_and_gender_sym(user_id, specie_id, Params::GENDER_FEMALE.to_s).sample if father.blank? or mother.blank? return false else mate_mob(father.id, mother.id) end end end |
+ (Object) random_split_mob(num, user_id = nil, specie_id = nil)
This method calls split_mob function after selected mob in this function.
Create new mob by splitting.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/models/mob_creator.rb', line 195 def self.random_split_mob(num, user_id = nil, specie_id = nil) # Validation return false if num < 1 if user_id.blank? user_id = HouddUser.all.sample.id end user = HouddUser.find(user_id) if specie_id.blank? specie_id = user.asexual_species.sample.id end return false if specie_id.blank? num.times do mother = Mob.find_all_by_houdd_user_id_and_specie_id(user_id, specie_id).sample if mother.blank? return false else split_mob(mother.id) end end end |
+ (Object) seed_mobs(specie_id, num)
Create seed mobs.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/mob_creator.rb', line 8 def self.seed_mobs(specie_id, num) return false if num < 1 specie = Specie.find(specie_id) return false if specie.blank? num.times do # Create mob. mob = Mob.new mob.specie_id = specie.id # Create genes and dnas. dna_quantity = Params::MOB_DNA_QUANTITY dna_quantity = 1 if mob.specie.family.asexual_flg dna_quantity.times do gene = Gene.new gene.specie_id = specie.id gene.dominance_flg = Tools.random_boolean gene = shake_gene(gene, mob.specie.mutation_rate) gene.save p gene dna = Dna.new dna.mob_id = mob.id dna.gene_id = gene.id dna.save mob.dnas << dna end # Create status modifying gene. mod_gene = Gene.new if mob.specie.family.asexual_flg mod_gene = mob.dnas.first.gene elsif mob.dnas.first.gene.dominance_flg and not mob.dnas.last.gene.dominance_flg mod_gene = mob.dnas.first.gene elsif not mob.dnas.first.gene.dominance_flg and mob.dnas.last.gene.dominance_flg mod_gene = mob.dnas.last.gene else mod_gene = mob.dnas.first.gene + mob.dnas.last.gene end # Create starting status. mob.specie_id = specie.id mob.father_id = nil mob.mother_id = nil mob.houdd_user_id = nil mob.squad_id = nil mob = new_mob(mob, mod_gene) end end |
+ (Boolean) specie_able_to_mate?(user_id, specie_id)
“able to mate” means both father mob and mother mob are existing.
Check the specie whether it is able to mate or not.
145 146 147 148 149 150 151 152 153 154 |
# File 'app/models/mob_creator.rb', line 145 def self.specie_able_to_mate?(user_id, specie_id) return false if user_id.blank? or specie_id.blank? father = Mob.find_all_by_houdd_user_id_and_specie_id_and_gender_sym(user_id, specie_id, Params::GENDER_MALE.to_s).sample mother = Mob.find_all_by_houdd_user_id_and_specie_id_and_gender_sym(user_id, specie_id, Params::GENDER_FEMALE.to_s).sample return false if father.blank? or mother.blank? return true end |
+ (Object) split_mob(mother_id)
This method is only for mobs of asexual reproduction.
Create new mob by splitting.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'app/models/mob_creator.rb', line 270 def self.split_mob(mother_id) mob = Mob.new mob.father_id = nil mob.mother_id = mother_id mob.specie_id = mob.mother.specie.id mob.houdd_user_id = mob.mother.houdd_user_id mob.generation = mob.mother.generation + 1 mod_gene = mob.mother.dnas.first.gene # Effect of miss print mod_gene = shake_gene(mod_gene, mob.specie.mutation_rate) # Create starting status. mob.squad_id = nil mob = new_mob(mob, mod_gene) # Set DNA. dna = Dna.new dna.mob_id = mob.id dna.gene_id = mod_gene.id dna.save mob.dnas << dna end |