/// <summary> /// Disconnect from game (use in test) /// </summary> /// <param name="message"></param> public void DisconnectOfGame(DisconnectMessage message) { if (string.IsNullOrWhiteSpace(message.GameHashId)) { throw HttpResponseExceptionHelper.Create("You must specify a game id to disconnect", HttpStatusCode.BadRequest); } var game = GameList.FirstOrDefault(x => x.HashId == message.GameHashId); if (game == null) { throw HttpResponseExceptionHelper.Create("No game exist with specified hash to disconnect", HttpStatusCode.BadRequest); } if (!game.ParticipantsHashId.Contains(UserToken.UserId)) { throw HttpResponseExceptionHelper.Create("You are not connected to this game", HttpStatusCode.BadRequest); } if (game.State == EnumsModel.GameState.Started) { game.DisconnectedHashId.Add(UserToken.UserId); } }
public void RemoveFriend(string HashId, string Username) { if (HashId.IsNullOrWhiteSpace() || Username.IsNullOrWhiteSpace()) { throw HttpResponseExceptionHelper.Create("Null friend name or id specified", HttpStatusCode.BadRequest); } if (UserToken == null) { throw HttpResponseExceptionHelper.Create("User token not specified", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { var toremove = db.Friends.FirstOrDefault(x => x.Player1HashId == UserToken.UserId && x.Player1Username == UserToken.Username && x.Player2HashId == HashId && x.Player2Username == Username); if (toremove == null) { toremove = db.Friends.FirstOrDefault(x => x.Player1HashId == HashId && x.Player1Username == Username && x.Player2HashId == UserToken.UserId && x.Player2Username == UserToken.Username); } if (toremove != null) { db.Friends.Remove(toremove); db.SaveChanges(); } } }
/// <summary> /// Reconnect (was in the disconnected list) to a game /// </summary> /// <param name="message"></param> public void ReconnectToGame(JoinGameMessage message) { if (string.IsNullOrWhiteSpace(message.GameHashId)) { throw HttpResponseExceptionHelper.Create("You must specify a game id to reconnect", HttpStatusCode.BadRequest); } // Check if game exist lock (LockObj) { var game = GameList.FirstOrDefault(x => x.HashId == message.GameHashId); if (game == null) { throw HttpResponseExceptionHelper.Create("No game exist with specified hash to reconnect", HttpStatusCode.BadRequest); } if (!game.DisconnectedHashId.Contains(UserToken.UserId)) { throw HttpResponseExceptionHelper.Create("You were not disconnected from this game", HttpStatusCode.BadRequest); } game.DisconnectedHashId.Remove(UserToken.UserId); } }
public void DeleteUser(UserToken userToken) { using (var db = new SystemDBContext()) { var user = db.Users.FirstOrDefault(x => x.HashId == userToken.UserId); if (user == null) { throw HttpResponseExceptionHelper.Create("Identifiant d'usager invalide", HttpStatusCode.BadRequest); } db.Users.Remove(user); db.SaveChanges(); } // delete all users friends var friendService = new FriendService(userToken); friendService.DeleteAllFriends(); // delete profile var profileService = new ProfileService(userToken); profileService.DeleteMyProfile(); // Delete daylies var dailyService = new DailyService(userToken); dailyService.DeleteAllDaily(); }
public void AddLeaderEntry(string zoneHashId, LeaderModel leader) { if (string.IsNullOrWhiteSpace(zoneHashId)) { throw HttpResponseExceptionHelper.Create("Empty map hash id specified", HttpStatusCode.BadRequest); } if (!LeaderModelHelper.IsValid(leader)) { throw HttpResponseExceptionHelper.Create("Invalid leader entry specified", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { var leaderboard = db.Leaderboards.FirstOrDefault(x => x.ZoneHashId == zoneHashId); if (leaderboard == null) { throw HttpResponseExceptionHelper.Create("Invalid map hash id provided. No related map.", HttpStatusCode.BadRequest); } leaderboard.Leaders = GetLeaderString(InsertLeader(leaderboard, leader)); db.SaveChanges(); } }
public FriendRequest CreateFriendRequest(string userId, string futureFriendId) { if (userId == futureFriendId) { throw HttpResponseExceptionHelper.Create("Cannot send a friend request to yourself", HttpStatusCode.BadRequest); } var existingFriendRequest = this.GetFriendRequest(userId, futureFriendId); if (existingFriendRequest != null) { if (existingFriendRequest.Accepted) { throw HttpResponseExceptionHelper.Create("You are already friends", HttpStatusCode.BadRequest); } throw HttpResponseExceptionHelper.Create("This friend request already exists", HttpStatusCode.BadRequest); } var userRepo = new UserRepository(this.DbContext); var user = userRepo.GetUserById(userId); var futureFriend = userRepo.GetUserById(futureFriendId); var friendRequest = user.SendFriendRequest(futureFriend); this.DbContext.SaveChanges(); return(friendRequest); }
public Friend AcceptFriendRequest(string userId, string futureFriendId) { var user = new UserRepository(this.DbContext).GetUserById(userId); var friendRequest = this.GetFriendRequest(userId, futureFriendId); if (friendRequest == null) { throw HttpResponseExceptionHelper.Create("This friend doesn't exists", HttpStatusCode.BadRequest); } if (friendRequest.FutureFriendId != user.Id || friendRequest.UserId == user.Id) { throw HttpResponseExceptionHelper.Create("You cannot accept this friend request", HttpStatusCode.BadRequest); } if (friendRequest.Accepted) { throw HttpResponseExceptionHelper.Create("This friend request has already been accepted", HttpStatusCode.BadRequest); } var friend = user.AcceptFriendRequest(friendRequest); if (friend == null) { throw HttpResponseExceptionHelper.Create("Could not create new friend entity", HttpStatusCode.BadRequest); } this.DbContext.SaveChanges(); return(friend); }
/// <summary> /// Update map information /// </summary> /// <param name="mapModel"></param> /// <returns></returns> public void UpdateMap(MapModel mapModel) { if (string.IsNullOrWhiteSpace(mapModel.HashId)) { throw HttpResponseExceptionHelper.Create("No map hash id specified", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { var map = db.Maps.FirstOrDefault(x => x.HashId == mapModel.HashId); if (map == null) { throw HttpResponseExceptionHelper.Create("No map correspond to the given hashId", HttpStatusCode.BadRequest); } // Update info if (!string.IsNullOrWhiteSpace(mapModel.Content)) { map.Content = mapModel.Content; } map.Level = mapModel.Level; map.UpdateTime = TimeHelper.CurrentCanadaTime(); // Mark entity as modified db.Entry(map).State = System.Data.Entity.EntityState.Modified; // call SaveChanges db.SaveChanges(); } }
/// <summary> /// Create a new game /// </summary> /// <param name="game"></param> /// <returns></returns> public GameModel CreateGame(GameModel game) { if (game == null || !IsValid(game)) { throw HttpResponseExceptionHelper.Create("Invalid format for specified new Game", HttpStatusCode.BadRequest); } var username = UserToken.Username; game.HostHashId = UserToken.UserId; game.HashId = Sha1Hash.GetSha1HashData(username + game.Name); // Add host to participants game.CurrentPlayerCount = 1; game.ParticipantsHashId.Add(UserToken.UserId); game.State = EnumsModel.GameState.Waiting; // insert in gameList lock (LockObj) { GameList.Add(game); return(ToPublic(game)); } }
/// <summary> /// Delete the specified map /// </summary> /// <param name="mapHashId"></param> public void DeleteMap(string mapHashId) { if (string.IsNullOrWhiteSpace(mapHashId)) { throw HttpResponseExceptionHelper.Create("Invalid map hash id provided", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { var map = db.Maps.FirstOrDefault(x => x.HashId == mapHashId && x.CreatorhashId == UserToken.UserId); if (map == null) { throw HttpResponseExceptionHelper.Create("No zone correspond to given hash id", HttpStatusCode.BadRequest); } db.Maps.Remove(map); db.SaveChanges(); var leaderboard = db.Leaderboards.FirstOrDefault(x => x.ZoneHashId == mapHashId); if (leaderboard != null) { db.Leaderboards.Remove(leaderboard); db.SaveChanges(); } } }
public Channel GetChannelById(Guid channelId) { if (!this.Channels.ContainsKey(channelId)) { throw HttpResponseExceptionHelper.Create("This channel doesn't exists", HttpStatusCode.BadRequest); } return(this.Channels[channelId]); }
public ProfileModel UpdateMyProfile(ProfileModel newProfile) { if (UserToken == null) { throw HttpResponseExceptionHelper.Create("User token not specified", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { var profile = db.Profiles.FirstOrDefault(x => x.UserHashId == UserToken.UserId); if (profile == null) { throw HttpResponseExceptionHelper.Create("No profile exist with specified user Id", HttpStatusCode.BadRequest); } // Modify profile if (newProfile == null) { throw HttpResponseExceptionHelper.Create("Invalid providen profile", HttpStatusCode.BadRequest); } profile.IsPrivate = newProfile.IsPrivate; profile.Description = newProfile.Description; profile.Picture = newProfile.Picture; if ((int)newProfile.PrincessTitle > (int)profile.PrincessTitle) { profile.PrincessTitle = newProfile.PrincessTitle; } if (newProfile.Experience > profile.Experience) { profile.Experience = newProfile.Experience; } if (newProfile.Level > profile.Level) { profile.Level = newProfile.Level; } if (!string.IsNullOrWhiteSpace(newProfile.StatsString)) { profile.StatsString = newProfile.StatsString; } if (!string.IsNullOrWhiteSpace(newProfile.AchievementsString) && !newProfile.AchievementsString.Contains("null")) { profile.AchievementsString = newProfile.AchievementsString; } // call SaveChanges db.SaveChanges(); return(profile); } }
public bool GetIsOnline(string userHashid) { if (string.IsNullOrWhiteSpace(userHashid)) { throw HttpResponseExceptionHelper.Create("Pas de hash spécifier", HttpStatusCode.BadRequest); } return(ConnexionWebsocket.ConnectedUsersHash.Contains(userHashid)); }
/// <summary> /// Join the specified game with the hash id /// </summary> /// <param name="message"></param> /// <returns></returns> public GameModel JoinGame(JoinGameMessage message) { if (string.IsNullOrWhiteSpace(message.GameHashId)) { throw HttpResponseExceptionHelper.Create("You must specify a game id to join", HttpStatusCode.BadRequest); } // Check if game exist lock (LockObj) { var game = GameList.FirstOrDefault(x => x.HashId == message.GameHashId); if (game == null) { throw HttpResponseExceptionHelper.Create("No game exist with specified hash to join", HttpStatusCode.BadRequest); } if (game.ParticipantsHashId.Contains(UserToken.UserId)) { throw HttpResponseExceptionHelper.Create("You already are in this game", HttpStatusCode.BadRequest); } if (game.IsPrivate) { if (string.IsNullOrWhiteSpace(message.Password)) { throw HttpResponseExceptionHelper.Create("This game is private, tou need a password to join it", HttpStatusCode.BadRequest); } if (game.Password != message.Password) { throw HttpResponseExceptionHelper.Create("Wrong password to join game", HttpStatusCode.BadRequest); } } if (game.MaxPlayersCount == game.CurrentPlayerCount) { throw HttpResponseExceptionHelper.Create("Game is full", HttpStatusCode.BadRequest); } if (game.State != EnumsModel.GameState.Waiting) { throw HttpResponseExceptionHelper.Create("Game has started or ended. You can't join it.", HttpStatusCode.BadRequest); } // OK game.CurrentPlayerCount++; game.ParticipantsHashId.Add(UserToken.UserId); return(ToPublic(game)); } }
public GameHost(WarsimUser owner, Map map, string password = "") { this.Map = map; this.Map.OwnerId = owner.UserId; if (!this.Map.ValidatePassword(password)) { throw HttpResponseExceptionHelper.Create("Wrong game password", HttpStatusCode.BadRequest); } }
public Map GetMapById(Guid mapId) { var map = this.DbContext.Maps.SingleOrDefault(x => x.Id.Equals(mapId)); if (map == null) { throw HttpResponseExceptionHelper.Create("This map doesn't exists", HttpStatusCode.BadRequest); } return(map); }
public ApplicationUser GetUserByUsername(string username) { var user = this.DbContext.Users.SingleOrDefault(x => x.Username == username); if (user == null) { throw HttpResponseExceptionHelper.Create("No user is associated to this username", HttpStatusCode.BadRequest); } return(user); }
public void CloseEvent(string eventID) { var ev = Get(eventID); if (ev == null) { throw HttpResponseExceptionHelper.Create("No event linked to ID when closing : " + eventID, HttpStatusCode.BadRequest); } Update(ev); }
public void DeleteMap(Map map) { if (map.IsLocked) { throw HttpResponseExceptionHelper.Create("Cannot delete a locked map, unlock it first", HttpStatusCode.BadRequest); } AzureBlobStorageHelper.DeleteMapThumbnail(map.Id.ToString()); this.DbContext.Maps.Remove(map); this.DbContext.SaveChanges(); }
public CreateExternalUserResult CreateExternalUser(ApplicationUserLogin userLogin) { if (string.IsNullOrEmpty(userLogin.ProviderUserId) || string.IsNullOrEmpty(userLogin.ProviderUsername)) { throw HttpResponseExceptionHelper.Create($"{userLogin.Provider} account login failed", HttpStatusCode.BadRequest); } var result = new CreateExternalUserResult(); // Check if the user login is already in use var existingUserLogin = this.DbContext.UserLogins .FirstOrDefault(x => x.Provider == userLogin.Provider && x.ProviderUserId == userLogin.ProviderUserId); // User already exists if (existingUserLogin != null) { result.User = this.GetUserById(existingUserLogin.UserId); result.UserCreated = false; return(result); } // Take only the first part if email var username = userLogin.ProviderUsername.Split('@').First(); // If username is already in use, tweak it until it's available while (this.DbContext.Users.FirstOrDefault(x => x.Username == username) != null) { username += new Random().Next(0, 9); } userLogin.UserId = ApplicationUserManager.GetUserHash(userLogin.ProviderUsername); var newUser = new ApplicationUser { Username = username, Id = userLogin.UserId, UserLogins = new List <ApplicationUserLogin> { userLogin } }; this.DbContext.Users.Add(newUser); this.DbContext.SaveChanges(); result.User = newUser; result.UserCreated = true; AzureBlobStorageHelper.UploadDefaultAvatar(newUser.Id); return(result); }
public void DeleteFriend(string userId, string friendId) { var friend = this.DbContext.Friends .SingleOrDefault(x => (x.User1Id == userId && x.User2Id == friendId) || (x.User1Id == friendId && x.User2Id == userId)); if (friend == null) { throw HttpResponseExceptionHelper.Create("Could not remove this user from your friends: user is not your friend", HttpStatusCode.BadRequest); } this.DbContext.Friends.Remove(friend); this.DbContext.SaveChanges(); }
public override void Create(Debt element) { using (var db = new SystemDbContext()) { if (element.AmountDue == 0) { throw HttpResponseExceptionHelper.Create("Do you really want to set up a 0$ Debt ?", HttpStatusCode.BadRequest); } db.Debts.Add(element); db.SaveChanges(); } }
public void DeleteNotification(string userId, Guid notificationId) { var notifications = this.GetNotifications(userId); var notification = notifications.SingleOrDefault(x => x.Id.Equals(notificationId)); if (notification == null) { throw HttpResponseExceptionHelper.Create("This notification doesn't exists for this user", HttpStatusCode.BadRequest); } this.DbContext.Notifications.Remove(notification); this.DbContext.SaveChanges(); }
public override void Create(EventSubscription element) { if (element == null) { throw HttpResponseExceptionHelper.Create("Element you want to create is null (Event Subscription)", HttpStatusCode.BadRequest); } using (var db = new SystemDbContext()) { element.EventSubscriptionID = EventSubscriptionHelper.GetEventSubscriptionId(element); db.EventSubscriptions.Add(element); db.SaveChanges(); } }
/// <summary> /// Get all the info related to a specified map hash id /// </summary> /// <param name="mapHashId"></param> /// <returns></returns> public MapModel GetMap(string mapHashId) { using (var db = new SystemDBContext()) { var map = db.Maps.FirstOrDefault(x => x.HashId == mapHashId); if (map == null) { throw HttpResponseExceptionHelper.Create("No map correspond to the given hashId", HttpStatusCode.BadRequest); } return(map); } }
public void Unsubscribe(SubscribeModel unsubscribModel) { var subscription = Get(EventSubscriptionHelper.GetEventSubscriptionId(unsubscribModel)); if (subscription == null) { throw HttpResponseExceptionHelper.Create("No subscription to unsubcribe from", HttpStatusCode.BadRequest); } // Delete Subscription Delete(new EventSubscription() { EventSubscriptionID = EventSubscriptionHelper.GetEventSubscriptionId(unsubscribModel) }); }
public IHttpActionResult Login(LoginMessage message) { var user = this._userRepo.GetUserByUsername(message.Username); if (user == null) { throw HttpResponseExceptionHelper.Create("Invalid username/password", HttpStatusCode.BadRequest); } if (!ApplicationUserManager.ValidateUserPassword(message.Password, user.Password)) { throw HttpResponseExceptionHelper.Create("Invalid username/password", HttpStatusCode.BadRequest); } return(this.Ok(ApplicationUserManager.CreateToken(user))); }
public void AddUser(WarsimUser user) { if (this.ActiveUsers.Count + 1 > this.MaxUserCount) { throw HttpResponseExceptionHelper.Create( $"Could not add user {user.Username}: " + $"channel already contains the maximum number of users ({this.MaxUserCount})", HttpStatusCode.BadRequest); } if (this.ActiveUsers.Contains(user)) { throw HttpResponseExceptionHelper.Create($"User {user.Username} is already inside this channel", HttpStatusCode.BadRequest); } this.ActiveUsers.Add(user); }
private UserModel CreateUser(UserModel user) { //Check if user is valid if (string.IsNullOrWhiteSpace(user.Username) && string.IsNullOrWhiteSpace(user.FacebookId)) { throw HttpResponseExceptionHelper.Create("Informations invalides", HttpStatusCode.BadRequest); } using (var db = new SystemDBContext()) { // save database var newUser = db.Users.Add(user); db.SaveChanges(); return(newUser); } }
/// <summary> /// Quit the game the user is in and delete it if the one who quit is the host /// </summary> /// <param name="gameHashId"></param> public void QuitGame(string gameHashId) { if (string.IsNullOrWhiteSpace(gameHashId)) { throw HttpResponseExceptionHelper.Create("You must specify a game id to quit", HttpStatusCode.BadRequest); } // Check if game exist lock (LockObj) { var game = GameList.FirstOrDefault(x => x.HashId == gameHashId); if (game == null) { throw HttpResponseExceptionHelper.Create("No game exist with specified hash to quit", HttpStatusCode.BadRequest); } if (game.HostHashId == UserToken.UserId) { DeleteGame(gameHashId); } else { if (game.ParticipantsHashId.Contains(UserToken.UserId)) { if (game.State == EnumsModel.GameState.Started) { game.DisconnectedHashId.Add(UserToken.UserId); } else { game.ParticipantsHashId.Remove(UserToken.UserId); game.CurrentPlayerCount--; } } else if (game.SpectatorsHashId.Contains(UserToken.UserId)) { game.SpectatorsHashId.Remove(UserToken.UserId); } else { throw HttpResponseExceptionHelper.Create("You are not in this game", HttpStatusCode.BadRequest); } } } }