Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
Update data of user with same uuid in world (workadventure#2762)
Browse files Browse the repository at this point in the history
Co-authored-by: grégoire parant <[email protected]>
  • Loading branch information
mlima95 and gparant authored Nov 2, 2022
1 parent 4e011c5 commit cc9bacc
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions back/src/Model/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ export class User implements Movable {
setVariable.getPersist()
)
.catch((e) => console.error("An error occurred while saving world variable: ", e));

this.updateDataUserSameUUID(setVariable, details);
} else if (scope === SetPlayerVariableMessage.Scope.ROOM) {
this.variables
.saveRoomVariable(
Expand All @@ -235,37 +237,7 @@ export class User implements Movable {
)
.catch((e) => console.error("An error occurred while saving room variable: ", e));

// Very special case: if we are updating a player variable AND if if the variable is persisted, we must also
// update the variable of all other users with the same UUID!
if (setVariable.getPersist()) {
// Let's have a look at all other users sharing the same UUID
const brothers = this.brothersFinder.getBrothers(this);
for (const brother of brothers) {
brother.variables
.saveRoomVariable(
setVariable.getName(),
setVariable.getValue(),
setVariable.getPublic(),
setVariable.getTtl()?.getValue(),
// We don't need to persist this for every player as this will write in the same place in DB.
false
)
.catch((e) =>
console.error(
"An error occurred while saving room variable for a user with same UUID: ",
e
)
);

// Let's dispatch the message to the user.
const playerDetailsUpdatedMessage = new PlayerDetailsUpdatedMessage();
playerDetailsUpdatedMessage.setUserid(brother.id);
playerDetailsUpdatedMessage.setDetails(details);
const subMessage = new SubMessage();
subMessage.setPlayerdetailsupdatedmessage(playerDetailsUpdatedMessage);
brother.emitInBatch(subMessage);
}
}
this.updateDataUserSameUUID(setVariable, details);
} else {
const _exhaustiveCheck: never = scope;
}
Expand All @@ -291,4 +263,41 @@ export class User implements Movable {
public getVariables(): PlayerVariables {
return this.variables;
}

private updateDataUserSameUUID(
setVariable: SetPlayerVariableMessage,
details: SetPlayerDetailsMessage | undefined
) {
// Very special case: if we are updating a player variable AND if if the variable is persisted, we must also
// update the variable of all other users with the same UUID!
console.log("GET PERSIST", setVariable.getPersist());
if (setVariable.getPersist()) {
// Let's have a look at all other users sharing the same UUID
const brothers = this.brothersFinder.getBrothers(this);
console.log("BROTHERS", brothers);

for (const brother of brothers) {
brother.variables
.saveRoomVariable(
setVariable.getName(),
setVariable.getValue(),
setVariable.getPublic(),
setVariable.getTtl()?.getValue(),
// We don't need to persist this for every player as this will write in the same place in DB.
false
)
.catch((e) =>
console.error("An error occurred while saving room variable for a user with same UUID: ", e)
);

// Let's dispatch the message to the user.
const playerDetailsUpdatedMessage = new PlayerDetailsUpdatedMessage();
playerDetailsUpdatedMessage.setUserid(brother.id);
playerDetailsUpdatedMessage.setDetails(details);
const subMessage = new SubMessage();
subMessage.setPlayerdetailsupdatedmessage(playerDetailsUpdatedMessage);
brother.emitInBatch(subMessage);
}
}
}
}

0 comments on commit cc9bacc

Please sign in to comment.