Added experimental 'Shared Favorite Songs' option.

This commit is contained in:
Thome Valentin
2022-05-16 06:47:59 +02:00
parent 90651282c0
commit 4a8e0707f0
8 changed files with 145 additions and 10 deletions
@@ -0,0 +1,93 @@
import { Extra } from "../models/extra";
import { FavoriteMusic } from "../models/favoritemusic";
import { isSharedFavoriteMusicEnabled } from "../utils";
import Logger from "../utils/logger";
const logger = new Logger("FavoriteMusic");
/**
* Extracts favorite music data from the given extra data container, and saves it to the database as shared favorite music data for the player with the given refid.
* This function has no effect if the 'Shared favorite music' option is not enabled.
* Note that shared favorite music is shared across both Guitar Freaks and Drummania, as well as all supported versions of the game.
* @param refid The refid of the player.
* @param extra The extra data container of the player, containing the favorite music lists to be saved.
* @returns {boolean} - whether the favorite music data was successfully saved.
*/
export async function saveSharedFavoriteMusicFromExtra(refid: string, extra: Extra) : Promise<boolean>
{
if (!isSharedFavoriteMusicEnabled()) {
return false
}
let result : FavoriteMusic = {
collection: 'favoritemusic',
pluginVer: 1,
list_1: extra.list_1,
list_2: extra.list_2,
list_3: extra.list_3,
recommend_musicid_list: extra.recommend_musicid_list,
}
try
{
await saveFavoriteMusic(refid, result)
logger.debugInfo(`Saved shared favorite music for profile ${refid} successfully.`);
return true
}
catch (e)
{
logger.error(`Failed to save shared favorite music for profile ${refid}.`);
logger.error(e);
return false
}
}
/**
* Retrieves shared favorite music data from the database for the player with the given refid, and applies the data to the provided extra data container.
* This function has no effect if the 'Shared favorite music' option is not enabled.
* Note that shared favorite music is shared across both Guitar Freaks and Drummania, as well as all supported versions of the game.
* @param refid - The refid of the player.
* @param extra - The destination object where favorite music data should be applied.
*/
export async function applySharedFavoriteMusicToExtra(refid : string, extra : Extra) : Promise<void>
{
if (!isSharedFavoriteMusicEnabled()) {
return
}
try
{
let favoriteMusic = await loadFavoriteMusic(refid)
if (favoriteMusic == null) {
logger.debugInfo(`No shared favourite music available for profile ${refid}. Using game specific favorites. Favorites will be saved as shared favorites at the end of the game session.`);
return
}
extra.list_1 = favoriteMusic.list_1
extra.list_2 = favoriteMusic.list_2
extra.list_3 = favoriteMusic.list_3
extra.recommend_musicid_list = favoriteMusic.recommend_musicid_list
logger.debugInfo(`Loaded shared favorite music for profile ${refid} successfully.`);
}
catch (e)
{
logger.error(`Failed to load shared favorite music for profile ${refid}.`);
logger.error(e);
}
}
export async function saveFavoriteMusic(refid: string, data : FavoriteMusic) : Promise<any>
{
return await DB.Upsert<FavoriteMusic>(refid, {
collection: 'favoritemusic',
}, data)
}
export async function loadFavoriteMusic(refid : string) : Promise<FavoriteMusic>
{
return await DB.FindOne<FavoriteMusic>(refid, {
collection: 'favoritemusic'
})
}
+7 -2
View File
@@ -11,6 +11,7 @@ import { PLUGIN_VER } from "../const";
import Logger from "../utils/logger"
import { isAsphyxiaDebugMode } from "../Utils/index";
import { SecretMusicEntry } from "../models/secretmusicentry";
import { applySharedFavoriteMusicToExtra, saveSharedFavoriteMusicFromExtra } from "./FavoriteMusic";
const logger = new Logger("profiles")
@@ -92,6 +93,8 @@ export const getPlayer: EPR = async (info, data, send) => {
const profile = dm ? dmProfile : gfProfile;
const extra = dm ? dmExtra : gfExtra;
await applySharedFavoriteMusicToExtra(refid, extra)
const record: any = {
gf: {},
dm: {},
@@ -820,6 +823,7 @@ export const savePlayers: EPR = async (info, data, send) => {
}
catch (e) {
logger.error(e)
logger.error(e.stack)
return send.deny();
}
};
@@ -831,7 +835,7 @@ async function saveSinglePlayer(dataplayer: KDataReader, refid: string, no: numb
const extra = await getExtra(refid, version, game) as any;
const rec = await getRecord(refid, version, game) as any;
const autoSet = (field: keyof Profile, path: string, array = false): void => {
const autoSet = function (field: keyof Profile, path: string, array = false): void {
if (array) {
profile[field] = dataplayer.numbers(path, profile[field])
} else {
@@ -964,10 +968,11 @@ async function saveSinglePlayer(dataplayer: KDataReader, refid: string, no: numb
await DB.Upsert(refid, { collection: 'extra', game, version }, extra)
const playedStages = dataplayer.elements('stage');
// logStagesPlayed(playedStages)
logStagesPlayed(playedStages)
const scores = await updatePlayerScoreCollection(refid, playedStages, version, game)
await saveScore(refid, version, game, scores);
await saveSharedFavoriteMusicFromExtra(refid, extra)
}
async function updatePlayerScoreCollection(refid, playedStages, version, game) {