Skip to content

Commit

Permalink
Merge pull request #12 from bookdude13/multiplayer
Browse files Browse the repository at this point in the history
Multiplayer enhancements
  • Loading branch information
bookdude13 authored Aug 23, 2022
2 parents 86da165 + f144a3f commit 7cec6d7
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 119 deletions.
21 changes: 13 additions & 8 deletions PrepareRelease.bat
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@

set BUILT_VERSION="1.1.1"
set MOD_NAME="SRVoting"
set BUILT_VERSION=1.2.0
set MOD_NAME=SRVoting

set RELEASE_BUILD_DIR=".\%MOD_NAME%\bin\Release"
set MAIN_DLL="%RELEASE_BUILD_DIR%\%MOD_NAME%.dll"
set LIB_DLL_DIR="%RELEASE_BUILD_DIR%\libs"
set RELEASE_BUILD_DIR=.\%MOD_NAME%\bin\Release
set MAIN_DLL=%RELEASE_BUILD_DIR%\%MOD_NAME%.dll
set LIB_DLL_DIR=%RELEASE_BUILD_DIR%\libs

set OUTPUT_DIR=".\build\%MOD_NAME%_v%BUILT_VERSION%"
set OUTPUT_DIR=.\build\%MOD_NAME%_v%BUILT_VERSION%
mkdir %OUTPUT_DIR%

copy %MAIN_DLL% %OUTPUT_DIR%
copy %LIB_DLL_DIR%\* %OUTPUT_DIR%
set OUTPUT_MODS_DIR=%OUTPUT_DIR%\Mods
mkdir %OUTPUT_MODS_DIR%

copy %MAIN_DLL% %OUTPUT_MODS_DIR%
copy %LIB_DLL_DIR%\* %OUTPUT_MODS_DIR%

powershell Compress-Archive %OUTPUT_MODS_DIR% %OUTPUT_DIR%\%MOD_NAME%_v%BUILT_VERSION%.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HarmonyLib;
using Util.Controller;

namespace SRVoting.Harmony
{
// This is called during initialization (for the host),
// and from Game_InfoProvider.OnEvent() with event code SetSelectedVersusTrack (for clients)
[HarmonyPatch(typeof(MultiplayerRoomController), nameof(MultiplayerRoomController.FillSelectedTrackData))]
public class Patch_MultiplayerRoomController_FillSelectedTrackData
{
public static void Postfix(object[] data)
{
SRVoting.Instance?.OnMultiplayerRoomUpdateTrackData();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Synth.SongSelection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;

namespace SRVoting.Harmony
{
[HarmonyPatch(typeof(SongSelectionManager), nameof(SongSelectionManager.OpenMultiplayerRoomMenu))]
public class Patch_SongSelectionManager_OpenMultiplayerRoomMenu
{
public static void Postfix(Synth.Versus.Room room)
{
SRVoting.Instance?.OnOpenMultiplayerRoomMenu(room);
}
}
}
37 changes: 37 additions & 0 deletions SRVoting/MonoBehaviors/VotingMainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections;
using UnityEngine;

namespace SRVoting.MonoBehaviors
{
public class VotingMainMenu : VotingMonoBehavior
{
protected override IEnumerator EnsureUIExists()
{
if (!upVoteComponent.IsUiCreated || !downVoteComponent.IsUiCreated)
{
logger.Msg("Initializing Main Menu UI...");

// Find existing pieces
var rootGO = GameObject.Find("CentralPanel/Song Selection/VisibleWrap");

var controlsGO = rootGO.transform.Find("Controls");
var volumeText = controlsGO.Find("MuteWrap/VALUE");
var volumeLeft = controlsGO.Find("MuteWrap/Arrow UP");
var volumeRight = controlsGO.Find("MuteWrap/Arrow Down");

var selectedTrackGO = rootGO.transform.Find("Selected Track");
var difficultiesGO = selectedTrackGO.transform.Find("Difficulties");
Transform hardButton = difficultiesGO.Find("StandardButton - Hard");
Transform customButton = difficultiesGO.Find("StandardButton - Custom");

// Create new pieces
upVoteComponent.CreateUIForVertical(selectedTrackGO, difficultiesGO, hardButton, volumeLeft, volumeText.gameObject);
downVoteComponent.CreateUIForVertical(selectedTrackGO, difficultiesGO, customButton, volumeRight, volumeText.gameObject);

logger.Msg("Done creating main menu UI");
}

yield return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
using SRVoting.UI;
using System.Collections;
using UnityEngine;
using VRTK.UnityEventHelper;

namespace SRVoting
namespace SRVoting.MonoBehaviors
{
class VotingMonoBehavior : MonoBehaviour
public abstract class VotingMonoBehavior : MonoBehaviour
{
private readonly string arrowUpName = "srvoting_voteArrowUp";
private readonly string arrowDownName = "srvoting_voteArrowDown";
protected readonly string arrowUpName = "srvoting_voteArrowUp";
protected readonly string arrowDownName = "srvoting_voteArrowDown";

private SRLogger logger;
private SynthriderzService synthriderzService;
protected SRLogger logger;
protected SynthriderzService synthriderzService;

private VoteDirectionComponent upVoteComponent;
private VoteDirectionComponent downVoteComponent;
protected VoteDirectionComponent upVoteComponent;
protected VoteDirectionComponent downVoteComponent;

private string currentSongHash = "";
private VoteState currentSongVote = VoteState.NO_VOTE;
Expand All @@ -33,53 +32,20 @@ public void Init(SRLogger logger, SynthriderzService synthriderzService)
StartCoroutine(EnsureUIExists());
}

protected abstract IEnumerator EnsureUIExists();

public void Refresh()
{
logger.Debug("Refreshing UI");
logger.Msg("Refreshing UI");
StartCoroutine(UpdateVoteUI());
}

private void Vote(object sender, VRTK.InteractableObjectEventArgs e)
{
string senderName = ((VRTK.VRTK_InteractableObject)sender).name;

// Translate sender into up/down
VoteState newVote = senderName == arrowUpName ? VoteState.VOTED_UP : VoteState.VOTED_DOWN;

// If same as existing, "unvote", otherwise cast desired vote
VoteState voteToSend = newVote == currentSongVote ? VoteState.NO_VOTE : newVote;

// Disable events until done voting
upVoteComponent.DisableEvents();
downVoteComponent.DisableEvents();

logger.Debug($"Voting for song {currentSongHash}. Old: {currentSongVote}, New: {voteToSend}");
StartCoroutine(VoteAndUpdateUI(voteToSend));
}

private IEnumerator VoteAndUpdateUI(VoteState vote)
Synth.Retro.Game_Track_Retro GetSelectedTrack()
{
VotesResponseModel getVotesResponse = null;
string errorMessage = null;

yield return synthriderzService.Vote(
currentSongHash,
vote,
response => getVotesResponse = response,
errorMsg => errorMessage = errorMsg
);

if (errorMessage != null)
{
HandleApiError(errorMessage);
}
else
{
HandleApiResponse(getVotesResponse);
}
return Synth.SongSelection.SongSelectionManager.GetInstance?.SelectedGameTrack;
}

private IEnumerator UpdateVoteUI()
protected virtual IEnumerator UpdateVoteUI()
{
if (synthriderzService == null)
{
Expand All @@ -98,18 +64,26 @@ private IEnumerator UpdateVoteUI()
yield return new WaitForSeconds(0.01f);

logger.Debug("Getting selected track...");
var selectedTrack = Synth.SongSelection.SongSelectionManager.GetInstance?.SelectedGameTrack;
var selectedTrack = GetSelectedTrack();
string songName = selectedTrack?.TrackName ?? "";
currentSongHash = selectedTrack?.LeaderboardHash ?? "";
bool isCustom = selectedTrack?.IsCustomSong ?? false;
logger.Msg($"{songName} selected. IsCustom? {isCustom}. Hash: {currentSongHash}");

if (!isCustom)
if (songName == "" || currentSongHash == "")
{
logger.Msg("No song selected");
upVoteComponent.UpdateUI(false, false, "");
downVoteComponent.UpdateUI(false, false, "");
}
else if (!isCustom)
{
logger.Msg($"OST track '{songName}' selected. Hash: {currentSongHash}");
upVoteComponent.UpdateUI(false, false, "N/A");
downVoteComponent.UpdateUI(false, false, "N/A");
}
else {
else
{
logger.Msg($"Custom song '{songName}' selected. Hash: {currentSongHash}");
VotesResponseModel getVotesResponse = null;
string errorMessage = null;
yield return synthriderzService.GetVotes(
Expand All @@ -131,6 +105,46 @@ private IEnumerator UpdateVoteUI()
}
}

private void Vote(object sender, VRTK.InteractableObjectEventArgs e)
{
string senderName = ((VRTK.VRTK_InteractableObject)sender).name;

// Translate sender into up/down
VoteState newVote = senderName == arrowUpName ? VoteState.VOTED_UP : VoteState.VOTED_DOWN;

// If same as existing, "unvote", otherwise cast desired vote
VoteState voteToSend = newVote == currentSongVote ? VoteState.NO_VOTE : newVote;

// Disable events until done voting
upVoteComponent.DisableEvents();
downVoteComponent.DisableEvents();

logger.Debug($"Voting for song {currentSongHash}. Old: {currentSongVote}, New: {voteToSend}");
StartCoroutine(VoteAndUpdateUI(voteToSend));
}

private IEnumerator VoteAndUpdateUI(VoteState vote)
{
VotesResponseModel getVotesResponse = null;
string errorMessage = null;

yield return synthriderzService.Vote(
currentSongHash,
vote,
response => getVotesResponse = response,
errorMsg => errorMessage = errorMsg
);

if (errorMessage != null)
{
HandleApiError(errorMessage);
}
else
{
HandleApiResponse(getVotesResponse);
}
}

private void HandleApiError(string errorMessage)
{
logger.Msg(errorMessage);
Expand All @@ -144,14 +158,14 @@ private void HandleApiResponse(VotesResponseModel getVotesResponse)
{
if (getVotesResponse == null)
{
logger.Debug("No vote data, disabling arrows...");
logger.Msg("No vote data, disabling arrows...");
currentSongVote = VoteState.NO_VOTE;
upVoteComponent.UpdateUI(false, false, "");
downVoteComponent.UpdateUI(false, false, "");
}
else
{
logger.Debug("Updating UI with returned vote info...");
logger.Msg("Updating UI with returned vote info...");
currentSongVote = getVotesResponse.MyVote();
upVoteComponent.UpdateUI(
true,
Expand All @@ -165,34 +179,5 @@ private void HandleApiResponse(VotesResponseModel getVotesResponse)
);
}
}

private IEnumerator EnsureUIExists()
{
if (!upVoteComponent.IsUiCreated || !downVoteComponent.IsUiCreated)
{
logger.Msg("Initializing UI...");

// Find existing pieces
var rootGO = GameObject.Find("CentralPanel/Song Selection/VisibleWrap");

var controlsGO = rootGO.transform.Find("Controls");
var volumeText = controlsGO.Find("MuteWrap/VALUE");
var volumeLeft = controlsGO.Find("MuteWrap/Arrow UP");
var volumeRight = controlsGO.Find("MuteWrap/Arrow Down");

var selectedTrackGO = rootGO.transform.Find("Selected Track");
var difficultiesGO = selectedTrackGO.transform.Find("Difficulties");
Transform hardButton = difficultiesGO.Find("StandardButton - Hard");
Transform customButton = difficultiesGO.Find("StandardButton - Custom");

// Create new pieces
upVoteComponent.CreateUI(selectedTrackGO, difficultiesGO, hardButton, volumeLeft, volumeText.gameObject);
downVoteComponent.CreateUI(selectedTrackGO, difficultiesGO, customButton, volumeRight, volumeText.gameObject);

logger.Msg("Done creating UI");
}

yield return null;
}
}
}
61 changes: 61 additions & 0 deletions SRVoting/MonoBehaviors/VotingMultiplayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using SRModCore;
using System;
using System.Collections;
using UnityEngine;

namespace SRVoting.MonoBehaviors
{
public class VotingMultiplayer: VotingMonoBehavior
{
protected override IEnumerator EnsureUIExists()
{
var elementsCreated = upVoteComponent.IsUiCreated && downVoteComponent.IsUiCreated;
if (!elementsCreated)
{
logger.Msg("Initializing Multiplayer UI...");

try
{
// Find existing pieces
var rootGO = GameObject.Find("Multiplayer/RoomPanel/Rooms/BottomPanel");

var arrowsContainerGO = rootGO.transform.Find("MuteWrap");
var volumeText = arrowsContainerGO.Find("VALUE");
var volumeLeft = arrowsContainerGO.Find("Arrow UP");
var volumeRight = arrowsContainerGO.Find("Arrow Down");

var favoriteWrapGO = rootGO.transform.Find("Favorite Wrap");

// Create new pieces
downVoteComponent.CreateUIForHorizontal(
favoriteWrapGO, -1.5f, TMPro.TextAlignmentOptions.Right, volumeRight, volumeText.gameObject
);
upVoteComponent.CreateUIForHorizontal(
favoriteWrapGO, 1.5f, TMPro.TextAlignmentOptions.Left, volumeLeft, volumeText.gameObject
);

logger.Msg("Done creating UI");
}
catch (Exception ex)
{
logger.Error("Failed to initialize UI", ex);
}
}

yield return null;
}

protected override IEnumerator UpdateVoteUI()
{
var countdownTimerWrap = GameObject.Find("Multiplayer/RoomPanel/Rooms/BottomPanel/SongInfo/TimeWrap");
if (countdownTimerWrap != null && countdownTimerWrap.activeInHierarchy)
{
logger.Msg("Moving countdown timer wrap");
// Originally at <2.4, 0.4, 0.0>
countdownTimerWrap.transform.localPosition = new Vector3(4.4f, 0.4f, 0.0f);
}

return base.UpdateVoteUI();
}
}
}
6 changes: 3 additions & 3 deletions SRVoting/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0")]

[assembly: MelonInfo(typeof(SRVoting.SRVoting), "SRVoting", "1.1.1", "bookdude13", "https://github.com/bookdude13/SRVoting")]
[assembly: MelonInfo(typeof(SRVoting.SRVoting), "SRVoting", "1.2.0", "bookdude13", "https://github.com/bookdude13/SRVoting")]
[assembly: MelonGame("Kluge Interactive", "SynthRiders")]

Loading

0 comments on commit 7cec6d7

Please sign in to comment.