Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
refactoring: dedicated SongListRequestor with SongListEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
achimmihca committed Mar 30, 2021
1 parent a29667d commit 7d864a4
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net;
using UniInject;
using UniRx;
using UnityEngine;

// Disable warning about fields that are never assigned, their values are injected.
#pragma warning disable CS0649

public abstract class AbstractHttpRequestor : MonoBehaviour, INeedInjection
{
protected IPEndPoint serverIPEndPoint;
protected int httpServerPort;

[Inject]
protected ClientSideConnectRequestManager clientSideConnectRequestManager;

protected void Start()
{
clientSideConnectRequestManager.ConnectEventStream
.Where(connectEvent => connectEvent.IsSuccess)
.Subscribe(connectEvent =>
{
serverIPEndPoint = connectEvent.ServerIpEndPoint;
httpServerPort = connectEvent.HttpServerPort;
});
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class SongListEvent
{
public string ErrorMessage { get; set; }
public LoadedSongsDto LoadedSongsDto { get; set; }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UniInject;
using UnityEngine;
using UniRx;

// Disable warning about fields that are never assigned, their values are injected.
#pragma warning disable CS0649

public class SongListRequestor : AbstractHttpRequestor
{
private Subject<SongListEvent> songListEventStream = new Subject<SongListEvent>();
public IObservable<SongListEvent> SongListEventStream => songListEventStream;

public bool SuccessfullyLoadedAllSongs { get; private set; }

public LoadedSongsDto LoadedSongsDto { get; private set; }

public void RequestSongList()
{
if (serverIPEndPoint == null
|| httpServerPort == 0)
{
FireErrorMessageEvent("Can not get song list. Not yet connected to main game.");
return;
}

string uri = $"http://{serverIPEndPoint.Address}:{httpServerPort}/api/rest/songs";
Debug.Log("GET song list from URI: " + uri);

StartCoroutine(WebRequestUtils.LoadTextFromUriCoroutine(uri,
HandleSongListResponse,
_ => FireErrorMessageEvent("Ups, something went wrong getting the song list.")));
}

private void HandleSongListResponse(string downloadHandlerText)
{
try
{
LoadedSongsDto = JsonConverter.FromJson<LoadedSongsDto>(downloadHandlerText);
if (!LoadedSongsDto.IsSongScanFinished
&& LoadedSongsDto.SongCount == 0)
{
SuccessfullyLoadedAllSongs = false;
FireErrorMessageEvent("No songs loaded yet.");
return;
}

if (LoadedSongsDto.IsSongScanFinished)
{
SuccessfullyLoadedAllSongs = true;
}

songListEventStream.OnNext(new SongListEvent
{
LoadedSongsDto = LoadedSongsDto,
});
}
catch (Exception e)
{
Debug.LogException(e);
SuccessfullyLoadedAllSongs = false;
FireErrorMessageEvent("Ups, something went wrong reading the song list response.");
}
}

private void FireErrorMessageEvent(string errorMessage)
{
songListEventStream.OnNext(new SongListEvent
{
ErrorMessage = errorMessage,
});
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public static void RegisterCallbackButtonTriggered(this Button button, Action ca

public static void Hide(this VisualElement visualElement)
{
visualElement.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
visualElement.style.display = DisplayStyle.None;
}

public static void Show(this VisualElement visualElement)
{
visualElement.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
visualElement.style.display = DisplayStyle.Flex;
}

public static void SetVisible(this VisualElement visualElement, bool isVisible)
Expand Down
33 changes: 33 additions & 0 deletions UltraStar Play Companion/Assets/Common/Util/WebRequestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public static class WebRequestUtils
{
public static IEnumerator LoadTextFromUriCoroutine(string uri, Action<string> onSuccess, Action<UnityWebRequest> onFailure = null)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
webRequest.SendWebRequest();

while (!webRequest.isDone)
{
yield return null;
}

if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.LogError("Error loading text from: " + uri);
Debug.LogError(webRequest.error);
if (onFailure != null)
{
onFailure(webRequest);
}
yield break;
}

onSuccess(webRequest.downloadHandler.text);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 45 additions & 1 deletion UltraStar Play Companion/Assets/Scenes/MainScene/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Transform:
m_Children:
- {fileID: 426713327}
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1601577406
MonoBehaviour:
Expand All @@ -317,6 +317,49 @@ MonoBehaviour:
waveformColor: {r: 1, g: 1, b: 1, a: 1}
dynTexture: {fileID: 426713328}
visualElementName: audioWaveForm
--- !u!1 &1757853344
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1757853346}
- component: {fileID: 1757853345}
m_Layer: 0
m_Name: SongListRequestor
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1757853345
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1757853344}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b90bd3d364804624ab27dd73355d6d2c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1757853346
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1757853344}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2047619670
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -349,6 +392,7 @@ MonoBehaviour:
versionPropertiesTextAsset: {fileID: 4900000, guid: d2f6d8e833e31724796bca2ae8f0d566, type: 3}
uiDoc: {fileID: 202961749}
audioWaveFormVisualizer: {fileID: 1601577406}
songListRequestor: {fileID: 1757853345}
--- !u!4 &2047619672
Transform:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit 7d864a4

Please sign in to comment.