forked from SoDOff-Project/sodoff
modify ``SendMessage`` add message of typeid 21 when reply is made reworks some things in ``BuddyService``
134 lines
5.2 KiB
C#
134 lines
5.2 KiB
C#
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 bool UpdateBuddyRelation(Viking viking, Viking buddyViking, BuddyStatus buddyStatus1, BuddyStatus buddyStatus2)
|
|
{
|
|
// find relation
|
|
Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id || e.BuddyVikingId == viking.Id)
|
|
.FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id || e.VikingId == buddyViking.Id);
|
|
|
|
if (buddy != null)
|
|
{
|
|
// update it
|
|
buddy.BuddyStatus1 = buddyStatus1;
|
|
buddy.BuddyStatus2 = buddyStatus2;
|
|
ctx.SaveChanges();
|
|
|
|
// return result
|
|
return true;
|
|
} else return false;
|
|
}
|
|
|
|
public bool RemoveBuddy(Viking viking, Viking buddyViking)
|
|
{
|
|
// find relation
|
|
Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id || e.BuddyVikingId == viking.Id)
|
|
.FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id || e.VikingId == buddyViking.Id);
|
|
|
|
if (buddy != null)
|
|
{
|
|
// remove it
|
|
ctx.Buddies.Remove(buddy);
|
|
ctx.SaveChanges();
|
|
|
|
return true;
|
|
}
|
|
else return false;
|
|
}
|
|
|
|
public BuddyList ConstructBuddyList(Viking viking)
|
|
{
|
|
// get all relationships viking has made
|
|
List<Model.Buddy> buddies = ctx.Buddies.Where(e => e.VikingId == viking.Id || 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() };
|
|
}
|
|
}
|