修改问题
This commit is contained in:
parent
b556c4c23b
commit
9621d635d9
|
|
@ -1,6 +1,7 @@
|
|||
using CloudGaming.Code.AppExtend;
|
||||
using CloudGaming.Code.DataAccess.MultiTenantUtil;
|
||||
using CloudGaming.Code.Filter;
|
||||
using CloudGaming.Code.Game;
|
||||
|
||||
using HuanMeng.DotNetCore.MiddlewareExtend;
|
||||
using HuanMeng.DotNetCore.SwaggerUtile;
|
||||
|
|
@ -85,7 +86,8 @@ builder.Services.AddSwaggerGen(c =>
|
|||
c.RequestBodyFilter<LowercaseRequestFilter>();
|
||||
});
|
||||
builder.AddAppConfigClient();
|
||||
|
||||
//添加游戏服务
|
||||
builder.AddPlayGameServer();
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
"secret": "95BB717C61D1ECB0E9FB82C932CC77FF",
|
||||
"nodes": "http://124.220.55.158:94", //多个节点使用逗号分隔
|
||||
"url": "http://124.220.55.158:94",
|
||||
"env": "DEV",
|
||||
"env": "TEST",
|
||||
"name": "payClient",
|
||||
"UserName": "admin",
|
||||
"Password": "dbt@com@1234"
|
||||
|
|
|
|||
|
|
@ -142,6 +142,7 @@ public class PlayGameBLL : CloudGamingBase
|
|||
await Dao.DaoPhone.Context.T_User_GameList.AddAsync(userGameList);
|
||||
await Dao.DaoPhone.Context.SaveChangesAsync();
|
||||
gameInfoCache?.PlayGameStart(gameResponse?.Data?.ScId ?? 0, userGameList.Id, playGameSettings.DisplayGrade, jyResponseData);
|
||||
gameInfoCache.Ip = playGameSettings.Ip;
|
||||
var gameResponse1 = JsonConvert.DeserializeObject<Dictionary<string, object>>(jyResponseData.ResponseContent);
|
||||
if (gameResponse1 != null && gameResponse1.TryGetValue("data", out var xxx))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,54 +1,62 @@
|
|||
using CloudGaming.Code.AppExtend;
|
||||
using CloudGaming.Code.Contract;
|
||||
using CloudGaming.Code.DataAccess;
|
||||
using CloudGaming.Code.JY;
|
||||
using CloudGaming.DtoModel.PlayGame;
|
||||
|
||||
using Flurl.Util;
|
||||
|
||||
using HuanMeng.DotNetCore.Processors;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using Refit;
|
||||
|
||||
using StackExchange.Redis;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.CreatePayDevicePrinterPrintOrderRequest.Types.Table.Types;
|
||||
|
||||
namespace CloudGaming.Code.Game;
|
||||
|
||||
/// <summary>
|
||||
/// 游戏服务
|
||||
/// </summary>
|
||||
public class PlayGameProcessor(IServiceProvider serviceProvider) : ThreadProcessor
|
||||
{
|
||||
protected override async void Proc_Do()
|
||||
{
|
||||
var appConfigs = AppConfigurationExtend.AppConfigs.Where(it => it.Key != "default")
|
||||
.Select(it => it.Value)
|
||||
.ToList();
|
||||
|
||||
// 为每个项目启动一个独立的任务
|
||||
foreach (var appConfig in appConfigs)
|
||||
{
|
||||
_ = Task.Run(() => ProcessAppConfigLoop(appConfig));
|
||||
}
|
||||
|
||||
// 主线程可以继续执行其他逻辑或保持服务运行
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单个项目的处理循环
|
||||
/// </summary>
|
||||
private async Task ProcessAppConfigLoop(AppConfig appConfig)
|
||||
{
|
||||
while (_isRunning)
|
||||
{
|
||||
// 多项目配置
|
||||
var appConfigs = AppConfigurationExtend.AppConfigs
|
||||
.Select(it => it.Value)
|
||||
.Where(it => it.DomainName != "default")
|
||||
.ToList();
|
||||
try
|
||||
{
|
||||
// 执行项目的查询和处理逻辑
|
||||
await ProcessAppConfig(appConfig).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录日志或处理异常
|
||||
Console.WriteLine($"处理项目 {appConfig.DomainName} 时发生异常: {ex.Message}");
|
||||
}
|
||||
|
||||
var tasks = appConfigs.Select(ProcessAppConfig).ToList();
|
||||
|
||||
// 等待所有任务完成
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// 使用异步延迟代替阻塞线程
|
||||
await Task.Delay(5);
|
||||
// 暂停5秒
|
||||
await Task.Delay(5000).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单次处理项目逻辑
|
||||
/// </summary>
|
||||
private async Task ProcessAppConfig(AppConfig appConfig)
|
||||
{
|
||||
var userNotActionDisconnect = DateTime.Now.AddSeconds(-appConfig.GameConfig.UserNotActionDisconnect);
|
||||
|
|
@ -60,34 +68,6 @@ public class PlayGameProcessor(IServiceProvider serviceProvider) : ThreadProcess
|
|||
var redisServer = appConfig.GetRedisServer();
|
||||
|
||||
var keyPattern = $"{PlayGameExtend.GetPlayGameKeyPrefix}:*";
|
||||
List<PlayGameUserInfo?> gameInfoList = await GetPlayGameUserInfoList(redis, redisServer, keyPattern).ConfigureAwait(false);
|
||||
|
||||
// 处理未操作断线的玩家
|
||||
var userPlayGameNotActionEndGame = gameInfoList
|
||||
.Where(it => it.GameStatus == PlayGameStatus.游戏掉线 && it.PlayGameHeartbeatAt < userNotActionEndGame)
|
||||
.ToList();
|
||||
|
||||
foreach (var user in userPlayGameNotActionEndGame)
|
||||
{
|
||||
await EndGameAsync(user, appConfig, dao, redis).ConfigureAwait(false);
|
||||
gameInfoList.Remove(user);
|
||||
}
|
||||
|
||||
// 处理未操作标记为掉线的玩家
|
||||
var userPlayGameNotAction = gameInfoList
|
||||
.Where(it => it.GameStatus == PlayGameStatus.游戏中 && it.PlayGameHeartbeatAt < userNotActionDisconnect)
|
||||
.ToList();
|
||||
|
||||
foreach (var user in userPlayGameNotAction)
|
||||
{
|
||||
//将用户标记为短线状态
|
||||
user.PlayGameUserNotAction();
|
||||
await user.SaveChangesAsync(dao, redis).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<List<PlayGameUserInfo?>> GetPlayGameUserInfoList(IDatabase redis, IServer redisServer, string keyPattern)
|
||||
{
|
||||
var keys = await redisServer.ScanKeysAsync(keyPattern, 2000).ConfigureAwait(false);
|
||||
|
||||
var tasks = keys.Select(async gameKey =>
|
||||
|
|
@ -97,9 +77,33 @@ public class PlayGameProcessor(IServiceProvider serviceProvider) : ThreadProcess
|
|||
});
|
||||
|
||||
var gameInfoList = (await Task.WhenAll(tasks).ConfigureAwait(false)).Where(x => x != null).ToList();
|
||||
return gameInfoList;
|
||||
|
||||
// 处理未操作断线的玩家
|
||||
var userPlayGameNotActionEndGame = gameInfoList
|
||||
.Where(it => it.GameStatus == PlayGameStatus.游戏掉线 && it.PlayGameHeartbeatAt < userNotActionEndGame)
|
||||
.ToList();
|
||||
if (userPlayGameNotActionEndGame.Count > 0)
|
||||
foreach (var user in userPlayGameNotActionEndGame)
|
||||
{
|
||||
await EndGameAsync(user, appConfig, dao, redis).ConfigureAwait(false);
|
||||
gameInfoList.Remove(user);
|
||||
}
|
||||
|
||||
// 处理未操作标记为掉线的玩家
|
||||
var userPlayGameNotAction = gameInfoList
|
||||
.Where(it => (it.GameStatus == PlayGameStatus.游戏中 || it.GameStatus == PlayGameStatus.开始游戏) && it.PlayGameHeartbeatAt < userNotActionDisconnect)
|
||||
.ToList();
|
||||
if (userPlayGameNotAction.Count > 0)
|
||||
foreach (var user in userPlayGameNotAction)
|
||||
{
|
||||
user.PlayGameUserNotAction();
|
||||
await user.SaveChangesAsync(dao, redis).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 结束游戏逻辑
|
||||
/// </summary>
|
||||
private async Task EndGameAsync(PlayGameUserInfo user, AppConfig appConfig, DAO dao, IDatabase redis)
|
||||
{
|
||||
using HttpClient httpClient = new HttpClient(new JYApiAppConfigHandler(appConfig, user.Ip));
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user