Skip to content

Commit

Permalink
ETTP song starting/selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Mar 27, 2018
1 parent 097dddf commit 3a32cd6
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Themes/_fallback/metrics.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4245,6 +4245,7 @@ Class="ScreenNetSelectMusic"
Fallback="ScreenNetSelectBase"
PrevScreen="ScreenNetRoom"
NextScreen="ScreenStageInformation"
DisconnectScreen="ScreenSelectMusic"
NoSongsScreen=Branch.TitleMenu()
RoomSelectScreen="ScreenNetRoom"
PlayerOptionsScreen="ScreenPlayerOptions"
Expand Down Expand Up @@ -4304,6 +4305,7 @@ Fallback="ScreenNetSelectBase"
PrevScreen=Branch.TitleMenu()
# XXX
NextScreen="ScreenGameplayBranch"
DisconnectScreen="ScreenTitleMenu"
MusicSelectScreen="ScreenNetSelectMusic"

RoomWheelX=SCREEN_CENTER_X+160
Expand Down
166 changes: 140 additions & 26 deletions src/NetworkSyncManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "ScreenNetSelectMusic.h"
#include "LocalizedString.h"
#include "JsonUtil.h"
#include "GameState.h"
#include "Style.h"
#include <cerrno>
#include <chrono>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -225,6 +227,7 @@ bool ETTProtocol::Connect(NetworkSyncManager * n, unsigned short port, RString a
this->connected = false;
this->ws = nullptr;
n->CloseConnection();
SCREENMAN->SendMessageToTopScreen(ETTP_Disconnect);
});
uWSh.onConnection([this, address](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
this->connected = true;
Expand Down Expand Up @@ -263,6 +266,8 @@ bool ETTProtocol::Connect(NetworkSyncManager * n, unsigned short port, RString a
if (difftime(time(0), start) > 2)
break;
}
if(connected)
n->isSMOnline = true;
return connected;
}
RoomData jsonToRoom(json& room)
Expand Down Expand Up @@ -309,9 +314,63 @@ void ETTProtocol::Update(NetworkSyncManager* n, float fDeltaTime)
break;
case ettps_selectchart:
{
n->chartkey = (*it).value("chartkey", "");
n->rate = (*it).value("rate", 0);
n->song = SONGMAN->GetSongByChartkey(n->chartkey);
n->song = nullptr;
n->steps = nullptr;
auto ch = (*it).at("chart");
n->rate = ch.value("rate", 0);
n->chartkey = ch.value("chartkey", "");
n->m_sFileHash = ch.value("filehash", "");
n->m_sMainTitle = ch.value("title", "");
n->m_sSubTitle = ch.value("subtitle", "");
n->m_sArtist = ch.value("artist", "");
n->difficulty = StringToDifficulty(ch.value("difficulty", "Invalid"));
n->meter = ch.value("meter", -1);
StepsType st = GAMESTATE->GetCurrentStyle(PLAYER_1)->m_StepsType;
if (!n->chartkey.empty()) {
auto song = SONGMAN->GetSongByChartkey(n->chartkey);
if ((n->m_sArtist.empty() || n->m_sArtist == song->GetTranslitArtist()) &&
(n->m_sMainTitle.empty() || n->m_sMainTitle == song->GetTranslitMainTitle()) &&
(n->m_sSubTitle.empty() || n->m_sSubTitle == song->GetTranslitSubTitle()) &&
(n->m_sFileHash.empty() || n->m_sFileHash == song->GetFileHash()))
{
for (auto& steps : song->GetStepsByStepsType(st)) {
if ((n->meter == -1 || n->meter == steps->GetMeter()) &&
(n->difficulty == Difficulty_Invalid || n->difficulty == steps->GetDifficulty()) &&
(n->chartkey == steps->GetChartKey())) {
n->song = song;
n->steps = steps;
break;
}
if (n->song != nullptr)
break;
}
}
}
else {
vector <Song *> AllSongs = SONGMAN->GetAllSongs();
for (int i = 0; i < AllSongs.size(); i++)
{
auto& m_cSong = AllSongs[i];
if ((n->m_sArtist.empty() || n->m_sArtist == m_cSong->GetTranslitArtist()) &&
(n->m_sMainTitle.empty() || n->m_sMainTitle == m_cSong->GetTranslitMainTitle()) &&
(n->m_sSubTitle.empty() || n->m_sSubTitle == m_cSong->GetTranslitSubTitle()) &&
(n->m_sFileHash.empty() || n->m_sFileHash == m_cSong->GetFileHash()))
{
for (auto& steps : m_cSong->GetStepsByStepsType(st)) {
if ((n->meter == -1 || n->meter == steps->GetMeter()) &&
(n->difficulty == Difficulty_Invalid || n->difficulty == steps->GetDifficulty())) {
n->song = m_cSong;
n->steps = steps;
break;
}
}
if (n->song != nullptr)
break;
n->song = m_cSong;
break;
}
}
}
json j;
if (n->song != nullptr) {
SCREENMAN->SendMessageToTopScreen(ETTP_SelectChart);
Expand All @@ -325,9 +384,63 @@ void ETTProtocol::Update(NetworkSyncManager* n, float fDeltaTime)
break;
case ettps_startchart:
{
n->chartkey = (*it).value("chartkey", "");
n->rate = (*it).value("rate", 0);
n->song = SONGMAN->GetSongByChartkey(n->chartkey);
n->song = nullptr;
n->steps = nullptr;
auto ch = (*it).at("chart");
n->rate = ch.value("rate", 0);
n->chartkey = ch.value("chartkey", "");
n->m_sFileHash = ch.value("filehash", "");
n->m_sMainTitle = ch.value("title", "");
n->m_sSubTitle = ch.value("subtitle", "");
n->m_sArtist = ch.value("artist", "");
n->difficulty = StringToDifficulty(ch.value("difficulty", "Invalid"));
n->meter = ch.value("meter", -1);
StepsType st = GAMESTATE->GetCurrentStyle(PLAYER_1)->m_StepsType;
if (!n->chartkey.empty()) {
auto song = SONGMAN->GetSongByChartkey(n->chartkey);
if ((n->m_sArtist.empty() || n->m_sArtist == song->GetTranslitArtist()) &&
(n->m_sMainTitle.empty() || n->m_sMainTitle == song->GetTranslitMainTitle()) &&
(n->m_sSubTitle.empty() || n->m_sSubTitle == song->GetTranslitSubTitle()) &&
(n->m_sFileHash.empty() || n->m_sFileHash == song->GetFileHash()))
{
for (auto& steps : song->GetStepsByStepsType(st)) {
if ((n->meter == -1 || n->meter == steps->GetMeter()) &&
(n->difficulty == Difficulty_Invalid || n->difficulty == steps->GetDifficulty()) &&
(n->chartkey == steps->GetChartKey())) {
n->song = song;
n->steps = steps;
break;
}
if (n->song != nullptr)
break;
}
}
}
else {
vector <Song *> AllSongs = SONGMAN->GetAllSongs();
for (int i = 0; i < AllSongs.size(); i++)
{
auto& m_cSong = AllSongs[i];
if ((n->m_sArtist.empty() || n->m_sArtist == m_cSong->GetTranslitArtist()) &&
(n->m_sMainTitle.empty() || n->m_sMainTitle == m_cSong->GetTranslitMainTitle()) &&
(n->m_sSubTitle.empty() || n->m_sSubTitle == m_cSong->GetTranslitSubTitle()) &&
(n->m_sFileHash.empty() || n->m_sFileHash == m_cSong->GetFileHash()))
{
for (auto& steps : m_cSong->GetStepsByStepsType(st)) {
if ((n->meter == -1 || n->meter == steps->GetMeter()) &&
(n->difficulty == Difficulty_Invalid || n->difficulty == steps->GetDifficulty())) {
n->song = m_cSong;
n->steps = steps;
break;
}
}
if (n->song != nullptr)
break;
n->song = m_cSong;
break;
}
}
}
json j;
if (n->song != nullptr) {
SCREENMAN->SendMessageToTopScreen(ETTP_StartChart);
Expand All @@ -351,13 +464,7 @@ void ETTProtocol::Update(NetworkSyncManager* n, float fDeltaTime)
msg.SetParam("type", type);
MESSAGEMAN->Broadcast(msg);
//Should end here
auto& strings = n->chat[{"a", 0}];
const char* const delim = "\n";
std::ostringstream imploded;
std::copy(strings.begin(), strings.end(),
std::ostream_iterator<std::string>(imploded, delim));
n->m_sChatText = imploded.str();
n->m_sChatText = n->m_sChatText.substr(0, n->m_sChatText.length()-1);
n->m_sChatText = (n->m_sChatText + "\n" + (*it)["msg"].get<string>()).c_str();
SCREENMAN->SendMessageToTopScreen(SM_AddToChat);
}
break;
Expand Down Expand Up @@ -895,7 +1002,7 @@ void NetworkSyncManager::StartRequest(short position)
}
void ETTProtocol::StartRequest(NetworkSyncManager* n, short position)
{
if (ws == nullptr)
if (ws == nullptr || position == 0)
return;
json startChart;
startChart["type"] = ettClientMessageMap[ettpc_startchart];
Expand All @@ -904,8 +1011,8 @@ void ETTProtocol::StartRequest(NetworkSyncManager* n, short position)
startChart["artist"] = GAMESTATE->m_pCurSong->m_sArtist;
startChart["filehash"] = GAMESTATE->m_pCurSong->GetFileHash();
startChart["chartkey"] = GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey();
startChart["options"] = GAMESTATE->m_SongOptions.GetCurrent().GetString();
startChart["rate"] = static_cast<int>((GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate * 100));
startChart["options"] = GAMESTATE->m_pPlayerState[PLAYER_1]->m_PlayerOptions.GetCurrent().GetString();
startChart["rate"] = static_cast<int>((GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate * 1000));
ws->send(startChart.dump().c_str());
}
void SMOProtocol::StartRequest(NetworkSyncManager* n, short position)
Expand Down Expand Up @@ -1345,16 +1452,23 @@ void ETTProtocol::SelectUserSong(NetworkSyncManager* n, Song* song)
{
if (ws == nullptr)
return;
json selectChart;
selectChart["type"] = ettClientMessageMap[ettpc_selectchart];
selectChart["title"] = n->m_sMainTitle;
selectChart["subtitle"] = n->m_sSubTitle;
selectChart["artist"] = n->m_sArtist;
selectChart["filehash"] = song->GetFileHash();
selectChart["chartkey"] = GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey();
selectChart["options"] = GAMESTATE->m_SongOptions.GetCurrent().GetString();
selectChart["rate"] = static_cast<int>((GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate * 100));
ws->send(selectChart.dump().c_str());
if (song == n->song) {
this->StartRequest(n, 1);
}
else {
json selectChart;
selectChart["type"] = ettClientMessageMap[ettpc_selectchart];
selectChart["title"] = n->m_sMainTitle.c_str();
selectChart["subtitle"] = n->m_sSubTitle.c_str();
selectChart["artist"] = n->m_sArtist.c_str();
selectChart["filehash"] = song->GetFileHash().c_str();
selectChart["chartkey"] = GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey().c_str();
selectChart["difficulty"] = DifficultyToString(GAMESTATE->m_pCurSteps[PLAYER_1]->GetDifficulty());
selectChart["meter"] = GAMESTATE->m_pCurSteps[PLAYER_1]->GetMeter();
selectChart["options"] = GAMESTATE->m_pPlayerState[PLAYER_1]->m_PlayerOptions.GetCurrent().GetString();
selectChart["rate"] = static_cast<int>((GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate * 1000));
ws->send(selectChart.dump().c_str());
}
}
void SMOProtocol::SelectUserSong(NetworkSyncManager * n, Song* song)
{
Expand Down
5 changes: 4 additions & 1 deletion src/NetworkSyncManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ class NetworkSyncManager
RString m_sSubTitle;
RString m_sFileHash;
string chartkey;
Song* song{nullptr};
Song* song{ nullptr };
Steps* steps{ nullptr };
Difficulty difficulty;
int meter;
int rate;

int m_sHash;
Expand Down
1 change: 1 addition & 0 deletions src/ScreenMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ AutoScreenMessage(SM_LoseFocus);
AutoScreenMessage(SM_Pause);
AutoScreenMessage(SM_Success);
AutoScreenMessage(SM_Failure);
AutoScreenMessage(SM_GoToDisconnectScreen);

static map<RString, ScreenMessage> *m_pScreenMessages;

Expand Down
1 change: 1 addition & 0 deletions src/ScreenMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern const ScreenMessage SM_MenuTimer;
extern const ScreenMessage SM_DoneFadingIn;
extern const ScreenMessage SM_BeginFadingOut;
extern const ScreenMessage SM_GoToNextScreen;
extern const ScreenMessage SM_GoToDisconnectScreen;
extern const ScreenMessage SM_GoToPrevScreen;
extern const ScreenMessage SM_GainFocus;
extern const ScreenMessage SM_LoseFocus;
Expand Down
22 changes: 21 additions & 1 deletion src/ScreenNetSelectMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ AutoScreenMessage(SM_BackFromPlayerOptions);
AutoScreenMessage(SM_ConfirmDeleteSong);
AutoScreenMessage(ETTP_SelectChart);
AutoScreenMessage(ETTP_StartChart);
AutoScreenMessage(ETTP_Disconnect);

REGISTER_SCREEN_CLASS( ScreenNetSelectMusic );

Expand Down Expand Up @@ -251,12 +252,24 @@ void ScreenNetSelectMusic::HandleScreenMessage( const ScreenMessage SM )
if( SM == SM_GoToPrevScreen )
{
SCREENMAN->SetNewScreen( THEME->GetMetric (m_sName, "PrevScreen") );
NSMAN->LeaveRoom();
}
else if( SM == SM_GoToNextScreen )
{
SOUND->StopMusic();
SCREENMAN->SetNewScreen( THEME->GetMetric (m_sName, "NextScreen") );
}
else if (SM == SM_GoToDisconnectScreen)
{
SOUND->StopMusic();
SCREENMAN->SetNewScreen(THEME->GetMetric(m_sName, "DisconnectScreen"));
}
else if (SM == ETTP_Disconnect)
{
SOUND->StopMusic();
TweenOffScreen();
Cancel(SM_GoToDisconnectScreen);
}
else if( SM == SM_UsersUpdate )
{
m_MusicWheel.Move( 0 );
Expand Down Expand Up @@ -426,6 +439,10 @@ void ScreenNetSelectMusic::HandleScreenMessage( const ScreenMessage SM )
SCREENMAN->PostMessageToTopScreen(SM_SetWheelSong, 0.710f);
m_MusicWheel.SelectSong(NSMAN->song);
}
if (NSMAN->steps != nullptr)
m_DC[PLAYER_1] = NSMAN->steps->GetDifficulty();
if (NSMAN->rate > 0)
GAMESTATE->m_SongOptions.GetPreferred().m_fMusicRate = NSMAN->rate/1000.0;
m_MusicWheel.Select();
m_MusicWheel.Move(-1);
m_MusicWheel.Move(1);
Expand All @@ -443,6 +460,10 @@ void ScreenNetSelectMusic::HandleScreenMessage( const ScreenMessage SM )
SCREENMAN->PostMessageToTopScreen(SM_SetWheelSong, 0.710f);
m_MusicWheel.SelectSong(NSMAN->song);
}
if(NSMAN->steps != nullptr)
m_DC[PLAYER_1] = NSMAN->steps->GetDifficulty();
if (NSMAN->rate > 0)
GAMESTATE->m_SongOptions.GetPreferred().m_fMusicRate = NSMAN->rate / 1000.0;
m_MusicWheel.Select();
m_MusicWheel.Move(-1);
m_MusicWheel.Move(1);
Expand Down Expand Up @@ -662,7 +683,6 @@ void ScreenNetSelectMusic::TweenOffScreen()
OFF_COMMAND( m_MusicWheel );

NSMAN->ReportNSSOnOff(0);
NSMAN->LeaveRoom();
}

void ScreenNetSelectMusic::StartSelectedSong()
Expand Down

0 comments on commit 3a32cd6

Please sign in to comment.