Skip to content

Commit fdf4359

Browse files
authored
feat(groups): get group active players (#9)
1 parent e824c5a commit fdf4359

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

server/groups/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export function GetGroupsByType(type: string) {
2020
}, [] as string[]);
2121
}
2222

23+
export function GetGroupActivePlayers(groupName: string) {
24+
const group = groups[groupName];
25+
26+
return group ? [...group.activePlayers] : [];
27+
}
28+
29+
export function GetGroupActivePlayersByType(type: string) {
30+
return Object.values(groups).reduce((acc, group) => {
31+
if (group.type === type) {
32+
acc.push(...group.activePlayers);
33+
}
34+
return acc;
35+
}, [] as number[]);
36+
}
37+
2338
export function SetGroupPermission(groupName: string, grade: number, permission: string, value: 'allow' | 'deny') {
2439
const permissions = GetGroupPermissions(groupName);
2540

@@ -48,6 +63,8 @@ function SetupGroup(data: DbGroup) {
4863
GlobalState[`${group.name}:count`] = 0;
4964
GlobalState[`${group.name}:activeCount`] = 0;
5065

66+
group.activePlayers = new Set();
67+
5168
groups[group.name] = group;
5269
group.grades = group.grades.reduce(
5370
(acc, value, index) => {
@@ -99,6 +116,7 @@ export async function CreateGroup(data: CreateGroupProperties) {
99116
grades: grades,
100117
accountRoles: accountRoles,
101118
hasAccount: data.hasAccount ?? false,
119+
activePlayers: new Set(),
102120
};
103121

104122
const response = await InsertGroup(group);
@@ -185,3 +203,5 @@ exports('SetGroupPermission', SetGroupPermission);
185203
exports('RemoveGroupPermission', RemoveGroupPermission);
186204
exports('CreateGroup', CreateGroup);
187205
exports('DeleteGroup', DeleteGroup);
206+
exports('GetGroupActivePlayers', GetGroupActivePlayers);
207+
exports('GetGroupActivePlayersByType', GetGroupActivePlayersByType);

server/player/class.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,19 @@ export class OxPlayer extends ClassInterface {
187187

188188
const currentActiveGroup = this.get('activeGroup');
189189

190-
if (currentActiveGroup) GlobalState[`${currentActiveGroup}:activeCount`] -= 1;
190+
if (currentActiveGroup) {
191+
GlobalState[`${currentActiveGroup}:activeCount`] -= 1;
191192

192-
if (groupName) GlobalState[`${groupName}:activeCount`] += 1;
193+
const group = GetGroup(currentActiveGroup);
194+
group.activePlayers.delete(+this.source);
195+
}
196+
197+
if (groupName) {
198+
GlobalState[`${groupName}:activeCount`] += 1;
199+
200+
const group = GetGroup(groupName);
201+
group.activePlayers.add(+this.source);
202+
}
193203

194204
SetActiveGroup(this.charId, temp ? undefined : groupName);
195205
this.set('activeGroup', groupName, true);
@@ -437,7 +447,10 @@ export class OxPlayer extends ClassInterface {
437447
delete this.#groups[group.name];
438448
GlobalState[`${group.name}:count`] -= 1;
439449

440-
if (canRemoveActiveCount && group.name === this.get('activeGroup')) GlobalState[`${group.name}:activeCount`] -= 1;
450+
if (canRemoveActiveCount && group.name === this.get('activeGroup')) {
451+
GlobalState[`${group.name}:activeCount`] -= 1;
452+
group.activePlayers.delete(+this.source);
453+
}
441454
}
442455

443456
/** Saves the active character to the database. */
@@ -582,8 +595,14 @@ export class OxPlayer extends ClassInterface {
582595
this.set('phoneNumber', phoneNumber, true);
583596
this.set('activeGroup', groups.find((group) => group.isActive)?.name, true);
584597

585-
if (this.get('activeGroup')) GlobalState[`${this.get('activeGroup')}:activeCount`] += 1;
586-
DEV: console.info(`Restored OxPlayer<${this.userId}> previous active group: ${this.get('activeGroup')}`);
598+
const activeGroup = this.get('activeGroup');
599+
if (activeGroup) {
600+
GlobalState[`${this.get('activeGroup')}:${activeGroup}`] += 1;
601+
602+
const group = GetGroup(activeGroup)
603+
group.activePlayers.add(+this.source);
604+
}
605+
DEV: console.info(`Restored OxPlayer<${this.userId}> previous active group: ${activeGroup}`);
587606

588607
OxPlayer.keys.charId[character.charId] = this;
589608

types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export interface DbGroup {
140140
type?: string;
141141
colour?: number;
142142
hasAccount: boolean;
143+
activePlayers: Set<number>;
143144
}
144145

145146
export interface OxGroup extends DbGroup {

0 commit comments

Comments
 (0)