mirror of
https://github.com/SoDOff-Project/sodoff.git
synced 2025-10-11 08:18:49 -07:00
Compare commits
4 Commits
bca383c4d0
...
1a6db72d7a
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1a6db72d7a | ||
![]() |
0923b80cdf | ||
![]() |
1b22c9c3dd | ||
![]() |
7dbcc456b9 |
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using sodoff.Model;
|
using sodoff.Model;
|
||||||
|
|
||||||
namespace sodoff.Attributes;
|
namespace sodoff.Attributes;
|
||||||
@ -12,6 +13,9 @@ public class VikingSession : Attribute, IAsyncActionFilter {
|
|||||||
public Modes Mode { get; set; } = Modes.VIKING;
|
public Modes Mode { get; set; } = Modes.VIKING;
|
||||||
public bool UseLock = false;
|
public bool UseLock = false;
|
||||||
|
|
||||||
|
private static Dictionary<string, SemaphoreSlim> semaphores = new();
|
||||||
|
private static SemaphoreSlim dictSemaphore = new(1, 1);
|
||||||
|
|
||||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) {
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) {
|
||||||
DBContext ctx = context.HttpContext.RequestServices.GetService(typeof(DBContext)) as DBContext;
|
DBContext ctx = context.HttpContext.RequestServices.GetService(typeof(DBContext)) as DBContext;
|
||||||
|
|
||||||
@ -44,15 +48,16 @@ public class VikingSession : Attribute, IAsyncActionFilter {
|
|||||||
// NOTE: we can't refer to session.User / session.Viking here,
|
// NOTE: we can't refer to session.User / session.Viking here,
|
||||||
// because it may cause to ignore modifications from the threads we are waiting for
|
// because it may cause to ignore modifications from the threads we are waiting for
|
||||||
// we can use its only after vikingMutex.WaitOne()
|
// we can use its only after vikingMutex.WaitOne()
|
||||||
|
string semKey = "SoDOffViking:" + userVikingId;
|
||||||
Mutex vikingMutex = new Mutex(false, "SoDOffViking:" + userVikingId);
|
SemaphoreSlim semaphore = await GetSemaphore(semKey);
|
||||||
try {
|
try {
|
||||||
vikingMutex.WaitOne();
|
await semaphore.WaitAsync();
|
||||||
context.ActionArguments["user"] = session.User;
|
context.ActionArguments["user"] = session.User;
|
||||||
context.ActionArguments["viking"] = session.Viking;
|
context.ActionArguments["viking"] = session.Viking;
|
||||||
await next();
|
await next();
|
||||||
} finally {
|
} finally {
|
||||||
vikingMutex.ReleaseMutex();
|
semaphore.Release();
|
||||||
|
await RemoveSemaphore(semKey, semaphore);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
context.ActionArguments["user"] = session.User;
|
context.ActionArguments["user"] = session.User;
|
||||||
@ -60,4 +65,22 @@ public class VikingSession : Attribute, IAsyncActionFilter {
|
|||||||
await next();
|
await next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task<SemaphoreSlim> GetSemaphore(string key) {
|
||||||
|
await dictSemaphore.WaitAsync();
|
||||||
|
if (!semaphores.TryGetValue(key, out SemaphoreSlim semaphore)) {
|
||||||
|
semaphore = new SemaphoreSlim(1, 1);
|
||||||
|
semaphores.Add(key, semaphore);
|
||||||
|
}
|
||||||
|
dictSemaphore.Release();
|
||||||
|
return semaphore;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task RemoveSemaphore(string key, SemaphoreSlim sem) {
|
||||||
|
await dictSemaphore.WaitAsync();
|
||||||
|
if (sem.CurrentCount == 1) {
|
||||||
|
semaphores.Remove(key);
|
||||||
|
}
|
||||||
|
dictSemaphore.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -570,7 +570,7 @@ public class ContentController : Controller {
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Produces("application/xml")]
|
[Produces("application/xml")]
|
||||||
[Route("V2/ContentWebService.asmx/CreatePet")]
|
[Route("V2/ContentWebService.asmx/CreatePet")]
|
||||||
[VikingSession]
|
[VikingSession(UseLock = true)]
|
||||||
public IActionResult CreatePet(Viking viking, [FromForm] string request) {
|
public IActionResult CreatePet(Viking viking, [FromForm] string request) {
|
||||||
RaisedPetRequest raisedPetRequest = XmlUtil.DeserializeXml<RaisedPetRequest>(request);
|
RaisedPetRequest raisedPetRequest = XmlUtil.DeserializeXml<RaisedPetRequest>(request);
|
||||||
// TODO: Investigate SetAsSelectedPet and UnSelectOtherPets - they don't seem to do anything
|
// TODO: Investigate SetAsSelectedPet and UnSelectOtherPets - they don't seem to do anything
|
||||||
@ -602,7 +602,6 @@ public class ContentController : Controller {
|
|||||||
|
|
||||||
if (raisedPetRequest.SetAsSelectedPet == true) {
|
if (raisedPetRequest.SetAsSelectedPet == true) {
|
||||||
viking.SelectedDragon = dragon;
|
viking.SelectedDragon = dragon;
|
||||||
ctx.Update(viking);
|
|
||||||
}
|
}
|
||||||
ctx.Dragons.Add(dragon);
|
ctx.Dragons.Add(dragon);
|
||||||
ctx.Images.Add(image);
|
ctx.Images.Add(image);
|
||||||
@ -905,7 +904,7 @@ public class ContentController : Controller {
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Produces("application/xml")]
|
[Produces("application/xml")]
|
||||||
[Route("ContentWebService.asmx/SetImage")]
|
[Route("ContentWebService.asmx/SetImage")]
|
||||||
[VikingSession]
|
[VikingSession(UseLock = true)]
|
||||||
public bool SetImage(Viking viking, [FromForm] string ImageType, [FromForm] int ImageSlot, [FromForm] string contentXML, [FromForm] string imageFile) {
|
public bool SetImage(Viking viking, [FromForm] string ImageType, [FromForm] int ImageSlot, [FromForm] string contentXML, [FromForm] string imageFile) {
|
||||||
// TODO: the other properties of contentXML
|
// TODO: the other properties of contentXML
|
||||||
ImageData data = XmlUtil.DeserializeXml<ImageData>(contentXML);
|
ImageData data = XmlUtil.DeserializeXml<ImageData>(contentXML);
|
||||||
@ -925,11 +924,8 @@ public class ContentController : Controller {
|
|||||||
image.ImageData = imageFile;
|
image.ImageData = imageFile;
|
||||||
image.TemplateName = data.TemplateName;
|
image.TemplateName = data.TemplateName;
|
||||||
|
|
||||||
if (newImage) {
|
if (newImage)
|
||||||
ctx.Images.Add(image);
|
ctx.Images.Add(image);
|
||||||
} else {
|
|
||||||
ctx.Images.Update(image);
|
|
||||||
}
|
|
||||||
ctx.SaveChanges();
|
ctx.SaveChanges();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -74,7 +74,7 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
|
@ -11,26 +11,26 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.20" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Choose>
|
<Choose>
|
||||||
<When Condition="$(DefineConstants.Contains('USE_SQLITE'))">
|
<When Condition="$(DefineConstants.Contains('USE_SQLITE'))">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.20" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</When>
|
</When>
|
||||||
</Choose>
|
</Choose>
|
||||||
<Choose>
|
<Choose>
|
||||||
<When Condition="$(DefineConstants.Contains('USE_POSTGRESQL'))">
|
<When Condition="$(DefineConstants.Contains('USE_POSTGRESQL'))">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.18" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</When>
|
</When>
|
||||||
</Choose>
|
</Choose>
|
||||||
<Choose>
|
<Choose>
|
||||||
<When Condition="$(DefineConstants.Contains('USE_MYSQL'))">
|
<When Condition="$(DefineConstants.Contains('USE_MYSQL'))">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
|
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.16" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</When>
|
</When>
|
||||||
</Choose>
|
</Choose>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user