EnderWiggins
EnderWiggins

Reputation: 390

Where to put complex logic in meteorJs application? How to approach the design of the application?

Background:
I am a meteorJs newbie and only built the basic applications with it - Leader Board and Todo list by following the tutorials online. Now that I have some understanding of the basics, I am building an online strategy game based out of meteor. However, I am not familiar with design patterns of Meteor Applications.

Problem:
There are two opposing generals and they command various units. A fair bit of logic is needed to determine the outcome of battle between them.

I have this for now
: Based on the FrontEnd user interaction, I call meteor methods to update General collection. The code below shows how I may add 1 infantry to the general's army:

  Template.generalData.events({
   'click #button' : function(){
      var selectedGeneral = Session.get('selectedGeneral');
      Meteor.call('modifyTroops', selectedGeneral, 'infantry', 1)
    },
      // more events 
   });

and on server side:

    if (Meteor.isServer) {   
     Meteor.methods({
        'modifyTroops' : function(selectedGeneral, unit, val){
          obj = {};
          obj[unit] = val;
          Generals.update({_id:selectedGeneral}, {$inc: obj});
        },
              // more methods   
    });

This is all easy .. I can even choose which two generals will fight each other once their army has been finalized.

 Like so: 
   Template.battleGenerals.events({
   'click #fight' :function(){
          var selectedGeneral1 = Session.get('selectedGeneral1');
          var selectedGeneral2 = Session.get('selectedGeneral2');
          var General1 = Generals.findOne(selectedGeneral1);
          var General2 = Generals.findOne(selectedGeneral2);
          // more code 
          // call a method?
     }
  });

So where do I put the battle logic? in a method? and method only makes changes to the MongoDB backend. I need to reactively show what unit kills what unit (not graphics - just text explaining what is going on) . How do I do that?

Moreover I have 14 types units under each general , so I cannot put all logic everything under one method. I will need to use functions to divide up my logic code and call them where needed in the method?

Another thing I have been thinking of using is mediator pattern and putting all logic in packages. After reading this article : http://www.manuel-schoebel.com/blog/meteorjs-package-only-app-structure-with-mediator-pattern I have more questions. Is that the right way to go? if so - where in packages exactly does the logic go?

If anyone has any idea how to approach this, please share your thoughts.

Thanks.

Upvotes: 0

Views: 167

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 151855

Splitting your code into packages is a strategy that pays off in the long run. You don't need to publish packages - you can keep them private in your repo, under a packages directory.

Upvotes: 1

Related Questions