Skip to content

Commit 09365eb

Browse files
authored
Rejoin shared instance (AscensionGameDev#2107)
* fix: (Day) Allow ex-party members to rejoin shared instances on disconnect * chore: (Day) Code review
1 parent d33aa79 commit 09365eb

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

Intersect.Server.Core/Entities/Player.cs

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,11 +2125,45 @@ public bool CanChangeToInstanceType(MapInstanceType? instanceType, bool fromLogi
21252125
case MapInstanceType.Shared:
21262126
if (fromLogin)
21272127
{
2128-
isValid = false;
2128+
// Check to see if the instance still exists & is populated
2129+
if (!InstanceProcessor.TryGetInstanceController(SharedMapInstanceId, out var sharedController) || sharedController.Players.Count == 0)
2130+
{
2131+
return false;
2132+
}
2133+
2134+
// If it does, get the party leader from it...
2135+
var partyMember = sharedController.Players.FirstOrDefault();
2136+
if (partyMember == default)
2137+
{
2138+
return false;
2139+
}
2140+
2141+
// are we in the party?
2142+
if (partyMember.Party?.Contains(this) ?? false)
2143+
{
2144+
return true;
2145+
}
2146+
// If the party was disbanded during the disconnect...
2147+
if (partyMember.PartyLeader == default)
2148+
{
2149+
// Rekindle the party members
2150+
return partyMember.TryAddParty(this);
2151+
}
2152+
else // otherwise, add the player back using the party leader
2153+
{
2154+
return partyMember.PartyLeader.TryAddParty(this);
2155+
}
21292156
}
2130-
if (Party != null && Party.Count > 0 && !Options.Instance.Instancing.RejoinableSharedInstances) // Always valid warp if solo/instances are rejoinable
2157+
// else here because we don't want someone logging on to an abandoned instance -- that could result in things
2158+
// like boss-kill abuse, etc.
2159+
else if (Options.Instance.Instancing.RejoinableSharedInstances)
21312160
{
2132-
if (Party[0].Id == Id) // if we are the party leader
2161+
return true;
2162+
}
2163+
2164+
if (Party != null && Party.Count > 0)
2165+
{
2166+
if (PartyLeader.Id == Id) // if we are the party leader
21332167
{
21342168
// And other players are using our shared instance, deny creation of a new instance until they are finished.
21352169
if (Party.FindAll((Player member) => member.Id != Id && member.InstanceType == MapInstanceType.Shared).Count > 0)
@@ -2141,12 +2175,12 @@ public bool CanChangeToInstanceType(MapInstanceType? instanceType, bool fromLogi
21412175
else
21422176
{
21432177
// Otherwise, if the party leader hasn't yet created a shared instance, deny creation of a new one.
2144-
if (Party[0].InstanceType != MapInstanceType.Shared)
2178+
if (PartyLeader.InstanceType != MapInstanceType.Shared)
21452179
{
21462180
isValid = false;
21472181
PacketSender.SendChatMsg(this, Strings.Parties.CannotCreateInstance, ChatMessageType.Party, CustomColors.Alerts.Error);
21482182
}
2149-
else if (Party[0].SharedMapInstanceId != SharedMapInstanceId)
2183+
else if (PartyLeader.SharedMapInstanceId != SharedMapInstanceId)
21502184
{
21512185
isValid = false;
21522186
PacketSender.SendChatMsg(this, Strings.Parties.InstanceInProgress, ChatMessageType.Party, CustomColors.Alerts.Error);
@@ -4897,7 +4931,7 @@ public void InviteToParty(Player fromPlayer)
48974931
}
48984932
}
48994933

4900-
public void AddParty(Player target)
4934+
public bool TryAddParty(Player target)
49014935
{
49024936
//If a new party, make yourself the leader
49034937
if (Party.Count == 0)
@@ -4906,42 +4940,43 @@ public void AddParty(Player target)
49064940
}
49074941
else
49084942
{
4909-
if (Party[0] != this)
4943+
if (PartyLeader != this)
49104944
{
49114945
PacketSender.SendChatMsg(this, Strings.Parties.leaderinvonly, ChatMessageType.Party, CustomColors.Alerts.Error);
49124946

4913-
return;
4947+
return false;
49144948
}
49154949

49164950
//Check for member being already in the party, if so cancel
49174951
for (var i = 0; i < Party.Count; i++)
49184952
{
49194953
if (Party[i] == target)
49204954
{
4921-
return;
4955+
return false;
49224956
}
49234957
}
49244958
}
49254959

4926-
if (Party.Count < Options.Party.MaximumMembers)
4960+
if (Party.Count >= Options.Party.MaximumMembers)
49274961
{
4928-
target.LeaveParty();
4929-
Party.Add(target);
4930-
4931-
//Update all members of the party with the new list
4932-
for (var i = 0; i < Party.Count; i++)
4933-
{
4934-
Party[i].Party = Party;
4935-
PacketSender.SendParty(Party[i]);
4936-
PacketSender.SendChatMsg(
4937-
Party[i], Strings.Parties.joined.ToString(target.Name), ChatMessageType.Party, CustomColors.Alerts.Accepted
4938-
);
4939-
}
4962+
PacketSender.SendChatMsg(this, Strings.Parties.limitreached, ChatMessageType.Party, CustomColors.Alerts.Error);
4963+
return false;
49404964
}
4941-
else
4965+
4966+
target.LeaveParty();
4967+
Party.Add(target);
4968+
4969+
//Update all members of the party with the new list
4970+
var cachedParty = Party.ToList();
4971+
foreach (var member in cachedParty)
49424972
{
4943-
PacketSender.SendChatMsg(this, Strings.Parties.limitreached, ChatMessageType.Party, CustomColors.Alerts.Error);
4973+
member.Party = cachedParty;
4974+
PacketSender.SendParty(member);
4975+
PacketSender.SendChatMsg(
4976+
Party[i], Strings.Parties.joined.ToString(target.Name), ChatMessageType.Party, CustomColors.Alerts.Accepted
4977+
);
49444978
}
4979+
return true;
49454980
}
49464981

49474982
public void KickParty(Guid target)
@@ -7315,6 +7350,8 @@ public bool TryAddToInstanceController()
73157350

73167351
[JsonIgnore, NotMapped] public List<Player> Party = new List<Player>();
73177352

7353+
[JsonIgnore, NotMapped] public Player PartyLeader => Party.FirstOrDefault();
7354+
73187355
[JsonIgnore, NotMapped] public Player PartyRequester;
73197356

73207357
[JsonIgnore, NotMapped] public Dictionary<Player, long> PartyRequests = new Dictionary<Player, long>();

Intersect.Server.Core/Networking/PacketHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,7 @@ public void HandlePacket(Client client, PartyInviteResponsePacket packet)
20082008
{
20092009
if (player.PartyRequester.IsValidPlayer)
20102010
{
2011-
player.PartyRequester.AddParty(player);
2011+
_ = player.PartyRequester.TryAddParty(player);
20122012
}
20132013
}
20142014
else

0 commit comments

Comments
 (0)