添加游戏心跳
This commit is contained in:
parent
6c5a7b8019
commit
83234ca339
|
|
@ -349,6 +349,66 @@ namespace CloudGaming.Code.Account
|
|||
}
|
||||
return await UserConsumeDiamondMoneyAsync(cloudGamingBase, money, it => { it.Title = title; it.OrderCode = orderId; });
|
||||
}
|
||||
/// <summary>
|
||||
/// 用户玩游戏消耗钻石
|
||||
/// </summary>
|
||||
/// <param name="cloudGamingBase"></param>
|
||||
/// <param name="money">金额</param>
|
||||
/// <param name="currencyLogId">资产记录日志id</param>
|
||||
/// <param name="gameName">游戏名称</param>
|
||||
/// <param name="diamondListId">资产收入支出记录id</param>
|
||||
/// <param name="gameXiaoHao">游戏每分钟消耗多少钻石</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<(bool, int, int)> UserPlayGameDiamondConsumeMoney(this CloudGamingBase cloudGamingBase, decimal money, string gameName, int currencyLogId,int diamondListId, string gameXiaoHao)
|
||||
{
|
||||
bool issuccess = false;
|
||||
int _currencyLogId = currencyLogId;
|
||||
try
|
||||
{
|
||||
var currency = new T_User_Currency();
|
||||
GameDiamondConsumeMoney gameDiamondConsumeMoney = new GameDiamondConsumeMoney(cloudGamingBase.Dao, cloudGamingBase._UserId, UserCurrencyType.钻石, currency);
|
||||
(issuccess, _currencyLogId) = await gameDiamondConsumeMoney.ConsumeMoneyAsync(money, currencyLogId);
|
||||
if (issuccess)
|
||||
{
|
||||
cloudGamingBase.UserInfo.Diamond = (int)currency.CurrencyMoney;
|
||||
await cloudGamingBase.SaveUserInfoCacheChangesAsync();
|
||||
UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
|
||||
T_User_DiamondList userDiamondList = null;
|
||||
if (diamondListId > 0)
|
||||
{
|
||||
userDiamondList = await cloudGamingBase.Dao.DaoUser.Context.T_User_DiamondList.FirstOrDefaultAsync(it => it.Id == diamondListId);
|
||||
}
|
||||
if (userDiamondList == null)
|
||||
{
|
||||
userDiamondList = new T_User_DiamondList()
|
||||
{
|
||||
CreateAt = DateTime.Now,
|
||||
Consume = 0,
|
||||
ConsumeType = (int)consumeType,
|
||||
CurrencyType = (int)UserCurrencyType.钻石,
|
||||
OrderCode = "",
|
||||
Title = $"游玩《{gameName}》",
|
||||
UpdateAt = DateTime.Now,
|
||||
UserId = cloudGamingBase.UserInfo.UserId,
|
||||
Extend = gameXiaoHao,
|
||||
};
|
||||
await cloudGamingBase.Dao.DaoUser.Context.AddAsync(userDiamondList);
|
||||
diamondListId = userDiamondList.Id;
|
||||
}
|
||||
userDiamondList.Consume += money;
|
||||
userDiamondList.UpdateAt = DateTime.Now;
|
||||
await cloudGamingBase.Dao.DaoUser.Context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
return (false, _currencyLogId, diamondListId); ;
|
||||
}
|
||||
return (issuccess, _currencyLogId, diamondListId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using Bogus.DataSets;
|
||||
|
||||
using CloudGaming.Code.Account.Contract;
|
||||
using CloudGaming.Code.DataAccess;
|
||||
using CloudGaming.DtoModel.Account.User;
|
||||
|
|
@ -8,75 +10,89 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudGaming.Code.Account.UserCurrency
|
||||
namespace CloudGaming.Code.Account.UserCurrency;
|
||||
|
||||
/// <summary>
|
||||
/// 扣除或者充值用户钻石
|
||||
/// </summary>
|
||||
/// <param name="dao"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="userCurrencyType"></param>
|
||||
/// <param name="userCurrency"></param>
|
||||
public class DiamondConsumeMoney(DAO dao, int userId, UserCurrencyType userCurrencyType, T_User_Currency userCurrency = null) : IUserConsumeMoney
|
||||
{
|
||||
/// <summary>
|
||||
/// 扣除或者充值用户钻石
|
||||
/// 用户余额扣除
|
||||
/// </summary>
|
||||
/// <param name="money"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
|
||||
public async Task<bool> ConsumeMoneyAsync(decimal money)
|
||||
{
|
||||
T_User_Currency? currency = await AddUserCurrencyAsync(dao, userId, userCurrencyType, money);
|
||||
UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
|
||||
var currency_Log = new T_User_Currency_Log()
|
||||
{
|
||||
Consume = money,
|
||||
ConsumeType = (int)consumeType,
|
||||
CreateAt = DateTime.Now,
|
||||
CurrencyType = (int)userCurrencyType,
|
||||
Remarks = $"于{DateTime.Now:yyyy-MM-dd HH:mm:ss}{consumeType}[{Math.Abs(money)}]{userCurrencyType};",
|
||||
UpdateAt = DateTime.Now,
|
||||
UserId = userId,
|
||||
};
|
||||
await dao.DaoUser.Context.T_User_Currency_Log.AddAsync(currency_Log);
|
||||
await dao.DaoUser.Context.SaveChangesAsync();
|
||||
if (userCurrency != null)
|
||||
{
|
||||
userCurrency.CurrencyMoney = currency.CurrencyMoney;
|
||||
userCurrency.UpdateAt = currency.UpdateAt;
|
||||
userCurrency.Id = currency.Id;
|
||||
userCurrency.CurrencyName = currency.CurrencyName;
|
||||
userCurrency.CreateAt = currency.CreateAt;
|
||||
userCurrency.CurrencyType = currency.CurrencyType;
|
||||
}
|
||||
else
|
||||
{
|
||||
userCurrency = currency;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="dao"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="userCurrencyType"></param>
|
||||
/// <param name="userCurrency"></param>
|
||||
public class DiamondConsumeMoney(DAO dao, int userId, UserCurrencyType userCurrencyType, T_User_Currency userCurrency = null) : IUserConsumeMoney
|
||||
/// <param name="money"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception">余额不足</exception>
|
||||
public async Task<T_User_Currency?> AddUserCurrencyAsync(decimal money)
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户余额扣除
|
||||
/// </summary>
|
||||
/// <param name="money"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
|
||||
public async Task<bool> ConsumeMoneyAsync(decimal money)
|
||||
var currency = await dao.DaoUser.Context.T_User_Currency.Where(it => it.CurrencyType == (int)userCurrencyType && it.UserId == userId).FirstOrDefaultAsync();
|
||||
if (currency == null)
|
||||
{
|
||||
|
||||
var currency = await dao.DaoUser.Context.T_User_Currency.Where(it => it.CurrencyType == (int)userCurrencyType && it.UserId == userId).FirstOrDefaultAsync();
|
||||
if (currency == null)
|
||||
currency = new T_User_Currency()
|
||||
{
|
||||
currency = new T_User_Currency()
|
||||
{
|
||||
CreateAt = DateTime.Now,
|
||||
CurrencyMoney = 0,
|
||||
CurrencyName = userCurrencyType.ToString(),
|
||||
CurrencyType = (int)userCurrencyType,
|
||||
UserId = userId,
|
||||
UpdateAt = DateTime.Now,
|
||||
};
|
||||
await dao.DaoUser.Context.T_User_Currency.AddAsync(currency);
|
||||
await dao.DaoUser.Context.SaveChangesAsync();
|
||||
}
|
||||
currency.CurrencyMoney += money;
|
||||
if (currency.CurrencyMoney < 0)
|
||||
{
|
||||
throw new Exception("余额不足");
|
||||
}
|
||||
UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
|
||||
var currency_Log = new T_User_Currency_Log()
|
||||
{
|
||||
Consume = money,
|
||||
ConsumeType = (int)consumeType,
|
||||
CreateAt = DateTime.Now,
|
||||
CurrencyMoney = 0,
|
||||
CurrencyName = userCurrencyType.ToString(),
|
||||
CurrencyType = (int)userCurrencyType,
|
||||
Remarks = $"于{DateTime.Now:yyyy-MM-dd HH:mm:ss}{consumeType}[{Math.Abs(money)}]{userCurrencyType};",
|
||||
UpdateAt = DateTime.Now,
|
||||
UserId = userId,
|
||||
UpdateAt = DateTime.Now,
|
||||
};
|
||||
await dao.DaoUser.Context.T_User_Currency_Log.AddAsync(currency_Log);
|
||||
await dao.DaoUser.Context.T_User_Currency.AddAsync(currency);
|
||||
await dao.DaoUser.Context.SaveChangesAsync();
|
||||
if (userCurrency != null)
|
||||
{
|
||||
userCurrency.CurrencyMoney = currency.CurrencyMoney;
|
||||
userCurrency.UpdateAt = currency.UpdateAt;
|
||||
userCurrency.Id = currency.Id;
|
||||
userCurrency.CurrencyName = currency.CurrencyName;
|
||||
userCurrency.CreateAt = currency.CreateAt;
|
||||
userCurrency.CurrencyType = currency.CurrencyType;
|
||||
}
|
||||
else
|
||||
{
|
||||
userCurrency = currency;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
currency.CurrencyMoney += money;
|
||||
if (currency.CurrencyMoney < 0)
|
||||
{
|
||||
throw new Exception("余额不足");
|
||||
}
|
||||
|
||||
return currency;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
using CloudGaming.Code.Account.Contract;
|
||||
using CloudGaming.Code.DataAccess;
|
||||
using CloudGaming.DtoModel.Account.User;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudGaming.Code.Account.UserCurrency;
|
||||
|
||||
/// <summary>
|
||||
/// 游戏扣除钻石
|
||||
/// </summary>
|
||||
/// <param name="dao"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="userCurrencyType"></param>
|
||||
/// <param name="userCurrency"></param>
|
||||
public class GameDiamondConsumeMoney(DAO dao, int userId, UserCurrencyType userCurrencyType, T_User_Currency userCurrency = null) : IUserConsumeMoney
|
||||
{
|
||||
/// <summary>
|
||||
/// 玩游戏消耗钻石
|
||||
/// </summary>
|
||||
/// <param name="money"></param>
|
||||
/// <param name="currencyLogId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<(bool, int)> ConsumeMoneyAsync(decimal money, int currencyLogId)
|
||||
{
|
||||
var currency = await new DiamondConsumeMoney(dao, userId, userCurrencyType, userCurrency).AddUserCurrencyAsync(money);
|
||||
if (currency == null)
|
||||
{
|
||||
throw new Exception("扣费失败");
|
||||
}
|
||||
UserCurrencyConsumeType consumeType = money >= 0 ? UserCurrencyConsumeType.收入 : UserCurrencyConsumeType.消耗;
|
||||
T_User_Currency_Log currency_Log = null;
|
||||
if (currencyLogId > 0)
|
||||
{
|
||||
currency_Log = await dao.DaoUser.Context.T_User_Currency_Log.FirstOrDefaultAsync(it => it.Id == currencyLogId);
|
||||
}
|
||||
if (currency_Log == null)
|
||||
{
|
||||
currency_Log = new T_User_Currency_Log()
|
||||
{
|
||||
Consume = 0,
|
||||
ConsumeType = (int)consumeType,
|
||||
CreateAt = DateTime.Now,
|
||||
CurrencyType = (int)userCurrencyType,
|
||||
Remarks = $"",
|
||||
UpdateAt = DateTime.Now,
|
||||
UserId = userId,
|
||||
};
|
||||
await dao.DaoUser.Context.T_User_Currency_Log.AddAsync(currency_Log);
|
||||
}
|
||||
currency_Log.Consume += money;
|
||||
currency_Log.Remarks = $"用户玩游戏{consumeType}[{Math.Abs(money)}]{userCurrencyType}";
|
||||
await dao.DaoUser.Context.SaveChangesAsync();
|
||||
if (userCurrency != null)
|
||||
{
|
||||
userCurrency.CurrencyMoney = currency.CurrencyMoney;
|
||||
userCurrency.UpdateAt = currency.UpdateAt;
|
||||
userCurrency.Id = currency.Id;
|
||||
userCurrency.CurrencyName = currency.CurrencyName;
|
||||
userCurrency.CreateAt = currency.CreateAt;
|
||||
userCurrency.CurrencyType = currency.CurrencyType;
|
||||
}
|
||||
else
|
||||
{
|
||||
userCurrency = currency;
|
||||
}
|
||||
return (true, currency_Log?.Id ?? 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用户余额扣除
|
||||
/// </summary>
|
||||
/// <param name="money"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
|
||||
public async Task<bool> ConsumeMoneyAsync(decimal money)
|
||||
{
|
||||
return await new DiamondConsumeMoney(dao, userId, userCurrencyType, userCurrency).ConsumeMoneyAsync(money);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ namespace CloudGaming.Code.Contract
|
|||
/// <param name="playGameQueue"></param>
|
||||
/// <returns></returns>
|
||||
[Post("/jyapi/reconPlayGame")]
|
||||
Task<JYApiResponse<Dictionary<string, object>>> ReconPlayGame([Body(BodySerializationMethod.UrlEncoded)] DisplayGradeSettings playGameQueue);
|
||||
Task<JYApiResponse<Dictionary<string, object>>> ReconPlayGame([Body(BodySerializationMethod.UrlEncoded)] ReconPlayGameSettings reconPlayGameSettings);
|
||||
|
||||
/// <summary>
|
||||
/// 停止游戏
|
||||
|
|
@ -77,6 +77,14 @@ namespace CloudGaming.Code.Contract
|
|||
[Post("/jyapi/stopGame")]
|
||||
Task<JYApiResponse<Dictionary<string, object>>> StopGame([Body(BodySerializationMethod.UrlEncoded)] PlayGameCommonSetting playGameQueue);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查看自己的会话列表
|
||||
/// </summary>
|
||||
/// <param name="requestBase"></param>
|
||||
/// <returns></returns>
|
||||
[Post("/jyapi/myScList")]
|
||||
Task<JYApiResponse<Dictionary<string, object>>> MyScList([Body(BodySerializationMethod.UrlEncoded)] RequestBaseModel requestBase);
|
||||
/// <summary>
|
||||
/// 获取会话列表
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
using Azure.Core;
|
||||
|
||||
using CloudGaming.Code.Account;
|
||||
using CloudGaming.Code.AppExtend.ConfigModel;
|
||||
using CloudGaming.Code.Contract;
|
||||
using CloudGaming.DtoModel.Account.User;
|
||||
using CloudGaming.DtoModel.Game;
|
||||
using CloudGaming.DtoModel.JY;
|
||||
using CloudGaming.DtoModel.PlayGame;
|
||||
|
|
@ -118,20 +122,10 @@ namespace CloudGaming.Code.Game
|
|||
};
|
||||
playGameRequest.Cpu = playGameRequest.Cpu;
|
||||
|
||||
// 调用游戏启动接口
|
||||
// 调用游戏启动接口
|
||||
var data = await JYApi.PlayGame(playGameSettings);
|
||||
|
||||
// 创建游戏启动日志对象
|
||||
var playGameLog = new T_User_PlayGmaeLog
|
||||
{
|
||||
CreateTime = DateTime.Now,
|
||||
DescribeContent = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 用户尝试启动游戏;",
|
||||
GameId = playGameRequest.GameId,
|
||||
UserId = _UserId,
|
||||
RequestContent = data.RequestStr,
|
||||
ResponseContent = data.ResponseStr,
|
||||
SessionId = gameInfoCache?.SessionId
|
||||
};
|
||||
//T_User_PlayGmaeLog playGameLog = await AddPlayGmaeLog(gameInfoCache, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 用户尝试启动游戏;", data.ResponseStr, data.ResponseContent);
|
||||
|
||||
var response = new BaseResponse<object>();
|
||||
// 用户进入排队
|
||||
|
|
@ -140,14 +134,13 @@ namespace CloudGaming.Code.Game
|
|||
var gameResponse = JsonConvert.DeserializeObject<JYResponseCode<PlayQueueInfoModel>>(data.ResponseContent);
|
||||
|
||||
gameInfoCache?.PlayQueueStart(gameResponse?.Data?.PlayQueueId ?? 0, gameResponse?.Data?.QueuePos ?? 0, playGameSettings.QueueGrade);
|
||||
playGameLog.DescribeContent += "用户进入排队状态;";
|
||||
//playGameLog.DescribeContent += "用户进入排队状态;";
|
||||
response = new BaseResponse<object>(ResponseCode.StartQueue, "开始排队", data.Data);
|
||||
}
|
||||
else if (data.IsSuccess)
|
||||
{
|
||||
// 游戏启动成功
|
||||
var gameResponse = JsonConvert.DeserializeObject<JYResponseCode<JYPlayGameModel>>(data.ResponseContent);
|
||||
|
||||
// 创建游戏记录和日志
|
||||
var userGameList = new T_User_GameList
|
||||
{
|
||||
|
|
@ -158,7 +151,6 @@ namespace CloudGaming.Code.Game
|
|||
UpdateTime = DateTime.Now,
|
||||
UserId = _UserId
|
||||
};
|
||||
|
||||
var userGameLog = new T_User_GameLog
|
||||
{
|
||||
Cpu = playGameRequest.Cpu,
|
||||
|
|
@ -169,7 +161,7 @@ namespace CloudGaming.Code.Game
|
|||
IP = HttpContextAccessor.HttpContext.GetClientIpAddress(),
|
||||
ModelName = playGameRequest.ModelName,
|
||||
QueueGrade = playGameSettings.QueueGrade,
|
||||
ScId = gameResponse?.Data?.ScId ?? "",
|
||||
ScId = (gameResponse?.Data?.ScId ?? 0).ToString(),
|
||||
Status = (int)PlayGameStatus.开始游戏,
|
||||
UpdateTime = DateTime.Now,
|
||||
UserId = _UserId,
|
||||
|
|
@ -177,25 +169,24 @@ namespace CloudGaming.Code.Game
|
|||
SessionId = gameInfoCache?.SessionId
|
||||
};
|
||||
|
||||
playGameLog.DescribeContent += "游戏启动成功;";
|
||||
//playGameLog.DescribeContent += "游戏启动成功;";
|
||||
|
||||
// 保存数据
|
||||
await Dao.DaoPhone.Context.T_User_GameLog.AddAsync(userGameLog);
|
||||
await Dao.DaoPhone.Context.T_User_GameList.AddAsync(userGameList);
|
||||
await Dao.DaoPhone.Context.SaveChangesAsync();
|
||||
|
||||
gameInfoCache?.PlayGameStart(userGameLog.ScId, userGameList.Id, playGameSettings.DisplayGrade);
|
||||
gameInfoCache?.PlayGameStart(gameResponse?.Data?.ScId ?? 0, userGameList.Id, playGameSettings.DisplayGrade);
|
||||
response = new BaseResponse<object>(ResponseCode.Success, "", data.Data);
|
||||
}
|
||||
else // 游戏启动失败
|
||||
{
|
||||
gameInfoCache?.PlayGameError();
|
||||
playGameLog.DescribeContent += "游戏启动失败;";
|
||||
//playGameLog.DescribeContent += "游戏启动失败;";
|
||||
await Dao.DaoPhone.Context.SaveChangesAsync();
|
||||
response = new BaseResponse<object>(ResponseCode.Error, data.Msg, data.Data);
|
||||
}
|
||||
|
||||
// 保存日志
|
||||
await Dao.DaoPhone.Context.T_User_PlayGmaeLog.AddAsync(playGameLog);
|
||||
|
||||
await Dao.DaoPhone.Context.SaveChangesAsync();
|
||||
|
||||
// 保存游戏缓存状态
|
||||
|
|
@ -203,13 +194,150 @@ namespace CloudGaming.Code.Game
|
|||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏心跳
|
||||
/// </summary>
|
||||
/// <param name="gameId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<PlayGameHeartbeatResponse> PlayGameHeartbeat(string gameId)
|
||||
{
|
||||
|
||||
// 检查用户是否已登录
|
||||
if (_UserId == 0)
|
||||
{
|
||||
throw MessageBox.ErrorShow("请先登录");
|
||||
}
|
||||
|
||||
// 检查游戏是否存在
|
||||
var gameCache = Cache.GameEntityCache;
|
||||
var gameInfo = gameCache[gameId];
|
||||
if (gameInfo == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("游戏不存在");
|
||||
}
|
||||
var userInfo = UserInfo;
|
||||
PlayGameUserInfo gameInfoCache = await PlayGameExtend.GetPlayGameUserInfoOrNull(this, userInfo, gameInfo);
|
||||
if (gameInfoCache == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("未找到游戏信息");
|
||||
}
|
||||
// 检查用户钻石是否足够
|
||||
if (userInfo.Diamond == 0 && gameInfo.ConsumeDiamondNumHour > 0)
|
||||
{
|
||||
PlayGameCommonSetting playGameCommonSetting = new PlayGameCommonSetting()
|
||||
{
|
||||
ScId = gameInfoCache.ScId,
|
||||
Sn = gameInfoCache.Sn,
|
||||
};
|
||||
var jyResponse = await JYApi.StopGame(playGameCommonSetting);
|
||||
gameInfoCache.PlayGameClose();
|
||||
throw MessageBox.Show(ResponseCode.NotMoney, "钻石不足");
|
||||
}
|
||||
PlayGameHeartbeatResponse playGameHeartbeatResponse = new PlayGameHeartbeatResponse()
|
||||
{
|
||||
Diamond = userInfo.Diamond,
|
||||
UserPlayGameTime = 999
|
||||
};
|
||||
//LastChargingAt
|
||||
if (gameInfo.ConsumeDiamondNumHour > 0)
|
||||
{
|
||||
//每分钟扣费
|
||||
var diamondNumHour = gameInfo.ConsumeDiamondNumHour / 60.0;
|
||||
playGameHeartbeatResponse.GameConsumeDiamond = (int)diamondNumHour;
|
||||
var chargingAt = DateTime.Now;
|
||||
var minutes = (int)chargingAt.Subtract(gameInfoCache.LastChargingAt).TotalMinutes;
|
||||
//判断扣费时间
|
||||
if (minutes > 1)
|
||||
{
|
||||
var gameDiamondNumHour = (int)diamondNumHour * minutes;
|
||||
//当前游玩的扣费金额
|
||||
if (gameDiamondNumHour > 0)
|
||||
{
|
||||
//如果游玩余额大于用户可扣款的余额
|
||||
if (gameDiamondNumHour > userInfo.Diamond)
|
||||
{
|
||||
//去数据库中在验证一下
|
||||
//(int)userCurrency.GetUserCurrency(UserCurrencyType.钻石);
|
||||
var currency = await Dao.DaoUser.Context.T_User_Currency.Where(it => it.UserId == userInfo.UserId && it.CurrencyType == (int)UserCurrencyType.钻石).FirstOrDefaultAsync();
|
||||
if (currency == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("用户数据错误");
|
||||
}
|
||||
if ((int)(currency.CurrencyMoney) != userInfo.Diamond)
|
||||
{
|
||||
userInfo.Diamond = (int)currency.CurrencyMoney;
|
||||
}
|
||||
if (gameDiamondNumHour > userInfo.Diamond)
|
||||
{
|
||||
//用户余额不足
|
||||
gameDiamondNumHour = userInfo.Diamond;
|
||||
playGameHeartbeatResponse.UserPlayGameTime = 0;
|
||||
}
|
||||
|
||||
}
|
||||
var (issuccess, currlogId, diamId) = await this.UserPlayGameDiamondConsumeMoney(-gameDiamondNumHour, gameInfoCache.GameName, gameInfoCache.CurrencyLogId, gameInfoCache.DiamondListId, $"{diamondNumHour}/分钟");
|
||||
if (!issuccess)
|
||||
{
|
||||
throw MessageBox.ErrorShow("扣款出现错误");
|
||||
}
|
||||
gameInfoCache.CurrencyLogId = currlogId;
|
||||
gameInfoCache.DiamondListId = diamId;
|
||||
//修改最后扣费时间
|
||||
gameInfoCache.LastChargingAt = gameInfoCache.LastChargingAt.AddMinutes(minutes);
|
||||
await gameInfoCache.SaveChangesAsync(this);
|
||||
await Dao.DaoUser.Context.SaveChangesAsync();
|
||||
//用户剩余游玩时间
|
||||
if (userInfo.Diamond > 0)
|
||||
{
|
||||
var userPlayGameTime = (int)(userInfo.Diamond / diamondNumHour);
|
||||
playGameHeartbeatResponse.UserPlayGameTime = userPlayGameTime;
|
||||
}
|
||||
|
||||
//重置一下用户钻石
|
||||
playGameHeartbeatResponse.Diamond = userInfo.Diamond;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return playGameHeartbeatResponse;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加日志
|
||||
/// </summary>
|
||||
/// <param name="gameInfoCache"></param>
|
||||
/// <param name="describeContent"></param>
|
||||
/// <param name="requestContent"></param>
|
||||
/// <param name="responseContent"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<T_User_PlayGmaeLog> AddPlayGmaeLog(PlayGameUserInfo gameInfoCache, string describeContent, string requestContent, string responseContent)
|
||||
{
|
||||
var playGameLog = new T_User_PlayGmaeLog
|
||||
{
|
||||
CreateTime = DateTime.Now,
|
||||
DescribeContent = describeContent,
|
||||
GameId = gameInfoCache.GameId,
|
||||
UserId = gameInfoCache.UserId,
|
||||
RequestContent = requestContent,
|
||||
ResponseContent = responseContent,
|
||||
SessionId = gameInfoCache?.SessionId
|
||||
};
|
||||
// 创建游戏启动日志对象
|
||||
|
||||
// 保存日志
|
||||
await Dao.DaoPhone.Context.T_User_PlayGmaeLog.AddAsync(playGameLog);
|
||||
return playGameLog;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 游戏排队
|
||||
/// </summary>
|
||||
/// <param name="gameRequest"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<BaseResponse<PlayQueueInfoModel>> PlayGameQueue(GameRequest gameRequest)
|
||||
public async Task<BaseResponse<PlayQueueInfoModel>> PlayGameQueueAsync(GameRequest gameRequest)
|
||||
{
|
||||
if (_UserId == 0)
|
||||
{
|
||||
|
|
@ -229,7 +357,7 @@ namespace CloudGaming.Code.Game
|
|||
PlayGameUserInfo gameInfoCache = await PlayGameExtend.GetPlayGameUserInfoOrNull(this, userInfo, gameInfo);
|
||||
if (gameInfoCache == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("未找到排队信息");
|
||||
throw MessageBox.ErrorShow("未找到游戏信息");
|
||||
}
|
||||
if (gameInfoCache.PlayQueueId == 0)
|
||||
{
|
||||
|
|
@ -247,6 +375,7 @@ namespace CloudGaming.Code.Game
|
|||
if (paidui.IsSuccess)
|
||||
{
|
||||
gameInfoCache.PlayQueueSuccess();
|
||||
|
||||
back = new BaseResponse<PlayQueueInfoModel>(0, "排队成功");
|
||||
}
|
||||
else if (paidui.IsLineUp)
|
||||
|
|
@ -269,7 +398,7 @@ namespace CloudGaming.Code.Game
|
|||
/// </summary>
|
||||
/// <param name="gameRequest"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<BaseResponse<bool>> CancelQueue(GameRequest gameRequest)
|
||||
public async Task<BaseResponse<bool>> CancelQueueAsync(GameRequest gameRequest)
|
||||
{
|
||||
if (_UserId == 0)
|
||||
{
|
||||
|
|
@ -289,13 +418,9 @@ namespace CloudGaming.Code.Game
|
|||
PlayGameUserInfo gameInfoCache = await PlayGameExtend.GetPlayGameUserInfoOrNull(this, userInfo, gameInfo);
|
||||
if (gameInfoCache == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("未找到排队信息");
|
||||
throw MessageBox.ErrorShow("未找到游戏信息");
|
||||
}
|
||||
if (gameInfoCache.PlayQueueId == 0)
|
||||
{
|
||||
throw MessageBox.ErrorShow("排队出错");
|
||||
}
|
||||
//CancelQueue
|
||||
//CancelQueueAsync
|
||||
//PlayGameCancelQueue playGameQueue
|
||||
PlayGameQueue playGameQueue = new PlayGameQueue()
|
||||
{
|
||||
|
|
@ -311,9 +436,105 @@ namespace CloudGaming.Code.Game
|
|||
return new BaseResponse<bool>(ResponseCode.Success, "取消排队成功");
|
||||
}
|
||||
|
||||
public async Task<BaseResponse<object> DisplayGrade(int queue_grade)
|
||||
/// <summary>
|
||||
/// 切换视频等级
|
||||
/// </summary>
|
||||
/// <param name="displayGrade"></param>
|
||||
/// <param name="gameId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<BaseResponse<object>> DisplayGrade(int displayGrade, string gameId)
|
||||
{
|
||||
return new BaseResponse<object>();
|
||||
|
||||
if (_UserId == 0)
|
||||
{
|
||||
throw MessageBox.ErrorShow("请先登录");
|
||||
}
|
||||
|
||||
// 检查游戏是否存在
|
||||
var gameCache = Cache.GameEntityCache;
|
||||
var gameInfo = gameCache[gameId];
|
||||
if (gameInfo == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("游戏不存在");
|
||||
}
|
||||
|
||||
// 获取用户和游戏信息
|
||||
var userInfo = UserInfo;
|
||||
PlayGameUserInfo gameInfoCache = await PlayGameExtend.GetPlayGameUserInfoOrNull(this, userInfo, gameInfo);
|
||||
if (gameInfoCache == null)
|
||||
{
|
||||
throw MessageBox.ErrorShow("未找到游戏信息");
|
||||
}
|
||||
if (gameInfoCache.GameStatus != PlayGameStatus.开始游戏 && gameInfoCache.GameStatus != PlayGameStatus.游戏中)
|
||||
{
|
||||
throw MessageBox.ErrorShow("不在游戏中");
|
||||
}
|
||||
DisplayGradeSettings displayGradeSettings = new DisplayGradeSettings()
|
||||
{
|
||||
DisplayGrade = displayGrade,
|
||||
ScId = gameInfoCache.ScId,
|
||||
Sn = gameInfoCache.Sn,
|
||||
};
|
||||
//切换视频等级
|
||||
var response = await JYApi.DisplayGrade(displayGradeSettings);
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
gameInfoCache.DisplayGrade = displayGrade;
|
||||
await gameInfoCache.SaveChangesAsync(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw MessageBox.ErrorShow("切换失败");
|
||||
}
|
||||
return new BaseResponse<object>(ResponseCode.Success, "", response.Data);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取可重连的会话列表
|
||||
/// </summary>
|
||||
/// <param name="sn"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<BaseResponse<object>> GetMyScList(string sn)
|
||||
{
|
||||
var requestParmat = new RequestBaseModel()
|
||||
{
|
||||
Sn = sn,
|
||||
};
|
||||
var response = await JYApi.MyScList(requestParmat);
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
return new BaseResponse<object>(ResponseCode.Success, "", response.Data) { };
|
||||
}
|
||||
throw response.ToMessageBox();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重连会话
|
||||
/// </summary>
|
||||
/// <param name="sc_id"></param>
|
||||
/// <param name="display_grade"></param>
|
||||
/// <param name="model_name"></param>
|
||||
/// <param name="sn"></param>
|
||||
/// <param name="cpu"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<BaseResponse<object>> ReconPlayGame(int sc_id, int display_grade, string model_name, string sn, string cpu)
|
||||
{
|
||||
var setting = new ReconPlayGameSettings()
|
||||
{
|
||||
ScId = sc_id,
|
||||
DisplayGrade = display_grade,
|
||||
Cpu = cpu,
|
||||
ModelName = model_name,
|
||||
Sn = sn,
|
||||
};
|
||||
var response = await JYApi.ReconPlayGame(setting);
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
return new BaseResponse<object>(ResponseCode.Success, "", response.Data) { };
|
||||
}
|
||||
throw response.ToMessageBox();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ namespace CloudGaming.Code.Game
|
|||
/// <param name="gameListId">游戏记录id</param>
|
||||
/// <param name="display_grade">游戏显示等级</param>
|
||||
/// <returns></returns>
|
||||
public static bool PlayGameStart(this PlayGameUserInfo playGameUserInfo, string scId, int gameListId, int display_grade)
|
||||
public static bool PlayGameStart(this PlayGameUserInfo playGameUserInfo, int scId, int gameListId, int display_grade)
|
||||
{
|
||||
playGameUserInfo.GameStatus = PlayGameStatus.开始游戏;
|
||||
playGameUserInfo.GameListId = gameListId;
|
||||
|
|
@ -95,6 +95,7 @@ namespace CloudGaming.Code.Game
|
|||
});
|
||||
playGameUserInfo.PlayGameStartAt = DateTime.Now;
|
||||
playGameUserInfo.PlayGameHeartbeatAt = DateTime.Now;
|
||||
playGameUserInfo.LastChargingAt = DateTime.Now;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -108,6 +109,18 @@ namespace CloudGaming.Code.Game
|
|||
playGameUserInfo.GameStatus = PlayGameStatus.游戏启动失败;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏启动失败
|
||||
/// </summary>
|
||||
/// <param name="playGameUserInfo"></param>
|
||||
/// <returns></returns>
|
||||
public static bool PlayGameClose(this PlayGameUserInfo playGameUserInfo)
|
||||
{
|
||||
playGameUserInfo.GameStatus = PlayGameStatus.游戏结束;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏心跳
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace CloudGaming.DtoModel.JY
|
|||
/// 本次游戏连接会话 id
|
||||
/// </summary>
|
||||
[JsonProperty("sc_id")]
|
||||
public string ScId { get; set; }
|
||||
public int ScId { get; set; }
|
||||
/// <summary>
|
||||
/// 链路唯一标识,
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -16,6 +16,6 @@ namespace CloudGaming.DtoModel.PlayGame
|
|||
/// 本次游戏连接会话 id
|
||||
/// </summary>
|
||||
[JsonPropertyName("sc_id")]
|
||||
public string ScId { get; set; }
|
||||
public int ScId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudGaming.DtoModel.PlayGame
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class PlayGameHeartbeatResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前游戏消耗钻石数/每分钟
|
||||
/// </summary>
|
||||
public int GameConsumeDiamond { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户剩余游玩时间(分钟)
|
||||
/// </summary>
|
||||
public int UserPlayGameTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户剩余钻石
|
||||
/// </summary>
|
||||
public int Diamond { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -53,6 +53,11 @@ namespace CloudGaming.DtoModel.PlayGame
|
|||
/// </summary>
|
||||
public DateTime PlayGameHeartbeatAt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最后一次扣费时间
|
||||
/// </summary>
|
||||
public DateTime LastChargingAt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 游戏记录id
|
||||
/// </summary>
|
||||
|
|
@ -78,7 +83,7 @@ namespace CloudGaming.DtoModel.PlayGame
|
|||
/// <summary>
|
||||
/// 鲸云scid
|
||||
/// </summary>
|
||||
public string ScId { get; set; }
|
||||
public int ScId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 第三方游戏id
|
||||
|
|
@ -118,5 +123,16 @@ namespace CloudGaming.DtoModel.PlayGame
|
|||
/// 用户操作内容
|
||||
/// </summary>
|
||||
public List<PlayGameUserOperation> GameUserOperation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 金额日志表id
|
||||
/// </summary>
|
||||
public int CurrencyLogId { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 资产记录表id
|
||||
/// </summary>
|
||||
public int DiamondListId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ namespace CloudGaming.DtoModel.PlayGame
|
|||
public ReconPlayGameSettings() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 会话 id
|
||||
/// </summary>
|
||||
[JsonPropertyName("sc_id")]
|
||||
public int ScId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示等级,可在后台配置,默认为 1
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user