This commit is contained in:
Putoo
2026-07-05 18:51:01 +08:00
parent fdb16f5d91
commit a1dbce8a96
15 changed files with 468 additions and 47 deletions

View File

@@ -60,6 +60,8 @@ namespace Application.Domain.Entity
[SugarColumn(IsNullable = true)]
public int? state { get; set; }
[SugarColumn(Length = 50, IsNullable = true)]
public string? winCode { get; set; }
/// <summary>
/// 胜利方
/// </summary>

View File

@@ -10,4 +10,12 @@
<ProjectReference Include="..\Application.Domain.Entity\Application.Domain.Entity.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="BusEvents\EventModel\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="BusEvents\EventModel\FightAwardEventModel.cs" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application.Domain
{
public class BusEventsEnum
{
public enum BusEventsName
{
PutFightAward,//战斗奖励事件
}
}
}

View File

@@ -0,0 +1,17 @@
namespace Application.Domain
{
public class BusEventsSubscriber : IEventSubscriber
{
/// <summary>
/// 处理贡献提成
/// </summary>
/// <param name="context"></param>
[EventSubscribe(BusEventsEnum.BusEventsName.PutFightAward, NumRetries = 0)]
public async Task HandleQueueBonus(EventHandlerExecutingContext context)
{
var data = context.GetPayload<game_fight_data>();
var fightService = App.GetService<IGameFightService>();
await fightService.HandleFight(data);
}
}
}

View File

@@ -4,3 +4,4 @@ global using Application.Domain.Entity;
global using Photon.Core.Redis;
global using Photon.Core.Assist;
global using Application.Service.Pub;
global using Jaina;

View File

@@ -15,11 +15,15 @@ public interface IGameFightService
string scene, string mapId,
string userId, long exp = 0, long copper = 0, long petExp = 0, string award = "", string pars = "");
Task<bool> SetFightWin(string fightId, string winUser);
Task<bool> SetFightWin(game_fight_data fightData, string winUser);
#endregion
#region
Task<int> GetUserFightHarm(string userId);
Task SetUserFightHarm(string userId, int harm);
Task HandleFight(game_fight_data eventData);
#endregion
}

View File

@@ -36,6 +36,12 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
return result;
}
private async Task RemoveUserFight(string userId)
{
string key = string.Format(FightCache.FightCacheKey, "UserFight");
bool result = await redis.AddHashAsync(key, userId, new List<string>());
}
#endregion
#region
@@ -55,7 +61,7 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
}
public async Task AddFightHarm(string fightId,string userId, long harm)
public async Task AddFightHarm(string fightId, string userId, long harm)
{
string key = string.Format(FightCache.FightCacheKeys, "FightData:Harm", fightId);
if (await redis.HExistsHashAsync(key, userId))
@@ -69,19 +75,24 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
await redis.AddHashAsync(key, userId, harm);
}
}
public async Task RemoveFightHarm(string fightId,string userId)
public async Task RemoveFightHarm(string fightId, string userId)
{
string key = string.Format(FightCache.FightCacheKeys, "FightData:Harm", fightId);
await redis.DelHashAsync(key, userId);
}
private async Task ClearFightInfoCache(string fightId, string keyId)
private async Task ClearFightInfoCache(string fightId, string keyId, bool remMonsterBool = false)
{
string key1 = string.Format(FightCache.FightCacheKeys, "FightData:Info", fightId);
string key2 = string.Format(FightCache.FightCacheKeys, "FightData:Blood", keyId);
await redis.DelAsync(key1, key2);
List<string> keys = new List<string>();
keys.Add(string.Format(FightCache.FightCacheKeys, "FightData:Info", fightId));
if (remMonsterBool)
{
keys.Add(string.Format(FightCache.FightCacheKeys, "FightData:Blood", keyId));
}
await redis.DelAsync(keys.ToArray());
}
public async Task<bool> AddFight(string fightId, string areaCode, string keyId, string mainId, string code,
@@ -153,28 +164,116 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
return result;
}
public async Task<bool> SetFightWin(string fightId, string winUser)
public async Task<bool> SetFightWin(game_fight_data fightData, string winUser)
{
bool result = false;
var data = await GetFightInfo(fightId);
if (data != null)
long okTime = TimeExtend.GetTimeStampSeconds;
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
if (fightData.code == nameof(GameEnum.FightCode.PVE))
{
long okTime = TimeExtend.GetTimeStampSeconds;
data.state = 1;
data.winUser = winUser;
data.okTime = okTime;
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
result = await db.Updateable<game_fight_data>().SetColumns(it => it.state == 1)
.SetColumns(it => it.winUser == winUser)
.SetColumns(it => it.okTime == okTime)
.Where(it => it.fightId == fightId).ExecuteCommandAsync() > 0;
if (fightData.userId == winUser)
{
var data = await db.Queryable<game_fight_data>()
.Where(it =>
it.keyId == fightData.keyId && it.state == 0 && it.code == nameof(GameEnum.FightCode.PVE))
.ToListAsync();
foreach (var item in data)
{
db.Updateable<game_fight_data>().SetColumns(it => it.state == 1)
.SetColumns(it => it.winUser == winUser)
.SetColumns(it => it.winCode == nameof(UserEnum.AttrCode.Person))
.SetColumnsIF(item.userId == winUser, it => it.okTime == okTime)
.Where(it => it.fightId == item.fightId).AddQueue();
}
result = await db.SaveQueuesAsync() > 0;
if (result)
{
foreach (var item in data)
{
await ClearFightInfoCache(item.fightId, item.keyId, true);
}
fightData.state = 1;
fightData.winCode = nameof(UserEnum.AttrCode.Person);
fightData.winUser = winUser;
fightData.okTime = okTime;
//此处调用战斗结束相关
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.PutFightAward,
fightData));
}
}
else
{
//清理相关pvp数据
result = await HandleFightUserDataByPvp(fightData.fightId, fightData.keyId,
nameof(UserEnum.AttrCode.Monster), winUser, fightData.userId, okTime);
if (result)
{
await RemoveUserFight(fightData.userId);
}
}
}
else if (fightData.code == nameof(GameEnum.FightCode.PVP))
{
string lowUser = fightData.userId == winUser ? fightData.mainId : fightData.userId;
result = await HandleFightUserDataByPvp(fightData.fightId, fightData.keyId,
nameof(UserEnum.AttrCode.Person), winUser, lowUser, okTime);
if (result)
{
await RemoveUserFight(lowUser);
}
}
return result;
}
private async Task<bool> HandleFightUserDataByPvp(string fightId, string keyId, string winCode, string winUser,
string lowUser, long okTime)
{
bool result = true;
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
var data = await db.Queryable<game_fight_data>()
.Where(it => it.state == 0 &&
((it.keyId.Contains(lowUser) && it.code == nameof(GameEnum.FightCode.PVP)) ||
(it.userId == lowUser && it.code == nameof(GameEnum.FightCode.PVE))))
.ToListAsync();
foreach (var item in data)
{
db.Updateable<game_fight_data>().SetColumns(it => it.state == 1)
.SetColumns(it => it.winCode == winCode)
.SetColumns(it => it.winUser == winUser)
.SetColumnsIF(item.userId == winUser && winCode == nameof(UserEnum.AttrCode.Person),
it => it.okTime == okTime)
.Where(it => it.fightId == item.fightId).AddQueue();
}
result = await db.SaveQueuesAsync() > 0;
if (result)
{
await ClearFightInfoCache(fightId, data.keyId);
//此处调用战斗结束相关
await HandleFight(data);
foreach (var item in data)
{
await ClearFightInfoCache(item.fightId, item.keyId, false);
}
if (winCode == nameof(UserEnum.AttrCode.Person))
{
var winFight = data.Find(it => it.userId == winUser);
if (winFight != null)
{
winFight.state = 1;
winFight.code = winCode;
winFight.winUser = winUser;
winFight.okTime = okTime;
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.PutFightAward,
winFight));
}
}
}
return result;
@@ -184,8 +283,52 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
#region
private async Task HandleFight(game_fight_data data)
public async Task<int> GetUserFightHarm(string userId)
{
int result = 0;
string key = string.Format(FightCache.FightCacheKey, "UserHarm");
if (await redis.HExistsHashAsync(key, userId))
{
result = await redis.GetHashAsync<int>(key, userId);
}
if (result > 0)
{
await redis.AddHashAsync(key, userId, 0);
}
return result;
}
public async Task SetUserFightHarm(string userId, int harm)
{
string key = string.Format(FightCache.FightCacheKey, "UserHarm");
await redis.AddHashAsync(key, userId, harm);
}
public async Task HandleFight(game_fight_data fightData)
{
string winUser = fightData.winUser;
#region
if (fightData.exp > 0)
{
var attrService = App.GetService<IUnitUserAttrService>();
await attrService.UpdateUserExp(winUser, (long)fightData.exp, 1);
}
if (fightData.copper > 0)
{
var accService = App.GetService<IUnitUserAccService>();
await accService.UpdateUserCopper(winUser, 1, (long)fightData.copper, "击杀怪物");
}
if (fightData.petExp > 0) //宠物经验
{
}
#endregion
}
#endregion

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Jaina" Version="4.6.7" />
<PackageReference Include="Photon.Core" Version="1.0.0.1" />
<PackageReference Include="Photon.Core.Assist" Version="1.0.0.1" />
<PackageReference Include="Photon.Core.Jwt" Version="1.0.0.2" />

View File

@@ -17,10 +17,11 @@ public class FightController : ControllerBase
private readonly IGameMapService _mapService;
private readonly IUnitUserAccService _accService;
private readonly IGameGoodsService _goodsService;
private readonly IUnitUserService _userService;
public FightController(IGameFightService fightService, IGameMonsterService monsterService,
IUnitUserAttrService attrService, IGameMapService mapService, IUnitUserAccService accService,
IGameGoodsService goodsService)
IGameGoodsService goodsService, IUnitUserService userService)
{
_fightService = fightService;
_monsterService = monsterService;
@@ -28,6 +29,7 @@ public class FightController : ControllerBase
_mapService = mapService;
_accService = accService;
_goodsService = goodsService;
_userService = userService;
}
/// <summary>
@@ -136,7 +138,7 @@ public class FightController : ControllerBase
return PoAction.Message("战斗结束", 100);
}
#region
#region
FightResultModel fightResult = new FightResultModel()
{
@@ -144,19 +146,20 @@ public class FightController : ControllerBase
};
if (!string.IsNullOrEmpty(parms.data))
{
fightResult = JsonConvert.DeserializeObject<FightResultModel>(parms.data);
fightResult = JsonConvert.DeserializeObject<FightResultModel>(parms.data);
}
#endregion
//更新攻击
if (fightResult.result == 0)
{
myHarm = fightResult.myHarm;
myHarm += await _fightService.GetUserFightHarm(userId);
otHarm = fightResult.otHarm;
await _attrService.UpdateUserBlood(userId, 1, fightResult.myHarm);
}
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
UserAttrModel otAttr = new UserAttrModel();
if (fight.code == nameof(GameEnum.FightCode.PVE))
@@ -172,19 +175,36 @@ public class FightController : ControllerBase
}
else
{
otAttr = await _attrService.GetUserAttrModel(fight.mainId, fight.scene);
if (fightResult.result == 0)
{
await _fightService.SetUserFightHarm(fight.mainId, fightResult.otHarm);
await _attrService.UpdateUserBlood(fight.mainId, 1, fightResult.otHarm);
}
otAttr = await _attrService.GetUserAttrModel(fight.mainId, fight.scene);
}
//结束战斗都此处直接返回
if (otAttr.blood < 1||myAttr.blood<1)
if (otAttr.blood < 1 || myAttr.blood < 1)
{
await _fightService.RemoveUserFight(userId, fightId);
if (otAttr.blood < 1)
{
await _fightService.SetFightWin(fight, userId);
}
else
{
await _fightService.SetFightWin(fight, fight.mainId);
}
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束", 100);
}
else
{
//自动使用药品
}
//获取个人药品栏
var myDrug = await _attrService.GetUserDrug(userId);
//获取个人负面
@@ -192,7 +212,7 @@ public class FightController : ControllerBase
//冷却时间
int cool = 1000;
if (myAttr.agility > otAttr.agility&&otAttr.agility>0)
if (myAttr.agility > otAttr.agility && otAttr.agility > 0)
{
decimal diff = myAttr.agility - otAttr.agility;
decimal bl = diff / otAttr.agility;
@@ -206,10 +226,10 @@ public class FightController : ControllerBase
{
cool = Convert.ToInt32(cool * (1 + atkCoolData.count * 0.1M));
}
//更新伤害提示
int exitCopper = otAttr.lev * 5;
var result = new
{
@@ -274,6 +294,7 @@ public class FightController : ControllerBase
{
if (await _fightService.RemoveUserFight(userId, fightId))
{
await _fightService.SetFightWin(fight, fight.mainId);
await _fightService.RemoveFightHarm(fightId, userId); //移除伤害
var nextFight = myFight.FindAll(it => it != fightId);
string nextId = nextFight.Count > 0 ? nextFight[0] : "";
@@ -305,6 +326,7 @@ public class FightController : ControllerBase
{
return PoAction.Message("战斗不存在!");
}
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
@@ -317,7 +339,7 @@ public class FightController : ControllerBase
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束!");
}
var myDrug = await _attrService.GetUserDrug(userId);
var onDrug = myDrug.drug.Find(it => it.id == drugId);
if (onDrug == null)
@@ -355,7 +377,93 @@ public class FightController : ControllerBase
{
return PoAction.Message("药品使用失败!");
}
}
/// <summary>
/// 战斗信息
/// </summary>
/// <param name="fightId"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetFightMsg(string fightId)
{
string userId = StateHelper.userId;
var myFight = await _fightService.GetUserFight(userId);
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
return PoAction.Message("战斗不存在!");
}
if (fight.userId != userId)
{
return PoAction.Message("战斗不存在!");
}
if (fight.state == 0)
{
return PoAction.Message("战斗未完成", 100);
}
int isWin = 0;
if (fight.winUser == fight.mainId)
{
isWin = 0;
}
else if(fight.winUser==userId)
{
isWin = 1;
}
else
{
isWin = 2;
}
string otName = string.Empty;
if (isWin == 0)
{
if (fight.code == nameof(GameEnum.FightCode.PVP))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.winUser);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.winUser);
otName = monsterInfo.name;
}
}
else if (isWin == 1)
{
if (fight.code == nameof(GameEnum.FightCode.PVP))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.mainId);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.mainId);
otName = monsterInfo.name;
}
}
else
{
if (fight.winCode == nameof(UserEnum.AttrCode.Person))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.winUser);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.winUser);
otName = monsterInfo.name;
}
}
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
return PoAction.Ok(new { isWin, otName, blood = myAttr.blood, upBlood = myAttr.upBlood, fight });
}
}

View File

@@ -1,4 +1,3 @@
using Microsoft.OpenApi.Models;
using Photon.Core.Redis;
@@ -82,6 +81,17 @@ builder.Services.AddCors(options =>
#endregion
#region 线
// 注册 EventBus 服务
builder.Services.AddEventBus(builder =>
{
// 注册 ToDo 事件订阅者
builder.AddSubscriber<BusEventsSubscriber>();
});
#endregion
builder.Services.AddSignalR();
var app = builder.Build();

View File

@@ -1,7 +1,7 @@
<template>
<div class="content">
<div class="common">
<Abutton>{{ autoFight == 0 ? "开启自动攻击" : "关闭自动攻击" }}</Abutton>
<Abutton @click="AutoAtk">{{ autoFight == 0 ? "开启自动攻击" : "关闭自动攻击" }}</Abutton>
</div>
<div class="common">
你向{{ otAttr.name }}发起了攻击!
@@ -72,6 +72,7 @@ const coolTime = ref(0);
let coolLock = false;//冷却锁
const coolTimer = ref(0);//冷却定时器
let dataLock = false;//数据锁
const autoAtkTimer = ref(0);//冷却定时器
onMounted(async () => {
try {
@@ -83,7 +84,8 @@ onMounted(async () => {
})
onUnmounted(() => {
clearTimeout(coolTimer.value)
clearTimeout(coolTimer.value);
clearTimeout(autoAtkTimer.value);
})
/**加载方法 */
@@ -98,7 +100,7 @@ const BindData = async (data: string): Promise<void> => {
coolTime.value = result.data.cool;
dataLock = false;
myHarm.value = result.data.myHarm;
otHarm.value = result.data.otHarm;
otHarm.value = result.data.otHarm;
}
else if (result.code == 100) {
PageExtend.RedirectTo("/fight/result?f=" + fightId);
@@ -149,8 +151,8 @@ const UseFightDrug = async (drugId: string) => {
const FightAtk = async () => {
if (coolLock == false && dataLock == false) {
StartCool();
let result = FightTool.FightHandle(myAttr.value, otAttr.value);
let data = JSON.stringify(result);
let result = FightTool.FightHandle(myAttr.value, otAttr.value);
let data = JSON.stringify(result);
await BindData(data);
}
}
@@ -162,4 +164,19 @@ const StartCool = () => {
coolLock = false;
}, coolTime.value)
}
/**自动攻击 */
const AutoAtk = () => {
if (autoFight.value == 0) {
autoFight.value = 1;
autoAtkTimer.value = setInterval(async () => {
await FightAtk()
}, coolTime.value)
}
else {
autoFight.value = 0;
clearTimeout(autoAtkTimer.value)
}
}
</script>

View File

@@ -1 +1,89 @@
<template></template>
<template>
<div class="content" v-if="isWin != -1">
<div class="common">
<span v-if="isWin == 1">战胜了{{ otName }}!</span>
<span v-if="isWin == 0">你被{{ otName }}击杀!</span>
<span v-if="isWin == 2">{{ otName }}击杀!</span>
</div>
<div class="common">
<Abar href="/map">继续</Abar>
</div>
<div class="common">
你体力:{{ blood }}/{{ upBlood }}
</div>
<div class="common" v-if="isWin == 1">
<div class="item" v-if="fight.exp > 0">
经验:+{{ fight.exp }}
</div>
<div class="item" v-if="fight.copper > 0">
铜贝:+{{ fight.copper }}
</div>
<div class="item" v-if="fight.petExp > 0">
宠物经验:+{{ fight.petExp }}
</div>
<div class="item" v-if="fight.award != ''" v-html="AwardTips">
</div>
</div>
</div>
</template>
<script setup lang="ts">
definePageMeta({
layout: layout.default,
middleware: 'page-loading'
})
const isWin = ref(-1);
const otName = ref('');
const blood = ref(0);
const upBlood = ref(0);
const fight = ref<any>({});
const AwardTips = ref('');
let fightId = PageExtend.QueryString("f");
onMounted(async () => {
try {
await BindData();
}
finally {
PageLoading.Close();
}
})
const BindData = async (): Promise<void> => {
let result = await FightService.GetFightMsg(fightId);
if (result.code == 0) {
isWin.value = result.data.isWin;
otName.value = result.data.otName;
blood.value = result.data.blood;
upBlood.value = result.data.upBlood;
fight.value = result.data.fight;
if (fight.value.award != '') {
AwardTips.value = awardTipsStr(JSON.parse(fight.value.award))
}
}
else if (result.code == 100) {
PageExtend.RedirectTo("/fight?f=" + fightId);
}
else {
MessageExtend.ShowDialogEvent("提示", result.msg, () => {
PageExtend.RedirectTo("/map");
})
}
};
const awardTipsStr = (data: any) => {
let result = '';
if (data.code == 'Default') {
result += "<span>掉落物品:</span><br>"
let awData: Array<any> = data.award;
awData.forEach(it => {
result += `${it.name}×${it.count}`;
})
return StringExtend.trimEnd(result, '、');
}
return result;
}
</script>

View File

@@ -7,7 +7,7 @@
等级:{{ monster.lev }}<br>
说明:{{ monster.remark }}<br>
体力:{{ monster.blood }}<br>
攻击:{{ monster.minAtk }}/{{ monster.maxAtk }}<br>
攻击:{{ monster.minAtk }}-{{ monster.maxAtk }}<br>
防御:{{ monster.defense }}<br>
敏捷:{{ monster.agility }}<br>
</div>

View File

@@ -35,4 +35,12 @@ export class FightService {
static async UseFightDrug(fightId: string, drugId: string) {
return await ApiService.Request("get", "/Fight/UseFightDrug", { fightId, drugId });
}
/**
* 战斗信息
* GET /Fight/GetFightMsg
*/
static async GetFightMsg(fightId: string) {
return await ApiService.Request("get", "/Fight/GetFightMsg", { fightId });
}
}

View File

@@ -57,7 +57,6 @@ export class FightTool {
}
}
}
//士气处理
if (atkUser.upMorale > 0) {
atkUser.minAtk = atkUser.minAtk * (1.0 + (atkUser.morale / atkUser.upMorale) * 0.3);
@@ -75,7 +74,6 @@ export class FightTool {
else {
defHarm = -1;
}
//暴击处理
if (atkUser.crit > 0) {
if (this.randomTrigger(atkUser.crit)) {