Add WA.player.language in the API

This commit is contained in:
Nolway 2021-12-09 01:31:18 +01:00 committed by Alexis Faizeau
parent 77f8426788
commit 8286cdd41d
5 changed files with 31 additions and 2 deletions

View File

@ -5,6 +5,7 @@ export const isGameStateEvent = new tg.IsInterface()
roomId: tg.isString, roomId: tg.isString,
mapUrl: tg.isString, mapUrl: tg.isString,
nickname: tg.isString, nickname: tg.isString,
language: tg.isUnion(tg.isString, tg.isUndefined),
uuid: tg.isUnion(tg.isString, tg.isUndefined), uuid: tg.isUnion(tg.isString, tg.isUndefined),
startLayerName: tg.isUnion(tg.isString, tg.isNull), startLayerName: tg.isUnion(tg.isString, tg.isNull),
tags: tg.isArray(tg.isString), tags: tg.isArray(tg.isString),

View File

@ -13,6 +13,12 @@ export const setPlayerName = (name: string) => {
playerName = name; playerName = name;
}; };
let playerLanguage: string | undefined;
export const setPlayerLanguage = (language: string | undefined) => {
playerLanguage = language;
};
let tags: string[] | undefined; let tags: string[] | undefined;
export const setTags = (_tags: string[]) => { export const setTags = (_tags: string[]) => {
@ -61,6 +67,15 @@ export class WorkadventurePlayerCommands extends IframeApiContribution<Workadven
return playerName; return playerName;
} }
get language(): string {
if (playerLanguage === undefined) {
throw new Error(
"Player language not initialized yet. You should call WA.player.language within a WA.onInit callback."
);
}
return playerLanguage;
}
get tags(): string[] { get tags(): string[] {
if (tags === undefined) { if (tags === undefined) {
throw new Error("Tags not initialized yet. You should call WA.player.tags within a WA.onInit callback."); throw new Error("Tags not initialized yet. You should call WA.player.tags within a WA.onInit callback.");

View File

@ -76,6 +76,7 @@ import { userIsAdminStore } from "../../Stores/GameStore";
import { contactPageStore } from "../../Stores/MenuStore"; import { contactPageStore } from "../../Stores/MenuStore";
import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent"; import type { WasCameraUpdatedEvent } from "../../Api/Events/WasCameraUpdatedEvent";
import { audioManagerFileStore, audioManagerVisibilityStore } from "../../Stores/AudioManagerStore"; import { audioManagerFileStore, audioManagerVisibilityStore } from "../../Stores/AudioManagerStore";
import { Translator } from "../../Translator/Translator";
import EVENT_TYPE = Phaser.Scenes.Events; import EVENT_TYPE = Phaser.Scenes.Events;
import Texture = Phaser.Textures.Texture; import Texture = Phaser.Textures.Texture;
@ -211,6 +212,7 @@ export class GameScene extends DirtyScene {
private loader: Loader; private loader: Loader;
private lastCameraEvent: WasCameraUpdatedEvent | undefined; private lastCameraEvent: WasCameraUpdatedEvent | undefined;
private firstCameraUpdateSent: boolean = false; private firstCameraUpdateSent: boolean = false;
private translator: Translator;
constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) { constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) {
super({ super({
@ -230,6 +232,7 @@ export class GameScene extends DirtyScene {
this.connectionAnswerPromiseResolve = resolve; this.connectionAnswerPromiseResolve = resolve;
}); });
this.loader = new Loader(this); this.loader = new Loader(this);
this.translator = Translator.getInstance();
} }
//hook preload scene //hook preload scene
@ -1321,6 +1324,7 @@ ${escapedMessage}
startLayerName: this.startPositionCalculator.startLayerName, startLayerName: this.startPositionCalculator.startLayerName,
uuid: localUserStore.getLocalUser()?.uuid, uuid: localUserStore.getLocalUser()?.uuid,
nickname: this.playerName, nickname: this.playerName,
language: Translator.getStringByLanguage(this.translator.getCurrentLanguage()),
roomId: this.roomUrl, roomId: this.roomUrl,
tags: this.connection ? this.connection.getAllTags() : [], tags: this.connection ? this.connection.getAllTags() : [],
variables: this.sharedVariablesManager.variables, variables: this.sharedVariablesManager.variables,

View File

@ -11,7 +11,7 @@ type LanguageObject = {
}; };
type TranslationParams = { type TranslationParams = {
[key: string]: string | number [key: string]: string | number;
}; };
class Translator { class Translator {
@ -122,6 +122,14 @@ class Translator {
return languageFound; return languageFound;
} }
/**
* Get the current language
* @returns {Language} Current language
*/
public getCurrentLanguage(): Language {
return this.currentLanguage;
}
/** /**
* Define the current language by the navigator or a cookie * Define the current language by the navigator or a cookie
*/ */

View File

@ -15,7 +15,7 @@ import ui from "./Api/iframe/ui";
import sound from "./Api/iframe/sound"; import sound from "./Api/iframe/sound";
import room, { setMapURL, setRoomId } from "./Api/iframe/room"; import room, { setMapURL, setRoomId } from "./Api/iframe/room";
import { createState } from "./Api/iframe/state"; import { createState } from "./Api/iframe/state";
import player, { setPlayerName, setTags, setUserRoomToken, setUuid } from "./Api/iframe/player"; import player, { setPlayerName, setPlayerLanguage, setTags, setUserRoomToken, setUuid } from "./Api/iframe/player";
import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor"; import type { ButtonDescriptor } from "./Api/iframe/Ui/ButtonDescriptor";
import type { Popup } from "./Api/iframe/Ui/Popup"; import type { Popup } from "./Api/iframe/Ui/Popup";
import type { Sound } from "./Api/iframe/Sound/Sound"; import type { Sound } from "./Api/iframe/Sound/Sound";
@ -30,6 +30,7 @@ const initPromise = queryWorkadventure({
data: undefined, data: undefined,
}).then((gameState) => { }).then((gameState) => {
setPlayerName(gameState.nickname); setPlayerName(gameState.nickname);
setPlayerLanguage(gameState.language);
setRoomId(gameState.roomId); setRoomId(gameState.roomId);
setMapURL(gameState.mapUrl); setMapURL(gameState.mapUrl);
setTags(gameState.tags); setTags(gameState.tags);