CodeIgniter Forums
Is that right way to inject data from models to another model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Is that right way to inject data from models to another model? (/showthread.php?tid=69354)



Is that right way to inject data from models to another model? - emilovbg - 11-08-2017

Hi, Im new CI user and writing php is my hobby from some years. I want to make football manager to play with my friends. So the most important part of my code is my Match Engine, where I calculate data from my players, team tactics, reffer, weather and etc. Have 8 leagues, 16 teams every league and 8 matches per round/league every day. I make controller that is a cronjob:

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Engine extends CI_Controller {
   
   public function __construct() {
      parent::__construct();
      $this->load->model('League_model', 'league');
      $this->load->model('Fixture_model', 'fixture');
      $this->load->model('Team_model', 'team');
      $this->load->model('Player_model', 'player');
      $this->load->model('Reffer_model', 'reffer');
      $this->load->model('Match_model', 'match');
   }
      
   public function run($id)
   {      
             //load the current league
           $league = $this->league->getLeague($id);

              //load all fixtures from this league for this round
           $fixtures = $this->fixture->getFixtures($league['id_league'], $league[$current_round]);
           
           foreach ($fixtures as $fixture) {
               
              //load reffer, home team and away team for every match
           $reffer = $this->reffer->getReffer($fixture['id_reffer']);
           
           $homeTeamData = $this->team->getTeam($fixture['id_homeTeam']);
           $homePlayerData = $this->player->getPlayer($homeTeamData['id']);
           
           $awayTeamData = $this->team->getTeam($fixture['id_awayTeam']);
           $awayPlayerData = $this->player->getPlayer($awayTeamData['id']);
             
              //all data is passed to my match engine where match result is calculated
           $resultData = $this->match->start($reffer,$homeTeamData, $homePlayerData,$awayTeamData, $awayPlayerData);  
           }        
   }
 
}
 
I that the right way to load all data to my Match_model, where I have a lot of methods that calculate the match result.  My first version was to load league_model, in league_model load fixture_model and etc... big nest ot claasses hard to debug. 

Is there better way to do that?


RE: Is that right way to inject data from models to another model? - PaulD - 11-09-2017

I am no expert at this, but I found your question really interesting, and what a fabulous project for a hobby project. Your method seems very good to me. I would say there is no 'right' way, which is part of the beauty of CI.

Just out of interest, how do you calculate the results of a particular match?

Paul


RE: Is that right way to inject data from models to another model? - emilovbg - 11-09-2017

It`s very complex system based on events.
Every event (pass, tackle, shoot, foul, yellow card, throw in, corner...etc) has a its own method and boolean property.

There is also base method - > eventManager - here engine decide what next on match (if event === true - call this method, if match minute >= 90 adintional time => end the match)

Each event is attack/defend player based. Both players moment skill (min - max) are compared - based on the skills, fitness (less = less current skill), moral (less moral = less max skill), penalty if player is not on his position, penalty/bonuses based of team tactic, some random stuff. Eache team has tempo of play - this is related to how long is one or other event. Each event decide what next event to make TRUE - each event update statistics of teams and players,

I have two more important properties - where is the ball, and who own the ball(home away team)

One of the team can bribe the refer, then this team will won, unless is has weaker players and tactics.

There i some other stuff, who give advantages of some player/team.

Sorry for my english Smile


RE: Is that right way to inject data from models to another model? - PaulD - 11-09-2017

Wow, that sounds amazing. An incredible level of detail. It sounds like a real labour of love.

Your English is excellent btw.

Wishing great success with it. Perhaps you could make a public access website for it and let us all join in playing! If you have, would love to have a look at it.

Best wishes,

Paul


RE: Is that right way to inject data from models to another model? - InsiteFX - 11-10-2017

If you are always using your models you can autoload them and they will always be available.


RE: Is that right way to inject data from models to another model? - XtreemDeveloper - 12-20-2017

No issue, You can call all methods in single model as you want but for great you can handle these calculation by making a library file match calculation. And if you really using models you this than you can autoload this model and use anywhere with load again.