Example #1
0
        /// <summary>
        /// Lists all public lobbies asynchronously
        /// </summary>
        /// <returns>Lobby views task</returns>
        public async Task<ILobbyViews> ListLobbiesAsync()
        {
            ILobbyViews ret = null;
            LobbyViewData[] lobby_views_data = await SendHTTPGETRequestAsync<LobbyViewData[]>(new Uri(HTTPHostURI, "/v1/lobby"));
            bool is_using_secure_protocols = IsUsingSecureProtocols;
            if (((lobby_views_data == null) || !Protection.IsValid(lobby_views_data)) && IsUsingSecureProtocols && IsAllowedToUseInsecureConnections)
            {
                lobby_views_data = await SendHTTPGETRequestAsync<LobbyViewData[]>(new Uri(InsecureHTTPHostURI, "/v1/lobby"));
                is_using_secure_protocols = false;
            }
            if ((lobby_views_data != null) && Protection.IsValid(lobby_views_data))
            {
                ILobbyView[] lobby_views = new ILobbyView[lobby_views_data.Length];
#if SCRIBBLERS_SHARP_NO_PARALLEL_LOOPS
                for (int lobby_view_index = 0; lobby_view_index < lobby_views.Length; lobby_view_index++)
#else
                Parallel.For(0, lobby_views_data.Length, (lobby_view_index) =>
#endif
                {
                    LobbyViewData lobby_view = lobby_views_data[lobby_view_index];
                    lobby_views[lobby_view_index] = new LobbyView(lobby_view.LobbyID, lobby_view.PlayerCount, lobby_view.MaximalPlayerCount, lobby_view.CurrentRound, lobby_view.MaximalRoundCount, lobby_view.DrawingTime, lobby_view.IsUsingCustomWords, lobby_view.IsVotekickingEnabled, lobby_view.MaximalClientsPerIPCount, lobby_view.Language);
                }
#if !SCRIBBLERS_SHARP_NO_PARALLEL_LOOPS
                );
#endif
                ret = new LobbyViews(is_using_secure_protocols, lobby_views);
            }
            return ret;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MazePresenter"/> class.
        /// Initializes a request handler, adds the options of the server,
        /// and adds the NewConnection and ReplyToClient methods to the delegate of the view and model respectively.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="view">The view.</param>
        public MazePresenter(IModel model, ILobbyView view)
        {
            Model   = model;
            View    = view;
            Handler = new RequestHandler();

            Handler.AddOption("generate", new GenerateOption(Model));
            Handler.AddOption("solve", new SolveOption(Model));
            Handler.AddOption("multiplayer", new MultiplayerOption(Model));
            Handler.AddOption("play", new PlayOption(Model));
            Handler.AddOption("close", new CloseOption(Model));

            View.OnConnect      += NewConnection;
            Model.TaskCompleted += ReplyToClient;
        }
Example #3
0
        public void ViewLobby(ILobbyView lobbyView)
        {
            var loggedInUser = LoginInfo.GetUser();

            lobbyView.ShowModaless(loggedInUser.FirstName);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="peer">Peer</param>
 /// <param name="token">Token</param>
 public ClientSynchronizer(IPeer peer, string token) : base()
 {
     Peer = peer ?? throw new ArgumentNullException(nameof(peer));
     AddAutomaticMessageParserWithFatality <AuthentificationAcknowledgedMessageData>
     (
         (_, message, __) =>
     {
         if (IsAuthentificated)
         {
             SendErrorMessageToPeer <AuthentificationAcknowledgedMessageData>(peer, EErrorType.InvalidMessageContext, "Authentification has been already acknowledged.", true);
         }
         else
         {
             Token = message.Token;
             user  = new ClientUser(message.GUID);
             RegisterUserEvents(user);
             OnAuthentificationAcknowledged?.Invoke(User);
         }
     }
     );
     AddAutomaticMessageParser <AuthentificationFailedMessageData>((currentPeer, message, _) => OnAuthentificationFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <ListLobbyResultsMessageData>
     (
         (_, message, __) =>
     {
         ILobbyView[] lobbies = new ILobbyView[message.Lobbies.Count];
         Parallel.For(0, lobbies.Length, (index) => lobbies[index] = (LobbyView)message.Lobbies[index]);
         OnLobbiesListed?.Invoke(lobbies);
     }
     );
     AddAutomaticMessageParser <ListLobbiesFailedMessageData>((currentPeer, message, _) => OnListLobbiesFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <ListAvailableGameModeResultsMessageData>((_, message, __) => OnAvailableGameModesListed?.Invoke(message.GameModes));
     AddAutomaticMessageParser <ListAvailableGameModesFailedMessageData>((currentPeer, message, _) => OnListAvailableGameModesFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <JoinLobbyAcknowledgedMessageData>
     (
         (_, message, __) => AssertIsUserAuthentificated <JoinLobbyAcknowledgedMessageData>
         (
             (clientUser) =>
     {
         if (clientUser.Lobby == null)
         {
             Dictionary <string, IUser> users     = new Dictionary <string, IUser>();
             List <IInternalClientUser> user_list = new List <IInternalClientUser>();
             foreach (UserData user in message.Users)
             {
                 IInternalClientUser client_user;
                 if (user.GUID == clientUser.GUID)
                 {
                     client_user = clientUser;
                     this.user.SetNameInternally(user.Name, false);
                     this.user.SetLobbyColorInternally(user.LobbyColor, false);
                 }
                 else
                 {
                     client_user = new ClientUser(user.GUID, user.GameColor, user.Name, user.LobbyColor);
                     RegisterUserEvents(client_user);
                 }
                 users.Add(user.GUID.ToString(), client_user);
                 user_list.Add(client_user);
             }
             IInternalClientLobby client_lobby     = new ClientLobby(this, message.Rules.LobbyCode, message.Rules.Name, message.Rules.GameMode, message.Rules.IsPrivate, message.Rules.MinimalUserCount, message.Rules.MaximalUserCount, message.Rules.IsStartingGameAutomatically, message.Rules.GameModeRules, users[message.OwnerGUID.ToString()], users);
             client_lobby.OnUserJoined            += (user) => OnUserJoined?.Invoke(client_lobby, user);
             client_lobby.OnUserLeft              += (user, reason, leaveMessage) => OnUserLeft?.Invoke(client_lobby, user, reason, leaveMessage);
             client_lobby.OnLobbyOwnershipChanged += () => OnLobbyOwnershipChanged?.Invoke(client_lobby);
             client_lobby.OnLobbyRulesChanged     += () => OnLobbyRulesChanged?.Invoke(client_lobby);
             client_lobby.OnGameStarted           += () => OnGameStarted?.Invoke(client_lobby);
             client_lobby.OnGameStartRequested    += (time) => OnGameStartRequested?.Invoke(client_lobby, time);
             client_lobby.OnGameRestarted         += () => OnGameRestarted?.Invoke(client_lobby);
             client_lobby.OnGameRestartRequested  += (time) => OnGameRestartRequested?.Invoke(client_lobby, time);
             client_lobby.OnGameStopped           += (gameStopUsers, results) => OnGameStopped?.Invoke(client_lobby, gameStopUsers, results);
             client_lobby.OnGameStopRequested     += (time) => OnGameStopRequested?.Invoke(client_lobby, time);
             client_lobby.OnStartGameCancelled    += () => OnStartGameCancelled?.Invoke(client_lobby);
             client_lobby.OnRestartGameCancelled  += () => OnRestartGameCancelled?.Invoke(client_lobby);
             client_lobby.OnStopGameCancelled     += () => OnStopGameCancelled?.Invoke(client_lobby);
             client_lobby.OnUserEntityCreated     += (user) => OnUserEntityCreated?.Invoke(client_lobby, user);
             client_lobby.OnUserEntityUpdated     += (user) => OnUserEntityUpdated?.Invoke(client_lobby, user);
             client_lobby.OnUserEntityDestroyed   += (user) => OnUserEntityDestroyed?.Invoke(client_lobby, user);
             client_lobby.OnEntityCreated         += (entity) => OnEntityCreated?.Invoke(client_lobby, entity);
             client_lobby.OnEntityUpdated         += (entity) => OnEntityUpdated?.Invoke(client_lobby, entity);
             client_lobby.OnEntityDestroyed       += (entity) => OnEntityDestroyed?.Invoke(client_lobby, entity);
             user.ClientLobby = client_lobby;
             foreach (IInternalClientUser client_user in user_list)
             {
                 client_user.ClientLobby = client_lobby;
             }
             user_list.Clear();
             OnLobbyJoinAcknowledged?.Invoke(client_lobby);
             user.InvokeUsernameUpdatedEvent();
             user.InvokeUserLobbyColorUpdatedEvent();
         }
         else
         {
             SendInvalidMessageContextErrorMessageToPeer <JoinLobbyAcknowledgedMessageData>(peer, $"User is already in lobby \"{ clientUser.Lobby.Name }\" with lobby code \"{ clientUser.Lobby.LobbyCode }\".");
         }
     }
         )
     );
     AddAutomaticMessageParser <JoinLobbyFailedMessageData>((currentPeer, message, _) => OnJoinLobbyFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <CreateLobbyFailedMessageData>((currentPeer, message, _) => OnCreateLobbyFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <LobbyRulesChangedMessageData>
     (
         (_, message, __) => AssertIsUserInLobby <LobbyRulesChangedMessageData>
         (
             (clientUser, clientLobby) =>
     {
         LobbyRulesData rules = message.Rules;
         clientLobby.ChangeLobbyRulesInternally
         (
             rules.LobbyCode,
             rules.Name,
             rules.GameMode,
             rules.IsPrivate,
             rules.MinimalUserCount,
             rules.MaximalUserCount,
             rules.IsStartingGameAutomatically,
             rules.GameModeRules
         );
     }
         )
     );
     AddAutomaticMessageParser <UserJoinedMessageData>
     (
         (_, message, __) => AssertIsUserInLobby <UserJoinedMessageData>
         (
             (clientUser, clientLobby) =>
     {
         IUser user = new ClientUser(message.GUID, message.GameColor, message.Name, message.LobbyColor);
         RegisterUserEvents(user);
         if (!clientLobby.AddUserInternally(user))
         {
             SendInvalidMessageContextErrorMessageToPeer <UserJoinedMessageData>(peer, $"Failed to add user \"{ user.Name }\" with GUID \"{ user.GUID }\" to lobby \"{ clientLobby.Name }\" with lobby code \"{ clientLobby.LobbyCode }\".");
         }
     }
         )
     );
     AddAutomaticMessageParser <UserLeftMessageData>
     (
         (_, message, __) => AssertTargetLobbyUser <UserLeftMessageData>
         (
             message.GUID,
             (clientUser, clientLobby, targetUser) =>
     {
         if (!clientLobby.RemoveUserInternally(targetUser, message.Reason, message.Message))
         {
             SendInvalidMessageContextErrorMessageToPeer <UserLeftMessageData>(peer, $"Failed to remove user \"{ targetUser.Name }\" with GUID \"{ targetUser.GUID }\" from lobby \"{ clientLobby.Name }\" with lobby code \"{ clientLobby.LobbyCode }\".");
         }
         targetUser.ClientLobby = null;
     }
         )
     );
     AddAutomaticMessageParser <LobbyOwnershipChangedMessageData>((_, message, __) => AssertTargetLobbyUser <LobbyOwnershipChangedMessageData>(message.NewOwnerGUID, (clientUser, clientLobby, targetUser) => clientLobby.ChangeLobbyOwnershipInternally(targetUser)));
     AddAutomaticMessageParser <UsernameChangedMessageData>((_, message, __) => AssertTargetLobbyUser <UsernameChangedMessageData>(message.GUID, (clientUser, clientLobby, targetUser) => targetUser.SetNameInternally(message.NewUsername)));
     AddAutomaticMessageParser <ChangeUsernameFailedMessageData>((currentPeer, message, _) => OnChangeUsernameFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <UserLobbyColorChangedMessageData>((_, message, __) => AssertTargetLobbyUser <UserLobbyColorChangedMessageData>(message.GUID, (clientUser, clientLobby, targetUser) => targetUser.SetLobbyColorInternally(message.NewLobbyColor)));
     AddAutomaticMessageParser <ChangeUserLobbyColorFailedMessageData>((currentPeer, message, _) => OnChangeUserLobbyColorFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <ChangeLobbyRulesFailedMessageData>((currentPeer, message, _) => OnChangeLobbyRulesFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <KickUserFailedMessageData>((currentPeer, message, _) => OnKickUserFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <GameStartRequestedMessageData>((_, message, __) => AssertIsUserInLobby <GameStartRequestedMessageData>((clientUser, clientLobby) => clientLobby.InvokeGameStartRequestedEventInternally(message.Time)));
     AddAutomaticMessageParser <GameStartedMessageData>((_, message, __) => AssertIsUserInLobby <GameStartedMessageData>((clientUser, clientLobby) => clientLobby.InvokeGameStartedEventInternally()));
     AddAutomaticMessageParser <StartGameFailedMessageData>((currentPeer, message, _) => OnStartGameFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <GameRestartRequestedMessageData>((_, message, __) => AssertIsUserInLobby <GameRestartRequestedMessageData>((clientUser, clientLobby) => clientLobby.InvokeGameRestartRequestedEventInternally(message.Time)));
     AddAutomaticMessageParser <GameRestartedMessageData>((_, message, js__on) => AssertIsUserInLobby <GameRestartedMessageData>((clientUser, clientLobby) => clientLobby.InvokeGameRestartedEventInternally()));
     AddAutomaticMessageParser <RestartGameFailedMessageData>((currentPeer, message, _) => OnRestartGameFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <GameStopRequestedMessageData>((_, message, __) => AssertIsUserInLobby <GameStopRequestedMessageData>((clientUser, clientLobby) => clientLobby.InvokeGameStopRequestedEventInternally(message.Time)));
     AddAutomaticMessageParser <GameStoppedMessageData>
     (
         (_, message, __) => AssertIsUserInLobby <GameStoppedMessageData>
         (
             (clientUser, clientLobby) =>
     {
         Dictionary <string, UserWithResults> users = new Dictionary <string, UserWithResults>();
         foreach (GameEndUserData user in message.Users)
         {
             string key = user.GUID.ToString();
             if (clientLobby.Users.ContainsKey(key))
             {
                 users.Add(key, new UserWithResults(clientLobby.Users[key], user.Results));
             }
             else
             {
                 SendErrorMessage <GameStoppedMessageData>(EErrorType.InvalidMessageContext, $"User with GUID \"{ key }\" is not in lobby \"{ clientLobby.Name }\" with lobby code \"{ clientLobby.LobbyCode }\".");
             }
         }
         clientLobby.InvokeGameStoppedEventInternally(users, message.Results);
     }
         )
     );
     AddAutomaticMessageParser <StopGameFailedMessageData>((currentPeer, message, _) => OnStopGameFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <StartGameCancelledMessageData>((currentPeer, message, _) => AssertIsUserInLobby <GameStoppedMessageData>((__, clientLobby) => clientLobby.InvokeStartGameCancelledEventInternally()));
     AddAutomaticMessageParser <RestartGameCancelledMessageData>((currentPeer, message, _) => AssertIsUserInLobby <GameStoppedMessageData>((__, clientLobby) => clientLobby.InvokeRestartGameCancelledEventInternally()));
     AddAutomaticMessageParser <StopGameCancelledMessageData>((currentPeer, message, _) => AssertIsUserInLobby <GameStoppedMessageData>((__, clientLobby) => clientLobby.InvokeStopGameCancelledEventInternally()));
     AddAutomaticMessageParser <CancelStartRestartStopGameTimerFailedMessageData>((currentPeer, message, _) => OnCancelStartRestartStopGameTimerFailed?.Invoke(currentPeer, message.Message, message.Reason));
     AddAutomaticMessageParser <ServerGameLoadingProcessFinishedMessageData>((currentPeer, message, _) => AssertTargetLobbyUser <ServerGameLoadingProcessFinishedMessageData>(message.UserGUID, (__, ___, targetUser) => targetUser.InvokeServerGameLoadingProcessFinishedEvent()));
     AddMessageParser <ServerTickMessageData>
     (
         (_, message, __) => AssertIsUserInLobby <ServerTickMessageData>((clientUser, clientLobby) => clientLobby.ProcessServerTickInternally(message.Time, message.Entities, message.Hits)),
         (_, message, __) => SendServerTickFailedMessage(message, EServerTickFailedReason.Unknown),
         MessageParseFailedEvent <ServerTickMessageData>
     );
     AddAutomaticMessageParser <ClientTickFailedMessageData>((currentPeer, message, _) => OnClientTickFailed?.Invoke(currentPeer, message.Message, message.Reason));
     OnPeerConnected += (_) => SendAuthenticateMessage(token);
 }
 public PokerLobbyPresenter(ILobbyView view)
 {
     HeaderMenuView.Instance.ShowInLobby();
     this.view = view;
     ViewStart();
 }
Example #6
0
 public PokerLobbyPresenter(ILobbyView view)
 {
     HeaderMenuView.Instance.ShowInLobby();
     this.view = view;
     ViewStart();
 }