forked from SoDOff-Project/sodoff
Buddy System #3
1310
src/Migrations/20250303212804_Buddies_CreateDate.Designer.cs
generated
Normal file
1310
src/Migrations/20250303212804_Buddies_CreateDate.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
114
src/Migrations/20250303212804_Buddies_CreateDate.cs
Normal file
114
src/Migrations/20250303212804_Buddies_CreateDate.cs
Normal file
@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace sodoff.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Buddies_CreateDate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Buddy_Vikings_BuddyVikingId",
|
||||
table: "Buddy");
|
||||
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Buddy_Vikings_VikingId",
|
||||
table: "Buddy");
|
||||
|
||||
migrationBuilder.DropPrimaryKey(
|
||||
name: "PK_Buddy",
|
||||
table: "Buddy");
|
||||
|
||||
migrationBuilder.RenameTable(
|
||||
name: "Buddy",
|
||||
newName: "Buddies");
|
||||
|
||||
migrationBuilder.RenameIndex(
|
||||
name: "IX_Buddy_BuddyVikingId",
|
||||
table: "Buddies",
|
||||
newName: "IX_Buddies_BuddyVikingId");
|
||||
|
||||
migrationBuilder.AddColumn<DateTime>(
|
||||
name: "CreatedAt",
|
||||
table: "Buddies",
|
||||
type: "TEXT",
|
||||
nullable: false,
|
||||
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
|
||||
|
||||
migrationBuilder.AddPrimaryKey(
|
||||
name: "PK_Buddies",
|
||||
table: "Buddies",
|
||||
column: "VikingId");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Buddies_Vikings_BuddyVikingId",
|
||||
table: "Buddies",
|
||||
column: "BuddyVikingId",
|
||||
principalTable: "Vikings",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Buddies_Vikings_VikingId",
|
||||
table: "Buddies",
|
||||
column: "VikingId",
|
||||
principalTable: "Vikings",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Buddies_Vikings_BuddyVikingId",
|
||||
table: "Buddies");
|
||||
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_Buddies_Vikings_VikingId",
|
||||
table: "Buddies");
|
||||
|
||||
migrationBuilder.DropPrimaryKey(
|
||||
name: "PK_Buddies",
|
||||
table: "Buddies");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "CreatedAt",
|
||||
table: "Buddies");
|
||||
|
||||
migrationBuilder.RenameTable(
|
||||
name: "Buddies",
|
||||
newName: "Buddy");
|
||||
|
||||
migrationBuilder.RenameIndex(
|
||||
name: "IX_Buddies_BuddyVikingId",
|
||||
table: "Buddy",
|
||||
newName: "IX_Buddy_BuddyVikingId");
|
||||
|
||||
migrationBuilder.AddPrimaryKey(
|
||||
name: "PK_Buddy",
|
||||
table: "Buddy",
|
||||
column: "VikingId");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Buddy_Vikings_BuddyVikingId",
|
||||
table: "Buddy",
|
||||
column: "BuddyVikingId",
|
||||
principalTable: "Vikings",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_Buddy_Vikings_VikingId",
|
||||
table: "Buddy",
|
||||
column: "VikingId",
|
||||
principalTable: "Vikings",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
}
|
||||
}
|
@ -84,6 +84,9 @@ namespace sodoff.Migrations
|
||||
b.Property<int>("BuddyVikingId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsBestFriend1")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
@ -94,7 +97,7 @@ namespace sodoff.Migrations
|
||||
|
||||
b.HasIndex("BuddyVikingId");
|
||||
|
||||
b.ToTable("Buddy");
|
||||
b.ToTable("Buddies");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("sodoff.Model.Dragon", b =>
|
||||
|
@ -17,6 +17,8 @@ public class Buddy
|
||||
public bool IsBestFriend1 { get; set; }
|
||||
public bool IsBestFriend2 { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public virtual Viking? Viking { get; set; }
|
||||
public virtual Viking? BuddyViking { get; set; }
|
||||
}
|
||||
|
22
src/Schema/BuddyActionResult.cs
Normal file
22
src/Schema/BuddyActionResult.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using sodoff.Schema;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace sodoff.Schema
|
||||
{
|
||||
[XmlRoot(ElementName = "BuddyActionResult", Namespace = "")]
|
||||
[Serializable]
|
||||
public class BuddyActionResult
|
||||
{
|
||||
// Token: 0x04000203 RID: 515
|
||||
[XmlElement(ElementName = "Result")]
|
||||
public BuddyActionResultType Result;
|
||||
|
||||
// Token: 0x04000204 RID: 516
|
||||
[XmlElement(ElementName = "Status")]
|
||||
public BuddyStatus Status;
|
||||
|
||||
// Token: 0x04000205 RID: 517
|
||||
[XmlElement(ElementName = "BuddyUserID")]
|
||||
public string BuddyUserID;
|
||||
}
|
||||
}
|
30
src/Schema/BuddyActionResultType.cs
Normal file
30
src/Schema/BuddyActionResultType.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace sodoff.Schema
|
||||
{
|
||||
[Flags]
|
||||
public enum BuddyActionResultType
|
||||
{
|
||||
// Token: 0x04000217 RID: 535
|
||||
[XmlEnum("0")]
|
||||
Unknown = 0,
|
||||
// Token: 0x04000218 RID: 536
|
||||
[XmlEnum("1")]
|
||||
Success = 1,
|
||||
// Token: 0x04000219 RID: 537
|
||||
[XmlEnum("2")]
|
||||
BuddyListFull = 2,
|
||||
// Token: 0x0400021A RID: 538
|
||||
[XmlEnum("3")]
|
||||
FriendBuddyListFull = 3,
|
||||
// Token: 0x0400021B RID: 539
|
||||
[XmlEnum("4")]
|
||||
AlreadyInList = 4,
|
||||
// Token: 0x0400021C RID: 540
|
||||
[XmlEnum("5")]
|
||||
InvalidFriendCode = 5,
|
||||
// Token: 0x0400021D RID: 541
|
||||
[XmlEnum("6")]
|
||||
CannotAddSelf = 6
|
||||
}
|
||||
}
|
117
src/Services/BuddyService.cs
Normal file
117
src/Services/BuddyService.cs
Normal file
@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using sodoff.Model;
|
||||
using sodoff.Schema;
|
||||
using sodoff.Util;
|
||||
|
||||
namespace sodoff.Services;
|
||||
|
||||
public class BuddyService
|
||||
{
|
||||
private readonly DBContext ctx;
|
||||
private readonly MessagingService messagingService;
|
||||
public BuddyService(DBContext ctx, MessagingService messagingService)
|
||||
{
|
||||
this.ctx = ctx;
|
||||
this.messagingService = messagingService;
|
||||
}
|
||||
|
||||
public BuddyActionResult CreateBuddyRelation(Viking viking1, Viking viking2, BuddyStatus buddyStatus1 = BuddyStatus.PendingApprovalFromOther, BuddyStatus buddyStatus2 = BuddyStatus.PendingApprovalFromSelf)
|
||||
{
|
||||
// get execution UTC timestamp
|
||||
DateTime now = DateTime.UtcNow;
|
||||
|
||||
// construct buddy
|
||||
Model.Buddy buddy = new Model.Buddy
|
||||
{
|
||||
Viking = viking1,
|
||||
VikingId = viking1.Id,
|
||||
BuddyViking = viking2,
|
||||
BuddyVikingId = viking2.Id,
|
||||
BuddyStatus1 = buddyStatus1,
|
||||
BuddyStatus2 = buddyStatus2,
|
||||
IsBestFriend1 = false,
|
||||
IsBestFriend2 = false,
|
||||
CreatedAt = now
|
||||
};
|
||||
|
||||
// do not add if viking1 is on a different game version than viking2
|
||||
if (viking1.GameVersion < viking2.GameVersion) return new BuddyActionResult { Result = BuddyActionResultType.InvalidFriendCode };
|
||||
|
||||
// do not add if relationship already exists
|
||||
Model.Buddy? existingBuddy = ctx.Buddies.FirstOrDefault(e => e.VikingId == buddy.VikingId && e.BuddyVikingId == buddy.BuddyVikingId);
|
||||
if (existingBuddy != null) return new BuddyActionResult { Result = BuddyActionResultType.AlreadyInList };
|
||||
|
||||
// add relationship to database
|
||||
ctx.Buddies.Add(buddy);
|
||||
ctx.SaveChanges();
|
||||
|
||||
// if this is a buddy relationship, use MessagingService to send buddy request message to viking2
|
||||
if (buddyStatus1 == BuddyStatus.PendingApprovalFromOther && buddyStatus2 == BuddyStatus.PendingApprovalFromSelf)
|
||||
messagingService.AddMessageToViking(viking1, viking2, MessageType.Data, MessageTypeID.BuddyList, MessageLevel.WhiteList, "[[Line1]]=[[{{BuddyUserName}} Wants To Add You As A Buddy!]]", "[[Line1]]=[[{{BuddyUserName}} Wants To Add You As A Buddy!]]", "[[Line1]]=[[{{BuddyUserName}} Wants To Add You As A Buddy!]]");
|
||||
|
||||
// return result
|
||||
return new BuddyActionResult
|
||||
{
|
||||
Result = BuddyActionResultType.Success,
|
||||
Status = buddy.BuddyStatus1,
|
||||
BuddyUserID = viking2.Uid.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
public void RemoveBuddy(Viking viking, Guid buddyUid)
|
||||
{
|
||||
// find buddy viking
|
||||
Viking? buddyViking = ctx.Vikings.FirstOrDefault(e => e.Uid == buddyUid);
|
||||
|
||||
if (buddyViking != null)
|
||||
{
|
||||
// find buddy relationship
|
||||
Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id)
|
||||
.FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id);
|
||||
|
||||
if (buddy != null)
|
||||
// remove it
|
||||
ctx.Buddies.Remove(buddy);
|
||||
else throw new InvalidOperationException("Cannot Remove A Buddy That Doesn't Exist");
|
||||
|
||||
ctx.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public BuddyList ConstructBuddyList(Viking viking)
|
||||
{
|
||||
// get all relationships viking has made
|
||||
List<Model.Buddy> buddies = ctx.Buddies.Where(e => e.BuddyVikingId == viking.Id).ToList();
|
||||
List<Schema.Buddy> schemaBuddies = new();
|
||||
foreach (var buddy in buddies)
|
||||
{
|
||||
// show differently depending on requester
|
||||
if (buddy.VikingId == viking.Id)
|
||||
schemaBuddies.Add(new Schema.Buddy
|
||||
{
|
||||
UserID = buddy.BuddyViking.Uid.ToString(),
|
||||
DisplayName = XmlUtil.DeserializeXml<AvatarData>(buddy.BuddyViking.AvatarSerialized).DisplayName,
|
||||
Status = buddy.BuddyStatus2,
|
||||
CreateDate = buddy.CreatedAt,
|
||||
Online = true, // hardcoded until mmo reports precense
|
||||
OnMobile = false,
|
||||
BestBuddy = buddy.IsBestFriend2
|
||||
});
|
||||
else
|
||||
schemaBuddies.Add(new Schema.Buddy
|
||||
{
|
||||
UserID = buddy.Viking.Uid.ToString(),
|
||||
DisplayName = XmlUtil.DeserializeXml<AvatarData>(buddy.Viking.AvatarSerialized).DisplayName,
|
||||
Status = buddy.BuddyStatus1,
|
||||
CreateDate = buddy.CreatedAt,
|
||||
Online = true, // hardcoded until mmo reports precense
|
||||
OnMobile = false,
|
||||
BestBuddy = buddy.IsBestFriend1
|
||||
});
|
||||
}
|
||||
|
||||
// return buddy list
|
||||
return new BuddyList { Buddy = schemaBuddies.ToArray() };
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user