示例#1
0
        private int ValidityGoal = Invalid; // Almacena la validad del gol reportado (0 = valido)

        #endregion Fields

        #region Constructors

        //
        // Inicializa el partido.
        // NOTE : En este momento la conexión todavía no puede utilizarse, todavía el cliente simulador no ha tomado el control
        //
        public RealtimeMatch(int matchID, RealtimePlayer firstPlayer, RealtimePlayer secondPlayer, int matchLength, int turnLength, Realtime mainRT)
        {
            mMatchID = matchID;
            MainRT = mainRT;

            // Añade los jugadores a la lista de jugadores
            Players[Player1] = firstPlayer;
            Players[Player2] = secondPlayer;

            // Creamos el estado de los jugados
            PlayersState[Player1] = new PlayerState();
            PlayersState[Player2] = new PlayerState();

            MatchLength = matchLength;
            TurnLength = turnLength;

            // Información del partido y versiones
            LogEx("Init Match: " + matchID + " FirstPlayer: " + firstPlayer.Name + " SecondPlayer: " + secondPlayer.Name, MATCHLOG);
            LogEx( "Server Version: " + ServerVersion + " MinClientVersion required: " + MinClientVersion, MATCHLOG );

            // NOTE : En este momento la conexión todavía no puede utilizarse, todavía el cliente simulador no ha tomado el control

            // Comienza a esperar a que los jugadores estén listos para arrancar la primera parte
            StartPart();
        }
示例#2
0
        private static RealtimePlayer FindBestOpponent(RealtimePlayer who, IEnumerable<RealtimePlayer> available)
        {
            RealtimePlayer closest = null;
            int bestSoFar = int.MaxValue;

            foreach (var other in available)
            {
                int absDiff = Math.Abs(other.TrueSkill - who.TrueSkill);
                if (absDiff < bestSoFar)
                {
                    bestSoFar = absDiff;
                    closest = other;
                }
            }

            // No lo damos por valido si no es partido puntuable
            if (bestSoFar > TrueSkillHelper.CUTOFF * TrueSkillHelper.MULTIPLIER)
                closest = null;

            return closest;
        }
示例#3
0
        private static void FillRealtimePlayerData(SoccerDataModelDataContext theContext, RealtimePlayer rtPlayer)
        {
            RealtimePlayerData data = new RealtimePlayerData();
            Player player = GetPlayerForRealtimePlayer(theContext, rtPlayer);

            data.Name = player.Team.Name;
            data.PredefinedTeamName = player.Team.PredefinedTeam.Name;
            data.TrueSkill = player.Team.TrueSkill;
            data.SpecialSkillsIDs = (from s in player.Team.SpecialTrainings
                                     where s.IsCompleted
                                     select s.SpecialTrainingDefinitionID).ToList();
            data.Formation = player.Team.Formation;

            var soccerPlayers = (from p in player.Team.SoccerPlayers
                                 where p.FieldPosition < 100
                                 orderby p.FieldPosition
                                 select p);

            // Multiplicamos por el fitness (entre 0 y 1)
            float daFitness = player.Team.Fitness / 100.0f;

            foreach (SoccerPlayer sp in soccerPlayers)
            {
                var spData = new RealtimePlayerData.SoccerPlayerData();

                spData.Name = sp.Name;
                spData.Number = sp.Number;

                spData.Power = (int)Math.Round(sp.Power * daFitness);
                spData.Control = (int)Math.Round(sp.Sliding * daFitness);
                spData.Defense = (int)Math.Round(sp.Weight * daFitness);

                data.SoccerPlayers.Add(spData);
            }

            rtPlayer.PlayerData = data;
        }
示例#4
0
        private static BDDModel.MatchParticipation CreateMatchParticipation(SoccerDataModelDataContext theContext, RealtimePlayer playerRT, bool asHome)
        {
            BDDModel.MatchParticipation part = new BDDModel.MatchParticipation();

            part.AsHome = asHome;
            part.Goals = 0;
            part.TurnsPlayed = 0;
            part.Team = GetPlayerForRealtimePlayer(theContext, playerRT).Team;

            return part;
        }
示例#5
0
        private static int CreateDatabaseMatch(SoccerDataModelDataContext theContext, RealtimePlayer homeRT, RealtimePlayer awayRT)
        {
            BDDModel.Match theNewMatch = new BDDModel.Match();
            theNewMatch.DateStarted = DateTime.Now;

            BDDModel.MatchParticipation homePart = CreateMatchParticipation(theContext, homeRT, true);
            BDDModel.MatchParticipation awayPart = CreateMatchParticipation(theContext, awayRT, false);

            homePart.Match = theNewMatch;
            awayPart.Match = theNewMatch;

            theContext.MatchParticipations.InsertOnSubmit(homePart);
            theContext.MatchParticipations.InsertOnSubmit(awayPart);

            theContext.Matches.InsertOnSubmit(theNewMatch);
            theContext.SubmitChanges();

            homeRT.MatchParticipationID = homePart.MatchParticipationID;
            awayRT.MatchParticipationID = awayPart.MatchParticipationID;

            return theNewMatch.MatchID;
        }
示例#6
0
        //
        // Obtiene el identificador del player a partir de su objeto
        //
        public int GetIdPlayer(RealtimePlayer player)
        {
            // Determinamos el identificador del player

            int idPlayer = Invalid;
            if (Players[Player1] == player)
                idPlayer = Player1;
            else if (Players[Player2] == player)
                idPlayer = Player2;
            else
                Debug.Assert(true, "GetIdPlayer: El player pasado no es ninguno de los jugadores actuales!");

            return (idPlayer);
        }
示例#7
0
        private void OnPlayerDisconnectedFromMatch(RealtimePlayer who)
        {
            RealtimeMatch theMatch = who.TheMatch;
            RealtimePlayer opp = theMatch.GetOpponentOf(who);

            // Simulamos la pulsación del botón de abortar...
            theMatch.OnAbort(theMatch.GetIdPlayer(who));

            // Y continuamos por el procedimiento normal...
            RealtimeMatchResult matchResult = OnFinishMatch(theMatch);

            // Hay que notificar al oponente de que ha habido cancelacion
            opp.TheConnection.Invoke("PushedOpponentDisconnected", matchResult);
        }
示例#8
0
        private void JoinPlayerToPreferredRoom(RealtimePlayer player)
        {
            Room theRoom = GetPreferredRoom();

            // Al que se une le enviamos los players que ya hay, sin contar con él mismo
            player.TheConnection.Invoke("PushedRefreshPlayersInRoom", theRoom.Name, theRoom.Players);

            // Informamos a todos los demas de que hay un nuevo player
            foreach (RealtimePlayer thePlayer in theRoom.Players)
            {
                thePlayer.TheConnection.Invoke("PushedNewPlayerJoinedTheRoom", player);
            }

            player.Room = theRoom;
            player.Room.Players.Add(player);
        }
示例#9
0
        //
        // Uno de los jugadores ha indicado que necesita los datos del partido
        //
        // Enviamos los datos del partido al jugador
        //
        public void OnRequestData(RealtimePlayer player)
        {
            // Determinamos el identificador del player
            int idPlayer = GetIdPlayer(player);
            LogEx( "OnRequestData: Datos del partido solicitador por el Player: " + idPlayer + "Configuración partido: TotalTime: " + MatchLength + " TurnTime: "+ TurnLength );

            // Envía la configuración del partido al jugador, indicándole además a quien controlan  ellos (LocalUser)
            Invoke(idPlayer, "InitMatch", this.mMatchID, Players[Player1].PlayerData, Players[Player2].PlayerData, idPlayer, MatchLength, TurnLength, MinClientVersion);
        }
示例#10
0
        //
        // Uno de los jugadores ha indicado que está listo para empezar
        //
        public void OnPlayerReady(RealtimePlayer player)
        {
            // Determinamos el identificador del player
            int idPlayer = GetIdPlayer(player);

            LogEx("OnPlayerReady: " + idPlayer);

            // Contabilizamos jugadores listos
            CountReadyPlayers++;

            // Si "TODOS=2" jugadores están listos continuamos el partido y notificamos a los clientes.
            // Además reseteamos las variables de espera
            if (CountReadyPlayers == 2)
            {
                LogEx( "Todos los jugadores han indicado que están listros. Les envíamos la notificación para que continuen" );
                Broadcast("OnAllPlayersReady");
                CountReadyPlayers = 0;
                this.CurState = State.Playing;
            }
        }
示例#11
0
        public void OnMsgToChatAdded(RealtimePlayer source, string msg)
        {
            Log.log(MATCHLOG, MatchID + " Chat: " + msg);

            // Solo permitos el chateo durante este estado, para evitar que cuando todavía esta cargando uno de los clientes le lleguen mensajes. Estaba pasando que se le mandaba
            // este invoke cuando no tenía asignado el cliente de la NetConnection a Game, sino a RTMPModel, con lo que este metodo no existia => excepcion #1069
            // Con esto, ocurrira q no se puede chatear al final de la primera parte, porque se reusa el estado WaittingState. Si realmente es un problema, se puede meter
            // un estado adicional para el final de la primera parte y hacer q aqui lo que se compruebe sea CurState != State.WaittingState
            if (CurState == State.Playing)
                Broadcast("OnChatMsg", msg);
        }
示例#12
0
 public bool IsRealtimePlayerInMatch(RealtimePlayer who)
 {
     return Players[Player1] == who || Players[Player2] == who;
 }
示例#13
0
 // Comprueba si un jugador ha abandonado el partido
 public bool HasPlayerAbandoned(RealtimePlayer player)
 {
     bool bAbandon = false;
     if (this.GetIdPlayer(player) == this.PlayerIdAbort)
         bAbandon = true;
     return (bAbandon);
 }
示例#14
0
        public RealtimePlayer GetOpponentOf(RealtimePlayer who)
        {
            RealtimePlayer ret = null;

            if (who == Players[Player1])
                ret = Players[Player2];
            else
                if (who == Players[Player2])
                    ret = Players[Player1];

            return ret;
        }
示例#15
0
 private static Player GetPlayerForRealtimePlayer(SoccerDataModelDataContext theContext, RealtimePlayer playerRT)
 {
     return (from s in theContext.Players
             where s.PlayerID == playerRT.PlayerID
             select s).FirstOrDefault();
 }
示例#16
0
        private static bool HasChallenge(RealtimePlayer first, RealtimePlayer second)
        {
            bool bRet = false;

            foreach (Challenge challenge in first.Challenges)
            {
                if (challenge.TargetPlayer == second)
                {
                    bRet = true;
                    break;
                }
            }

            if (!bRet)
            {
                foreach (Challenge challenge in second.Challenges)
                {
                    if (challenge.TargetPlayer == first)
                    {
                        bRet = true;
                        break;
                    }
                }
            }

            return bRet;
        }
示例#17
0
        private String GetStringPlayer(RealtimePlayer player)
        {
            String ret = null;

            if (player == Players[Player1])
                ret = PLAYER_1;
            else
                if (player == Players[Player2])
                    ret = PLAYER_2;

            return ret;
        }
示例#18
0
        private void LeaveRoom(RealtimePlayer who)
        {
            Room theRoom = who.Room;

            if (!theRoom.Players.Remove(who))
                throw new Exception("WTF: Player was not in room");

            // Tenemos que quitar todos los challenges en los que participara, bien como Source o como Target
            foreach (Challenge leftChallenge in who.Challenges)
            {
                RealtimePlayer other = leftChallenge.SourcePlayer == who ? leftChallenge.TargetPlayer : leftChallenge.SourcePlayer;
                bool hadChallenge = other.Challenges.Remove(leftChallenge);

                if (!hadChallenge)
                    throw new Exception("WTF");
            }
            who.Challenges.Clear();
            who.Room = null;

            foreach (RealtimePlayer thePlayer in theRoom.Players)
            {
                thePlayer.TheConnection.Invoke("PushedPlayerLeftTheRoom", who);
            }
        }
示例#19
0
        public bool LogInToDefaultRoom(NetPlug myConnection, string facebookSession)
        {
            lock (mGlobalLock)
            {
                bool bRet = false;

                // Como se vuelve a llamar aqui despues de jugar un partido, nos aseguramos de que la conexion este limpia para
                // la correcta recreacion del RealtimePlayer
                myConnection.UserData = null;

                using (SoccerDataModelDataContext theContext = new SoccerDataModelDataContext())
                {
                    Session theSession = (from s in theContext.Sessions
                                          where s.FacebookSession == facebookSession
                                          select s).FirstOrDefault();

                    if (theSession == null)
                        throw new Exception("Invalid session sent by client");

                    Player theCurrentPlayer = theSession.Player;

                    // Sólo permitimos una conexión para un player dado.
                    CloseOldConnectionForPlayer(theCurrentPlayer);

                    Team theCurrentTeam = theCurrentPlayer.Team;

                    // Unico punto de creacion del RealtimePlayer
                    RealtimePlayer theRealtimePlayer = new RealtimePlayer();

                    theRealtimePlayer.PlayerID = theCurrentPlayer.PlayerID;
                    theRealtimePlayer.ClientID = myConnection.ID;
                    theRealtimePlayer.FacebookID = theCurrentPlayer.FacebookID;
                    theRealtimePlayer.Name = theCurrentTeam.Name;
                    theRealtimePlayer.PredefinedTeamName = theCurrentTeam.PredefinedTeam.Name;
                    theRealtimePlayer.TrueSkill = theCurrentTeam.TrueSkill;

                    myConnection.UserData = theRealtimePlayer;
                    theRealtimePlayer.TheConnection = myConnection;

                    JoinPlayerToPreferredRoom(theRealtimePlayer);

                    bRet = true;

                    Log.log(REALTIME_DEBUG, theCurrentPlayer.FacebookID + " " + theRealtimePlayer.ClientID +  " logged in: " + theCurrentPlayer.Name + " " + theCurrentPlayer.Surname + ", Team: " + theCurrentTeam.Name);
                }

                return bRet;
            }
        }
示例#20
0
        private void StartMatch(RealtimePlayer firstPlayer, RealtimePlayer secondPlayer, int matchLength, int turnLength)
        {
            LeaveRoom(firstPlayer);
            LeaveRoom(secondPlayer);

            int matchID = -1;

            // Generacion de los datos de inicializacion para el partido. No valen con los del RealtimePlayer, hay que refrescarlos.
            using (SoccerDataModelDataContext theContext = new SoccerDataModelDataContext())
            {
                matchID = CreateDatabaseMatch(theContext, firstPlayer, secondPlayer);
                FillRealtimePlayerData(theContext, firstPlayer);
                FillRealtimePlayerData(theContext, secondPlayer);
            }

            RealtimeMatch theNewMatch = new RealtimeMatch(matchID, firstPlayer, secondPlayer, matchLength, turnLength, this);
            mMatches.Add(theNewMatch);

            firstPlayer.TheMatch = theNewMatch;
            secondPlayer.TheMatch = theNewMatch;

            firstPlayer.TheConnection.Invoke("PushedStartMatch", firstPlayer.ClientID, secondPlayer.ClientID);
            secondPlayer.TheConnection.Invoke("PushedStartMatch", firstPlayer.ClientID, secondPlayer.ClientID);
        }
示例#21
0
 public int GetGoals(RealtimePlayer player)
 {
     return GetGoals(GetStringPlayer(player));
 }