Skip to content

Commit

Permalink
Leave handling while joining
Browse files Browse the repository at this point in the history
  • Loading branch information
veblush committed Jul 13, 2016
1 parent af18688 commit 8217713
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 6 deletions.
197 changes: 197 additions & 0 deletions src/GameClient/Assets/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ RectTransform:
m_Children:
- {fileID: 586380279}
- {fileID: 1729007792}
- {fileID: 1768358409}
m_Father: {fileID: 1558733645}
m_RootOrder: 0
m_AnchorMin: {x: 0, y: 0}
Expand Down Expand Up @@ -2078,6 +2079,80 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1562630848}
--- !u!1 &1567658630
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 224: {fileID: 1567658631}
- 222: {fileID: 1567658633}
- 114: {fileID: 1567658632}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1567658631
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1567658630}
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_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 1768358409}
m_RootOrder: 0
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1567658632
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1567658630}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0, g: 0, b: 0, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 12800000, guid: 73e23e58d71f4974c8027187073263dc, type: 3}
m_FontSize: 50
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 50
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Leave
--- !u!222 &1567658633
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1567658630}
--- !u!1 &1620969388
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2308,6 +2383,128 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1760781729}
--- !u!1 &1768358408
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 224: {fileID: 1768358409}
- 222: {fileID: 1768358412}
- 114: {fileID: 1768358411}
- 114: {fileID: 1768358410}
m_Layer: 5
m_Name: LeaveButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1768358409
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1768358408}
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_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 1567658631}
m_Father: {fileID: 615108433}
m_RootOrder: 2
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -545}
m_SizeDelta: {x: 500, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1768358410
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1768358408}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1392445389, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 1768358411}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1868978847}
m_MethodName: OnLeaveButtonClick
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!114 &1768358411
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1768358408}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -765806418, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.9294118, g: 0.42352945, b: 0.12941177, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
--- !u!222 &1768358412
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1768358408}
--- !u!1 &1818035792
GameObject:
m_ObjectHideFlags: 0
Expand Down
62 changes: 56 additions & 6 deletions src/GameClient/Assets/Scripts/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class GameScene : MonoBehaviour, IUserPairingObserver, IGameObserver, IBy
private ClientZone _zone;
private ProtobufChannelToClientZoneInbound _zoneChannel;

private bool _isJoining;
private bool _isLeaveRequested;

protected void Start()
{
ClientEntityFactory.Default.RootTransform = GameEntityRoot;
Expand Down Expand Up @@ -93,6 +96,24 @@ private IEnumerator ProcessLoginUser(string server, string id, string password)
}

private IEnumerator ProcessJoinGame()
{
_isJoining = true;

yield return StartCoroutine(ProcessJoinGameInternal());

if (_gameClient == null)
{
SceneManager.LoadScene("MainScene");
yield break;
}

if (_isLeaveRequested)
LeaveAndReturnToMainScene();

_isJoining = false;
}

private IEnumerator ProcessJoinGameInternal()
{
G.Logger.Info("ProcessJoinGame");

Expand All @@ -107,7 +128,7 @@ private IEnumerator ProcessJoinGame()
yield return G.User.RegisterPairing(G.GameDifficulty, pairingObserver).WaitHandle;

var startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalSeconds < 5 && _pairedGame == null)
while ((DateTime.Now - startTime).TotalSeconds < 5 && _pairedGame == null && _isLeaveRequested == false)
{
yield return null;
}
Expand All @@ -117,25 +138,25 @@ private IEnumerator ProcessJoinGame()
if (_pairedGame == null)
{
yield return G.User.UnregisterPairing().WaitHandle;
var box = UiMessageBox.Show("Cannot find game");
yield return StartCoroutine(box.WaitForHide());
if (_isLeaveRequested == false)
yield return UiMessageBox.Show("Cannot find game").WaitForHide();
SceneManager.LoadScene("MainScene");
yield break;
}

// Join Game

var gameObserver = G.Communicator.ObserverRegistry.Create<IGameObserver>(this, startPending: true);
gameObserver.GetEventDispatcher().KeepingOrder = true; // remove after Akka.NET network layer is upgraded
gameObserver.GetEventDispatcher().KeepingOrder = true;

var roomId = _pairedGame.Item1;
var joinRet = G.User.JoinGame(roomId, gameObserver);
yield return joinRet.WaitHandle;

if (joinRet.Exception != null)
{
UiMessageBox.Show("Failed to join\n" + joinRet.Exception);
G.Communicator.ObserverRegistry.Remove(gameObserver);
yield return UiMessageBox.Show("Failed to join\n" + joinRet.Exception).WaitForHide();
yield break;
}

Expand All @@ -150,10 +171,16 @@ private IEnumerator ProcessJoinGame()
yield return connectTask.WaitHandle;
if (connectTask.Exception != null)
{
UiMessageBox.Show("Failed to connect\n" + joinRet.Exception);
_gameClient = null;
G.Communicator.ObserverRegistry.Remove(gameObserver);
yield return UiMessageBox.Show("Failed to connect\n" + joinRet.Exception).WaitForHide();
yield break;
}
((IChannel)_gameClient.RequestWaiter).StateChanged += (_, state) =>
{
if (state == ChannelStateType.Closed)
ChannelEventDispatcher.Post(OnChannelClose, _);
};
}

_zone = new ClientZone(
Expand All @@ -175,6 +202,12 @@ private IEnumerator ProcessJoinGame()
LoadingText.text = "Waiting for " + _pairedGame.Item2 + "...";
}

private void OnChannelClose(object channel)
{
if (this != null)
OnLeaveButtonClick();
}

void IUserPairingObserver.MakePair(long gameId, string opponentName)
{
Debug.Log(string.Format("IUserPairingObserver.MakePair {0} {1}", gameId, opponentName));
Expand Down Expand Up @@ -255,11 +288,28 @@ private void EndGame(int winnerId)
}

public void OnLeaveButtonClick()
{
if (_isLeaveRequested)
return;

_isLeaveRequested = true;

if (_isJoining)
return;

LeaveAndReturnToMainScene();
}

private void LeaveAndReturnToMainScene()
{
if (_gameInfo != null)
{
G.User.LeaveGame(_gameInfo.Id);
G.Communicator.ObserverRegistry.Remove(_gameObserver);

var gameClientChannel = (IChannel)_gameClient.RequestWaiter;
if (gameClientChannel != G.Communicator.Channels[0])
gameClientChannel.Close();
}

SceneManager.LoadScene("MainScene");
Expand Down

0 comments on commit 8217713

Please sign in to comment.