Ejemplo n.º 1
0
        /// <summary>
        /// Adds a player by name to the given room, from JSON elements: name and room. Inviter must be leader of the room
        /// </summary>
        /// <param name="inviter"></param>
        /// <returns></returns>
        public bool Invite(ChatPlayer inviter)
        {
            int room = inviter.chatContext.payload.room;
            string name = inviter.chatContext.payload.name;

            // Checks if client is leader
            if (!chatRooms.ContainsKey(room) || !chatRooms[room].IsLeader(inviter) || chatRooms[room].isStatic)
            {
                inviter.chatContext.SendTo(new Response(ChatService.ResponseType.NOT_LEADER, "You are not the leader of room " + room));
                Console.WriteLine("[CHAT] " + inviter.name + " tried to invite without being leader");
                return false;
            }

            ChatPlayer toInvite = OnlinePlayers.GetInstance().gameList.Where(x => x.Value.name == name).FirstOrDefault().Value.chPlayer;

            if (toInvite != null && toInvite.chatContext == null)
            {
                inviter.chatContext.SendTo(new Response(ChatService.ResponseType.INVITED_CLIENT, toInvite.name + " has deactivated the chat"));
                return false;
            }
            else if (chatRooms[room].AddClient(toInvite))
            {
                inviter.chatContext.SendTo(new Response(ChatService.ResponseType.INVITED_CLIENT, "You invited " + toInvite.name + " to room " + room));
                toInvite.chatContext.SendTo(new Response(ChatService.ResponseType.GOT_INVITED, "You got invited to room " + room + " by " + inviter.name));
                Console.WriteLine("[CHAT] " + inviter.name + " invited " + name + " to room: " + room);
                return true;
            }
            else
            {
                inviter.chatContext.SendTo(new Response(ChatService.ResponseType.CLIENT_NOT_FOUND, "Problem adding " + name + " to room " + room + ". Is the player online?"));
                Console.WriteLine("[CHAT] Problem adding " + name + " to room " + room + ". Already in room?");
                return false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Kicks a player by name from the given room, from JSON elements: name and room. Kicker must be leader of the room
        /// </summary>
        /// <param name="kicker"></param>
        /// <returns></returns>
        public bool Kick(ChatPlayer kicker)
        {
            int room = kicker.chatContext.payload.room;
            string name = kicker.chatContext.payload.name;

            // Checks if client is leader
            if (!chatRooms[room].IsLeader(kicker) || chatRooms[room].isStatic)
            {
                kicker.chatContext.SendTo(new Response(ChatService.ResponseType.NOT_LEADER, "You are not leader of room " + room));
                Console.WriteLine("[CHAT] " + kicker.name + " tried to kick without being leader");
                return false;
            }

            ChatPlayer toKick = OnlinePlayers.GetInstance().gameList.FirstOrDefault(x => x.Value.name == name).Value.chPlayer;

            if (chatRooms[room].RemoveClient(toKick))
            {
                kicker.chatContext.SendTo(new Response(ChatService.ResponseType.KICKED_CLIENT, "You kicked " + toKick.name + " from room " + room));
                toKick.chatContext.SendTo(new Response(ChatService.ResponseType.GOT_KICKED, "You got kicked from room " + room + " by " + kicker.name));
                Console.WriteLine("[CHAT] " + kicker.name + " kicked " + name + " from room: " + room);
            }
            else Console.WriteLine("[CHAT] Problem kicking " + name + " from room " + room + ". Client not in room?");
            return true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Broadcasts a message to all members in the specified room unless they have deactivated chat (checked in Broadcast function)
        /// </summary>
        /// <param name="client"></param>
        public void SendMessage(ChatPlayer client)
        {
            int room = client.chatContext.payload.room;
            string message = client.chatContext.payload.message;
            string name = client.name;

            ChatRoom chRoom;
            bool success = client.memberRooms.TryGetValue(room, out chRoom);

            if (success)
            {

                JObject retObj = new JObject(
                    new JProperty("message", message),
                    new JProperty("room", room),
                    new JProperty("name", name)
                    );

                chRoom.Broadcast(new Response(ChatService.ResponseType.CHAT_MESSAGE, retObj));
            }
            else
            {
                client.chatContext.SendTo(new Response(ChatService.ResponseType.CHAT_NOT_MEMBER, "You are not a member of that room!"));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Makes the specified client leave the given room, from JSON: room. If client was the last one in that room, the room gets deleted
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public bool LeaveRoom(ChatPlayer client)
        {
            int room = client.chatContext.payload.room;
            bool wasLeader = chatRooms[room].IsLeader(client);

            chatRooms[room].RemoveClient(client);

            if (chatRooms[room].clients.Count() == 0)
            {
                chatRooms.Remove(room);
                return true;
            }
            else if (chatRooms[room].clients.Contains(client))
            {
                client.chatContext.SendTo(new Response(ChatService.ResponseType.CHAT_ERROR, "Error leaving room " + room));
                Console.WriteLine("[CHAT] Error removing " + client.name + " from room " + room);
                return false;
            }
            else
            {
                chatRooms[room].Broadcast(new Response(ChatService.ResponseType.CHAT_MESSAGE, client.name + " left the room"));
                if (wasLeader)
                    chatRooms[room].clients.First().chatContext.SendTo(new Response(ChatService.ResponseType.MADE_LEADER, "You are now the leader of room " + room));
                client.chatContext.SendTo(new Response(ChatService.ResponseType.LEFT_ROOM, "You left room " + room));
                Console.WriteLine("[CHAT] " + client.name + " left room " + room);
                return true;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a chat room with the input client as the leader. Room is dynamic by default. Leader may invite and kick.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public bool Request_NewGameRoom(ChatPlayer client)
        {
            ChatRoom chatRoom = new ChatRoom(client);
            chatRooms.Add(roomCounter, chatRoom);

            client.chatContext.SendTo(new Response(ChatService.ResponseType.CHAT_ROOM_MADE, "You made a new chat room with id " + roomCounter));
            Console.WriteLine("[CHAT] " + client.name + " created room with id: " + roomCounter);

            return (chatRooms[roomCounter++].Equals(chatRoom)) ? true : false;
        }
Ejemplo n.º 6
0
        public void ChatLogin()
        {
            string hash = this.payload.hash;

            Console.WriteLine("Should be +1: " + OnlinePlayers.GetInstance().gameList.Count());

            KeyValuePair<General, Player> chClient = OnlinePlayers.GetInstance().chatList.Where(x => x.Value.hash == hash).FirstOrDefault(); //Chat Record
            // Remove chat client if it already exists //TODO?
            if(chClient.Key != null)
            {
                Player p;
                OnlinePlayers.GetInstance().chatList.TryRemove(chClient.Key, out p);
            }

            KeyValuePair<General, Player> gClient = OnlinePlayers.GetInstance().gameList.Where(x => x.Value.hash == hash).FirstOrDefault(); //Game Record
            if (gClient.Key != null)
            {
                Console.WriteLine("> [CHAT]: Found open game connection Linking.....");

                Player existingPlayer = gClient.Value;

                // Update the playerObject contexts
                existingPlayer.chatContext = this;

                // Insert the chatConnection record
                OnlinePlayers.GetInstance().chatList.TryAdd(this, existingPlayer);

                // Create a new chatPlayer
                if (existingPlayer.chPlayer == null)
                {
                    ChatPlayer chPlayer = new ChatPlayer(existingPlayer.name);
                    existingPlayer.chPlayer = chPlayer;
                    chPlayer.chatContext = existingPlayer.chatContext;
                }
                else
                {
                    // Update the context
                    existingPlayer.chPlayer.chatContext = existingPlayer.chatContext;
                    existingPlayer.chPlayer.AnnounceConnect();
                }

            }

            else // No existing game Player was found, create new player
            {
                 // General function for creating player
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Adds the specified client to this room unless it is null or already a member
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public bool AddClient(ChatPlayer client)
 {
     if (clients.Contains(client) || client == null) return false; // Prevents adding duplicates or nulls
     clients.AddLast(client); // Add client to room
     return (clients.Contains(client)) ? true : false; // Checks if the client was added successfully
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a dynamic chat room with the specified player as leader
 /// </summary>
 /// <param name="client"></param>
 public ChatRoom(ChatPlayer client)
 {
     isStatic = false;
     clients = new LinkedList<ChatPlayer>();
     clients.AddFirst(client);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Removes the specified client from this room
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public bool RemoveClient(ChatPlayer client)
 {
     return (clients.Remove(client)) ? true : false;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Checks if the specified client is the leader of this room
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public bool IsLeader(ChatPlayer client)
 {
     return (this.clients.First().Equals(client)) ? true : false;
 }