-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartySystem.cs
More file actions
188 lines (168 loc) · 6.82 KB
/
PartySystem.cs
File metadata and controls
188 lines (168 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// there are different ways to implement a party system:
//
// - Player.cs can have a '[SyncVar] party' and broadcast it to all party members
// when something changes in the party. there is no one source of truth, which
// makes this a bit weird. it works, but only until we need a global party
// list, e.g. for dungeon instances.
//
// - Player.cs can have a Party class reference that all members share. Mirror
// can only serialize structs, which makes syncing more difficult then. There
// is also the question of null vs. not null and we would have to not only
// kick/leave parties, but also never forget to set .party to null. This
// results in a lot of complicated code.
//
// - PartySystem could have a list of Party classes. But then the client would
// need to have a local syncedParty class, which makes .party access on server
// and client different (and hence very difficult).
//
// - PartySystem could control the parties. When anything is changed, it
// automatically sets each member's '[SyncVar] party' which Mirror syncs
// automatically. Server and client could access Player.party to read anything
// and use PartySystem to modify parties.
//
// => This seems to be the best solution for a party system with Mirror.
// => PartySystem is almost independent from Unity. It's just a party system
// with names and partyIds.
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace uMMORPG
{
public static class PartySystem
{
static Dictionary<int, Party> parties = new Dictionary<int, Party>();
// start partyIds at 1. 0 means no party, because default party struct's
// partyId is 0.
static int nextPartyId = 1;
// copy party to someone
static void BroadcastTo(string member, Party party)
{
if (Player.onlinePlayers.TryGetValue(member, out Player player))
player.party.party = party;
}
// copy party to all members & save in dictionary
static void BroadcastChanges(Party party)
{
foreach (string member in party.members)
BroadcastTo(member, party);
parties[party.partyId] = party;
}
// check if a partyId exists
public static bool PartyExists(int partyId)
{
return parties.ContainsKey(partyId);
}
// creating a party requires at least two members. it's not a party if
// someone is alone in it.
public static void FormParty(string creator, string firstMember)
{
// create party
int partyId = nextPartyId++;
Party party = new Party(partyId, creator, firstMember);
// broadcast and save in dict
BroadcastChanges(party);
Debug.Log(creator + " formed a new party with " + firstMember);
}
public static void AddToParty(int partyId, string member)
{
// party exists and not full?
Party party;
if (parties.TryGetValue(partyId, out party) && !party.IsFull())
{
// add to members
Array.Resize(ref party.members, party.members.Length + 1);
party.members[party.members.Length - 1] = member;
// broadcast and save in dict
BroadcastChanges(party);
Debug.Log(member + " was added to party " + partyId);
}
}
public static void KickFromParty(int partyId, string requester, string member)
{
// party exists?
Party party;
if (parties.TryGetValue(partyId, out party))
{
// requester is party master, member is in party, not same?
if (party.master == requester && party.Contains(member) && requester != member)
{
// reuse the leave function
LeaveParty(partyId, member);
}
}
}
public static void LeaveParty(int partyId, string member)
{
// party exists?
Party party;
if (parties.TryGetValue(partyId, out party))
{
// requester is not master but is in party?
if (party.master != member && party.Contains(member))
{
// remove from list
party.members = party.members.Where(name => name != member).ToArray();
// still > 1 people?
if (party.members.Length > 1)
{
// broadcast and save in dict
BroadcastChanges(party);
BroadcastTo(member, Party.Empty); // clear for kicked person
}
// otherwise remove party. no point in having only 1 member.
else
{
// broadcast and remove from dict
BroadcastTo(party.members[0], Party.Empty); // clear for master
BroadcastTo(member, Party.Empty); // clear for kicked person
parties.Remove(partyId);
}
Debug.Log(member + " left the party");
}
}
}
public static void DismissParty(int partyId, string requester)
{
// party exists?
Party party;
if (parties.TryGetValue(partyId, out party))
{
// is master?
if (party.master == requester)
{
// clear party for everyone
foreach (string member in party.members)
BroadcastTo(member, Party.Empty);
// remove from dict
parties.Remove(partyId);
Debug.Log(requester + " dismissed the party");
}
}
}
public static void SetPartyExperienceShare(int partyId, string requester, bool value)
{
// party exists and master?
Party party;
if (parties.TryGetValue(partyId, out party) && party.master == requester)
{
// set new value
party.shareExperience = value;
// broadcast and save in dict
BroadcastChanges(party);
}
}
public static void SetPartyGoldShare(int partyId, string requester, bool value)
{
// party exists and master?
Party party;
if (parties.TryGetValue(partyId, out party) && party.master == requester)
{
// set new value
party.shareGold = value;
// broadcast and save in dict
BroadcastChanges(party);
}
}
}
}