Added the user-room token created from admin when we access a room

This commit is contained in:
Valdo Romao 2021-12-01 15:38:34 +00:00
parent 9eb4206fe0
commit dd79f7d0f4
13 changed files with 63 additions and 2 deletions

View File

@ -97,6 +97,7 @@ export class SocketManager {
}
const roomJoinedMessage = new RoomJoinedMessage();
roomJoinedMessage.setTagList(joinRoomMessage.getTagList());
roomJoinedMessage.setUserroomtoken(joinRoomMessage.getUserroomtoken());
for (const [itemId, item] of room.getItemsState().entries()) {
const itemStateMessage = new ItemStateMessage();

View File

@ -58,6 +58,29 @@ WA.onInit().then(() => {
})
```
### Get the user-room token of the player
```
WA.player.userRoomToken: string;
```
The user-room token is available from the `WA.player.userRoomToken` property.
This token is generated in WorkAdventure and contains information such as the player's room ID and its associated membership ID.
{.alert.alert-warn}
This token is used when you change your logo using a configured variable.
Indeed, to change your logo you need to perform an upload in order to get a file URL. This type of actions must be validated on our side.
If you are using a self-hosted version of WorkAdventure you will not have the possibility to perform actions that depends on the user-room token, unless you create an API that support it.
{.alert.alert-info}
You need to wait for the end of the initialization before accessing `WA.player.userRoomToken`
```typescript
WA.onInit().then(() => {
console.log('Token: ', WA.player.userRoomToken);
})
```
### Listen to player movement
```
WA.player.onPlayerMove(callback: HasPlayerMovedEventCallback): void;

View File

@ -9,6 +9,7 @@ export const isGameStateEvent = new tg.IsInterface()
startLayerName: tg.isUnion(tg.isString, tg.isNull),
tags: tg.isArray(tg.isString),
variables: tg.isObject,
userRoomToken: tg.isUnion(tg.isString, tg.isUndefined),
})
.get();
/**

View File

@ -20,6 +20,12 @@ export const setTags = (_tags: string[]) => {
let uuid: string | undefined;
let userRoomToken: string | undefined;
export const setUserRoomToken = (token: string | undefined) => {
userRoomToken = token;
};
export const setUuid = (_uuid: string | undefined) => {
uuid = _uuid;
};
@ -67,6 +73,13 @@ export class WorkadventurePlayerCommands extends IframeApiContribution<Workadven
}
return uuid;
}
get userRoomToken(): string | undefined {
if (userRoomToken === undefined) {
throw new Error("User-room token not initialized yet. You should call WA.player.userRoomToken within a WA.onInit callback.");
}
return userRoomToken;
}
}
export default new WorkadventurePlayerCommands();

View File

@ -68,6 +68,7 @@ export class RoomConnection implements RoomConnection {
private static websocketFactory: null | ((url: string) => any) = null; // eslint-disable-line @typescript-eslint/no-explicit-any
private closed: boolean = false;
private tags: string[] = [];
private _userRoomToken: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static setWebsocketFactory(websocketFactory: (url: string) => any): void {
@ -211,6 +212,7 @@ export class RoomConnection implements RoomConnection {
this.userId = roomJoinedMessage.getCurrentuserid();
this.tags = roomJoinedMessage.getTagList();
this._userRoomToken = roomJoinedMessage.getUserroomtoken();
this.dispatch(EventMessage.CONNECT, {
connection: this,
@ -710,4 +712,8 @@ export class RoomConnection implements RoomConnection {
public getAllTags(): string[] {
return this.tags;
}
public get userRoomToken(): string | undefined {
return this._userRoomToken;
}
}

View File

@ -1165,6 +1165,7 @@ ${escapedMessage}
roomId: this.roomUrl,
tags: this.connection ? this.connection.getAllTags() : [],
variables: this.sharedVariablesManager.variables,
userRoomToken: this.connection ? this.connection.userRoomToken : ''
};
});
this.iframeSubscriptionList.push(

View File

@ -15,11 +15,11 @@ import ui from "./Api/iframe/ui";
import sound from "./Api/iframe/sound";
import room, { setMapURL, setRoomId } from "./Api/iframe/room";
import state, { initVariables } from "./Api/iframe/state";
import player, { setPlayerName, setTags, setUuid } from "./Api/iframe/player";
import player, { setPlayerName, setTags, setUserRoomToken, setUuid } from "./Api/iframe/player";
import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor";
import type { Popup } from "./Api/iframe/Ui/Popup";
import type { Sound } from "./Api/iframe/Sound/Sound";
import { answerPromises, queryWorkadventure, sendToWorkadventure } from "./Api/iframe/IframeApiContribution";
import { answerPromises, queryWorkadventure } from "./Api/iframe/IframeApiContribution";
// Notify WorkAdventure that we are ready to receive data
const initPromise = queryWorkadventure({
@ -32,6 +32,7 @@ const initPromise = queryWorkadventure({
setTags(state.tags);
setUuid(state.uuid);
initVariables(state.variables as Map<string, unknown>);
setUserRoomToken(state.userRoomToken);
});
const wa = {

View File

@ -4,6 +4,7 @@ WA.onInit().then(() => {
console.log('Player name: ', WA.player.name);
console.log('Player id: ', WA.player.id);
console.log('Player tags: ', WA.player.tags);
console.log('Player token: ', WA.player.userRoomToken);
});
WA.room.getTiledMap().then((data) => {

View File

@ -198,6 +198,7 @@ message RoomJoinedMessage {
int32 currentUserId = 4;
repeated string tag = 5;
repeated VariableMessage variable = 6;
string userRoomToken = 7;
}
message WebRtcStartMessage {
@ -297,6 +298,7 @@ message JoinRoomMessage {
string IPAddress = 7;
CompanionMessage companion = 8;
string visitCardUrl = 9;
string userRoomToken = 10;
}
message UserJoinedZoneMessage {

View File

@ -186,6 +186,7 @@ export class IoSocketController {
let memberTags: string[] = [];
let memberVisitCardUrl: string | null = null;
let memberMessages: unknown;
let memberUserRoomToken: string | undefined;
let memberTextures: CharacterTexture[] = [];
const room = await socketManager.getOrCreateRoom(roomId);
let userData: FetchMemberDataByUuidResponse = {
@ -196,6 +197,7 @@ export class IoSocketController {
textures: [],
messages: [],
anonymous: true,
userRoomToken: undefined,
};
if (ADMIN_API_URL) {
try {
@ -232,6 +234,8 @@ export class IoSocketController {
memberTags = userData.tags;
memberVisitCardUrl = userData.visitCardUrl;
memberTextures = userData.textures;
memberUserRoomToken = userData.userRoomToken;
if (
room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY &&
(userData.anonymous === true || !room.canAccess(memberTags))
@ -281,6 +285,7 @@ export class IoSocketController {
messages: memberMessages,
tags: memberTags,
visitCardUrl: memberVisitCardUrl,
userRoomToken: memberUserRoomToken,
textures: memberTextures,
position: {
x: x,

View File

@ -44,4 +44,5 @@ export interface ExSocketInterface extends WebSocket, Identificable {
textures: CharacterTexture[];
backConnection: BackConnection;
listenedZones: Set<Zone>;
userRoomToken: string | undefined;
}

View File

@ -29,6 +29,7 @@ export interface FetchMemberDataByUuidResponse {
textures: CharacterTexture[];
messages: unknown[];
anonymous?: boolean;
userRoomToken: string | undefined;
}
class AdminApi {

View File

@ -151,6 +151,11 @@ export class SocketManager implements ZoneEventListener {
joinRoomMessage.setName(client.name);
joinRoomMessage.setPositionmessage(ProtobufUtils.toPositionMessage(client.position));
joinRoomMessage.setTagList(client.tags);
if (client.userRoomToken) {
joinRoomMessage.setUserroomtoken(client.userRoomToken);
}
if (client.visitCardUrl) {
joinRoomMessage.setVisitcardurl(client.visitCardUrl);
}