public void SendLobbyMessage(string message, SynchronizedGame game)
        {
            var callback = OperationContext.Current.GetCallbackChannel<IGameManagerCallback>();

            var userName = playerList[callback].Name;
            message = string.Format("{0} ({1}): {2}", userName, DateTime.Now.ToShortTimeString(), message);

            game = getGameByGuid(game.GameId);
            var userCallbacks = playerList.Where(kvp => game.Players.Contains(kvp.Value)).Select(kvp => kvp.Key);
            foreach (var userCallback in userCallbacks) userCallback.PropagateLobbyMessage(message);
        }
        public SynchronizedGame CreateGame(string lobbyName, int playerLimit, DolphinOptions dolphinOptions)
        {
            var callback = OperationContext.Current.GetCallbackChannel<IGameManagerCallback>();

            var newGame = new SynchronizedGame(lobbyName, playerLimit, dolphinOptions);
            newGame.Players.Add(playerList[callback]);

            gameList.Add(newGame);

            //Propagate change to all users
            propagateChanges(newGame);

            return newGame;
        }
        public string JoinGame(SynchronizedGame game)
        {
            var callback = OperationContext.Current.GetCallbackChannel<IGameManagerCallback>();

            game = getGameByGuid(game.GameId);

            if (game == null) return "Game no longer exists.";

            game.Players.Add(playerList[callback]);

            //Propagate change to all users
            propagateChanges(game);

            return null;
        }
        public string LeaveGame(SynchronizedGame game)
        {
            var callback = OperationContext.Current.GetCallbackChannel<IGameManagerCallback>();

            return leaveGame(callback, game);
        }
        private void propagateChanges(SynchronizedGame game)
        {
            var disconnectedPlayers = new List<IGameManagerCallback>();

            //Propagate change to all users
            foreach (var callbackChannel in playerList.Keys)
            {
                try
                {
                    callbackChannel.PropagateChange(game);
                }
                catch (Exception ex)
                {
                    if (ex is CommunicationException || ex is TimeoutException || ex is ObjectDisposedException)
                    {
                        disconnectedPlayers.Add(callbackChannel);
                    }
                    else throw;
                }
            }

            //Remove all players that could not be contacted
            foreach (var keyToRemove in disconnectedPlayers) playerList.Remove(keyToRemove);
        }
        private string leaveGame(IGameManagerCallback callback, SynchronizedGame game)
        {
            game = getGameByGuid(game.GameId);

            if (game == null) return "Game no longer exists.";

            game.Players.Remove(playerList[callback]);

            //If no one left in game, remove it
            if (game.Players.Count == 0)
            {
                gameList.Remove(game);
            }

            propagateChanges(game);

            return null;
        }
Ejemplo n.º 7
0
 public GameInfo(SynchronizedGame game)
 {
     Game = game;
 }