/// <summary> /// Gets the achievement in question if the player is allowed to attain /// achievements and saves the information immediately to the database /// </summary> /// <param name="player">The player in question</param> /// <param name="achievement">The achievement gained</param> public void GetAchievement(PlayerEntity player, string achievement) { // Check if the player is achievement player or not if (player.PartitionKey.Equals(PlayerEntity.UserType.Regular.ToString())) { // Regular player, just return from here return; } // If the achievements list does not exist, create it (just to be sure) if (player.Achievements == null) { player.Achievements = new Dictionary<string,DateTime>(); } // Now, save the achivement to the player information if (!player.Achievements.ContainsKey(achievement)) { player.Achievements.Add(achievement, DateTime.Now); // And save the values to the database DatabaseManager.Instance.SavePlayer(player); } }
/// <summary> /// Creates the cookie to the player in question /// </summary> /// <param name="userType">The type of user that is playing</param> /// <param name="id">The id of the player in question</param> /// <param name="response">The HttpResponseBase used to send something to browser</param> /// <param name="request">The HttpRequestBase used to query for the cookie in the browser</param> public void CreateCookie(PlayerEntity.UserType userType, int id, HttpResponseBase response, HttpRequestBase request) { // First, check if the cookie exists if (CheckIfCookieExists(request) == true) { // Just leave return; } // Otherwise, create the cookie HttpCookie lutExplorerCookie = new HttpCookie(cookieName); // Set the cookie values lutExplorerCookie[playerType] = userType.ToString(); lutExplorerCookie[playerId] = id.ToString(); // Set the expiration date lutExplorerCookie.Expires = DateTime.Now.AddDays(expirationDays); // And finally, save the cookie to the browser response.Cookies.Add(lutExplorerCookie); }
/// <summary> /// Searches the table storage for the given player entity /// </summary> /// <param name="playerEntity">The player entity to be searched for</param> /// <returns>Found player entity</returns> public PlayerEntity FindPlayerEntity(PlayerEntity playerEntity) { try { // Get the entity with given values IQueryable<PlayerEntity> listEntities = (from e in serviceContext.CreateQuery<PlayerEntity>(tableName) where e.PartitionKey == playerEntity.PartitionKey && e.RowKey == playerEntity.RowKey select e); if (listEntities.ToList().Count > 0) { return listEntities.FirstOrDefault(); } // Otherwise return null return null; } catch (Exception) { return null; } }
public void TestAddPlayer() { // Create the player PlayerEntity player = new PlayerEntity(PlayerEntity.UserType.Regular, 1); // And save it to the storage serviceContext.AddObject(tableName, player); // Submit the operation to the table service serviceContext.SaveChangesWithRetries(); Trace.WriteLine("Saved the information!"); }
/// <summary> /// Saves the player entity to the storage database /// </summary> /// <param name="playerEntity">The player entity to be saved</param> public void SavePlayer(PlayerEntity playerEntity) { // Basically, calling the query later on will b0rk the service context // therefore we should update it before // but that can't be done before the player is saved to the db in the first place. // TL;DR DO NOT USE FOR UPDATING ANYTHING, IT WILL NOT WORK! //serviceContext.UpdateObject(playerEntity); //serviceContext.SaveChangesWithRetries(); // now this is the worst idea I've ever had. So f**k me. // Salvage all the important data from the entity because otherwise the query will destroy it. int currentSearchedTreasure = playerEntity.CurrentSearchedTreasure; int route = playerEntity.CurrentRoute; Dictionary<int, DateTime> treasureChest = playerEntity.TreasureChest; Dictionary<string, DateTime> achievements = playerEntity.Achievements; // then do the query. // First, check if the entity exists in the database or not //PlayerEntity checker = FindPlayerEntity(playerEntity); PlayerEntity checker = FindPlayerEntity(playerEntity.PartitionKey, playerEntity.RowKey); if (checker != null) { // Just update the value checker.CurrentSearchedTreasure = currentSearchedTreasure; checker.CurrentRoute = route; checker.Achievements = achievements; checker.TreasureChest = treasureChest; serviceContext.UpdateObject(checker); } else { // Otherwise, save the entity serviceContext.AddObject(tableName, playerEntity); } // And finally, save the changes DataServiceResponse data = serviceContext.SaveChangesWithRetries(); }
public void SaveNextTreasure(PlayerEntity playerEntity, int nextTreasure) { // Find the player entity - !! what the f**k for?!? //PlayerEntity pe = FindPlayerEntity(playerEntity); if (playerEntity != null) { // Update the value playerEntity.CurrentSearchedTreasure = nextTreasure; serviceContext.UpdateObject(playerEntity); serviceContext.SaveChangesWithRetries(); } }
/// <summary> /// Tries to create the database entry with the player in question /// </summary> /// <param name="playerId">The id to be inserted</param> /// <param name="playerType">The type of the player</param> /// <returns>True if succesful, false if not</returns> public bool TryCreateDatabasePlayerEntry(int playerId, PlayerEntity.UserType playerType) { // First, try to find if the player entity exists PlayerEntity playerEntity = DatabaseManager.Instance.FindPlayerEntity(playerType.ToString(), playerId.ToString()); // If the value is not null (== already exists) if (playerEntity != null) { // Return false, as this cannot be created return false; } // Otherwise, keep on going // First, create the new player entity to be stored playerEntity = new PlayerEntity(playerType, playerId); // Then, store it to the database DatabaseManager.Instance.SavePlayer(playerEntity); // And finally return true as a mark of success return true; }
/// <summary> /// The constructor for the player creator /// </summary> public PlayerCreator() { NewPlayerEntity = new PlayerEntity(CreateUserType(), CreatePlayerId()); }
/// <summary> /// Gets the current searched treasure /// </summary> /// <param name="player">The player in question</param> /// <returns>The number of the treasure currently looked for</returns> public int GetCurrentSearchedTreasure(PlayerEntity player) { // Return the value return player.CurrentSearchedTreasure; }
/// <summary> /// Checks if the treasure found is the correct one the player is looking for BUT doesn't require the parameters as previous, just the PlayerEntity /// </summary> /// <param name="player">PlayerEntity, the player in question</param> /// <returns>Boolean</returns> public bool CheckIfCorrectTreasure(PlayerEntity player, int found) { return CheckIfCorrectTreasure(player.CurrentSearchedTreasure, found); }
/// <summary> /// Gets the treasure defined and stores it to the player treasure chest /// </summary> /// <param name="player">The player entity in question</param> /// <param name="treasure">The treasure that was found</param> /// <param name="nextTreasure">The next treasure to be attained</param> public void GetTreasure(PlayerEntity player, int treasure) { int nextTreasure = RouteManager.getNext(player.CurrentRoute, treasure); // If the treasure chest does not exist, create it (just to be sure) if (player.TreasureChest == null) { player.TreasureChest = new Dictionary<int, DateTime>(); } // Check if the player does not have the treasure yet if (!player.TreasureChest.ContainsKey(treasure)) { // Add the treasure to the treasure chest player.TreasureChest.Add(treasure, DateTime.Now); // Set the next treasure to be the current one that is searched player.CurrentSearchedTreasure = nextTreasure; // Save the changes DatabaseManager.Instance.SavePlayer(player); } }
/// <summary> /// The only method that needs to be called outside this class itself /// Works out what content needs to be on the page and returns it in a tuple. /// Calls all other necessary methods that return the actual raw-html as a string. /// </summary> /// <param name="player">Player entity</param> /// <param name="pageNumber">Number of the page that loaded</param> /// <returns>All of the necessary page content in a string, string, string, string -type Tuple</returns> public Tuple<string, string, string, string> getPageContent(PlayerEntity player, int pageNumber) { // player is at the right checkpoint if (player.CurrentSearchedTreasure == 0) { player.CurrentSearchedTreasure = 1; DatabaseManager.Instance.SavePlayer(player); return new Tuple<string, string, string, string>("Olet jo päässyt pelin läpi! <br>Aloita uusi kierros pääaulasta.", "", "", ""); } if (player.CurrentSearchedTreasure == pageNumber) { // you can has treasure // my precious GetTreasure(player, pageNumber); //what is the player's type? switch (player.PartitionKey) { case "Regular": return new Tuple<string, string, string, string>("Löysit rastin!", "", "", getPageClue(RouteManager.getNext(player.CurrentRoute, pageNumber)) ); case "Achievements": return new Tuple<string, string, string, string>("Löysit rastin!", "", "", getPageClue(RouteManager.getNext(player.CurrentRoute, pageNumber))); case "Context": return new Tuple<string, string, string, string>("Löysit rastin!", getPageContext(pageNumber), "", getPageClue(RouteManager.getNext(player.CurrentRoute, pageNumber))); case "ContextAchievements": return new Tuple<string, string, string, string>("Löysit rastin!", getPageContext(pageNumber), "", getPageClue(RouteManager.getNext(player.CurrentRoute, pageNumber))); default: return new Tuple<string, string, string, string>(" ", " ", " ", " " + " "); } } //if this is just a page reload else if (pageNumber == RouteManager.getPrevious(player.CurrentRoute, player.CurrentSearchedTreasure) ) { return new Tuple<string, string, string, string>(" ", " ", " ", " " + getPageClue(player.CurrentSearchedTreasure)); } // player is at the wrong checkpoint else { return new Tuple<string, string, string, string>("Olet väärällä rastilla", " ", " ", " " + getPageClue(player.CurrentSearchedTreasure) ); } }