Files
isaac-port/src/leaderboard.cpp
T

282 lines
11 KiB
C++

#include "leaderboard.h"
#include "fx_layers.h"
#include "game.h"
#include "log.h"
#include "manager.h"
#include "menu_manager.h"
#include "render_passes.h"
#include <algorithm>
constexpr const char* SCORE_MENU_ANM2 = "gfx/ui/ScoreMenu.anm2";
constexpr const char* LEADERBOARD_MENU_ANM2 = "gfx/ui/LeaderboardMenu.anm2";
constexpr const char* ANIMATION_APPEAR = "Appear";
constexpr const char* ANIMATION_LOADING = "Loading";
constexpr const char* ANIMATION_RANK_START = "DisplayRankStart";
constexpr const char* ANIMATION_RANK = "DisplayRank";
constexpr const char* ANIMATION_SCORE_DISAPPEAR = "Dissapear";
constexpr const char* ANIMATION_BOARD_DISAPPEAR = "Dissappear";
constexpr int ROWS_PER_PAGE = 10;
constexpr int GAME_FADE_TARGET_MENU = 2;
constexpr float GAME_FADE_SPEED = 0.08f;
constexpr float BUTTON_HIDDEN = 96;
constexpr float DIM_SHADE = 0.3f;
constexpr float DIM_ALPHA = 0.5f;
// Whether a sprite stopped, or never started.
static bool is_sprite_stopped(const ANM2& sprite)
{
return !sprite.animState.animData || !sprite.animState.isPlaying;
}
// Fades the run out to the menus, as leaving the leaderboard during a run does.
static void game_fade_out()
{
Game& game = *g_game;
KColor black = kcolor_argb(BLACK_ARGB);
game.fadeSpeed = GAME_FADE_SPEED;
game.fadeOutColor = kcolor_make(black._red, black._green, black._blue, game.fadeOutColor._alpha + GAME_FADE_SPEED);
game.fadeOutTarget = GAME_FADE_TARGET_MENU;
}
static bool is_in_run()
{
return g_game && g_manager->state == MANAGER_STATE_GAME;
}
// 00857220 Loads the breakdown and board sheets, hidden, with the boards next.
void Leaderboard::LoadAnm2()
{
scoreSprite.Load(SCORE_MENU_ANM2, GRAPHICS_LOAD);
leaderboardSprite.Load(LEADERBOARD_MENU_ANM2, GRAPHICS_LOAD);
isClosed = false;
mode = LEADERBOARD_BOARDS;
displayState = LEADERBOARD_HIDDEN;
}
// 00857400 The board of the current kind found online, or none.
uint32_t Leaderboard::board_get()
{
if (rankingState >= BOARD_SCORES_SOLO && rankingState < BOARD_KIND_COUNT) return boards[rankingState];
log_printf(LOG_ERROR, "Invalid leaderboard type \n");
return 0;
}
// The furthest a page can start: ten rows before the board's end, or the start without a board.
int Leaderboard::row_offset_max_get()
{
if (!board_get()) return 0;
if (liveSelectionState == SCOPE_FRIENDS) return entryCount - ROWS_PER_PAGE;
reconstruction_missing("Leaderboard::update_input 008576c0: a found board's row count, which is online");
}
// Plays a sheet out and marks the leaderboard closed once it stops.
void Leaderboard::close(ANM2& sprite, std::string_view animation)
{
displayState = LEADERBOARD_CLOSING;
sprite.Play(animation);
isClosed = true;
}
// 00857450 Steps the shown sheet every other frame. The breakdown loads, shows the ranks once both scores were
// submitted, then hands over to the boards as it goes out. The boards load until their rows come. A closed sheet hides
// once it stops. The popup buttons stay hidden meanwhile.
void Leaderboard::Update()
{
if (displayState == LEADERBOARD_HIDDEN || (g_manager->framecount & 1) == 1) return;
ANM2& sprite = mode == LEADERBOARD_BREAKDOWN ? scoreSprite : leaderboardSprite;
sprite.Update();
if (mode == LEADERBOARD_BREAKDOWN)
{
if (displayState == LEADERBOARD_APPEARING && is_sprite_stopped(sprite))
{
sprite.Play(ANIMATION_LOADING);
displayState = LEADERBOARD_LOADING;
}
if (sprite.IsPlaying(ANIMATION_LOADING) && isScoreSubmitted && isTimeSubmitted)
{
sprite.Play(ANIMATION_RANK_START);
displayState = LEADERBOARD_SHOWN;
}
if (sprite.animState.animData && !sprite.animState.isPlaying && sprite.GetAnimation() == ANIMATION_RANK_START)
sprite.Play(ANIMATION_RANK);
if (displayState == LEADERBOARD_CLOSING && is_sprite_stopped(sprite))
{
mode = LEADERBOARD_BOARDS;
displayState = LEADERBOARD_APPEARING;
leaderboardSprite.Play(ANIMATION_APPEAR);
}
}
else
{
bool isAppearing = displayState == LEADERBOARD_APPEARING;
if (isAppearing && is_sprite_stopped(sprite))
{
displayState = LEADERBOARD_LOADING;
sprite.Play(ANIMATION_LOADING);
}
if (!isAppearing || displayState == LEADERBOARD_LOADING)
{
if (displayState == LEADERBOARD_LOADING && entryCount >= 0)
{
displayState = LEADERBOARD_SHOWN;
sprite.Play(ANIMATION_IDLE);
}
if (displayState == LEADERBOARD_CLOSING && is_sprite_stopped(sprite)) displayState = LEADERBOARD_HIDDEN;
}
}
if (isClosed && is_sprite_stopped(sprite))
{
isClosed = false;
displayState = LEADERBOARD_HIDDEN;
}
g_manager->popupButtons.buttons[0].isVisible = false;
g_manager->popupButtons.buttons[1].isVisible = false;
}
// 008576c0 Reads the leaderboard's input. On the breakdown, confirm moves on to the boards once both scores were
// submitted. On the boards, with rows listed, LB changes the kind, RB the scope, left and right the day and up and down
// the page. Back, or confirm on the boards, leaves: out of the run when in one, else the sheet plays out.
void Leaderboard::update_input()
{
if (displayState == LEADERBOARD_HIDDEN) return;
bool isLeaving = is_action(ACTION_MENU_BACK, ACTION_TRIGGERED);
if (mode == LEADERBOARD_BREAKDOWN && isScoreSubmitted && isTimeSubmitted &&
is_action(ACTION_MENU_CONFIRM, ACTION_TRIGGERED))
{
displayState = LEADERBOARD_CLOSING;
scoreSprite.Play(ANIMATION_SCORE_DISAPPEAR);
get_leaderboard(rankingState <= BOARD_TIMES_SOLO ? BOARD_SCORES_SOLO : BOARD_SCORES_COOP, SCOPE_AROUND_PLAYER);
}
if (mode == LEADERBOARD_BOARDS) isLeaving = isLeaving || is_action(ACTION_MENU_CONFIRM, ACTION_TRIGGERED);
if (mode == LEADERBOARD_BOARDS && !isLeaving && entryCount >= 0)
{
if (is_action(ACTION_MENU_LB, ACTION_TRIGGERED))
get_leaderboard((LeaderboardKind)((rankingState + 1) % BOARD_KIND_COUNT), liveSelectionState);
else if (is_action(ACTION_MENU_RB, ACTION_TRIGGERED))
{
constexpr LeaderboardScope NEXT_SCOPE[] = {SCOPE_AROUND_PLAYER, SCOPE_GLOBAL, SCOPE_FRIENDS};
liveSelectionState = NEXT_SCOPE[liveSelectionState];
rowOffset = -1;
download_entries(rankingState, liveSelectionState);
}
else if (is_action(ACTION_MENU_LEFT, ACTION_TRIGGERED) || is_action(ACTION_MENU_RIGHT, ACTION_TRIGGERED))
reconstruction_missing("Leaderboard::update_input 008576c0: stepping the day, which needs DateTime");
else if (is_action(ACTION_MENU_UP, ACTION_TRIGGERED) && rowOffset > 0)
{
rowOffset = std::max(rowOffset - ROWS_PER_PAGE, 0);
download_entries(rankingState, liveSelectionState);
}
else if (is_action(ACTION_MENU_DOWN, ACTION_TRIGGERED) && row_offset_max_get() > rowOffset)
{
rowOffset = std::min(rowOffset + ROWS_PER_PAGE, row_offset_max_get());
download_entries(rankingState, liveSelectionState);
}
return;
}
if (!isLeaving) return;
if (is_in_run()) game_fade_out();
else if (mode == LEADERBOARD_BREAKDOWN) close(scoreSprite, ANIMATION_SCORE_DISAPPEAR);
else close(leaderboardSprite, ANIMATION_BOARD_DISAPPEAR);
}
// 00858050 Dims the screen under the shown sheet, then draws the breakdown or the boards.
void Leaderboard::Render()
{
if (displayState == LEADERBOARD_HIDDEN) return;
Vector screen = vector_make(g_screenWidth, g_screenHeight);
KColor color = kcolor_make(DIM_SHADE, DIM_SHADE, DIM_SHADE, DIM_ALPHA);
render_color_shader_pass(KAGE::Graphics::color_quad_rect(vector_make(0, 0), screen, color));
if (mode == LEADERBOARD_BREAKDOWN) render_daily_score_breakdown();
else render_scores();
}
// 008580e0 Shows a daily run's breakdown for a date and its score sheet, ranks unknown, then looks up the day's score
// board around the player. Without submitting, both scores count as submitted at once.
void Leaderboard::Show(uint32_t date, const ScoreSheet& sheet, bool isSubmitScore, bool isCoop)
{
std::array<PopupButton, POPUP_BUTTON_COUNT>& buttons = g_manager->popupButtons.buttons;
scoreRank = 0;
timeRank = 0;
mode = LEADERBOARD_BREAKDOWN;
displayState = LEADERBOARD_APPEARING;
scoreSprite.Play(ANIMATION_APPEAR);
shownDate = date;
runDate = date;
dailyChallenge.Init(date);
boards.fill(0);
scoreSheet = sheet;
for (PopupButton& button : buttons)
{
button.targetSlideOffset = BUTTON_HIDDEN;
button.slideOffset = BUTTON_HIDDEN;
}
isScoreSubmitted = !isSubmitScore;
isTimeSubmitted = !isSubmitScore;
get_leaderboard(isCoop ? BOARD_SCORES_COOP : BOARD_SCORES_SOLO, SCOPE_AROUND_PLAYER);
}
// 008583e0 Lists a board of a kind and scope from its first page: its rows when the board was found, else it is looked
// up first. The shown boards load meanwhile.
// Offline the lookup fails at once. The original then reads the missing result and crashes.
// Here the board stays unfound.
void Leaderboard::get_leaderboard(LeaderboardKind kind, LeaderboardScope scope)
{
liveSelectionState = scope;
rankingState = kind;
rowOffset = -1;
entryCount = -1;
if (board_get()) download_entries(kind, scope);
if (mode == LEADERBOARD_BOARDS && displayState == LEADERBOARD_SHOWN)
{
displayState = LEADERBOARD_LOADING;
leaderboardSprite.Play(ANIMATION_LOADING);
}
}
// 00858610 Asks for a page of a board's rows: ten from the page start, the friends', or those around the player. The
// shown boards load meanwhile. Without a board found, no rows come.
void Leaderboard::download_entries(LeaderboardKind kind, LeaderboardScope scope)
{
rankingState = kind;
liveSelectionState = scope;
entryCount = -1;
isEntriesDownloaded = false;
if (scope == SCOPE_GLOBAL && rowOffset == -1) rowOffset = 0;
bool isPaged = scope == SCOPE_GLOBAL || (scope == SCOPE_AROUND_PLAYER && rowOffset != -1);
if (board_get()) reconstruction_missing("Leaderboard::download_entries 00858610: a found board's rows, online");
else if (!isPaged)
log_printf(LOG_WARN, "Attempting to download entries from leaderboard that hasn't been downloaded\n");
if (mode == LEADERBOARD_BOARDS && displayState == LEADERBOARD_SHOWN)
{
displayState = LEADERBOARD_LOADING;
leaderboardSprite.Play(ANIMATION_LOADING);
}
}
// 008588f0 Draws the breakdown: the bonuses, penalties and total of the run, its finish time, and the score and time
// ranks once submitted.
void Leaderboard::render_daily_score_breakdown()
{
reconstruction_missing("Leaderboard::render_daily_score_breakdown 008588f0: shown after a daily run, which needs "
"the online boards");
}
// 00859290 Draws the boards: their kind, scope and day, and the page of rows.
void Leaderboard::render_scores()
{
reconstruction_missing("Leaderboard::render_scores 00859290: a found board's rows, which are online");
}