Skip to content

Commit

Permalink
roomcodes, player account names in lobby, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SheridanR committed Jul 20, 2022
1 parent e60b036 commit 4002422
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 87 deletions.
31 changes: 27 additions & 4 deletions src/eos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "interface/ui.hpp"
#include "lobbies.hpp"
#include "prng.hpp"
#include "ui/MainMenu.hpp"

EOSFuncs EOS;

Expand Down Expand Up @@ -400,14 +401,16 @@ void EOS_CALL EOSFuncs::OnCreateLobbyFinished(const EOS_Lobby_CreateLobbyCallbac
EOSFuncs::logError("OnCreateLobbyFinished: null data");
return;
}
else if ( data->ResultCode == EOS_EResult::EOS_Success )

EOS.CurrentLobbyData.LobbyCreationResult = data->ResultCode;
if ( data->ResultCode == EOS_EResult::EOS_Success )
{
EOS.CurrentLobbyData.LobbyId = data->LobbyId;

EOS.CurrentLobbyData.LobbyAttributes.lobbyName = EOS.CurrentUserInfo.Name + "'s lobby";
strncpy(EOS.currentLobbyName, EOS.CurrentLobbyData.LobbyAttributes.lobbyName.c_str(), 31);

Uint32 keygen = local_rng.rand() % (1679615 + 1); // limit of 'zzzz' as base-36 string
Uint32 keygen = local_rng.uniform(0, (36 * 36 * 36 * 36) - 1); // limit of 'zzzz' as base-36 string
EOS.CurrentLobbyData.LobbyAttributes.gameJoinKey = EOS.getLobbyCodeFromGameKey(keygen);
std::chrono::system_clock::duration epochDuration = std::chrono::system_clock::now().time_since_epoch();
EOS.CurrentLobbyData.LobbyAttributes.lobbyCreationTime = std::chrono::duration_cast<std::chrono::seconds>(epochDuration).count();
Expand Down Expand Up @@ -895,19 +898,28 @@ void EOS_CALL EOSFuncs::OnMemberStatusReceived(const EOS_Lobby_LobbyMemberStatus
{
if ( data->CurrentStatus == EOS_ELobbyMemberStatus::EOS_LMS_CLOSED
|| (data->CurrentStatus == EOS_ELobbyMemberStatus::EOS_LMS_KICKED
&& (data->TargetUserId == EOS.CurrentUserInfo.getProductUserIdHandle()))
&& data->TargetUserId == EOS.CurrentUserInfo.getProductUserIdHandle())
)
{
// if lobby closed or we got kicked, then clear data.
LobbyLeaveCleanup(EOS.CurrentLobbyData);
switch (data->CurrentStatus) {
case EOS_ELobbyMemberStatus::EOS_LMS_CLOSED:
//MainMenu::disconnectedFromServer("The host has shutdown the lobby.");
break;
case EOS_ELobbyMemberStatus::EOS_LMS_KICKED:
//MainMenu::disconnectedFromServer("You have been kicked\nfrom the online lobby.");
break;
default:
break;
}
}
else
{
EOS.CurrentLobbyData.updateLobby();
EOSFuncs::logInfo("OnMemberStatusReceived: received user: %s, event: %d, updating lobby",
EOSFuncs::Helpers_t::shortProductIdToString(data->TargetUserId).c_str(),
static_cast<int>(data->CurrentStatus));
return;

This comment has been minimized.

Copy link
@WALLOFJUSTICE

WALLOFJUSTICE Jul 20, 2022

Collaborator

re-add this return

}
}
break;
Expand Down Expand Up @@ -1817,6 +1829,17 @@ void EOSFuncs::searchLobbies(LobbyParameters_t::LobbySearchOptions searchType,
{
LobbySearchResults.lastResultWasFiltered = false;

if ( LobbySearchResults.useLobbyCode )
{
for ( int c = 0; c < 4 && EOS.lobbySearchByCode[c] != 0; ++c )
{
if ( EOS.lobbySearchByCode[c] >= 'A' && EOS.lobbySearchByCode[c] <= 'Z' )
{
EOS.lobbySearchByCode[c] = 'a' + (EOS.lobbySearchByCode[c] - 'A'); // to lowercase.
}
}
}

LobbyHandle = EOS_Platform_GetLobbyInterface(PlatformHandle);
logInfo("searchLobbies: starting search");
EOS_Lobby_CreateLobbySearchOptions CreateSearchOptions = {};
Expand Down
1 change: 1 addition & 0 deletions src/eos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class EOSFuncs
bool bLobbyHasBasicDetailsRead = false;
bool bAwaitingLeaveCallback = false;
bool bAwaitingCreationCallback = false;
EOS_EResult LobbyCreationResult = EOS_EResult::EOS_Success;
bool bDenyLobbyJoinEvent = false;

class PlayerLobbyData_t {
Expand Down
16 changes: 9 additions & 7 deletions src/lobbies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ std::string LobbyHandler_t::getLobbyJoinFailedConnectString(int result)
snprintf(buf, 1023, "Failed to join lobby:\n\nLobby is full.");
break;
#ifdef USE_EOS
#ifdef STEAMWORKS
case static_cast<int>(EOS_EResult::EOS_InvalidUser):
snprintf(buf, 1023, "Failed to join lobby:\n\nCrossplay not enabled.");
break;
#else
case static_cast<int>(EOS_EResult::EOS_InvalidUser):
snprintf(buf, 1023, "Failed to join lobby:\n\nNot connected to Epic Online.");
break;
#endif
case static_cast<int>(EOS_EResult::EOS_NotFound):
snprintf(buf, 1023, "Failed to join lobby:\n\nLobby no longer exists.");
break;
Expand Down Expand Up @@ -713,13 +722,6 @@ void LobbyHandler_t::searchLobbyWithFilter(button_t* my)
{
#ifdef USE_EOS
EOS.LobbySearchResults.showLobbiesInProgress = LobbyHandler.filterShowInProgressLobbies;
for ( int c = 0; c < 4 && EOS.lobbySearchByCode[c] != 0; ++c )
{
if ( EOS.lobbySearchByCode[c] >= 'A' && EOS.lobbySearchByCode[c] <= 'Z' )
{
EOS.lobbySearchByCode[c] = 'a' + (EOS.lobbySearchByCode[c] - 'A'); // to lowercase.
}
}

if ( strcmp(EOS.lobbySearchByCode, "") != 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11278,7 +11278,7 @@ void openSteamLobbyWaitWindow(button_t* my)
#ifdef STEAMWORKS
//c_SteamMatchmaking_RequestLobbyList();
//SteamMatchmaking()->RequestLobbyList(); //TODO: Is this sufficient for it to work?
cpp_SteamMatchmaking_RequestLobbyList();
//cpp_SteamMatchmaking_RequestLobbyList();
#endif

LobbyHandler.selectedLobbyInList = 0;
Expand Down
54 changes: 54 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ui/GameUI.hpp"
#include "ui/Frame.hpp"
#include "ui/Slider.hpp"
#include "lobbies.hpp"

#ifdef NINTENDO
#include "nintendo/baronynx.hpp"
Expand Down Expand Up @@ -5537,3 +5538,56 @@ void Player::clearGUIPointers()
}
skillSheetEntryFrames[playernum].legendFrame = nullptr;
}

const char* Player::getAccountName() const {
if (directConnect) {
switch (playernum) {
case 0: return "Player 1";
case 1: return "Player 2";
case 2: return "Player 3";
case 3: return "Player 4";
default: return "Player X";
}
} else {
if (LobbyHandler.getP2PType() == LobbyHandler_t::LobbyServiceType::LOBBY_STEAM) {
#ifdef STEAMWORKS
if (isLocalPlayer()) {
return SteamFriends()->GetPersonaName();
} else {
for (int remoteIDIndex = 0; remoteIDIndex < MAXPLAYERS; ++remoteIDIndex) {
if (steamIDRemote[remoteIDIndex]) {
const char* memberNumChar = SteamMatchmaking()->GetLobbyMemberData(
*static_cast<CSteamID*>(currentLobby),
*static_cast<CSteamID*>(steamIDRemote[remoteIDIndex]),
"clientnum");
if (memberNumChar) {
std::string str = memberNumChar;
if (!str.empty()) {
int memberNum = std::stoi(str);
if (memberNum >= 0 && memberNum < MAXPLAYERS && memberNum == playernum) {
return SteamFriends()->GetFriendPersonaName(
*static_cast<CSteamID*>(steamIDRemote[remoteIDIndex]));
}
}
}
}
}
}
#endif
}
else if (LobbyHandler.getP2PType() == LobbyHandler_t::LobbyServiceType::LOBBY_CROSSPLAY) {
#if defined USE_EOS
if (isLocalPlayer()) {
return EOS.CurrentUserInfo.Name.c_str();
} else {
for (auto& player : EOS.CurrentLobbyData.playersInLobby) {
if (player.clientNumber == playernum) {
return player.name.c_str();
}
}
}
#endif
}
}
return "Unknown";
}
2 changes: 2 additions & 0 deletions src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ class Player
void init();
void cleanUpOnEntityRemoval();

const char* getAccountName() const;

view_t& camera() const { return *cam; }
const int camera_x1() const { return cam->winx; }
const int camera_x2() const { return cam->winx + cam->winw; }
Expand Down
54 changes: 47 additions & 7 deletions src/steam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#ifdef STEAMWORKS

static std::string roomkey_cached;
Uint32 numSteamLobbies = 0;
int selectedSteamLobby = 0;
char lobbyText[MAX_STEAM_LOBBIES][64];
Expand Down Expand Up @@ -679,12 +680,19 @@ SteamAPICall_t cpp_SteamMatchmaking_RequestAppTicket()
return m_SteamCallResultEncryptedAppTicket;
}

SteamAPICall_t cpp_SteamMatchmaking_RequestLobbyList()
SteamAPICall_t cpp_SteamMatchmaking_RequestLobbyList(const char* roomkey)
{
if (roomkey) {
SteamMatchmaking()->AddRequestLobbyListStringFilter("roomkey", roomkey, ELobbyComparison::k_ELobbyComparisonEqual);
roomkey_cached = roomkey;
} else {
roomkey_cached = "";
}
SteamMatchmaking()->AddRequestLobbyListDistanceFilter(ELobbyDistanceFilter::k_ELobbyDistanceFilterWorldwide);
SteamMatchmaking()->AddRequestLobbyListNearValueFilter("lobbyCreationTime", SteamUtils()->GetServerRealTime());
SteamMatchmaking()->AddRequestLobbyListNumericalFilter("lobbyModifiedTime",
SteamUtils()->GetServerRealTime() - 8, k_ELobbyComparisonEqualToOrGreaterThan);
auto realtime = SteamUtils()->GetServerRealTime();
SteamMatchmaking()->AddRequestLobbyListNumericalFilter("lobbyModifiedTime",
realtime - 8, k_ELobbyComparisonEqualToOrGreaterThan);
SteamAPICall_t m_SteamCallResultLobbyMatchList = SteamMatchmaking()->RequestLobbyList();
steam_server_client_wrapper->m_SteamCallResultLobbyMatchList_Set(m_SteamCallResultLobbyMatchList);
return m_SteamCallResultLobbyMatchList;
Expand Down Expand Up @@ -1397,6 +1405,7 @@ void steam_OnLobbyMatchListCallback( void* pCallback, bool bIOFailure )
{
// we had a Steam I/O failure - we probably timed out talking to the Steam back-end servers
// doesn't matter in this case, we can just act if no lobbies were received
printlog("steam_OnLobbyMatchListCallback() failed - are we disconnected from steam?");
}

// lobbies are returned in order of closeness to the user, so add them to the list in that order
Expand All @@ -1409,10 +1418,12 @@ void steam_OnLobbyMatchListCallback( void* pCallback, bool bIOFailure )
lobbyIDs[iLobby] = steamIDLobby;

// pull some info from the lobby metadata (name, players, etc)
const char* lobbyName = SteamMatchmaking()->GetLobbyData(*static_cast<CSteamID*>(steamIDLobby), "name"); //TODO: Again with the void pointers.
const char* lobbyVersion = SteamMatchmaking()->GetLobbyData(*static_cast<CSteamID*>(steamIDLobby), "ver"); //TODO: VOID.
int numPlayers = SteamMatchmaking()->GetNumLobbyMembers(*static_cast<CSteamID*>(steamIDLobby)); //TODO MORE VOID POINTERS.
const char* lobbyNumMods = SteamMatchmaking()->GetLobbyData(*static_cast<CSteamID*>(steamIDLobby), "svNumMods"); //TODO: VOID.
auto lobby = *static_cast<CSteamID*>(steamIDLobby);
const char* lobbyTime = SteamMatchmaking()->GetLobbyData(lobby, "lobbyModifiedTime");
const char* lobbyName = SteamMatchmaking()->GetLobbyData(lobby, "name");
const char* lobbyVersion = SteamMatchmaking()->GetLobbyData(lobby, "ver");
const int numPlayers = SteamMatchmaking()->GetNumLobbyMembers(lobby);
const char* lobbyNumMods = SteamMatchmaking()->GetLobbyData(lobby, "svNumMods");
int numMods = atoi(lobbyNumMods);
string versionText = lobbyVersion;
if ( versionText == "" )
Expand Down Expand Up @@ -1455,6 +1466,13 @@ void steam_OnLobbyMatchListCallback( void* pCallback, bool bIOFailure )
lobbyPlayers[iLobby] = 0;
}
}
if (!roomkey_cached.empty()) {
roomkey_cached = "";
if (numSteamLobbies) {
multiplayer = SINGLE;
MainMenu::receivedInvite(lobbyIDs[0]);
}
}
}

//Helper func. //TODO: Bugger it!
Expand Down Expand Up @@ -1532,6 +1550,22 @@ void* cpp_LobbyCreated_Lobby(void* pCallback)
return id;
}

static std::string generateRoomKey(Uint32 key)
{
const char allChars[37] = "0123456789abcdefghijklmnopqrstuvwxyz";
std::string code = "";
while ( key != 0 )
{
code += (allChars[key % 36]);
key /= 36;
}
while ( code.size() < 4 )
{
code += '0';
}
return code;
}

void steam_OnLobbyCreated( void* pCallback, bool bIOFailure )
{
#ifdef STEAMDEBUG
Expand All @@ -1555,6 +1589,12 @@ void steam_OnLobbyCreated( void* pCallback, bool bIOFailure )
// set the game version of the lobby
SteamMatchmaking()->SetLobbyData(*lobby, "ver", VERSION);

// set room key
Uint32 keygen = local_rng.uniform(0, (36 * 36 * 36 * 36) - 1); // limit of 'zzzz' as base-36 string
auto key = generateRoomKey(keygen);
SteamMatchmaking()->SetLobbyData(*lobby, "roomkey", key.c_str());
printlog("Steam room key is: %s", key.c_str());

// set lobby server flags
char svFlagsChar[16];
snprintf(svFlagsChar, 15, "%d", svFlags);
Expand Down
2 changes: 1 addition & 1 deletion src/steam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern int connectingToLobbyStatus;
//These are all an utter bodge.
//They should not exist, but potato.
//TODO: Remove all of these wrappers and access the steam stuff directly.
SteamAPICall_t cpp_SteamMatchmaking_RequestLobbyList();
SteamAPICall_t cpp_SteamMatchmaking_RequestLobbyList(const char* roomkey);
SteamAPICall_t cpp_SteamMatchmaking_JoinLobby(CSteamID steamIDLobby);
SteamAPICall_t cpp_SteamMatchmaking_CreateLobby(ELobbyType eLobbyType, int cMaxMembers);
SteamAPICall_t cpp_SteamMatchmaking_RequestAppTicket();
Expand Down
Loading

0 comments on commit 4002422

Please sign in to comment.