Skip to content

Commit

Permalink
修复折扣页面筛选价格无效的bug,完成分享游戏库页面前半部分
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleFish-233 committed Jul 2, 2023
1 parent 0a0f575 commit 14e93f4
Show file tree
Hide file tree
Showing 31 changed files with 721 additions and 230 deletions.
Expand Up @@ -292,8 +292,14 @@ public async Task<ApplicationUser> GetAPICurrentUserAsync(HttpContext context)
return null;
}
var user = await _userRepository.FirstOrDefaultAsync(s => s.Id == id);
if(user!=null)
if (user != null)
{
//是否更新SteamId
if (string.IsNullOrEmpty(user.SteamId) || user.SteamId.Replace("", ",").Replace("", ",").Contains(',') == false)
{
user.SteamId = context.User?.Claims?.GetUserSteamId();
await _userRepository.UpdateAsync(user);
}
return user;
}

Expand Down
Expand Up @@ -14,6 +14,6 @@ public interface ISteamInforService
/// <returns></returns>
Task<bool> UpdateUserSteam(ApplicationUser user);

Task<List<SteamUserInfor>> GetSteamUserInfors(List<string> steamids);
Task<List<SteamUserInforModel>> GetSteamUserInfors(List<string> steamids, ApplicationUser user);
}
}
Expand Up @@ -18,7 +18,7 @@ namespace CnGalWebSite.APIServer.Application.SteamInfors
{
public class SteamInforService : ISteamInforService
{
private readonly IRepository<SteamInfor, long> _steamInforRepository;
private readonly IRepository<StoreInfo, long> _storeInfoRepository;
private readonly IRepository<ApplicationUser, string> _userRepository;
private readonly IRepository<PlayedGame, long> _playedGameRepository;
private readonly IRepository<Entry, int> _entryRepository;
Expand All @@ -27,11 +27,11 @@ public class SteamInforService : ISteamInforService
private readonly HttpClient _httpClient;
private readonly ILogger<SteamInforService> _logger;

public SteamInforService(IRepository<SteamInfor, long> steamInforRepository, IRepository<ApplicationUser, string> userRepository, IRepository<Entry, int> entryRepository,
public SteamInforService(IRepository<StoreInfo, long> storeInfoRepository, IRepository<ApplicationUser, string> userRepository, IRepository<Entry, int> entryRepository,
IConfiguration configuration, IRepository<PlayedGame, long> playedGameRepository, IRepository<SteamUserInfor, long> steamUserInforRepository, ILogger<SteamInforService> logger,
HttpClient httpClient)
{
_steamInforRepository = steamInforRepository;
_storeInfoRepository = storeInfoRepository;
_userRepository = userRepository;
_configuration = configuration;
_playedGameRepository = playedGameRepository;
Expand Down Expand Up @@ -79,15 +79,15 @@ public async Task<bool> UpdateUserSteam(ApplicationUser user)

//查找
var userGames = await _playedGameRepository.GetAll().Where(s => s.ApplicationUserId == user.Id).ToListAsync();
var appids = steamGames.games.Select(s => s.appid).Distinct();
var steams = await _steamInforRepository.GetAll().AsNoTracking()
var appids = steamGames.games.Select(s => s.appid.ToString()).Distinct();
var steams = await _storeInfoRepository.GetAll().AsNoTracking()
.Include(s => s.Entry)
.Where(s => string.IsNullOrWhiteSpace(s.Entry.Name) == false && s.Entry.IsHidden == false)
.Where(s => appids.Contains(s.SteamId))
.Where(s => string.IsNullOrWhiteSpace(s.Entry.Name) == false && s.Entry.IsHidden == false && s.PlatformType == PublishPlatformType.Steam)
.Where(s => appids.Contains(s.Link))
.Select(s => new
{
s.EntryId,
s.SteamId
SteamId = s.Link
})
.ToListAsync();

Expand All @@ -98,7 +98,7 @@ public async Task<bool> UpdateUserSteam(ApplicationUser user)
if (steamTemp != null)
{
item.IsInSteam = true;
item.PlayDuration = steamGames.games.FirstOrDefault(s => s.appid == steamTemp.SteamId)?.playtime_forever ?? 0;
item.PlayDuration = steamGames.games.FirstOrDefault(s => s.appid.ToString() == steamTemp.SteamId)?.playtime_forever ?? 0;
}
else
{
Expand All @@ -120,9 +120,9 @@ public async Task<bool> UpdateUserSteam(ApplicationUser user)
_ = await _playedGameRepository.InsertAsync(new PlayedGame
{
IsInSteam = true,
PlayDuration = steamGames.games.FirstOrDefault(s => s.appid == item.SteamId)?.playtime_forever ?? 0,
PlayDuration = steamGames.games.FirstOrDefault(s => s.appid.ToString() == item.SteamId)?.playtime_forever ?? 0,
EntryId = item.EntryId,
Type = ((steamGames.games.FirstOrDefault(s => s.appid == item.SteamId)?.playtime_forever ?? 0) > 0) ? PlayedGameType.Played : PlayedGameType.UnPlayed,
Type = ((steamGames.games.FirstOrDefault(s => s.appid.ToString() == item.SteamId)?.playtime_forever ?? 0) > 0) ? PlayedGameType.Played : PlayedGameType.UnPlayed,
ApplicationUserId = user.Id,
ShowPublicly = true,
LastEditTime = now
Expand All @@ -132,23 +132,42 @@ public async Task<bool> UpdateUserSteam(ApplicationUser user)
}


public async Task<List<SteamUserInfor>> GetSteamUserInfors(List<string> steamids)
public async Task<List<SteamUserInforModel>> GetSteamUserInfors(List<string> steamids, ApplicationUser user)
{
var model = await _steamUserInforRepository.GetAllListAsync(s => steamids.Contains(s.SteamId));
var steams = await _steamUserInforRepository.GetAllListAsync(s => steamids.Contains(s.SteamId));

foreach (var item in steamids)
{
//不存在则获取
if (model.Any(s => s.SteamId == item) == false)
if (steams.Any(s => s.SteamId == item) == false)
{
var temp = await UpdateSteamUserInfor(item);
if (temp != null)
{
model.Add(temp);
steams.Add(temp);
}
}
}

var model = new List<SteamUserInforModel>();

var gameIds = user == null ? new List<int>() : await _playedGameRepository.GetAll().AsNoTracking().Where(s => s.ApplicationUserId == user.Id && s.IsInSteam && s.EntryId != null).Select(s => s.EntryId.Value).ToListAsync();

foreach (var item in steams)
{
//计算总价格
var price = gameIds.Any() ? await _storeInfoRepository.GetAll().AsNoTracking().Where(s => s.PlatformType == PublishPlatformType.Steam && s.OriginalPrice != null && s.EntryId != null && gameIds.Contains(s.EntryId.Value)).SumAsync(s => s.OriginalPrice.Value) : 0;

model.Add(new SteamUserInforModel
{

SteamId = item.SteamId,
Image = item.Image,
Name = item.Name,
Price = price
});
}

return model;
}

Expand Down
Expand Up @@ -26,22 +26,24 @@ public class SteamAPIController : ControllerBase

private readonly IRepository<Entry, int> _entryRepository;
private readonly IRepository<Tag, int> _tagRepository;
private readonly IRepository<SteamInfor, int> _steamInforRepository;
private readonly IRepository<StoreInfo, long> _storeInfoRepository;
private readonly IRepository<PlayedGame, long> _playedGameRepository;
private readonly IAppHelper _appHelper;
private readonly IHttpService _httpService;
private readonly ISteamInforService _steamInforService;
private readonly IRepository<ApplicationUser, string> _userRepository;

public SteamAPIController(IRepository<SteamInfor, int> steamInforRepository, ISteamInforService steamInforService, IRepository<Tag, int> tagRepository,
public SteamAPIController(IRepository<StoreInfo, long> storeInfoRepository, ISteamInforService steamInforService, IRepository<Tag, int> tagRepository, IRepository<PlayedGame, long> playedGameRepository,
IAppHelper appHelper, IRepository<Entry, int> entryRepository, IRepository<ApplicationUser, string> userRepository, IHttpService httpService)
{
_entryRepository = entryRepository;
_appHelper = appHelper;
_steamInforRepository = steamInforRepository;
_storeInfoRepository = storeInfoRepository;
_steamInforService = steamInforService;
_userRepository = userRepository;
_tagRepository = tagRepository;
_httpService = httpService;
_playedGameRepository = playedGameRepository;
}

[AllowAnonymous]
Expand All @@ -54,7 +56,7 @@ public async Task<string> GetSteamHtml(int steamId)

[AllowAnonymous]
[HttpGet("{id}")]
public async Task<ActionResult<List<SteamUserInfor>>> GetUserSteamInforAsync(string id)
public async Task<ActionResult<List<SteamUserInforModel>>> GetUserSteamInforAsync(string id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
Expand All @@ -68,12 +70,12 @@ public async Task<ActionResult<List<SteamUserInfor>>> GetUserSteamInforAsync(str

if (string.IsNullOrWhiteSpace(objectUser.SteamId))
{
return new List<SteamUserInfor>();
return new List<SteamUserInforModel>();
}

var steamids = objectUser.SteamId.Replace("", ",").Replace("", ",").Split(',');

var model = await _steamInforService.GetSteamUserInfors(steamids.ToList());
var model = await _steamInforService.GetSteamUserInfors(steamids.ToList(),user);

return model;

Expand Down Expand Up @@ -102,6 +104,43 @@ public async Task<ActionResult<List<EntryInforTipViewModel>>> GetFreeGamesAsync(
return model;
}

[AllowAnonymous]
[HttpGet]
public async Task<ActionResult<SteamGamesOverviewModel>> GetSteamGamesOverview()
{
var users = await _playedGameRepository.GetAll().AsNoTracking().Include(s => s.ApplicationUser).Where(s => s.IsInSteam && s.ApplicationUser != null).GroupBy(s => s.ApplicationUserId).Where(s => s.Any()).Select(s => new HasMostGamesUserModel
{
Count = s.Count(),
Name = s.First().ApplicationUser.UserName,
Image = s.First().ApplicationUser.PhotoPath,
PersonalSignature = s.First().ApplicationUser.PersonalSignature,
}).OrderByDescending(s => s.Count).Take(10).ToListAsync();

var userCount = await _playedGameRepository.GetAll().AsNoTracking().Include(s => s.ApplicationUser).Where(s => s.IsInSteam && s.ApplicationUser != null).GroupBy(s => s.ApplicationUserId).Where(s => s.Any()).CountAsync();

var games = await _playedGameRepository.GetAll().AsNoTracking().Include(s => s.Entry).Where(s => s.IsInSteam && s.Entry != null && string.IsNullOrWhiteSpace(s.Entry.Name) == false && s.Entry.IsHidden == false)
.GroupBy(s => s.EntryId).Where(s => s.Any()).Select(s => new
{
Rate = (double)s.Count() / userCount,
s.First().Entry
}).OrderByDescending(s => s.Rate).Take(10).ToListAsync();

var highestGames = new List<PossessionRateHighestGameModel>();
foreach (var item in games)
{
var entry =new PossessionRateHighestGameModel();
entry.SynchronizationProperties(_appHelper.GetEntryInforTipViewModel(item.Entry));
entry.Rate = item.Rate;
highestGames.Add(entry);
}

return new SteamGamesOverviewModel
{
Count = await _storeInfoRepository.GetAll().AsNoTracking().CountAsync(s => s.PlatformType == PublishPlatformType.Steam && string.IsNullOrWhiteSpace(s.Link) == false),
HasMostGamesUsers = users,
PossessionRateHighestGames = highestGames
};
}

}
}
Expand Up @@ -31,4 +31,26 @@ public enum JudgableGamesDisplayType
[Display(Name = "条幅")]
LongCard,
}

public enum PlayedGamesSortType
{
[Display(Name = "游玩状态")]
Type,
[Display(Name = "游玩时间")]
Time,
[Display(Name = "我的评分")]
Score,
[Display(Name = "名称")]
Name,
}

public enum PlayedGamesDisplayType
{
[Display(Name = "大卡片")]
LargeCard,
[Display(Name = "预览图")]
SmallCard,
[Display(Name = "条幅")]
LongCard,
}
}
Expand Up @@ -117,7 +117,7 @@ public void SetItems()
//筛选价格区间
if (maxOriginalPrice < MaxOriginalPriceLimit && maxOriginalPrice >= 0)
{
Items = Items.Where(s => s.OriginalPrice <= maxOriginalPrice*100);
Items = Items.Where(s => s.OriginalPrice <= maxOriginalPrice);
}


Expand Down
@@ -0,0 +1,34 @@
using CnGalWebSite.DataModel.ViewModel.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CnGalWebSite.DataModel.ViewModel.Steam
{
public class SteamGamesOverviewModel
{
public int Count { get; set; }

public List<PossessionRateHighestGameModel> PossessionRateHighestGames { get; set; }

public List<HasMostGamesUserModel> HasMostGamesUsers { get; set; }
}

public class PossessionRateHighestGameModel : EntryInforTipViewModel
{
public double Rate { get; set; }
}

public class HasMostGamesUserModel
{
public string Name { get; set; }

public string Image { get; set; }

public int Count { get; set; }

public string PersonalSignature { get; set; }
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CnGalWebSite.DataModel.ViewModel.Steam
{
public class SteamUserInforModel
{
public string SteamId { get; set; }

public string Image { get; set; }

public string Name { get; set; }

public double Price { get; set; }
}
}
23 changes: 23 additions & 0 deletions CnGalWebSite/CnGalWebSite.DataModel/ViewModel/Theme/ThemeModel.cs
Expand Up @@ -72,6 +72,12 @@ public enum ThemeType
}

public class AnniversariesSetting
{
public JudgableGamesSetting JudgableGamesSetting { get;set; }=new JudgableGamesSetting();
public PlayedGameSetting PlayedGameSetting { get; set; } = new PlayedGameSetting();
}

public class JudgableGamesSetting
{
public string SearchString { get; set; }
public JudgableGamesSortType SortType { get; set; }
Expand All @@ -87,4 +93,21 @@ public class AnniversariesSetting

public int CurrentPage { get; set; } = 1;
}

public class PlayedGameSetting
{
public string SearchString { get; set; }
public PlayedGamesSortType SortType { get; set; }
public PlayedGamesDisplayType DisplayType { get; set; }

public int TabIndex { get; set; } = 1;

public int MaxCount { get; set; } = 24;

public int Count { get; set; }

public int TotalPages => (Count / MaxCount) + 1;

public int CurrentPage { get; set; } = 1;
}
}
5 changes: 5 additions & 0 deletions CnGalWebSite/CnGalWebSite.Extensions/ClaimExtensions.cs
Expand Up @@ -55,5 +55,10 @@ public static string GetUserId(this IEnumerable<Claim> claims)
{
return claims?.FirstOrDefault(s => s.Type == JwtClaimTypes.Subject || s.Type == ClaimTypes.NameIdentifier)?.Value;
}

public static string GetUserSteamId(this IEnumerable<Claim> claims)
{
return claims?.FirstOrDefault(s => s.Type == "SteamId")?.Value;
}
}
}

0 comments on commit 14e93f4

Please sign in to comment.