public static Player EnsurePlayerIsCreated(SoccerDataModelDataContext theContext, string facebookUserID, GetFBUserDelegate theFBUser) { var player = (from dbPlayer in theContext.Players where dbPlayer.FacebookID == facebookUserID select dbPlayer).FirstOrDefault(); if (player == null) { // Tenemos un nuevo jugador (unico punto donde se crea) player = new Player(); player.FacebookID = facebookUserID; player.CreationDate = DateTime.Now; // En horario del servidor... player.Liked = false; if (theFBUser != null) { // Probablemente llamada Rest Facebook.Schema.user theFBUSer = theFBUser(); player.Name = theFBUSer.first_name; player.Surname = theFBUSer.last_name; } else { // Queremos evitar la llamada en los Test de debug player.Name = "PlayerName"; player.Surname = "PlayerSurname"; } theContext.Players.InsertOnSubmit(player); } return player; }
public static BDDModel.Session EnsureSessionIsCreated(SoccerDataModelDataContext theContext, Player thePlayer, string sessionKey) { var session = (from dbSession in theContext.Sessions where dbSession.FacebookSession == sessionKey select dbSession).FirstOrDefault(); if (session == null) { session = new BDDModel.Session(); session.Player = thePlayer; session.FacebookSession = sessionKey; session.CreationDate = DateTime.Now; // En horario del servidor theContext.Sessions.InsertOnSubmit(session); } return session; }
private void ProcessInFacebookSessionUser() { using (SoccerDataModelDataContext theContext = new SoccerDataModelDataContext()) { Player player = EnsurePlayerIsCreated(theContext, Api.Session.UserId.ToString(), () => Api.Users.GetInfo() ); string sessionKey = Request.Form["fb_sig_session_key"]; if (sessionKey != null) { EnsureSessionIsCreated(theContext, player, sessionKey); theContext.SubmitChanges(); string queryStringToClient = Request.Form.ToString(); if (player.Liked) queryStringToClient += "&liked=true"; // Seria mejor hacer un transfer, pero no sabemos como librarnos de la exception, a pesar del catch parece que la relanza?? Response.Redirect("SoccerClientV1/SoccerClientV1.html?" + queryStringToClient, false); } else { ProcessSessionError("No session key"); } } }
public ServerStatsProfile() { mDC = new SoccerDataModelDataContext(); }
public ServerStatsRanking() { mDC = new SoccerDataModelDataContext(); }
public TransferModel.TeamDetails RefreshTeamDetails(string facebookID) { using (mContext = new SoccerDataModelDataContext()) { BDDModel.Team theTeam = (from t in mContext.Teams where t.Player.FacebookID == facebookID select t).First(); return RefreshTeamDetailsInner(theTeam); } }
private SoccerDataModelDataContext CreateDataForRequest() { mContext = new SoccerDataModelDataContext(); HttpContext theCurrentHttp = HttpContext.Current; if (!theCurrentHttp.Request.QueryString.AllKeys.Contains("SessionKey")) throw new Exception("SessionKey is missing"); string sessionKey = theCurrentHttp.Request.QueryString["SessionKey"]; mSession = (from s in mContext.Sessions where s.FacebookSession == sessionKey select s).FirstOrDefault(); if (mSession == null) throw new Exception("Invalid SessionKey: " + sessionKey); mPlayer = mSession.Player; return mContext; }
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); }
List<List<float>> GenerateStats() { SoccerDataModelDataContext dc = new SoccerDataModelDataContext(); List<List<float>> stats = new List<List<float>>(); for (int c = 0; c < 11; c++) { var innerList = new List<float>(); for (int d = 0; d < 12; d++) innerList.Add(0); stats.Add(innerList); } foreach (var player in dc.Players) { var creationDate = player.CreationDate.Date; for (int dayIdx = 0; dayIdx < 11; ++dayIdx) { var currentDate = creationDate.AddDays(dayIdx); // Número de partidos que se echó el día N int matchesCount = 0; if (dayIdx < 10) { matchesCount = (from m in dc.MatchParticipations where m.Team.Player.PlayerID == player.PlayerID && m.Match.DateStarted.Date == currentDate select m).Count(); } else { matchesCount = (from m in dc.MatchParticipations where m.Team.Player.PlayerID == player.PlayerID && m.Match.DateStarted.Date >= currentDate select m).Count(); } if (matchesCount <= 10) stats[dayIdx][matchesCount]++; else stats[dayIdx][11]++; } } // Pasamos a % int numPlayers = dc.Players.Count(); if (numPlayers != 0) { for (int c = 0; c < 11; c++) { for (int d = 0; d < 12; d++) { stats[c][d] = 100 * stats[c][d] / numPlayers; } } } return stats; }
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; }
private static Player GetPlayerForRealtimePlayer(SoccerDataModelDataContext theContext, RealtimePlayer playerRT) { return (from s in theContext.Players where s.PlayerID == playerRT.PlayerID select s).FirstOrDefault(); }
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; }
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; }
internal RealtimeMatchResult OnFinishMatch(RealtimeMatch realtimeMatch) { RealtimeMatchResult matchResult = null; RealtimePlayer player1 = realtimeMatch.GetRealtimePlayer(RealtimeMatch.PLAYER_1); RealtimePlayer player2 = realtimeMatch.GetRealtimePlayer(RealtimeMatch.PLAYER_2); using (SoccerDataModelDataContext theContext = new SoccerDataModelDataContext()) { Player bddPlayer1 = GetPlayerForRealtimePlayer(theContext, player1); Player bddPlayer2 = GetPlayerForRealtimePlayer(theContext, player2); // Los BDDPlayers se actualizan dentro de la funcion (... old GiveMatchRewards) matchResult = new RealtimeMatchResult(theContext, realtimeMatch, bddPlayer1, bddPlayer2); // Actualizacion del BDDMatch... BDDModel.Match theBDDMatch = (from m in theContext.Matches where m.MatchID == realtimeMatch.MatchID select m).FirstOrDefault(); theBDDMatch.DateEnded = DateTime.Now; theBDDMatch.WasTooManyTimes = matchResult.WasTooManyTimes; theBDDMatch.WasJust = matchResult.WasJust; theBDDMatch.WasAbandoned = matchResult.WasAbandoned; theBDDMatch.WasAbandonedSameIP = matchResult.WasAbandonedSameIP; // ... y de las MatchParticipations de la BDD (from p in theContext.MatchParticipations where p.MatchParticipationID == player1.MatchParticipationID select p).FirstOrDefault().Goals = matchResult.GetGoalsFor(player1); (from p in theContext.MatchParticipations where p.MatchParticipationID == player2.MatchParticipationID select p).FirstOrDefault().Goals = matchResult.GetGoalsFor(player2); theContext.SubmitChanges(); } player1.PlayerData = null; player2.PlayerData = null; player1.MatchParticipationID = -1; player2.MatchParticipationID = -1; player1.TheMatch = null; player2.TheMatch = null; // Borramos el match, dejamos que ellos se unan a la habitacion mMatches.Remove(realtimeMatch); return matchResult; }
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; } }
public ServerStatsGlobalMatches() { mDC = new SoccerDataModelDataContext(); }