コード例 #1
0
        public PublicChannel CreatePublicChannel(WarsimUser owner, string name)
        {
            var channel = new PublicChannel(name, owner);

            this.Channels.TryAdd(channel.Id, channel);

            return(channel);
        }
コード例 #2
0
        public Conversation CreateConversation(WarsimUser creator, WarsimUser secondUser)
        {
            var channel = new Conversation(creator, secondUser);

            this.Channels.TryAdd(channel.Id, channel);

            return(channel);
        }
コード例 #3
0
        public Channel JoinChannel(WarsimUser user, Guid channelId)
        {
            var channel = this.GetChannelById(channelId);

            channel.AddUser(user);

            return(channel);
        }
コード例 #4
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
        public GameHost(WarsimUser owner, Map map, string password = "")
        {
            this.Map         = map;
            this.Map.OwnerId = owner.UserId;

            if (!this.Map.ValidatePassword(password))
            {
                throw HttpResponseExceptionHelper.Create("Wrong game password", HttpStatusCode.BadRequest);
            }
        }
コード例 #5
0
 public static WarsimClientUser Map(WarsimUser user)
 {
     return(new WarsimClientUser
     {
         Id = user.UserId,
         Username = user.Username,
         GameId = user.ActiveGameId,
         IsConnected = true
     });
 }
コード例 #6
0
        public bool RemoveUser(WarsimUser user)
        {
            if (this.ActiveUsers.Contains(user))
            {
                this.ActiveUsers.Remove(user);

                return(true);
            }

            return(false);
        }
コード例 #7
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
        public void Spectate(WarsimUser user, string password = "")
        {
            if (this.Spectators.ContainsKey(user.UserId))
            {
                throw new GameException(2006, "The user is already spectating");
            }

            if (!this.Map.ValidatePassword(password))
            {
                throw new GameException(2004, "Wrong game password");
            }

            this.Spectators.TryAdd(user.UserId, user);
        }
コード例 #8
0
        public void SendGameInvite(GameHost game, WarsimUser inviter, string inviteeId)
        {
            var msg = new GameInviteMessage
            {
                GameId   = game.Id,
                Title    = game.Map.Title,
                UserId   = inviter.UserId,
                Username = inviter.Username
            };

            var not = this.AddNotificationEntity(msg, inviteeId);

            msg.NotificationId = not.Id;
            this.SendWebSocketNotification(inviteeId, msg);
        }
コード例 #9
0
        public void AddUser(WarsimUser user)
        {
            if (this.ActiveUsers.Count + 1 > this.MaxUserCount)
            {
                throw HttpResponseExceptionHelper.Create(
                          $"Could not add user {user.Username}: " +
                          $"channel already contains the maximum number of users ({this.MaxUserCount})", HttpStatusCode.BadRequest);
            }

            if (this.ActiveUsers.Contains(user))
            {
                throw HttpResponseExceptionHelper.Create($"User {user.Username} is already inside this channel", HttpStatusCode.BadRequest);
            }

            this.ActiveUsers.Add(user);
        }
コード例 #10
0
        public void SendMessage(WarsimUser user, Channel channel, DateTime timestamp, string message)
        {
            // If the user is inside the channel
            if (channel.ActiveUsers.Any(x => x.UserId == user.UserId) && user.ActiveChannelId == channel.Id)
            {
                channel.Messages.Add(new Message
                {
                    UserId    = user.UserId,
                    Content   = message,
                    Timestamp = timestamp
                });

                return;
            }

            throw HttpResponseExceptionHelper.Create($"Could not send message: user {user.Username} isn't inside this channel", HttpStatusCode.BadRequest);
        }
コード例 #11
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
        // Returns a bool stating if the game is still active or not
        public bool Leave(WarsimUser user)
        {
            this.Players.TryRemove(user.UserId, out user);

            // No more players, game is over now
            if (this.Players.Count == 0)
            {
                return(false);
            }

            // If the owner leaves, find a new one
            if (this.OwnerId == user.UserId)
            {
                this.Map.OwnerId = this.Players.Keys.First();
            }

            return(true);
        }
コード例 #12
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
        public void Join(WarsimUser user, string password = "")
        {
            if (this.Players.ContainsKey(user.UserId))
            {
                throw new GameException(2003, "The user is playing");
            }

            if (!this.Map.ValidatePassword(password))
            {
                throw new GameException(2004, "Wrong game password");
            }

            if (this.Players.Count >= MaxPlayerCount)
            {
                throw new GameException(2005, $"Game is full ({MaxPlayerCount} players)");
            }

            this.Players.TryAdd(user.UserId, user);
        }
コード例 #13
0
        protected override void OnWebSocketOpen()
        {
            var userToken       = JwtHelper.DecodeToken(this.Context.QueryString.Get("auth_token"));
            var channelIdString = this.Context.QueryString.Get("channel_id");

            Guid channelId;

            // If invalid token, close the connection
            if (userToken == null || string.IsNullOrEmpty(channelIdString) || !Guid.TryParse(channelIdString, out channelId))
            {
                this.Sessions.CloseSession(this.ID, 2000, "Invalid query parameters");
                return;
            }

            try
            {
                this._warsimUser = this._gameManager.GetUser(userToken.UserId);
            }
            catch
            {
                this.Sessions.CloseSession(this.ID, 2001, "This user isn't connected");
                return;
            }

            this._channel = this._chatManager.JoinChannel(this._warsimUser, channelId);

            this._warsimUser.ChatWebSocketId = this.ID;
            this._warsimUser.ActiveChannelId = channelId;

            // Send current channel state to new user
            this.Send(EventBuilder.Build(new ChannelStateMessage
            {
                ActiveUsers = this._channel.ActiveUsers.Select(WarsimClientUser.Map).ToList(),
                Messages    = this._channel.Messages
            }).Serialize());

            this.BroadcastToChannel(EventBuilder.Build(new UserJoinedChannelMessage
            {
                UserId   = this._warsimUser.UserId,
                Username = this._warsimUser.Username
            }).Serialize());
        }
コード例 #14
0
        protected override void OnWebSocketOpen()
        {
            var userToken = JwtHelper.DecodeToken(this.Context.QueryString.Get("auth_token"));
            var udpPort   = this.Context.QueryString.Get("udp_port");

            // If invalid query parameters, close the connection
            if (userToken == null || string.IsNullOrEmpty(udpPort))
            {
                this.Sessions.CloseSession(this.ID, 2000, "Invalid query parameters");
                return;
            }

            // Add user to connected users
            if (this._userManager.Users.ContainsKey(userToken.UserId))
            {
                this.Sessions.CloseSession(this.ID, 2001, "User already connected");
                return;
            }

            this._warsimUser = this._userManager.ConnectUser(userToken.UserId, userToken.Username);
            this._warsimUser.UserWebSocketId = this.ID;

            // Save user UDP endpoint
            this._warsimUser.GameUdpEndPoint = new IPEndPoint(
                this.Context.UserEndPoint.Address,
                int.Parse(udpPort));

            // User has now connected to the game and is visible to other users
            var ev = EventBuilder.Build(new UserConnectedMessage
            {
                UserId   = this._warsimUser.UserId,
                Username = this._warsimUser.Username
            });

            this.Sessions.Broadcast(ev.Serialize());
        }
コード例 #15
0
 public void LeaveChannel(WarsimUser user, Guid channelId)
 {
     this.GetChannelById(channelId).RemoveUser(user);
 }
コード例 #16
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
 public GameHost(WarsimUser owner, IList <Node> sceneObjects, string title, string description, string password = "")
 {
     this.Map = Map.CreateNewMap(sceneObjects, owner.UserId, title, description, password);
 }
コード例 #17
0
ファイル: GameHost.cs プロジェクト: sebastiencrevier/Warsim
 public GameHost(WarsimUser owner, string title, string description, string password = "")
 {
     this.Map = Map.CreateEmptyMap(owner.UserId, title, description, password);
 }
コード例 #18
0
 public PublicChannel(string name, WarsimUser owner)
     : base(ChannelType.PublicChannel, 20)
 {
     this.Name  = name;
     this.Owner = owner;
 }
コード例 #19
0
 public Conversation(WarsimUser user1, WarsimUser user2)
     : base(ChannelType.PrivateConversation, 2)
 {
     this.User1 = user1;
     this.User2 = user2;
 }
コード例 #20
0
        protected override void OnWebSocketOpen()
        {
            var userToken   = JwtHelper.DecodeToken(this.Context.QueryString.Get("auth_token"));
            var mapIdString = this.Context.QueryString.Get("map_id");

            Guid mapId;

            if (userToken == null || string.IsNullOrEmpty(mapIdString) || !Guid.TryParse(mapIdString, out mapId))
            {
                this.Sessions.CloseSession(this.ID, 2000, "Invalid query parameters");
                return;
            }

            this._warsimUser = this._gameManager.GetUser(userToken.UserId);

            if (this._warsimUser == null)
            {
                this.Sessions.CloseSession(this.ID, 2001, "This user isn't connected");
                return;
            }

            var mapRepo = new MapRepository(new ApplicationDbContext());

            var map = mapRepo.GetMapById(mapId);

            if (map == null)
            {
                this.Sessions.CloseSession(this.ID, 2002, "This map doesn't exist");
                return;
            }

            var password = this.Context.QueryString.Get("map_password");

            if (!map.ValidatePassword(password))
            {
                this.Sessions.CloseSession(this.ID, 2003, "Wrong map password");
                return;
            }

            if (map.IsLocked)
            {
                this.Sessions.CloseSession(this.ID, 2004, "This map is locked");
                return;
            }

            this._localGame = new LocalGame
            {
                Map    = map,
                UserId = this._warsimUser.UserId
            };

            this._warsimUser.LocalGameWebSocketId = this.ID;
            this._gameManager.StartLocalGame(this._localGame);

            var localGameState = new LocalGameStateMessage
            {
                SceneObjects = map.SceneObjects
            };

            this.Sessions.SendTo(EventBuilder.Build(localGameState).Serialize(), this._warsimUser.LocalGameWebSocketId);
        }
コード例 #21
0
        protected override void OnWebSocketOpen()
        {
            var userToken    = JwtHelper.DecodeToken(this.Context.QueryString.Get("auth_token"));
            var gameIdString = this.Context.QueryString.Get("game_id");

            Guid gameId;

            if (userToken == null || string.IsNullOrEmpty(gameIdString) || !Guid.TryParse(gameIdString, out gameId))
            {
                this.Sessions.CloseSession(this.ID, 2000, "Invalid query parameters");
                return;
            }

            if (!this._gameManager.GameHosts.ContainsKey(gameId))
            {
                this.Sessions.CloseSession(this.ID, 2001, "This game doesn't exist");
                return;
            }

            this._gameHost   = this._gameManager.GameHosts[gameId];
            this._warsimUser = this._gameManager.GetUser(userToken.UserId);

            if (this._warsimUser == null)
            {
                this.Sessions.CloseSession(this.ID, 2002, "This user isn't connected");
                return;
            }

            var password   = this.Context.QueryString.Get("game_password");
            var playerType = this.Context.QueryString.Get("player_type");

            if (playerType == "spectator")
            {
                // Start spectating, without telling other players
                try
                {
                    this._gameHost.Spectate(this._warsimUser, password);
                }
                catch (GameException e)
                {
                    this.Sessions.CloseSession(this.ID, e.Code, e.Message);
                    return;
                }
            }
            else
            {
                try
                {
                    this._gameHost.Join(this._warsimUser, password);
                }
                catch (GameException e)
                {
                    this.Sessions.CloseSession(this.ID, e.Code, e.Message);
                    return;
                }

                var msg = new PlayerConnectedMessage
                {
                    UserId   = this._warsimUser.UserId,
                    Username = this._warsimUser.Username
                };

                this.BroadcastToCurrentGame(EventBuilder.Build(msg).Serialize());
            }

            var statsUpdate = new GameStatisticsUpdate {
                UserId = this._warsimUser.UserId
            };

            if (this.IsGameOwner())
            {
                statsUpdate.GameCreatedCount++;
            }
            else
            {
                statsUpdate.GameJoinedCount++;
            }

            new UserRepository(new ApplicationDbContext()).UpdateUserStatistics(statsUpdate);

            this._warsimUser.GameHostWebSocketId = this.ID;
            this._warsimUser.ActiveGameId        = this._gameHost.Id;
        }