This commit is contained in:
Putoo
2026-06-03 18:21:22 +08:00
parent 8550d85659
commit 8b8eb732ae
11 changed files with 653 additions and 4 deletions

View File

@@ -0,0 +1,69 @@
using SqlSugar;
using System;
namespace Application.Domain.Entity
{
[Tenant("Kg.SeaTime.Game")]
public class unit_user_run
{
/// <summary>
/// userId
/// </summary>
[SugarColumn(IsPrimaryKey = true, Length = 50)]
public string userId { get; set; }
/// <summary>
/// 区服
/// </summary>
[SugarColumn(IsNullable = true)]
public int? areaId { get; set; }
/// <summary>
/// 路线
/// </summary>
[SugarColumn(Length = 255, IsNullable = true)]
public string? lineCode { get; set; }
/// <summary>
/// onMap
/// </summary>
[SugarColumn(Length = 10, IsNullable = true)]
public string? onMap { get; set; }
/// <summary>
/// toMap
/// </summary>
[SugarColumn(Length = 10, IsNullable = true)]
public string? toMap { get; set; }
/// <summary>
/// 距离
/// </summary>
[SugarColumn(IsNullable = true)]
public int? distance { get; set; }
/// <summary>
/// 位置
/// </summary>
[SugarColumn(IsNullable = true)]
public int? position { get; set; }
/// <summary>
/// 深度
/// </summary>
[SugarColumn(IsNullable = true)]
public int? depth { get; set; }
/// <summary>
/// 状态
/// </summary>
[SugarColumn(IsNullable = true)]
public int? status { get; set; }
/// <summary>
/// 更新时间
/// </summary>
[SugarColumn(IsNullable = true)]
public long? upTime { get; set; }
}
}

View File

@@ -41,5 +41,11 @@ namespace Application.Domain.Entity
/// </summary>
[SugarColumn(IsNullable = true)]
public int? weight { get; set; }
/// <summary>
/// copper
/// </summary>
[SugarColumn(IsNullable = true)]
public int? copper { get; set; }
}
}

View File

@@ -5,6 +5,7 @@ public interface IGameMapService
#region
Task<game_city> GetCityInfo(int cityId);
Task<game_city> GetCityInfoByMapId(string mapId);
Task<List<game_city>> GetCityData();
Task<List<game_city_map>> GetCityMap(int cityId);
Task<List<game_city_map>> GetCityShowMap(int cityId);
@@ -41,5 +42,15 @@ public interface IGameMapService
Task<List<MapLine>> CreateAutoMap(string start, string end);
#endregion
#region
Task<unit_user_run> GetUserRun(string userId);
Task<bool> StopUserRun(string userId);
Task<bool> SaveUserRunInfo(string userId, int areaId, string onMap, string toMap);
Task<bool> UpdateUserRunInfo(unit_user_run data);
#endregion
}

View File

@@ -17,6 +17,18 @@ public class GameMapService(ISqlSugarClient DbClient, IRedisCache redis) : IGame
return data;
}
public async Task<game_city> GetCityInfoByMapId(string mapId)
{
var data = await GetMapInfo(mapId);
if (data != null)
{
return await GetCityInfo((int)data.cityId);
}
else
{
return new game_city();
}
}
public async Task<List<game_city>> GetCityData()
{
@@ -89,6 +101,7 @@ public class GameMapService(ISqlSugarClient DbClient, IRedisCache redis) : IGame
}
}
public async Task<List<game_city_npc>> GetMapNpc(string mapId)
{
string key = string.Format(BaseCache.BaseCacheKeys, "CityData", "NpcData");
@@ -566,4 +579,102 @@ public class GameMapService(ISqlSugarClient DbClient, IRedisCache redis) : IGame
}
#endregion
#region
public async Task<unit_user_run> GetUserRun(string userId)
{
string key = string.Format(UserCache.BaseCacheKey, "UserMapRun");
if (await redis.HExistsHashAsync(key, userId))
{
return await redis.GetHashAsync<unit_user_run>(key, userId);
}
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_run>();
var data = await db.Queryable<unit_user_run>().Where(it => it.userId == userId).SingleAsync();
if (data == null)
{
data = await ResetUserRun(userId,false);
}
await redis.AddHashAsync(key, userId, data);
return data;
}
private async Task ClearUserRunCache(string userId)
{
string key = string.Format(UserCache.BaseCacheKey, "UserMapRun");
await redis.DelHashAsync(key, userId);
}
private async Task<unit_user_run> ResetUserRun(string userId, bool ClearCache = true)
{
unit_user_run runInfo = new unit_user_run();
runInfo.userId = userId;
runInfo.areaId = 0;
runInfo.lineCode = "";
runInfo.onMap = "";
runInfo.toMap = "";
runInfo.distance = 0;
runInfo.position = 0;
runInfo.depth = 0;
runInfo.status = 0;
runInfo.upTime = TimeExtend.GetTimeStampSeconds;
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_run>();
bool result = await db.Saveable(runInfo).ExecuteCommandAsync() > 0;
if (result)
{
if (ClearCache)
{
await ClearUserRunCache(userId);
}
return runInfo;
}
return null;
}
public async Task<bool> StopUserRun(string userId)
{
var data = await ResetUserRun(userId, true);
return data == null ? false : true;
}
public async Task<bool> SaveUserRunInfo(string userId, int areaId, string onMap, string toMap)
{
unit_user_run runInfo = new unit_user_run();
runInfo.userId = userId;
runInfo.areaId = areaId;
runInfo.lineCode = StringAssist.GetSortKey(onMap, toMap);
runInfo.onMap = onMap;
runInfo.toMap = toMap;
int mile = GameTool.ComputeMile(onMap, toMap);
runInfo.distance = mile;
runInfo.position = 0;
runInfo.depth = 0;
runInfo.status = 1;
runInfo.upTime = TimeExtend.GetTimeStampSeconds;
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_run>();
bool result = await db.Saveable(runInfo).ExecuteCommandAsync() > 0;
if (result)
{
await ClearUserRunCache(userId);
}
return result;
}
public async Task<bool> UpdateUserRunInfo(unit_user_run data)
{
string key = string.Format(UserCache.BaseCacheKey, "UserMapRun");
bool result = await redis.AddHashAsync(key, data.userId, data);
if (result)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_run>();
await db.Updateable(data).ExecuteCommandAsync();
}
return result;
}
#endregion
}

View File

@@ -46,6 +46,15 @@ public class MapController : ControllerBase
int area = StateHelper.areaId;
game_city_map mapInfo = new game_city_map();
#region
var userRunInfo = await _mapService.GetUserRun(userId);
if (userRunInfo.status != 0)
{
return PoAction.Message("正在航行状态", 103);
}
#endregion
#region
bool isSelfMap = false;
@@ -97,7 +106,18 @@ public class MapController : ControllerBase
#endregion
object ret = new { mapInfo, cityInfo, npcData, chatData, cityShow, nearUser, noReadMsg = noReadMsg[3] };
object ret = new
{
mapInfo,
cityInfo,
npcData,
chatData,
cityShow,
nearUser,
noReadMsg = noReadMsg[3]
};
return PoAction.Ok(ret);
}
@@ -298,7 +318,7 @@ public class MapController : ControllerBase
}
var MySeaMap = await _mapService.GetUserCityMapData(userId);
if (MySeaMap.Any(it => it.cityId == cityId)==false)
if (MySeaMap.Any(it => it.cityId == cityId) == false)
{
return PoAction.Message("您暂未获取该城市海图!");
}
@@ -345,6 +365,210 @@ public class MapController : ControllerBase
}
}
/// <summary>
/// 用户航海接口
/// </summary>
/// <param name="npcId"></param>
/// <param name="cityId"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> UserRunMap(int npcId, int cityId)
{
string userId = StateHelper.userId;
#region NPC验证
var npcInfo = await _mapService.GetNpcInfo(npcId);
if (npcInfo == null)
{
return PoAction.Message("Npc不存在!");
}
if (npcInfo.status != 1)
{
return PoAction.Message("Npc不存在!");
}
if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.MapTo)))
{
return PoAction.Message("业务不可用!");
}
var onMap = await _mapService.GetUserOnMap(userId);
if (npcInfo.mapId != onMap.mapId)
{
return PoAction.Message("Npc不存在!");
}
#endregion
var cityInfo = await _mapService.GetCityInfo(cityId);
if (cityInfo == null)
{
return PoAction.Message("城市不存在!");
}
var onMapInfo = await _mapService.GetMapInfo(onMap.mapId);
if (onMapInfo.cityId == cityId)
{
return PoAction.Message("您已在当前城市,无需航行!");
}
var MySeaMap = await _mapService.GetUserCityMapData(userId);
if (MySeaMap.Any(it => it.cityId == cityId) == false)
{
return PoAction.Message("您暂未获取该城市海图!");
}
var userShip = await _weightService.GetUserShip(userId);
if (userShip.Count == 0)
{
return PoAction.Message("购买船只后才可以航行哦!");
}
var runInfo = await _mapService.GetUserRun(userId);
if (runInfo.status != 0)
{
return PoAction.Message("您已经在航线上啦!");
}
bool isCanTo = false;
var MyLev = await _attrService.GetUserLev(userId);
if (MyLev < 80)
{
isCanTo = true;
}
int need = 0;
if (isCanTo == false) //判断费用
{
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
int jl = GameTool.ComputeMile(onMap.mapId, cityInfo.toMap);
need = userShip.Max(it => (int)it.copper) * jl;
if (myAcc < need)
{
return PoAction.Message("铜贝不足!");
}
isCanTo = true;
}
if (isCanTo)
{
if (await _mapService.SaveUserRunInfo(userId, StateHelper.areaId, onMap.mapId, cityInfo.toMap))
{
if (need > 0)
{
await _accService.UpdateUserCopper(userId, 0, need, $"传送到【{cityInfo.cityName}】费用");
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("传送失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("传送失败,请稍后尝试!");
}
}
/// <summary>
/// 获取用户航行状态
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetUserRun()
{
var userId = StateHelper.userId;
var runInfo = await _mapService.GetUserRun(userId);
if (runInfo.status != 1)
{
return PoAction.Message("不在航行状态", 100);
}
var onCity = await _mapService.GetCityInfoByMapId(runInfo.onMap);
var toCity = await _mapService.GetCityInfoByMapId(runInfo.toMap);
int isShip = 0;
int safety = 0;
return PoAction.Ok(new
{
onCity = onCity.cityName,
toCity = toCity.cityName,
distance = runInfo.distance,
position = runInfo.position,
isShip = isShip,
safety = safety
});
}
/// <summary>
/// 取消航行
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> StopUserRun()
{
var userId = StateHelper.userId;
var runInfo = await _mapService.GetUserRun(userId);
if (runInfo.status != 1)
{
return PoAction.Message("不在航行状态", 100);
}
if (await _mapService.StopUserRun(userId))
{
return PoAction.Ok(true);
}
else
{
return PoAction.Message("取消失败,请稍后尝试!");
}
}
[HttpGet]
public async Task<IPoAction> UserMapRuning()
{
var userId = StateHelper.userId;
var runInfo = await _mapService.GetUserRun(userId);
if (runInfo.status != 1)
{
return PoAction.Message("不在航行状态", 100);
}
var userShip = await _weightService.GetUserShip(userId);
if (userShip.Count == 0)
{
return PoAction.Message("购买船只后才可以航行哦!");
}
int spend = userShip.Sum(it => (int)it.speed) / userShip.Count;
if ((runInfo.position + spend) >= runInfo.distance)
{
await _mapService.StopUserRun(userId);
var onMap = await _mapService.GetUserOnMap(userId);
await _mapService.UpdateUserOnMap(onMap, runInfo.toMap);
return PoAction.Message("航行完成!", 100);
}
else
{
runInfo.position += spend;
runInfo.upTime = TimeExtend.GetTimeStampSeconds;
if (await _mapService.UpdateUserRunInfo(runInfo))
{
return PoAction.Ok(true);
}
else
{
return PoAction.Message("航行错误,请重试!");
}
}
}
#endregion
#region npc相关

View File

@@ -120,6 +120,9 @@ const BindData = async (map: string): Promise<void> => {
console.log(result.data);
}
else if (result.code == 103) {
PageExtend.Redirect("/map/runing");
}
else {
MessageExtend.ShowDialog("异常错误", result.msg);
}

View File

@@ -1 +1,75 @@
<template></template>
<template>
<div class="content">
请选择航线:<br>
<div class="item" v-for="(item, index) in city" :key="index">
{{ index + 1 }}.<Abutton @click="UserRunMap(item.cityId)">{{ item.cityName }}({{ item.distance }}海里)</Abutton>
</div>
<span v-if="city.length == 0">
暂无海图.
</span>
</div>
<div class="content">
<span v-for="item in area">
<Acheak @click="ChangeCity(item.cityId)" :on-value="cityId.toString()" :on-cheak="item.cityId.toString()">
{{ item.cityName }}</Acheak>
</span>
</div>
</template>
<script setup lang="ts">
definePageMeta({
layout: layout.default,
middleware: 'page-loading'
})
const area = ref<Array<any>>([]);
const city = ref<Array<any>>([]);;
const cityId = ref(0);
onMounted(async () => {
try {
await BindData();
}
finally {
PageLoading.Close();
}
})
const BindData = async (): Promise<void> => {
let npc = LocalStorageHelper.GetOnNpc();
let result = await MapService.GetCityData(Number(npc), cityId.value);
if (result.code == 0) {
area.value = result.data.area;
city.value = result.data.city;
if (cityId.value == 0) {
cityId.value = result.data.cityId;
}
}
else {
MessageExtend.ShowDialogEvent("提示", result.msg, () => {
PageExtend.Redirect("/map");
});
}
};
const ChangeCity = async (_cityId: number) => {
cityId.value = _cityId;
await BindData();
}
const UserRunMap = async (_cityId: number) => {
MessageExtend.LoadingToast("航行准备中...");
let npc = LocalStorageHelper.GetOnNpc();
let result = await MapService.UserRunMap(Number(npc), _cityId);
MessageExtend.LoadingClose();
if (result.code == 0) {
PageExtend.RedirectTo("/map/runing");
}
else {
MessageExtend.ShowToast(result.msg);
}
}
</script>

View File

@@ -0,0 +1,104 @@
<template>
<div class="content">
<Abutton @click="Runing">航行</Abutton>.<Abutton @click="AutoRuning">{{ runState == 0 ? "自动航行" : "取消自动" }}
</Abutton>.
<Abutton @click="StopRun">取消</Abutton><br>
航线{{ data.onCity }} {{ data.toCity }}<br>
航距{{ position }}/{{ distance - position }}/{{ distance }}海里<br>
你可以:
<span v-if="isShip == 1">
<Abutton>钓鱼</Abutton>
</span>
<br>
安全{{ GameTool.GetSeaMapSafetyName(safety) }}<br>
</div>
<div class="content">
您看到<br>
</div>
</template>
<script setup lang="ts">
definePageMeta({
layout: layout.default,
middleware: 'page-loading'
})
const data = ref<any>({});
const distance = ref(0);
const position = ref(0);
const isShip = ref(0);
const safety = ref(0);
const runState = ref(0);
onMounted(async () => {
try {
await BindData();
}
finally {
PageLoading.Close();
}
})
const BindData = async (): Promise<void> => {
let result = await MapService.GetUserRun();
if (result.code == 0) {
data.value = result.data;
distance.value = result.data.distance;
position.value = result.data.position;
isShip.value = result.data.isShip;
safety.value = result.data.safety;
if (runState.value == 1) {
setTimeout(async () => {
await Runing();
}, 1000);
}
}
else if (result.code == 100) {
PageExtend.RedirectTo("/map");
}
else {
MessageExtend.ShowDialogEvent("提示", result.msg, () => {
PageExtend.Redirect("/map");
});
}
};
const Runing = async () => {
MessageExtend.LoadingToast("航行中...");
let result = await MapService.UserMapRuning();
MessageExtend.LoadingClose();
if (result.code == 0) {
await BindData();
}
else if (result.code == 100) {
PageExtend.RedirectTo("/map");
}
else {
MessageExtend.ShowDialog("提示", result.msg);
}
}
const StopRun = async () => {
MessageExtend.ShowConfirmDialogAsyc("航海操作", `您确定要取消航行吗?`, async () => {
let result = await MapService.StopUserRun();
if (result.code == 0) {
PageExtend.RedirectTo("/map");
}
else {
MessageExtend.Notify(result.msg, "danger");
}
return true;
});
}
const AutoRuning = async () => {
if (runState.value == 0) {
runState.value = 1;
await Runing()
}
else {
runState.value = 0;
}
}
</script>

View File

@@ -26,7 +26,6 @@ definePageMeta({
})
const area = ref<Array<any>>([]);
const city = ref<Array<any>>([]);;
const copper = ref('');
const tips = ref('');
const cityId = ref(0);
onMounted(async () => {

View File

@@ -47,6 +47,38 @@ export class MapService {
return await ApiService.Request("get", "/Map/Map/UserToMap", { npcId, cityId });
}
/**
* 用户航海接口
* GET /Map/Map/UserRunMap
*/
static async UserRunMap(npcId: number, cityId: number) {
return await ApiService.Request("get", "/Map/Map/UserRunMap", { npcId, cityId });
}
/**
* 获取用户航行状态
* GET /Map/Map/GetUserRun
*/
static async GetUserRun() {
return await ApiService.Request("get", "/Map/Map/GetUserRun");
}
/**
* 取消航行
* GET /Map/Map/StopUserRun
*/
static async StopUserRun() {
return await ApiService.Request("get", "/Map/Map/StopUserRun");
}
/**
* UserMapRuning
* GET /Map/Map/UserMapRuning
*/
static async UserMapRuning() {
return await ApiService.Request("get", "/Map/Map/UserMapRuning");
}
/**
* 获取NPC相关信息
* GET /Map/Map/GetNpcInfo

View File

@@ -110,5 +110,21 @@ export class GameTool {
return result;
}
public static GetSeaMapSafetyName(safety: number) {
let result = "安全海域";
switch (safety) {
case 0:
result = "安全海域";
break;
case 1:
result = "普通海域";
break;
case 2:
result = "危险海域";
break;
}
return result;
}
}