public async Task <IActionResult> CreateUserByDeviceId([FromBody] UserDevice userDevice) { if (string.IsNullOrEmpty(userDevice?.DeviceId)) { return(BadRequest("DeviceId was invalid.")); } // NOTE: We may want to enable this again later // Check if DeviceId exists //var dbUser = await m_CoreDbContext.Users // .Where(dbu => dbu.DeviceId == userDevice.DeviceId) // .SingleOrDefaultAsync(); // If it does not exist, create a new user //if (dbUser == null) //{ var userId = Guid.NewGuid().ToString(); var authToken = Guid.NewGuid().ToString(); // User data var dbUser = new DbUser { UserId = userId, DeviceId = userDevice.DeviceId, AuthToken = authToken }; // Users default profile var dbUserProfile = new DbUserProfile { UsersProfileId = Guid.NewGuid().ToString(), UserId = userId, Name = m_ConfigDbContext.UserDefaults.Profile.Name, Xp = m_ConfigDbContext.UserDefaults.Profile.Xp, CreateDate = DateTime.UtcNow, LastSeenDate = DateTime.UtcNow, LastDailyRewardDate = DateTime.UtcNow, GoldMineDaysRemaining = 0, GemMineDaysRemaining = 0, Level = 0, FriendCount = 0, }; // Users default wallet var dbUserWallet = new DbUserWallet { UsersWalletId = Guid.NewGuid().ToString(), UserId = userId, HappyTokens = m_ConfigDbContext.UserDefaults.Wallet.HappyTokens, Gems = m_ConfigDbContext.UserDefaults.Wallet.Gems, Gold = m_ConfigDbContext.UserDefaults.Wallet.Gold, }; // User default happiness var dbUserHappiness = new DbUserHappiness { UsersHappinessId = Guid.NewGuid().ToString(), UserId = userId, Wealth = m_ConfigDbContext.UserDefaults.Happiness.Wealth, Experience = m_ConfigDbContext.UserDefaults.Happiness.Experience, Health = m_ConfigDbContext.UserDefaults.Happiness.Health, Skill = m_ConfigDbContext.UserDefaults.Happiness.Skill, Social = m_ConfigDbContext.UserDefaults.Happiness.Social }; // Create default avatars (Avatars give happiness, allocate based on Level1) var dbUsersAvatars = new List <DbUserAvatar>(); foreach (var avatarType in m_ConfigDbContext.UserDefaults.AvatarTypes) { var userAvatar = new DbUserAvatar() { UsersAvatarId = Guid.NewGuid().ToString(), UserId = userId, AvatarType = avatarType, Level = 1, Pieces = 0 }; // Grab the default avatar config var avatar = m_ConfigDbContext.Avatars.Avatars.Find(i => i.AvatarType == avatarType); // Add the happiness gained from Level1 to the users Happiness var happinessType = avatar.HappinessType; var happinessAmount = avatar.Levels[0].Happiness; dbUserHappiness.Add(happinessType, happinessAmount); dbUsersAvatars.Add(userAvatar); } // Create default buildings (Buildings give happiness, allocate based on Level1) var dbUsersBuildings = new List <DbUserBuilding>(); foreach (var buildingType in m_ConfigDbContext.UserDefaults.BuildingTypes) { var userBuilding = new DbUserBuilding() { UsersBuildingId = Guid.NewGuid().ToString(), UserId = userId, BuildingType = buildingType, Level = 1 }; // Grab the default avatar config var building = m_ConfigDbContext.Buildings.Buildings.Find(i => i.BuildingType == buildingType); // Add the happiness gained from Level1 to the users Happiness var happinessType = building.HappinessType; var happinessAmount = building.Levels[0].Happiness; dbUserHappiness.Add(happinessType, happinessAmount); dbUsersBuildings.Add(userBuilding); } // Add the new user await m_CoreDbContext.Users.AddAsync(dbUser); await m_CoreDbContext.UsersProfiles.AddAsync(dbUserProfile); await m_CoreDbContext.UsersWallets.AddAsync(dbUserWallet); await m_CoreDbContext.UsersHappiness.AddAsync(dbUserHappiness); await m_CoreDbContext.UsersAvatars.AddRangeAsync(dbUsersAvatars); await m_CoreDbContext.UsersBuildings.AddRangeAsync(dbUsersBuildings); // Save changes await m_CoreDbContext.SaveChangesAsync(); // Create the user to send back to the client var response = new UserAuthPair { UserId = userId, AuthToken = authToken }; return(Ok(response)); // } // User with this DeviceId already exists // return Forbid(); }
public async Task <IActionResult> BuyAvatar([FromBody] AvatarType avatarType) { var userId = this.GetClaimantUserId(); if (!this.IsValidUserId(userId)) { return(BadRequest("UserId is invalid.")); } var dbAvatar = m_ConfigDbContext.Store.Avatars.Find(i => i.AvatarType == avatarType); if (dbAvatar == null) { return(BadRequest("AvatarType is invalid.")); } // Check if User owns the Avatar and avatar is 1 - Upgrade level var dbUsersAvatars = await m_CoreDbContext.UsersAvatars .Where(i => i.UserId == userId) .ToListAsync(); if (dbUsersAvatars.Exists(i => i.AvatarType == avatarType)) { return(BadRequest("User already owns this Avatar.")); } // Check User has the currency required var dbUserWallet = await m_CoreDbContext.UsersWallets .Where(i => i.UserId == userId) .SingleOrDefaultAsync(); if (dbUserWallet == null) { return(BadRequest("Could not find users wallet.")); } if (dbUserWallet.Gold < dbAvatar.Gold) { return(BadRequest("User does not have enough Gold for this Avatar.")); } if (dbUserWallet.Gems < dbAvatar.Gems) { return(BadRequest("User does not have enough Gems for this Avatar.")); } if (dbUserWallet.HappyTokens < dbAvatar.HappyTokens) { return(BadRequest("User does not have enough HappyTokens for this Avatar.")); } // Deduct the currencies required for the Avatar from the users Wallet dbUserWallet.Gold -= dbAvatar.Gold; dbUserWallet.Gems -= dbAvatar.Gems; dbUserWallet.HappyTokens -= dbAvatar.HappyTokens; // Create the new UserAvatar var dbUserAvatar = new DbUserAvatar() { UsersAvatarId = Guid.NewGuid().ToString(), UserId = userId, AvatarType = avatarType, Level = 1, Pieces = 0 }; // Add the happiness gained from Level1 to the users Happiness var avatar = m_ConfigDbContext.Avatars.Avatars.Find(i => i.AvatarType == avatarType); var happinessType = avatar.HappinessType; var happinessAmount = avatar.Levels[0].Happiness; var dbUserHappiness = await m_CoreDbContext.UsersHappiness .Where(i => i.UserId == userId) .SingleOrDefaultAsync(); // Update the users Avatars and Happiness accordingly dbUserHappiness.Add(happinessType, happinessAmount); await m_CoreDbContext.UsersAvatars.AddAsync(dbUserAvatar); await m_CoreDbContext.SaveChangesAsync(); // Send the updated Wallet back to the user var wallet = (Wallet)dbUserWallet; return(Ok(wallet)); }