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

@@ -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()
{
@@ -88,6 +100,7 @@ public class GameMapService(ISqlSugarClient DbClient, IRedisCache redis) : IGame
return 0;
}
}
public async Task<List<game_city_npc>> GetMapNpc(string mapId)
{
@@ -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
}