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

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