forked from SoDOff-Project/sodoff
implement buddy codes
This commit is contained in:
parent
5d0cb1581d
commit
44aa98ef3a
@ -1159,6 +1159,31 @@ public class ContentController : Controller {
|
||||
else return Ok(new BuddyActionResult{ Result = BuddyActionResultType.InvalidFriendCode });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Produces("application/xml")]
|
||||
[Route("ContentWebService.asmx/AddBuddyByFriendCode")]
|
||||
[VikingSession]
|
||||
public IActionResult AddBuddyByFriendCode(Viking viking, [FromForm] string friendCode, [FromForm] string apiKey)
|
||||
{
|
||||
// make sure viking doesn't add self as buddy
|
||||
if (viking.BuddyCode != null && viking.BuddyCode == friendCode) return Ok(new BuddyActionResult{ Result = BuddyActionResultType.CannotAddSelf });
|
||||
|
||||
// find viking
|
||||
Viking? buddyViking = ctx.Vikings.FirstOrDefault(e => e.BuddyCode == friendCode);
|
||||
|
||||
if (buddyViking != null)
|
||||
{
|
||||
uint gameVersion = ClientVersion.GetVersion(apiKey);
|
||||
// check if game clients are different
|
||||
if (
|
||||
(buddyViking.GameVersion != gameVersion) &&
|
||||
!(buddyViking.GameVersion >= ClientVersion.Min_SoD && gameVersion >= ClientVersion.Min_SoD) &&
|
||||
!(buddyViking.GameVersion >= ClientVersion.WoJS && gameVersion >= ClientVersion.WoJS && buddyViking.GameVersion < ClientVersion.WoJS_NewAvatar && gameVersion < ClientVersion.WoJS_NewAvatar)
|
||||
) return Ok(new BuddyActionResult { Result = BuddyActionResultType.InvalidFriendCode });
|
||||
else return Ok(buddyService.CreateBuddyRelation(viking, buddyViking));
|
||||
} else return Ok(new BuddyActionResult{ Result = BuddyActionResultType.InvalidFriendCode });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Produces("application/xml")]
|
||||
[Route("ContentWebService.asmx/RemoveBuddy")]
|
||||
@ -1209,6 +1234,17 @@ public class ContentController : Controller {
|
||||
return Ok(buddyService.ConstructBuddyList(viking));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Produces("application/xml")]
|
||||
[Route("ContentWebService.asmx/GetFriendCode")]
|
||||
public IActionResult GetFriendCode([FromForm] Guid userId)
|
||||
{
|
||||
Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId);
|
||||
|
||||
if (viking == null) return NotFound();
|
||||
else return Ok(buddyService.GetOrSetBuddyCode(viking));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Produces("application/xml")]
|
||||
[Route("/ContentWebService.asmx/RedeemMysteryBoxItems")]
|
||||
|
1320
src/Migrations/20250308012001_Viking_BuddyCode.Designer.cs
generated
Normal file
1320
src/Migrations/20250308012001_Viking_BuddyCode.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
28
src/Migrations/20250308012001_Viking_BuddyCode.cs
Normal file
28
src/Migrations/20250308012001_Viking_BuddyCode.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace sodoff.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Viking_BuddyCode : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "BuddyCode",
|
||||
table: "Vikings",
|
||||
type: "TEXT",
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BuddyCode",
|
||||
table: "Vikings");
|
||||
}
|
||||
}
|
||||
}
|
@ -817,6 +817,9 @@ namespace sodoff.Migrations
|
||||
b.Property<DateTime?>("BirthDate")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("BuddyCode")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime?>("CreationDate")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
|
@ -19,6 +19,8 @@ public class Viking {
|
||||
|
||||
public string? AvatarSerialized { get; set; }
|
||||
|
||||
public string? BuddyCode { get; set; }
|
||||
|
||||
public int? SelectedDragonId { get; set; }
|
||||
|
||||
public virtual ICollection<Session> Sessions { get; set; } = null!;
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Security;
|
||||
using sodoff.Model;
|
||||
using sodoff.Schema;
|
||||
using sodoff.Util;
|
||||
@ -8,8 +9,12 @@ namespace sodoff.Services;
|
||||
|
||||
public class BuddyService
|
||||
{
|
||||
private char[] BuddyCodeCharacterList = new char[]
|
||||
{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0' };
|
||||
|
||||
private readonly DBContext ctx;
|
||||
private readonly MessagingService messagingService;
|
||||
|
||||
public BuddyService(DBContext ctx, MessagingService messagingService)
|
||||
{
|
||||
this.ctx = ctx;
|
||||
@ -95,6 +100,31 @@ public class BuddyService
|
||||
} else return false;
|
||||
}
|
||||
|
||||
public string GetOrSetBuddyCode(Viking viking)
|
||||
{
|
||||
if (viking.BuddyCode != null) return viking.BuddyCode;
|
||||
else
|
||||
{
|
||||
// generate a buddy code and set it
|
||||
Random rnd = new Random();
|
||||
string code = "";
|
||||
do
|
||||
{
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
code += BuddyCodeCharacterList[rnd.Next(0, BuddyCodeCharacterList.Length)];
|
||||
}
|
||||
} while (ctx.Vikings.FirstOrDefault(e => e.BuddyCode == code) != null);
|
||||
|
||||
// set code on viking
|
||||
viking.BuddyCode = code;
|
||||
ctx.SaveChanges();
|
||||
|
||||
// return it
|
||||
return viking.BuddyCode;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveBuddy(Viking viking, Viking buddyViking)
|
||||
{
|
||||
// find relation
|
||||
|
Loading…
x
Reference in New Issue
Block a user