home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Esportes / CrossingCup.swf / scripts / __Packages / CTournament.as < prev    next >
Encoding:
Text File  |  2007-12-11  |  15.9 KB  |  499 lines

  1. class CTournament
  2. {
  3.    var tournamentPhase;
  4.    var currentGroupFixture;
  5.    var gameover;
  6.    var secondRoundMatches;
  7.    var quarterFinalMatches;
  8.    var semiFinalMatches;
  9.    var FinalMatches;
  10.    var firstRoundMatches;
  11.    var currentMatch;
  12.    static var _oI;
  13.    function CTournament()
  14.    {
  15.       this.tournamentPhase = -1;
  16.    }
  17.    function newTournament()
  18.    {
  19.       this.tournamentPhase = 1;
  20.       this.currentGroupFixture = 0;
  21.       this.gameover = false;
  22.       this.createFirstRoundMatches();
  23.       this.secondRoundMatches = new Array();
  24.       this.quarterFinalMatches = new Array();
  25.       this.semiFinalMatches = new Array();
  26.       this.FinalMatches = new Array();
  27.       this.gotoNextMatch();
  28.    }
  29.    function endTournament()
  30.    {
  31.       this.gameover = true;
  32.    }
  33.    function createFirstRoundMatches()
  34.    {
  35.       this.firstRoundMatches = new Array();
  36.       this.currentMatch = null;
  37.       var _loc2_ = 0;
  38.       while(_loc2_ < 8)
  39.       {
  40.          var _loc6_ = CTeamManager.ins().teams[_loc2_ * 4 + 0];
  41.          var _loc5_ = CTeamManager.ins().teams[_loc2_ * 4 + 1];
  42.          var _loc4_ = CTeamManager.ins().teams[_loc2_ * 4 + 2];
  43.          var _loc3_ = CTeamManager.ins().teams[_loc2_ * 4 + 3];
  44.          _loc6_.restartTeam(_loc2_);
  45.          _loc5_.restartTeam(_loc2_);
  46.          _loc4_.restartTeam(_loc2_);
  47.          _loc3_.restartTeam(_loc2_);
  48.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc6_,_loc4_));
  49.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc5_,_loc3_));
  50.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc6_,_loc5_));
  51.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc3_,_loc4_));
  52.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc3_,_loc6_));
  53.          this.firstRoundMatches.push(new CMatch(_loc2_,_loc4_,_loc5_));
  54.          _loc2_ = _loc2_ + 1;
  55.       }
  56.    }
  57.    function createSecondRoundMatches()
  58.    {
  59.       this.secondRoundMatches = new Array();
  60.       this.currentMatch = null;
  61.       var _loc2_ = undefined;
  62.       var _loc6_ = undefined;
  63.       var _loc7_ = undefined;
  64.       var _loc8_ = undefined;
  65.       var _loc9_ = undefined;
  66.       var _loc3_ = 0;
  67.       while(_loc3_ < 8)
  68.       {
  69.          var _loc5_ = this.firstRoundMatches[_loc3_ * 6];
  70.          var _loc4_ = this.firstRoundMatches[_loc3_ * 6 + 1];
  71.          _loc2_ = new Array();
  72.          _loc2_.push(_loc5_.team1);
  73.          _loc2_.push(_loc5_.team2);
  74.          _loc2_.push(_loc4_.team1);
  75.          _loc2_.push(_loc4_.team2);
  76.          _loc2_.sortOn("points",Array.NUMERIC | Array.DESCENDING);
  77.          _loc6_ = _loc2_[0];
  78.          _loc7_ = _loc2_[1];
  79.          _loc5_ = this.firstRoundMatches[(_loc3_ + 1) * 6];
  80.          _loc4_ = this.firstRoundMatches[(_loc3_ + 1) * 6 + 1];
  81.          _loc2_ = new Array();
  82.          _loc2_.push(_loc5_.team1);
  83.          _loc2_.push(_loc5_.team2);
  84.          _loc2_.push(_loc4_.team1);
  85.          _loc2_.push(_loc4_.team2);
  86.          _loc2_.sortOn("points",Array.NUMERIC | Array.DESCENDING);
  87.          _loc8_ = _loc2_[0];
  88.          _loc9_ = _loc2_[1];
  89.          this.secondRoundMatches.push(new CMatch(0,_loc6_,_loc9_));
  90.          this.secondRoundMatches.push(new CMatch(0,_loc8_,_loc7_));
  91.          _loc3_ += 2;
  92.       }
  93.    }
  94.    function createQuarterFinalMatches()
  95.    {
  96.       this.quarterFinalMatches = new Array();
  97.       this.currentMatch = null;
  98.       this.quarterFinalMatches.push(new CMatch(0,this.winnerof(this.secondRoundMatches[0]),this.winnerof(this.secondRoundMatches[2])));
  99.       this.quarterFinalMatches.push(new CMatch(0,this.winnerof(this.secondRoundMatches[1]),this.winnerof(this.secondRoundMatches[3])));
  100.       this.quarterFinalMatches.push(new CMatch(0,this.winnerof(this.secondRoundMatches[4]),this.winnerof(this.secondRoundMatches[6])));
  101.       this.quarterFinalMatches.push(new CMatch(0,this.winnerof(this.secondRoundMatches[5]),this.winnerof(this.secondRoundMatches[7])));
  102.    }
  103.    function createSemiFinalMatches()
  104.    {
  105.       this.semiFinalMatches = new Array();
  106.       this.currentMatch = null;
  107.       this.semiFinalMatches.push(new CMatch(0,this.winnerof(this.quarterFinalMatches[0]),this.winnerof(this.quarterFinalMatches[2])));
  108.       this.semiFinalMatches.push(new CMatch(0,this.winnerof(this.quarterFinalMatches[1]),this.winnerof(this.quarterFinalMatches[3])));
  109.    }
  110.    function createFinalMatches()
  111.    {
  112.       this.FinalMatches = new Array();
  113.       this.currentMatch = null;
  114.       this.FinalMatches.push(new CMatch(0,this.winnerof(this.semiFinalMatches[0]),this.winnerof(this.semiFinalMatches[1])));
  115.    }
  116.    function winnerof(ma)
  117.    {
  118.       if(ma.scoreteam1 > ma.scoreteam2)
  119.       {
  120.          return ma.team1;
  121.       }
  122.       return ma.team2;
  123.    }
  124.    function finishedPhase()
  125.    {
  126.       while(this.tournamentPhase < 8)
  127.       {
  128.          this.nextPhase();
  129.       }
  130.    }
  131.    function nextPhase()
  132.    {
  133.       var _loc2_ = "";
  134.       this.tournamentPhase = this.tournamentPhase + 1;
  135.       this.gotoNextMatch();
  136.       switch(this.tournamentPhase)
  137.       {
  138.          case 4:
  139.             this.createSecondRoundMatches();
  140.             this.currentMatch = this.inCurrentMatches();
  141.             _loc2_ = "";
  142.             if(this.currentMatch == null)
  143.             {
  144.                this.gameover = true;
  145.             }
  146.             else
  147.             {
  148.                _loc2_ = "Congratulation, You have been qualified for playoff round!";
  149.             }
  150.             break;
  151.          case 5:
  152.             this.createQuarterFinalMatches();
  153.             this.currentMatch = this.inCurrentMatches();
  154.             _loc2_ = "";
  155.             if(this.currentMatch == null)
  156.             {
  157.                this.gameover = true;
  158.             }
  159.             else
  160.             {
  161.                _loc2_ = "Congratulation, You\'re going to the quarter final!";
  162.             }
  163.             break;
  164.          case 6:
  165.             this.createSemiFinalMatches();
  166.             this.currentMatch = this.inCurrentMatches();
  167.             _loc2_ = "";
  168.             if(this.currentMatch == null)
  169.             {
  170.                this.gameover = true;
  171.             }
  172.             else
  173.             {
  174.                _loc2_ = "Congratulation, You\'re going to semi final!";
  175.             }
  176.             break;
  177.          case 7:
  178.             this.createFinalMatches();
  179.             this.currentMatch = this.inCurrentMatches();
  180.             _loc2_ = "";
  181.             if(this.currentMatch == null)
  182.             {
  183.                this.gameover = true;
  184.             }
  185.             else
  186.             {
  187.                _loc2_ = "Congratulation, You\'re going to final!";
  188.             }
  189.             break;
  190.          case 8:
  191.             _loc2_ = "";
  192.             this.gameover = true;
  193.       }
  194.       return _loc2_;
  195.    }
  196.    function gotoNextMatch()
  197.    {
  198.       switch(this.tournamentPhase)
  199.       {
  200.          case 1:
  201.          case 2:
  202.          case 3:
  203.          case 4:
  204.             var _loc3_ = false;
  205.             while(!_loc3_ && this.currentGroupFixture < 6)
  206.             {
  207.                var _loc2_ = this.inCurrentMatches();
  208.                if(_loc2_ != null)
  209.                {
  210.                   if(!_loc2_.played)
  211.                   {
  212.                      _loc3_ = true;
  213.                      this.currentMatch = _loc2_;
  214.                      return undefined;
  215.                   }
  216.                   this.simulateMatches();
  217.                }
  218.                else
  219.                {
  220.                   this.simulateMatches();
  221.                }
  222.                this.currentGroupFixture = this.currentGroupFixture + 1;
  223.             }
  224.             break;
  225.          case 5:
  226.          case 6:
  227.          case 7:
  228.          case 8:
  229.             this.simulateMatches();
  230.       }
  231.    }
  232.    function inCurrentMatches()
  233.    {
  234.       if(this.tournamentPhase < 4)
  235.       {
  236.          var _loc2_ = 0;
  237.          while(_loc2_ < 8)
  238.          {
  239.             var _loc3_ = this.firstRoundMatches[_loc2_ * 6 + this.currentGroupFixture];
  240.             if(_loc3_.team1 == CTeamManager.ins().currentTeam || _loc3_.team2 == CTeamManager.ins().currentTeam)
  241.             {
  242.                return _loc3_;
  243.             }
  244.             _loc2_ = _loc2_ + 1;
  245.          }
  246.          return null;
  247.       }
  248.       switch(this.tournamentPhase)
  249.       {
  250.          case 4:
  251.             _loc2_ = 0;
  252.             while(_loc2_ < this.secondRoundMatches.length)
  253.             {
  254.                _loc3_ = this.secondRoundMatches[_loc2_];
  255.                if(_loc3_.team1 == CTeamManager.ins().currentTeam || _loc3_.team2 == CTeamManager.ins().currentTeam)
  256.                {
  257.                   return _loc3_;
  258.                }
  259.                _loc2_ = _loc2_ + 1;
  260.             }
  261.             break;
  262.          case 5:
  263.             _loc2_ = 0;
  264.             while(_loc2_ < this.quarterFinalMatches.length)
  265.             {
  266.                _loc3_ = this.quarterFinalMatches[_loc2_];
  267.                if(_loc3_.team1 == CTeamManager.ins().currentTeam || _loc3_.team2 == CTeamManager.ins().currentTeam)
  268.                {
  269.                   return _loc3_;
  270.                }
  271.                _loc2_ = _loc2_ + 1;
  272.             }
  273.             break;
  274.          case 6:
  275.             _loc2_ = 0;
  276.             while(_loc2_ < this.semiFinalMatches.length)
  277.             {
  278.                _loc3_ = this.semiFinalMatches[_loc2_];
  279.                if(_loc3_.team1 == CTeamManager.ins().currentTeam || _loc3_.team2 == CTeamManager.ins().currentTeam)
  280.                {
  281.                   return _loc3_;
  282.                }
  283.                _loc2_ = _loc2_ + 1;
  284.             }
  285.             break;
  286.          case 7:
  287.             _loc2_ = 0;
  288.             while(_loc2_ < this.FinalMatches.length)
  289.             {
  290.                _loc3_ = this.FinalMatches[_loc2_];
  291.                if(_loc3_.team1 == CTeamManager.ins().currentTeam || _loc3_.team2 == CTeamManager.ins().currentTeam)
  292.                {
  293.                   return _loc3_;
  294.                }
  295.                _loc2_ = _loc2_ + 1;
  296.             }
  297.       }
  298.       return null;
  299.    }
  300.    function simulateMatches()
  301.    {
  302.       if(this.tournamentPhase < 5)
  303.       {
  304.          var _loc2_ = 0;
  305.          while(_loc2_ < 8)
  306.          {
  307.             var _loc3_ = this.firstRoundMatches[_loc2_ * 6 + this.currentGroupFixture];
  308.             if(_loc3_ != this.currentMatch)
  309.             {
  310.                _loc3_.randomMatch(true);
  311.             }
  312.             _loc3_.calculateResult();
  313.             _loc2_ = _loc2_ + 1;
  314.          }
  315.       }
  316.       else
  317.       {
  318.          switch(this.tournamentPhase)
  319.          {
  320.             case 5:
  321.                _loc2_ = 0;
  322.                while(_loc2_ < this.secondRoundMatches.length)
  323.                {
  324.                   _loc3_ = this.secondRoundMatches[_loc2_];
  325.                   if(_loc3_ != this.currentMatch)
  326.                   {
  327.                      _loc3_.randomMatch(false);
  328.                   }
  329.                   _loc3_.calculateResult();
  330.                   _loc2_ = _loc2_ + 1;
  331.                }
  332.                break;
  333.             case 6:
  334.                _loc2_ = 0;
  335.                while(_loc2_ < this.quarterFinalMatches.length)
  336.                {
  337.                   _loc3_ = this.quarterFinalMatches[_loc2_];
  338.                   if(_loc3_ != this.currentMatch)
  339.                   {
  340.                      _loc3_.randomMatch(false);
  341.                   }
  342.                   _loc3_.calculateResult();
  343.                   _loc2_ = _loc2_ + 1;
  344.                }
  345.                break;
  346.             case 7:
  347.                _loc2_ = 0;
  348.                while(_loc2_ < this.semiFinalMatches.length)
  349.                {
  350.                   _loc3_ = this.semiFinalMatches[_loc2_];
  351.                   if(_loc3_ != this.currentMatch)
  352.                   {
  353.                      _loc3_.randomMatch(false);
  354.                   }
  355.                   _loc3_.calculateResult();
  356.                   _loc2_ = _loc2_ + 1;
  357.                }
  358.                break;
  359.             case 8:
  360.                _loc2_ = 0;
  361.                while(_loc2_ < this.FinalMatches.length)
  362.                {
  363.                   _loc3_ = this.FinalMatches[_loc2_];
  364.                   if(_loc3_ != this.currentMatch)
  365.                   {
  366.                      _loc3_.randomMatch(false);
  367.                   }
  368.                   _loc3_.calculateResult();
  369.                   _loc2_ = _loc2_ + 1;
  370.                }
  371.          }
  372.       }
  373.    }
  374.    function getTournamentMatchData(mat)
  375.    {
  376.       var _loc2_ = new Object();
  377.       var _loc3_ = this.getMatchFronCurrentMatchSet(mat);
  378.       _loc2_.team1 = _loc3_.team1.teamname;
  379.       _loc2_.team2 = _loc3_.team2.teamname;
  380.       _loc2_.teamwin = _loc3_.teamwin.teamname;
  381.       return _loc2_;
  382.    }
  383.    function getTournamentGroupData(grp)
  384.    {
  385.       var _loc2_ = new Object();
  386.       var _loc5_ = this.firstRoundMatches[grp * 6];
  387.       var _loc4_ = this.firstRoundMatches[grp * 6 + 1];
  388.       var _loc10_ = this.firstRoundMatches[grp * 6 + 2];
  389.       var _loc9_ = this.firstRoundMatches[grp * 6 + 3];
  390.       var _loc8_ = this.firstRoundMatches[grp * 6 + 4];
  391.       var _loc7_ = this.firstRoundMatches[grp * 6 + 5];
  392.       _loc2_.nat1 = _loc5_.team1.teamname;
  393.       _loc2_.nat3 = _loc5_.team2.teamname;
  394.       _loc2_.nat2 = _loc4_.team1.teamname;
  395.       _loc2_.nat4 = _loc4_.team2.teamname;
  396.       _loc2_.a1 = _loc5_.scoreteam1;
  397.       _loc2_.a2 = _loc5_.scoreteam2;
  398.       _loc2_.b1 = _loc4_.scoreteam1;
  399.       _loc2_.b2 = _loc4_.scoreteam2;
  400.       _loc2_.c1 = _loc10_.scoreteam1;
  401.       _loc2_.c2 = _loc10_.scoreteam2;
  402.       _loc2_.d1 = _loc9_.scoreteam1;
  403.       _loc2_.d2 = _loc9_.scoreteam2;
  404.       _loc2_.e1 = _loc8_.scoreteam1;
  405.       _loc2_.e2 = _loc8_.scoreteam2;
  406.       _loc2_.f1 = _loc7_.scoreteam1;
  407.       _loc2_.f2 = _loc7_.scoreteam2;
  408.       var _loc3_ = new Array();
  409.       _loc3_.push(_loc5_.team1);
  410.       _loc3_.push(_loc5_.team2);
  411.       _loc3_.push(_loc4_.team1);
  412.       _loc3_.push(_loc4_.team2);
  413.       _loc3_.sortOn("points",Array.NUMERIC | Array.DESCENDING);
  414.       _loc2_.win1 = _loc3_[0].teamname;
  415.       _loc2_.win2 = _loc3_[1].teamname;
  416.       _loc2_.win3 = _loc3_[2].teamname;
  417.       _loc2_.win4 = _loc3_[3].teamname;
  418.       _loc2_.w1 = _loc3_[0].won;
  419.       _loc2_.w2 = _loc3_[1].won;
  420.       _loc2_.w3 = _loc3_[2].won;
  421.       _loc2_.w4 = _loc3_[3].won;
  422.       _loc2_.t1 = _loc3_[0].tied;
  423.       _loc2_.t2 = _loc3_[1].tied;
  424.       _loc2_.t3 = _loc3_[2].tied;
  425.       _loc2_.t4 = _loc3_[3].tied;
  426.       _loc2_.l1 = _loc3_[0].lost;
  427.       _loc2_.l2 = _loc3_[1].lost;
  428.       _loc2_.l3 = _loc3_[2].lost;
  429.       _loc2_.l4 = _loc3_[3].lost;
  430.       _loc2_.p1 = _loc3_[0].score;
  431.       _loc2_.p2 = _loc3_[1].score;
  432.       _loc2_.p3 = _loc3_[2].score;
  433.       _loc2_.p4 = _loc3_[3].score;
  434.       return _loc2_;
  435.    }
  436.    function getMatchFronCurrentMatchSet(mat)
  437.    {
  438.       switch(this.tournamentPhase)
  439.       {
  440.          case 4:
  441.             return this.secondRoundMatches[mat];
  442.          case 5:
  443.             return this.quarterFinalMatches[mat];
  444.          case 6:
  445.             return this.semiFinalMatches[mat];
  446.          case 7:
  447.             return this.FinalMatches[mat];
  448.          default:
  449.       }
  450.    }
  451.    function numMatchSet()
  452.    {
  453.       switch(this.tournamentPhase)
  454.       {
  455.          case 4:
  456.             return 16;
  457.          case 5:
  458.             return 8;
  459.          case 6:
  460.             return 4;
  461.          case 7:
  462.             return 2;
  463.          default:
  464.       }
  465.    }
  466.    function getToutnamentStatus()
  467.    {
  468.       switch(this.tournamentPhase)
  469.       {
  470.          case 1:
  471.             return "First Round - fixture 1";
  472.          case 2:
  473.             return "First Round - fixture 2";
  474.          case 3:
  475.             return "First Round - fixture 3";
  476.          case 4:
  477.             return "Playoff - Second Round";
  478.          case 5:
  479.             return "Playoff - Quarter Finals";
  480.          case 6:
  481.             return "Playoff - Semi Finals";
  482.          case 7:
  483.             return "Playoff - Finals";
  484.          case 8:
  485.             return "World Champion";
  486.          default:
  487.       }
  488.    }
  489.    static function _buildInstance()
  490.    {
  491.       CTournament._oI = new CTournament();
  492.       return CTournament._oI;
  493.    }
  494.    static function ins()
  495.    {
  496.       return !(CTournament._oI instanceof CTournament) ? CTournament._buildInstance() : CTournament._oI;
  497.    }
  498. }
  499.