Ejemplo n.º 1
0
        public static void BattleLauncher()
        {
            // lanceur de combat
            Console.WriteLine("Thread d'activation des combats [ok]");
            // lancement des combat et changement des status si le timestamp depasse MainClass.InitialisationBattleWaitTime
            while (true)
            {
                try
                {
                    for (int cnt = Battle.Battles.Count; cnt > 0; cnt--)
                    {
                        Battle _battle = Battle.Battles[cnt - 1];
                        if (_battle.State == Enums.battleState.state.initialisation && CommonCode.ReturnTimeStamp() - _battle.Timestamp > MainClass.InitialisationBattleWaitTime)
                        {
                            #region état initialisation
                            // supprimer les 2 images qui represente le combat dans le map
                            (DataBase.DataTables.mapobj as List <mysql.mapobj>).RemoveAll(f => f.idBattle == _battle.IdBattle);

                            // informer les joueurs abonnées au map de combat et qui ne sont pas en combat de la supression des 2 objets
                            List <NetConnection> nc = MainClass.netServer.Connections.FindAll(f => (f.Tag as Actor).inBattle == 0 && (f.Tag as Actor).map == _battle.Map);
                            for (int cnt2 = 0; cnt2 < nc.Count; cnt2++)
                            {
                                MapObjetRemovedResponseMessage mapObjetRemovedResponseMessage = new MapObjetRemovedResponseMessage();
                                object[] o = new object[2];
                                o[0] = _battle.BattleType;
                                o[1] = _battle.IdBattle;

                                mapObjetRemovedResponseMessage.Initialize(o, nc[cnt2]);
                                mapObjetRemovedResponseMessage.Serialize();
                                mapObjetRemovedResponseMessage.Send();
                            }
                            // etape final, informer les joueurs du combat pour le commencement
                            // envoyer tous les infos des states au joueurs abonnés au combat
                            string data = "";
                            for (int cnt2 = 0; cnt2 < _battle.SideA.Count; cnt2++)
                            {
                                string sorts = "";
                                for (int cnt3 = 0; cnt3 < _battle.SideA[cnt2].sorts.Count; cnt3++)
                                {
                                    sorts += _battle.SideA[cnt2].sorts[cnt3].SpellId + ":" + _battle.SideA[cnt2].sorts[cnt3].SpellPlace + ":" + _battle.SideA[cnt2].sorts[cnt3].Level + ":" + _battle.SideA[cnt2].sorts[cnt3].SpellColor + "/";
                                }
                                if (sorts != "")
                                {
                                    sorts = sorts.Substring(0, sorts.Length - 1);
                                }

                                data += _battle.SideA[cnt2].Pseudo + "#" + _battle.SideA[cnt2].classeName + "#" + _battle.SideA[cnt2].hiddenVillage + "#" + _battle.SideA[cnt2].maskColorString + "#" + _battle.SideA[cnt2].level + "#" + _battle.SideA[cnt2].officialRang + "#" + _battle.SideA[cnt2].currentHealth + "#" + _battle.SideA[cnt2].maxHealth + "#" + _battle.SideA[cnt2].doton + "#" + _battle.SideA[cnt2].katon + "#" + _battle.SideA[cnt2].futon + "#" + _battle.SideA[cnt2].raiton + "#" + _battle.SideA[cnt2].suiton + "#" + _battle.SideA[cnt2].usingDoton + "#" + _battle.SideA[cnt2].usingKaton + "#" + _battle.SideA[cnt2].usingFuton + "#" + _battle.SideA[cnt2].usingRaiton + "#" + _battle.SideA[cnt2].usingSuiton + "#" + _battle.SideA[cnt2].equipedDoton + "#" + _battle.SideA[cnt2].equipedKaton + "#" + _battle.SideA[cnt2].equipedFuton + "#" + _battle.SideA[cnt2].equipedRaiton + "#" + _battle.SideA[cnt2].equipedSuiton + "#" + _battle.SideA[cnt2].originalPc + "#" + _battle.SideA[cnt2].originalPm + "#" + _battle.SideA[cnt2].pe + "#" + _battle.SideA[cnt2].cd + "#" + _battle.SideA[cnt2].summons + "#" + _battle.SideA[cnt2].initiative + "#" + _battle.SideA[cnt2].dodgePC + "#" + _battle.SideA[cnt2].dodgePM + "#" + _battle.SideA[cnt2].dodgePE + "#" + _battle.SideA[cnt2].dodgeCD + "#" + _battle.SideA[cnt2].removePC + "#" + _battle.SideA[cnt2].removePM + "#" + _battle.SideA[cnt2].removePE + "#" + _battle.SideA[cnt2].removeCD + "#" + _battle.SideA[cnt2].escape + "#" + _battle.SideA[cnt2].blocage + "#" + _battle.SideA[cnt2].power + "#" + _battle.SideA[cnt2].equipedPower + "#" + sorts + "|";
                            }
                            if (data != "")
                            {
                                data = data.Substring(0, data.Length - 1) + "•";
                            }

                            for (int cnt2 = 0; cnt2 < _battle.SideB.Count; cnt2++)
                            {
                                string sorts = "";
                                for (int cnt3 = 0; cnt3 < _battle.SideB[cnt2].sorts.Count; cnt3++)
                                {
                                    sorts += _battle.SideB[cnt2].sorts[cnt3].SpellId + ":" + _battle.SideB[cnt2].sorts[cnt3].SpellPlace + ":" + _battle.SideB[cnt2].sorts[cnt3].Level + ":" + _battle.SideB[cnt2].sorts[cnt3].SpellColor + "/";
                                }
                                if (sorts != "")
                                {
                                    sorts = sorts.Substring(0, sorts.Length - 1);
                                }

                                data += _battle.SideB[cnt2].Pseudo + "#" + _battle.SideB[cnt2].classeName + "#" + _battle.SideB[cnt2].hiddenVillage + "#" + _battle.SideB[cnt2].maskColorString + "#" + _battle.SideB[cnt2].level + "#" + _battle.SideB[cnt2].officialRang + "#" + _battle.SideB[cnt2].currentHealth + "#" + _battle.SideB[cnt2].maxHealth + "#" + _battle.SideB[cnt2].doton + "#" + _battle.SideB[cnt2].katon + "#" + _battle.SideB[cnt2].futon + "#" + _battle.SideB[cnt2].raiton + "#" + _battle.SideB[cnt2].suiton + "#" + _battle.SideB[cnt2].usingDoton + "#" + _battle.SideB[cnt2].usingKaton + "#" + _battle.SideB[cnt2].usingFuton + "#" + _battle.SideB[cnt2].usingRaiton + "#" + _battle.SideB[cnt2].usingSuiton + "#" + _battle.SideB[cnt2].equipedDoton + "#" + _battle.SideB[cnt2].equipedKaton + "#" + _battle.SideB[cnt2].equipedFuton + "#" + _battle.SideB[cnt2].equipedRaiton + "#" + _battle.SideB[cnt2].equipedSuiton + "#" + _battle.SideB[cnt2].originalPc + "#" + _battle.SideB[cnt2].originalPm + "#" + _battle.SideB[cnt2].pe + "#" + _battle.SideB[cnt2].cd + "#" + _battle.SideB[cnt2].summons + "#" + _battle.SideB[cnt2].initiative + "#" + _battle.SideB[cnt2].dodgePC + "#" + _battle.SideB[cnt2].dodgePM + "#" + _battle.SideB[cnt2].dodgePE + "#" + _battle.SideB[cnt2].dodgeCD + "#" + _battle.SideB[cnt2].removePC + "#" + _battle.SideB[cnt2].removePM + "#" + _battle.SideB[cnt2].removePE + "#" + _battle.SideB[cnt2].removeCD + "#" + _battle.SideB[cnt2].escape + "#" + _battle.SideB[cnt2].blocage + "#" + _battle.SideB[cnt2].power + "#" + _battle.SideB[cnt2].equipedPower + "#" + sorts + "|";
                            }
                            if (data != "")
                            {
                                data = data.Substring(0, data.Length - 1);
                            }

                            // verifier si les 2 leaders du team1 et team2 ont la meme initiative, si oui,choisir un aléatoirement
                            // reorganisation des joueurs selons leurs initiative
                            ReorderPlayersInTeam ropit = new ReorderPlayersInTeam();
                            _battle.SideA.Sort(ropit);
                            _battle.SideB.Sort(ropit);

                            // contien le nom du joueur qui a le plus d'initiative, quand et seulement quand les 2 adversaires ont la meme initiative
                            string ranSelectedPlayer = "";
                            if (_battle.SideA[0].initiative == _battle.SideB[0].initiative)
                            {
                                // selection aléatoire de l'un des 2 team
                                Random rand = new Random();
                                int    r    = rand.Next(0, 2);
                                if (r == 0)
                                {
                                    ranSelectedPlayer = _battle.SideA[0].Pseudo;
                                }
                                else
                                {
                                    ranSelectedPlayer = _battle.SideB[0].Pseudo;
                                }
                            }
                            else
                            {
                                if (_battle.SideA[0].initiative > _battle.SideB[0].initiative)
                                {
                                    ranSelectedPlayer = _battle.SideA[0].Pseudo;
                                }
                                else
                                {
                                    ranSelectedPlayer = _battle.SideB[0].Pseudo;
                                }
                            }
                            // reorganisation et alimentaion de la liste qui contiens tous les joueurs
                            // reorganization de tous les joueurs selon l'initiative et basculation entre les team
                            int team1Cnt = 0;
                            int team2Cnt = 0;

                            if (_battle.SideA[0].initiative > _battle.SideB[0].initiative)
                            {
                                team1Cnt = 1;
                                _battle.AllPlayersByOrder.Add(_battle.SideA[0]);
                            }
                            else if (_battle.SideA[0].initiative < _battle.SideB[0].initiative)
                            {
                                team2Cnt = 1;
                                _battle.AllPlayersByOrder.Add(_battle.SideB[0]);
                            }
                            else
                            {
                                // les 2 leaders ont la meme initiative
                                Actor piib = _battle.SideA.Find(f => f.Pseudo == ranSelectedPlayer);
                                if (piib != null)
                                {
                                    team1Cnt = 1;
                                    _battle.AllPlayersByOrder.Add(_battle.SideA[0]);
                                }
                                else
                                {
                                    team2Cnt = 1;
                                    _battle.AllPlayersByOrder.Add(_battle.SideB[0]);
                                }
                            }

                            // insertion des joueurs dans la liste Battle.AllPlayersByOrder
                            while (team1Cnt < _battle.SideA.Count || team2Cnt < _battle.SideB.Count)
                            {
                                int last = _battle.AllPlayersByOrder.Count - 1;
                                if (_battle.AllPlayersByOrder[last].teamSide == Enums.Team.Side.A)
                                {
                                    if (_battle.SideB.Count >= team2Cnt)
                                    {
                                        _battle.AllPlayersByOrder.Add(_battle.SideB[team2Cnt]);
                                        team2Cnt++;
                                    }
                                }
                                else if (_battle.AllPlayersByOrder[last].teamSide == Enums.Team.Side.A)
                                {
                                    if (_battle.SideA.Count >= team1Cnt)
                                    {
                                        _battle.AllPlayersByOrder.Add(_battle.SideA[team1Cnt]);
                                        team1Cnt++;
                                    }
                                }
                            }
                            //////////////////////////////////////////////////////

                            // envoie les données des joueurs a tous les joueurs dans le combat
                            for (int cnt2 = 0; cnt2 < _battle.SideA.Count; cnt2++)
                            {
                                NetConnection nc2 = MainClass.netServer.Connections.Find(f => (f.Tag as Actor).Pseudo == _battle.SideA[cnt2].Pseudo);
                                if (nc2 != null)
                                {
                                    CommonCode.SendMessage("cmd•BattleStarted•" + data + "•" + ranSelectedPlayer + "•" + MainClass.TimeToPlayInBattle, nc2, true);
                                    Console.WriteLine("<--cmd•BattleStarted•" + data + "•" + ranSelectedPlayer + " to " + (nc2.Tag as Actor).Pseudo);
                                }
                            }
                            for (int cnt2 = 0; cnt2 < _battle.SideB.Count; cnt2++)
                            {
                                NetConnection nc2 = MainClass.netServer.Connections.Find(f => (f.Tag as Actor).Pseudo == _battle.SideB[cnt2].Pseudo);
                                if (nc2 != null)
                                {
                                    CommonCode.SendMessage("cmd•BattleStarted•" + data + "•" + ranSelectedPlayer + "•" + MainClass.TimeToPlayInBattle, nc2, true);
                                    Console.WriteLine("<--cmd•BattleStarted•" + data + "•" + ranSelectedPlayer + "•" + MainClass.TimeToPlayInBattle + " to " + (nc2.Tag as Actor).Pseudo);
                                }
                            }

                            // mise a jour du timestamp du joueur qui a la main
                            _battle.TimeLeftToPlay = CommonCode.ReturnTimeStamp();
                            // changement de state =Started, pour eviter tous changement de position ou accée au combat
                            _battle.State = Enums.battleState.state.started;
                            CommonCode.FinishTurn(_battle, false);
                            #endregion
                        }
                        else if (_battle.State == Enums.battleState.state.started)
                        {
                            #region started
                            // code pour passer la main a l'autre joueur
                            if (Battle.Battles.Count > 0 && CommonCode.ReturnTimeStamp() - _battle.TimeLeftToPlay > TimeToPlayInBattle)
                            {
                                CommonCode.FinishTurn(_battle, true);
                            }

                            if (_battle.SideA.FindAll(f => f.species == Species.Name.Human).Count + _battle.SideB.FindAll(f => f.species == Species.Name.Human).Count < 0)
                            {
                                // le combat est términé
                                CommonCode.IsClosedBattle(_battle, true);
                            }
                            #endregion
                        }
                        else if (_battle.State == Enums.battleState.state.closed)
                        {
                            #region closed
                            // le combat a été cloturé suite a la deconnexion de quelqu'un, ou un joueur est mort apres un lancement de sort
                            // supprimer les 2 images qui represente le combat dans le map
                            (DataBase.DataTables.mapobj as List <mysql.mapobj>).RemoveAll(f => f.idBattle == _battle.IdBattle);

                            // mise du statut inBattle = 0, idBattle = -1 pour les joueurs
                            foreach (mysql.players player in (DataBase.DataTables.players as List <mysql.players>).FindAll(f => f.inBattleID == _battle.IdBattle))
                            {
                                player.inBattle     = 0;
                                player.inBattleID   = 0;
                                player.inBattleType = "";
                            }

                            // mise du statut inBattle = 0;, idBattle = -1 dans les sessions PlayerInfo de chaque joueur
                            List <Actor> bl = Battle.Battles[cnt - 1].AllPlayersByOrder.FindAll(f => f.species == Species.Name.Human);
                            bl.InsertRange(0, Battle.Battles[cnt - 1].DeadPlayers.FindAll(f => f.species == Species.Name.Human));

                            foreach (Actor t in bl)
                            {
                                NetConnection ncCnt2 = MainClass.netServer.Connections.Find(f => (f.Tag as Actor).Pseudo == t.Pseudo);

                                if (ncCnt2 != null)
                                {
                                    Actor piTag = ncCnt2.Tag as Actor;
                                    piTag.inBattle           = 0;
                                    piTag.idBattle           = -1;
                                    piTag.teamSide           = Team.Side.None;
                                    piTag.isAlive            = true;
                                    piTag.animatedAction     = AnimatedActions.Name.idle;
                                    piTag.PlayerChallengeYou = "";
                                    piTag.YouChallengePlayer = "";
                                    piTag.BuffsList.Clear();
                                }
                            }

                            // Informer les utilisateurs abonnées au map et qui ne sont pas en combat de l'affichage des joueurs qui étés en combat
                            foreach (Actor pi in bl)
                            {
                                // un controle inutile mais par précaution

                                var           pi1 = pi;
                                NetConnection nt  = netServer.Connections.Find(f => ((Actor)f.Tag).Pseudo == pi1.Pseudo);
                                if (nt != null)
                                {
                                    CommonCode.RefreshStats(nt);

                                    // pi2 = la vrais instance qui pointe vers le joueur en itérations, pi pointe vers le clone qui as les donnée playerInfo qui correspand au combat, comme la position dans le combat, ses stats differente apres un boost ou retrait..
                                    // si non on envois au abonnées du map la mauvaises position des joueurs avec leurs position lorsqu'il été en combat et non la position réel
                                    Actor pi2 = nt.Tag as Actor;

                                    // envoie d'une requette de réaparition des joueurs qui étés en combats
                                    List <NetConnection> abonnedMap = MainClass.netServer.Connections.FindAll(f => f.Tag != null && (f.Tag as Actor).Pseudo != pi2.Pseudo && (f.Tag as Actor).map == pi2.map);
                                    foreach (NetConnection t in abonnedMap)
                                    {
                                        #region
                                        // pseudo#classe#pvp:spirit:spiritLvl#village#MaskColors#map_position#orientation#level#action#waypoint   - separateur entre plusieurs joueurs
                                        string data = pi2.Pseudo + "#" + pi2.classeName + "#";
                                        if (pi2.Pvp == false)
                                        {
                                            data += "0:null:null#";
                                        }
                                        else
                                        {
                                            data += "1:" + pi2.spirit + ":" + pi2.spiritLvl.ToString() + "#";
                                        }
                                        data += pi2.hiddenVillage + "#" + pi2.maskColorString + "#" + pi2.map_position.X + "/" + pi2.map_position.Y + "#" + pi2.directionLook.ToString() + "#" + pi2.level.ToString() + "##";

                                        SpawnActorResponseMessage spawnActorResponseMessage = new SpawnActorResponseMessage();
                                        spawnActorResponseMessage.Initialize(new[] { data }, t);
                                        spawnActorResponseMessage.Serialize();
                                        spawnActorResponseMessage.Send();
                                        #endregion
                                    }
                                }
                            }

                            // netoyage des listes
                            Battle.Battles[cnt - 1].SideA.Clear();
                            Battle.Battles[cnt - 1].SideB.Clear();
                            Battle.Battles[cnt - 1].AllPlayersByOrder.Clear();
                            // supression du combat de la liste
                            Battle.Battles.RemoveAt(cnt - 1);
                            Console.WriteLine("battle finished");
                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 2
0
        static void server_LostConnection(NetIncomingMessage im, string reason)
        {
            Console.WriteLine("client " + im.SenderConnection.RemoteUniqueIdentifier + " was diconnected " + reason);
            Actor pi = im.SenderConnection.Tag as Actor;

            if (pi.Username != string.Empty)
            {
                ((List <mysql.connected>)DataBase.DataTables.connected).RemoveAll(f => f.user == pi.Username);
            }

            // il faut informer tous les autre clients abonnées au map de la deconnexion du client
            // seulement si le client n'est pas dans un combat ou dans un combat FreeChallenge
            if (pi.inBattle == 0)
            {
                List <NetConnection> abonnedPlayers = netServer.Connections.FindAll(p => ((Actor)p.Tag).map == pi.map && ((Actor)p.Tag).Pseudo != pi.Pseudo && pi.inBattle == 0);
                for (int cnt = 0; cnt < abonnedPlayers.Count; cnt++)
                {
                    ActorDisconnectedResponseMessage playerDisconnectedResponseMessage = new ActorDisconnectedResponseMessage();
                    playerDisconnectedResponseMessage.Initialize(new[] { pi.Pseudo }, abonnedPlayers[cnt]);
                    playerDisconnectedResponseMessage.Serialize();
                    playerDisconnectedResponseMessage.Send();
                }
                abonnedPlayers.Clear();
            }
            else
            {
                List <NetConnection> abonnedPlayers = MainClass.netServer.Connections.FindAll(p => ((Actor)p.Tag).map == pi.map && ((Actor)p.Tag).Pseudo != pi.Pseudo && ((Actor)p.Tag).idBattle == pi.idBattle);
                foreach (NetConnection t in abonnedPlayers)
                {
                    ActorDisconnectedResponseMessage playerDisconnectedResponseMessage = new ActorDisconnectedResponseMessage();
                    playerDisconnectedResponseMessage.Initialize(new[] { pi.Pseudo }, t);
                    playerDisconnectedResponseMessage.Serialize();
                    playerDisconnectedResponseMessage.Send();
                }
                abonnedPlayers.Clear();
            }

            if (pi.inBattle == 1 && Battle.Battles.Find(f => f.IdBattle == pi.idBattle).BattleType == BattleType.Type.FreeChallenge)
            {
                // le joueur été en combat, il faut annuler le combat s'il ya seulement 2 joueurs
                // check s'il le combat est du type FreeChallenge
                Battle battle = Battle.Battles.Find(f => f.IdBattle == pi.idBattle);
                if (battle != null && battle.BattleType == BattleType.Type.FreeChallenge)
                {
                    // effacement de notre joueur de la combat afin de verifier apres
                    battle.SideA.Remove(battle.SideA.Find(f => f.Pseudo == pi.Pseudo));
                    battle.SideB.Remove(battle.SideB.Find(f => f.Pseudo == pi.Pseudo));
                    // il faut informer les abonnés du map du combat et qui ne sont pas en combat que le combat en cours a été annulé pour supprimer l'objet qui represente le combat
                    // informer les joueurs abonnées au map de combat et qui ne sont pas en combat de la supression des 2 objets
                    List <NetConnection> nc = netServer.Connections.FindAll(f => ((Actor)f.Tag).inBattle == 0 && ((Actor)f.Tag).map == battle.Map);
                    foreach (NetConnection t in nc)
                    {
                        MapObjetRemovedResponseMessage mapObjetRemovedResponseMessage = new MapObjetRemovedResponseMessage();
                        object[] o = new object[2];
                        o[0] = Enums.BattleType.Type.FreeChallenge;
                        o[1] = battle.IdBattle;

                        mapObjetRemovedResponseMessage.Initialize(o, t);
                        mapObjetRemovedResponseMessage.Serialize();
                        mapObjetRemovedResponseMessage.Send();
                    }
                }
                if (CommonCode.IsClosedBattle(battle, true))
                {
                    battle.State = battleState.state.closed;
                    Console.WriteLine("battle closed");
                    foreach (Actor t in battle.AllPlayersByOrder)
                    {
                        t.inBattle = 0;
                        ((List <mysql.players>)DataBase.DataTables.players).Find(f => f.pseudo == t.Pseudo).inBattle = 0;
                    }
                }
            }

            if (pi.PlayerChallengeYou != "")
            {
                //check si le joueur est connecté
                NetConnection nt = netServer.Connections.Find(f => ((Actor)f.Tag).Pseudo == pi.PlayerChallengeYou);
                if (nt != null)
                {
                    ((Actor)nt.Tag).YouChallengePlayer = "";
                    ((Actor)nt.Tag).PlayerChallengeYou = "";
                }
            }
            else if (pi.YouChallengePlayer != "")
            {
                //check si le joueur est connecté
                NetConnection nt = netServer.Connections.Find(f => ((Actor)f.Tag).Pseudo == pi.PlayerChallengeYou);
                if (nt != null)
                {
                    ((Actor)nt.Tag).YouChallengePlayer = "";
                    ((Actor)nt.Tag).PlayerChallengeYou = "";
                }
            }
        }