Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
feat: add full rpc locatilization for 'PT'
Browse files Browse the repository at this point in the history
  • Loading branch information
renanrcp committed Feb 19, 2023
1 parent 7880309 commit 90f4e42
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 27 deletions.
39 changes: 22 additions & 17 deletions src/discord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Client, register } from "discord-rpc";
import { BrowserWindow, dialog } from "electron";
import { EOL } from "os";
import { CPPS_MAP, DISCORD_RPC_CLIENT_APP_ID, LARGE_IMAGE_KEY, UNLOGGED, WADDLING } from "./discord/constants";
import { CPPS_MAP, DISCORD_RPC_CLIENT_APP_ID, LARGE_IMAGE_KEY } from "./discord/constants";
import { getLocalizedPlaying, getLocalizedTalkingWith, getLocalizedUnlogged, getLocalizedVisiting, getLocalizedWaddling, getLocalizedWaddlingAt } from "./discord/localization/localization";
import { startRequestListener } from "./discord/requestHandler";
import { Store } from "./store";
import { CPLocation, CPLocationType, DiscordState } from "./store/DiscordState";
Expand Down Expand Up @@ -39,50 +40,54 @@ export const setLocationsInStore = (store: Store, locations: CPLocation[]) => {
setDiscordStateInStore(store, state);
};

export const setUnloggedStatus = (state: DiscordState) => {
export const setUnloggedStatus = (store: Store) => {
const state = getDiscordStateFromStore(store);

return state.client.setActivity({
details: state.gameName,
state: UNLOGGED,
state: getLocalizedUnlogged(store),
startTimestamp: state.startTimestamp,
largeImageKey: LARGE_IMAGE_KEY,
});
};

const setWaddlingStatus = (state: DiscordState) => {
const setWaddlingStatus = (store: Store) => {
const state = getDiscordStateFromStore(store);

return state.client.setActivity({
details: state.gameName,
state: WADDLING,
state: getLocalizedWaddling(store),
startTimestamp: state.startTimestamp,
largeImageKey: LARGE_IMAGE_KEY,
});
};

export const setUnknownLocationStatus = (state: DiscordState, match: string) => {
export const setUnknownLocationStatus = (store: Store, state: DiscordState, match: string) => {
const result = match.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);

return state.client.setActivity({
details: state.gameName,
state: `Waddling at ${finalResult}`,
state: `${getLocalizedWaddlingAt(store)} ${finalResult}`,
startTimestamp: state.startTimestamp,
largeImageKey: LARGE_IMAGE_KEY,
});
};

export const setLocationStatus = (state: DiscordState, location: CPLocation) => {
export const setLocationStatus = (store: Store, state: DiscordState, location: CPLocation) => {
let msgPrefix: string;

if (location.name.toLowerCase().includes('sensei')) {
msgPrefix = 'Talking with ';
msgPrefix = getLocalizedTalkingWith(store);
} else if (location.type === CPLocationType.Game) {
msgPrefix = 'Playing ';
msgPrefix = getLocalizedPlaying(store);
} else if (location.name.toLowerCase().includes('igloo')) {
msgPrefix = 'Visiting an ';
msgPrefix = getLocalizedVisiting(store);
} else {
msgPrefix = 'Waddling at ';
msgPrefix = getLocalizedWaddlingAt(store);
}

const locationMsg = msgPrefix + location.name;
const locationMsg = msgPrefix + ' ' + location.name;

return state.client.setActivity({
details: state.gameName,
Expand Down Expand Up @@ -123,7 +128,7 @@ const registerWindowReload = (store: Store, mainWindow: BrowserWindow) => {
// In case URL changed
state.gameName = getGameName(store);

setUnloggedStatus(state);
setUnloggedStatus(store);

setDiscordStateInStore(store, state);
});
Expand Down Expand Up @@ -155,7 +160,7 @@ export const startDiscordRPC = (store: Store, mainWindow: BrowserWindow) => {
client.on('ready', () => {
client.setActivity({
details: gameName,
state: rpcTrackingEnabled ? UNLOGGED : WADDLING,
state: rpcTrackingEnabled ? getLocalizedUnlogged(store) : getLocalizedWaddling(store),
startTimestamp: startTimestamp,
largeImageKey: LARGE_IMAGE_KEY,
});
Expand Down Expand Up @@ -247,12 +252,12 @@ export const enableOrDisableDiscordRPCLocationTracking = async (store: Store, ma
updateDiscordRPCTrackingEnabledInStore(store);

if (!getDiscordRPCTrackingEnabledFromStore(store)) {
setWaddlingStatus(getDiscordStateFromStore(store));
setWaddlingStatus(store);

return;
}

setUnloggedStatus(getDiscordStateFromStore(store));
setUnloggedStatus(store);

mainWindow.reload();
};
2 changes: 0 additions & 2 deletions src/discord/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export const SWF_QUESTS_GAMES_PATH = '/games/quests/';
export const SWF_IGLOO_ROOM_PATH = 'igloo.swf';
export const DISCORD_RPC_CLIENT_APP_ID = '1076298709073145907';
export const LARGE_IMAGE_KEY = 'main-logo';
export const UNLOGGED = 'Unlogged';
export const WADDLING = 'Waddling';
export const CPPS_MAP = new Map([
['newcp.net', 'New Club Penguin'],
['cpbrasil.pw', 'Club Penguin Brasil'],
Expand Down
12 changes: 12 additions & 0 deletions src/discord/localization/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const EN_LOCALIZATION = new Map([
['UNLOGGED', 'Unlogged'],
['WADDLING', 'Waddling'],
['WADDLING_AT', 'Waddling at'],
['TALKING_WITH', 'Talking with'],
['PLAYING', 'Playing'],
['VISITING_IGLOO', 'Visiting an'],
]);

export const getEnLocalizedString = (stringKey: string) => {
return EN_LOCALIZATION.get(stringKey);
};
67 changes: 67 additions & 0 deletions src/discord/localization/localization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Store } from "../../store";
import { getEnLocalizedString } from "./en";
import { getPtLocalizedString } from "./pt";

export const STRING_KEYS = {
UNLOGGED_KEY: 'UNLOGGED',
WADDLING_KEY: 'WADDLING',
WADDLING_AT_KEY: 'WADDLING_AT',
TALKING_WITH_KEY: 'TALKING_WITH',
PLAYING_KEY: 'PLAYING',
VISITING_IGLOO_KEY: 'VISITING_IGLOO',
};

export const SUPPORTED_LANGS = [
'en',
'pt'
];

export const setLanguageInStore = (store: Store, language: string) => {
if (!SUPPORTED_LANGS.includes(language)) {
return;
}

store.public.set('lastLanguage', language);
};

export const getLanguageInStore = (store: Store) => {
return store.public.get('lastLanguage');
};

export const getLocalizedUnlogged = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.UNLOGGED_KEY);
};

export const getLocalizedWaddling = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.WADDLING_KEY);
};

export const getLocalizedWaddlingAt = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.WADDLING_AT_KEY);
};

export const getLocalizedTalkingWith = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.TALKING_WITH_KEY);
};

export const getLocalizedPlaying = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.PLAYING_KEY);
};

export const getLocalizedVisiting = (store: Store) => {
return getLocalizedString(store, STRING_KEYS.VISITING_IGLOO_KEY);
};

export const getLocalizedString = (store: Store, stringKey: string) => {
const lang = getLanguageInStore(store);

switch (lang) {
case 'pt':
return getPtLocalizedString(stringKey);

case 'en':
default:
return getEnLocalizedString(stringKey);
}
};

12 changes: 12 additions & 0 deletions src/discord/localization/pt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const PT_LOCALIZATION = new Map([
['UNLOGGED', 'Deslogado'],
['WADDLING', 'Pinguinando'],
['WADDLING_AT', 'Pinguinando em'],
['TALKING_WITH', 'Falando com o'],
['PLAYING', 'Jogando'],
['VISITING_IGLOO', 'Visitando um'],
]);

export const getPtLocalizedString = (stringKey: string) => {
return PT_LOCALIZATION.get(stringKey);
};
4 changes: 2 additions & 2 deletions src/discord/parsers/locationParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export const parseAndUpdateLocation = async (store: Store, params: any) => {
state.currentLocation = location;

if (!location) {
await setUnknownLocationStatus(state, match);
await setUnknownLocationStatus(store, state, match);
} else {
await setLocationStatus(state, location);
await setLocationStatus(store, state, location);
}

setDiscordStateInStore(store, state);
Expand Down
4 changes: 2 additions & 2 deletions src/discord/parsers/roomParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRoomsJsonFromParams } from "../requestHandler";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindow, params: any) => {
const result = await getRoomsJsonFromParams(mainWindow, params);
const result = await getRoomsJsonFromParams(store, mainWindow, params);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rooms = Object.values(result.roomsJson) as any[];
Expand Down Expand Up @@ -135,7 +135,7 @@ export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindo
if (localizedRooms) {
const localizedName = localizedRooms.filter(localizedRoom => {
return room.display_name === localizedRoom.display_name;
})[0].name;
})[0]?.name;

if (localizedName) {
name = localizedName;
Expand Down
9 changes: 5 additions & 4 deletions src/discord/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ROOMS_JSONP_NAME, ROOMS_PATH, SWF_MIME_FILE } from "./constants";
import { parseAndUpdateLocation } from "./parsers/locationParser";
import { parseAndUpdateRooms } from "./parsers/roomParser";
import fetch from 'electron-fetch';
import { setLanguageInStore } from "./localization/localization";

const parseJSONP = (jsonp: string, name: string) => {
const nameLength = name.length;
Expand All @@ -19,7 +20,7 @@ type RoomsResponse = {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params: any): Promise<RoomsResponse> => {
export const getRoomsJsonFromParams = async (store: Store, mainWindow: BrowserWindow, params: any): Promise<RoomsResponse> => {
let plainResponseBody;
let localizedResponseBody;

Expand Down Expand Up @@ -47,20 +48,20 @@ export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params:

const lang = urlStart.substring(langIndex + 1);

setLanguageInStore(store, lang);

const enUrl = url.replace(lang, 'en');

const enResponse = await fetch(enUrl);

const enResponseBuffer = await enResponse.buffer();

plainResponseBody = enResponseBuffer.toString();


}

return {
roomsJson: parseJSONP(plainResponseBody, ROOMS_JSONP_NAME),
localizedJson: parseJSONP(localizedResponseBody, ROOMS_JSONP_NAME),
localizedJson: localizedResponseBody ? parseJSONP(localizedResponseBody, ROOMS_JSONP_NAME) : undefined,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const defaultPublicValues: PublicSchema = {
disableAds: false,
enableDiscord: false,
enableDiscordTracker: false,
lastLanguage: 'pt',
};

const createPublicStore = () => {
Expand Down
1 change: 1 addition & 0 deletions src/store/PublicSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type PublicSchema = {
disableAds: boolean;
enableDiscord: boolean;
enableDiscordTracker: boolean;
lastLanguage: string;
}

0 comments on commit 90f4e42

Please # to comment.