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 5f4cabc commit 8622d4c
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions src/GameClient/Assets/Scripts/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class GameScene : MonoBehaviour, IUserPairingObserver, IGameObserver
private GameInfo _gameInfo;
private GamePlayerRef _myPlayer;

private bool _isJoining;
private bool _isLeaveRequested;

protected void Start()
{
UiManager.Initialize();
Expand Down Expand Up @@ -78,6 +81,24 @@ private IEnumerator ProcessLoginUser(string server, string id, string password)
}

private IEnumerator ProcessJoinGame()
{
_isJoining = true;

yield return StartCoroutine(ProcessJoinGameInternal());

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

if (_isLeaveRequested)
LeaveAndReturnToMainScene();

_isJoining = false;
}

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

Expand All @@ -92,36 +113,40 @@ private IEnumerator ProcessJoinGame()
yield return G.User.RegisterPairing(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;
}

G.Communicator.ObserverRegistry.Remove(pairingObserver);

if (_isLeaveRequested)
yield break;

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 break;
var box = UiMessageBox.Show("Failed to join\n" + joinRet.Exception);
yield return StartCoroutine(box.WaitForHide());
SceneManager.LoadScene("MainScene");
}

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

gameObserver.GetEventDispatcher().Pending = false;
LoadingText.text = "Waiting for " + _pairedGame.Item2 + "...";
}

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

private void BeginGame(int playerId)
{
var opponentName = _gameInfo.PlayerNames[2 - _myPlayerId];
Expand Down Expand Up @@ -238,11 +276,28 @@ private void OnBoardGridClicked(int x, int y)
}

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 myPlayerChannel = (IChannel)_myPlayer.RequestWaiter;
if (myPlayerChannel != G.Communicator.Channels[0])
myPlayerChannel.Close();
}

SceneManager.LoadScene("MainScene");
Expand Down

0 comments on commit 8622d4c

Please sign in to comment.