Пример #1
0
 /// <summary>
 /// Validates the the session is valid and bumps its Timeout. Throws InvalidSession exception if not valid. Sets Session property if session is valid.
 /// </summary>
 /// <param name="session"></param>
 private void ValidateSession(Guid session)
 {
     ClearOldSessions();
     User = (from s in _context.DB_LoginSessions where s.Id == session select s.DB_User).FirstOrDefault();
     if (User == null)
     {
         throw new FOEServiceException(FOEStatusCodes.InvalidSession);
     }
     try
     {
         lock (validateLock)
         {
             DataAccess.Database.DB_LoginSession da_session = User.DB_LoginSessions.FirstOrDefault(s => s.Id == session);
             if (da_session == null)
             {
                 throw new FOEServiceException(FOEStatusCodes.InvalidSession);
             }
             da_session.Timeout = DateTime.UtcNow + TimeSpan.FromHours(1);
             _context.SubmitChanges();
             Session = session;
         }
     }
     catch (ChangeConflictException)
     {
         // This can happen if multiple request is performed
         // simultaneously on the same session.
         // Verify that the session is still valid
         // and accept the missing update.
         _context.Refresh(RefreshMode.OverwriteCurrentValues, User.DB_LoginSessions);
         if (User.DB_LoginSessions.FirstOrDefault(s => s.Id == session) == null)
         {
             throw new FOEServiceException(FOEStatusCodes.InvalidSession);
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Checks for the Game in the database and updates it if found (excluding Id, MapId and name). If not found in DB it will be created.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static DB_Game FromGame(Game game, FOEDatabaseDataContext context)
        {
            DB_Game da_game = null;

            if (game.Id.HasValue)
            {
                da_game = (from g in context.DB_Games where g.Id == game.Id.Value select g).FirstOrDefault();
            }

            //game didnt exist, create it.
            if (da_game == null)
            {
                da_game    = new DB_Game();
                da_game.Id = Guid.NewGuid();
                if (!game.MapId.HasValue)
                {
                    throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Parameter MapId can not be null");
                }
                da_game.MapId = game.MapId.Value;

                if (!string.IsNullOrEmpty(game.Name))
                {
                    da_game.Name = game.Name;
                }

                context.DB_Games.InsertOnSubmit(da_game);
            }

            //Update the values of the game (excluding Id, MapId and name since we dont ever weant those to change in an update.)
            if (game.GermanPlayer.HasValue)
            {
                da_game.GermanPlayer = game.GermanPlayer.Value;
            }
            if (game.JapanesePlayer.HasValue)
            {
                da_game.JapanesePlayer = game.JapanesePlayer.Value;
            }
            if (game.AmericanPlayer.HasValue)
            {
                da_game.AmericanPlayer = game.AmericanPlayer.Value;
            }
            if (game.GermanPlayer.HasValue)
            {
                da_game.EnglishPlayer = game.GermanPlayer.Value;
            }
            if (game.GermanPlayer.HasValue)
            {
                da_game.RussianPlayer = game.GermanPlayer.Value;
            }
            da_game.Phase    = game.Phase;
            da_game.Turn     = game.Turn;
            da_game.IsActive = game.IsActive;

            context.SubmitChanges();

            return(da_game);
        }
Пример #3
0
        /// <summary>
        /// Looks for the inviteId in teh database, if not found it creates a new DB_Invite and returns that.
        /// </summary>
        /// <param name="invite"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static DB_Invite FromInvite(Invite invite, FOEDatabaseDataContext context)
        {
            DB_Invite da_invite = null;

            //if user exists, get it.
            if (invite.Id.HasValue)
            {
                da_invite = (from i in context.DB_Invites where i.Id == invite.Id.Value select i).FirstOrDefault();
                if (da_invite == null)
                {
                    throw new FOEServiceException(FOEStatusCodes.InvalidInvite, "Id reference to invite unknown.");
                }
            }
            else
            {
                //if no Id was passed in, create a user.
                da_invite              = new DB_Invite();
                da_invite.InvitedUser  = invite.InvitedUser;
                da_invite.InvitingUser = invite.InvitingUser;
                da_invite.Id           = invite.Id.Value;
                da_invite.Status       = invite.Status;
                da_invite.GameId       = invite.GameId;

                context.DB_Invites.InsertOnSubmit(da_invite);
                context.SubmitChanges();
            }

            //Update the user
            if (da_invite != null)
            {
                da_invite.Status = invite.Status;
            }

            context.SubmitChanges();

            return(da_invite);
        }
Пример #4
0
        /// <summary>
        /// Extracts information from the provided User and adds it as an entity to the db.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static DB_User FromUser(User user, FOEDatabaseDataContext context)
        {
            DB_User da_user = null;

            //Check that the user doesnt already exist in the db.
            if (user.Id.HasValue)
            {
                da_user = (from u in context.DB_Users where u.Id == user.Id.Value select u).FirstOrDefault();
                if (da_user == null)
                {
                    throw new FOEServiceException(FOEStatusCodes.UnknownUser, String.Format("Id reference to user is unknown: {0}", user.Id.Value));
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(user.UserName))
                {
                    da_user = (from u in context.DB_Users where u.UserName == user.UserName select u).FirstOrDefault();
                }
            }

            //User didnt exist, try to create it.
            if (da_user == null)
            {
                //TODO: Add canInsert check here?

                if (string.IsNullOrEmpty(user.UserName))
                {
                    throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Paremeter UserName cannot be null");
                }
                if (string.IsNullOrEmpty(user.Password))
                {
                    throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Paremeter Password cannot be null");
                }

                da_user = new DB_User()
                {
                    Id = Guid.NewGuid(), UserName = user.UserName, Password = user.Password, Email = user.Email
                };
                context.DB_Users.InsertOnSubmit(da_user);
                context.SubmitChanges();
            }
            else
            {
                //TODO: Add canUpdate check here?

                if (!string.IsNullOrEmpty(user.Password))
                {
                    da_user.Password = user.Password;
                }

                if (!string.IsNullOrEmpty(user.UserName))
                {
                    da_user.UserName = user.UserName;
                }

                if (!string.IsNullOrEmpty(user.Email))
                {
                    da_user.Email = user.Email;
                }
            }

            if (da_user == null)
            {
                throw new FOEServiceException(FOEStatusCodes.InternalError, "Unable to create DB_User");
            }

            return(da_user);
        }