Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display rank information on ranked CSS #391

Draft
wants to merge 22 commits into
base: slippi
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5380a33
initial commit
walz0 Jan 5, 2023
54ac91b
add rank icons to slpCSS
walz0 Jan 5, 2023
211df65
add ratingUpdateCount to RankInfo
walz0 Jul 27, 2023
a6773cf
resolve merge conflicts
walz0 Jul 27, 2023
ead04b6
remove wonky else if chain
walz0 Jul 27, 2023
e7d3c68
add rank icons to slpCSS
walz0 Jul 27, 2023
5670d1c
return unranked as a base case
walz0 Jul 27, 2023
3929bc1
Merge remote-tracking branch 'origin/slippi' into feature/rank_display
walz0 Aug 9, 2023
078b65f
rank notifications wip
walz0 Aug 10, 2023
d2b0575
Merge remote-tracking branch 'origin/slippi' into feature/rank_display
walz0 Aug 10, 2023
1a9acff
Merge branch 'project-slippi:slippi' into feature/rank_display
walz0 Aug 11, 2023
1266f9e
changes to RankInfo struct
walz0 Aug 11, 2023
01dc006
Merge branch 'feature/rank_display' of github.com:walz0/ishiiruka int…
walz0 Aug 11, 2023
2195b87
fix rank change calculation
walz0 Aug 12, 2023
3183361
fix icon scaling and refactor handleGetRank
walz0 Aug 12, 2023
19920e1
change type on rankChange from u8 to int
walz0 Aug 12, 2023
eb87343
convert gql query to multiline string
walz0 Aug 12, 2023
3fc84f8
Merge remote-tracking branch 'origin/slippi' into feature/rank_display
walz0 Aug 14, 2023
f219977
initialize user rank values
walz0 Sep 15, 2023
c4b28c2
Merge remote-tracking branch 'origin/slippi' into feature/rank_display
walz0 Feb 4, 2024
2b3a4a1
Merge branch 'project-slippi:slippi' into feature/rank_display
walz0 May 8, 2024
86550eb
Merge branch 'project-slippi:slippi' into feature/rank_display
walz0 Sep 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Data/Sys/GameFiles/GALE01/slpCSS.dat
Binary file not shown.
32 changes: 31 additions & 1 deletion Source/Core/Core/HW/EXI_DeviceSlippi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define SLEEP_TIME_MS 8
#define WRITE_FILE_SLEEP_TIME_MS 85

// #define LOCAL_TESTING
#define LOCAL_TESTING
// #define CREATE_DIFF_FILES

static std::unordered_map<u8, std::string> slippi_names;
Expand Down Expand Up @@ -3138,6 +3138,33 @@ void CEXISlippi::handleGetPlayerSettings()
m_read_queue.insert(m_read_queue.end(), data_ptr, data_ptr + sizeof(SlippiExiTypes::GetPlayerSettingsResponse));
}

void CEXISlippi::handleGetRank()
{
auto userInfo = user->GetUserInfo();
auto prevRankInfo = user->GetRankInfo();
auto rankInfo = user->FetchUserRank(userInfo.connectCode);

u8 rank = rankInfo.rank;
float ratingOrdinal = rankInfo.ratingOrdinal;
u8 global = rankInfo.globalPlacing;
u8 regional = rankInfo.regionalPlacing;
u8 ratingUpdateCount = rankInfo.ratingUpdateCount;
float ratingChange = rankInfo.ratingChange;
int rankChange = rankInfo.rankChange;

m_read_queue.clear();
m_read_queue.push_back(rank);

appendWordToBuffer(&m_read_queue, *(u32 *)&ratingOrdinal);

m_read_queue.push_back(global);
m_read_queue.push_back(regional);
m_read_queue.push_back(ratingUpdateCount);

appendWordToBuffer(&m_read_queue, *(u32 *)&ratingChange);
appendWordToBuffer(&m_read_queue, *(u32 *)&rankChange);
}

void CEXISlippi::DMAWrite(u32 _uAddr, u32 _uSize)
{
u8 *memPtr = Memory::GetPointer(_uAddr);
Expand Down Expand Up @@ -3310,6 +3337,9 @@ void CEXISlippi::DMAWrite(u32 _uAddr, u32 _uSize)
case CMD_GET_PLAYER_SETTINGS:
handleGetPlayerSettings();
break;
case CMD_GET_RANK:
handleGetRank();
break;
case CMD_PLAY_MUSIC:
{
auto args = SlippiExiTypes::Convert<SlippiExiTypes::PlayMusicQuery>(&memPtr[bufLoc]);
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/Core/HW/EXI_DeviceSlippi.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class CEXISlippi : public IEXIDevice
CMD_GP_FETCH_STEP = 0xC1,
CMD_REPORT_SET_COMPLETE = 0xC2,
CMD_GET_PLAYER_SETTINGS = 0xC3,
CMD_GET_RANK = 0xC4,

// Misc
CMD_LOG_MESSAGE = 0xD0,
Expand Down Expand Up @@ -143,6 +144,7 @@ class CEXISlippi : public IEXIDevice
{CMD_GP_FETCH_STEP, static_cast<u32>(sizeof(SlippiExiTypes::GpFetchStepQuery) - 1)},
{CMD_REPORT_SET_COMPLETE, static_cast<u32>(sizeof(SlippiExiTypes::ReportSetCompletionQuery) - 1)},
{CMD_GET_PLAYER_SETTINGS, 0},
{CMD_GET_RANK, 0},

// Misc
{CMD_LOG_MESSAGE, 0xFFFF}, // Variable size... will only work if by itself
Expand Down Expand Up @@ -222,6 +224,7 @@ class CEXISlippi : public IEXIDevice
void prepareGamePrepOppStep(const SlippiExiTypes::GpFetchStepQuery &query);
void handleCompleteSet(const SlippiExiTypes::ReportSetCompletionQuery &query);
void handleGetPlayerSettings();
void handleGetRank();

// replay playback stuff
void prepareGameInfo(u8 *payload);
Expand Down
198 changes: 198 additions & 0 deletions Source/Core/Core/Slippi/SlippiUser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,208 @@ std::vector<std::string> ConvertChatMessagesFromRust(RustChatMessages *rsMessage
SlippiUser::SlippiUser(uintptr_t rs_exi_device_ptr)
{
slprs_exi_device_ptr = rs_exi_device_ptr;

InitUserRank();
}

SlippiUser::~SlippiUser() {}

void SlippiUser::InitUserRank() {
userRank.rank = SlippiUser::SlippiRank (0);
userRank.ratingOrdinal = 0.0f;
userRank.globalPlacing = 0;
userRank.regionalPlacing = 0;
userRank.ratingUpdateCount = 0;
userRank.ratingChange = 0.0f;
userRank.rankChange = 0;
}

SlippiUser::RankInfo SlippiUser::GetRankInfo() {
return userRank;
}

SlippiUser::SlippiRank SlippiUser::GetRank(float ratingOrdinal, int globalPlacing, int regionalPlacing, int ratingUpdateCount)
{
if (ratingUpdateCount < 5)
return RANK_UNRANKED;
if (ratingOrdinal > 0 && ratingOrdinal <= 765.42)
return RANK_BRONZE_1;
if (ratingOrdinal > 765.43 && ratingOrdinal <= 913.71)
return RANK_BRONZE_2;
if (ratingOrdinal > 913.72 && ratingOrdinal <= 1054.86)
return RANK_BRONZE_3;
if (ratingOrdinal > 1054.87 && ratingOrdinal <= 1188.87)
return RANK_SILVER_1;
if (ratingOrdinal > 1188.88 && ratingOrdinal <= 1315.74)
return RANK_SILVER_2;
if (ratingOrdinal > 1315.75 && ratingOrdinal <= 1435.47)
return RANK_SILVER_3;
if (ratingOrdinal > 1435.48 && ratingOrdinal <= 1548.06)
return RANK_GOLD_1;
if (ratingOrdinal > 1548.07 && ratingOrdinal <= 1653.51)
return RANK_GOLD_2;
if (ratingOrdinal > 1653.52 && ratingOrdinal <= 1751.82)
return RANK_GOLD_3;
if (ratingOrdinal > 1751.83 && ratingOrdinal <= 1842.99)
return RANK_PLATINUM_1;
if (ratingOrdinal > 1843 && ratingOrdinal <= 1927.02)
return RANK_PLATINUM_2;
if (ratingOrdinal > 1927.03 && ratingOrdinal <= 2003.91)
return RANK_PLATINUM_3;
if (ratingOrdinal > 2003.92 && ratingOrdinal <= 2073.66)
return RANK_DIAMOND_1;
if (ratingOrdinal > 2073.67 && ratingOrdinal <= 2136.27)
return RANK_DIAMOND_2;
if (ratingOrdinal > 2136.28 && ratingOrdinal <= 2191.74)
return RANK_DIAMOND_3;
if (ratingOrdinal >= 2191.75 && globalPlacing && regionalPlacing)
return RANK_GRANDMASTER;
if (ratingOrdinal > 2191.75 && ratingOrdinal <= 2274.99)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if ratingOrdinal == 2191.75 but !(globalPlacing && regionalPlacing)?

return RANK_MASTER_1;
if (ratingOrdinal > 2275 && ratingOrdinal <= 2350)
return RANK_MASTER_2;
if (ratingOrdinal > 2350)
return RANK_MASTER_3;
return RANK_UNRANKED;
}

SlippiUser::RankInfo SlippiUser::FetchUserRank(std::string connectCode)
{
RankInfo info;

const char *query =
"fragment profileFields on NetplayProfile {\n"
" id\n"
" ratingOrdinal\n"
" ratingUpdateCount\n"
" wins\n"
" losses\n"
" dailyGlobalPlacement\n"
" dailyRegionalPlacement\n"
" continent\n characters {\n"
" id\n"
" character\n"
" gameCount\n"
" __typename\n"
" }\n"
" __typename\n"
"}\n"
"\n"
"fragment userProfilePage on User {\n"
" fbUid\n"
" displayName\n"
" connectCode {\n"
" code\n"
" __typename\n"
" }\n"
" status\n"
" activeSubscription {\n"
" level\n"
" hasGiftSub\n"
" __typename\n"
" }\n"
" rankedNetplayProfile {\n"
" ...profileFields\n"
" __typename\n"
" }\n"
" netplayProfiles {\n"
" ...profileFields\n"
" season {\n"
" id\n"
" startedAt\n"
" endedAt\n"
" name\n"
" status\n"
" __typename\n"
" }\n"
" __typename\n"
" }\n"
" __typename\n"
"}\n"
"\n"
"query AccountManagementPageQuery($cc: String!, $uid: String!) {\n"
" getUser(fbUid: $uid) {\n"
" ...userProfilePage\n"
" __typename\n"
" }\n"
" getConnectCode(code: $cc) {\n"
" user {\n"
" ...userProfilePage\n"
" __typename\n"
" }\n"
" __typename\n"
" }\n"
"}\n";

std::string url = "https://gql-gateway-dot-slippi.uc.r.appspot.com/graphql";
json body = {{"operationName", "AccountManagementPageQuery"},
{"variables", {{"cc", connectCode}, {"uid", connectCode}}},
{"query", query}};

// INFO_LOG(SLIPPI_ONLINE, "Preparing request...");
// Perform curl request
std::string resp;
curl_easy_setopt(m_curl, CURLOPT_URL, (url).c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &resp);
curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, (body.dump()).c_str());
curl_easy_setopt(m_curl, CURLOPT_CUSTOMREQUEST, "POST");

CURLcode res = curl_easy_perform(m_curl);

// INFO_LOG(SLIPPI_ONLINE, "Request sent");
if (res != 0)
{
ERROR_LOG(SLIPPI, "[User] Error fetching user info from server, code: %d", res);
//return info;
}

long responseCode;
curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &responseCode);
if (responseCode != 200)
{
ERROR_LOG(SLIPPI, "[User] Server responded with non-success status: %d", responseCode);
//return info;
}

auto r = json::parse(resp);
auto rankedObject = r["data"]["getConnectCode"]["user"]["rankedNetplayProfile"];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably make sure json::parse(resp) returned something sane before you start dereferencing everything.


float ratingOrdinal = rankedObject["ratingOrdinal"];
INFO_LOG(SLIPPI_ONLINE, "Rating: %0000.00f", ratingOrdinal);

u8 global = (rankedObject["dailyGlobalPlacement"]).is_null() ? 0 : rankedObject["dailyGlobalPlacement"];
INFO_LOG(SLIPPI_ONLINE, "Global Placing: %d", global);

u8 regional = (rankedObject["dailyRegionalPlacement"]).is_null() ? 0 : rankedObject["dailyRegionalPlacement"];
INFO_LOG(SLIPPI_ONLINE, "Regional Placing: %d", regional);

u8 ratingUpdateCount = (rankedObject["ratingUpdateCount"]).is_null() ? 0 : rankedObject["ratingUpdateCount"];
INFO_LOG(SLIPPI_ONLINE, "Rating Update Count: %d", ratingUpdateCount);

SlippiRank rank = GetRank(ratingOrdinal, global, regional, ratingUpdateCount);
INFO_LOG(SLIPPI_ONLINE, "Rank: %d", rank);

float ratingChange = (userRank.ratingOrdinal > 0.001f) ? ratingOrdinal - userRank.ratingOrdinal : 0;
INFO_LOG(SLIPPI_ONLINE, "Rating Change: %0.1f", ratingChange);

int rankChange = (userRank.rank > 0.001f) ? rank - userRank.rank : 0;
INFO_LOG(SLIPPI_ONLINE, "userRank: %d", userRank.rank);
INFO_LOG(SLIPPI_ONLINE, "Rank Change: %d", rankChange);

info.rank = rank;
info.ratingOrdinal = ratingOrdinal;
info.globalPlacing = global;
info.regionalPlacing = regional;
info.ratingUpdateCount = ratingUpdateCount;
info.ratingChange = ratingChange;
info.rankChange = rankChange;

// Set user rank
userRank = info;

return info;
}

bool SlippiUser::AttemptLogin()
{
return slprs_user_attempt_login(slprs_exi_device_ptr);
Expand Down
55 changes: 55 additions & 0 deletions Source/Core/Core/Slippi/SlippiUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@
class SlippiUser
{
public:
enum SlippiRank
{
RANK_UNRANKED,
RANK_BRONZE_1,
RANK_BRONZE_2,
RANK_BRONZE_3,
RANK_SILVER_1,
RANK_SILVER_2,
RANK_SILVER_3,
RANK_GOLD_1,
RANK_GOLD_2,
RANK_GOLD_3,
RANK_PLATINUM_1,
RANK_PLATINUM_2,
RANK_PLATINUM_3,
RANK_DIAMOND_1,
RANK_DIAMOND_2,
RANK_DIAMOND_3,
RANK_MASTER_1,
RANK_MASTER_2,
RANK_MASTER_3,
RANK_GRANDMASTER,
};

// This type is filled in with data from the Rust side.
// Eventually, this entire class will disappear.
struct UserInfo
Expand All @@ -33,6 +57,28 @@ class SlippiUser
std::vector<std::string> chatMessages;
};

struct RankInfo
{
SlippiRank rank;
float ratingOrdinal;
u8 globalPlacing;
u8 regionalPlacing;
u8 ratingUpdateCount;
float ratingChange;
int rankChange;
};

struct RankInfo
{
SlippiRank rank;
float ratingOrdinal;
u8 globalPlacing;
u8 regionalPlacing;
u8 ratingUpdateCount;
float ratingChange;
int rankChange;
};

SlippiUser(uintptr_t rs_exi_device_ptr);
~SlippiUser();

Expand All @@ -46,6 +92,15 @@ class SlippiUser
std::vector<std::string> GetUserChatMessages();
std::vector<std::string> GetDefaultChatMessages();
bool IsLoggedIn();
void FileListenThread();

RankInfo FetchUserRank(std::string connectCode);
RankInfo GetRankInfo();
void InitUserRank();

SlippiRank GetRank(float ratingOrdinal, int globalPlacing, int regionalPlacing, int ratingUpdateCount);

const static std::vector<std::string> defaultChatMessages;

protected:
// A pointer to a "shadow" EXI Device that lives on the Rust side of things.
Expand Down