Example #1
0
        public void HandleMessage(LobbyPlayer player, Message message)
        {
            switch (message.Type)
            {
            case "finishTutorial":
            {
                player.PlayerObject.Set("tutorialVersion", Config.TutorialVersion);
                player.PlayerObject.Save(() => player.Send("finishTutorial"));
                return;
            }

            case "acceptTerms":
            {
                player.PlayerObject.Set("termsVersion", Config.TermsVersion);
                if (player.PlayerObject.Contains("acceptTerms"))
                {
                    player.PlayerObject.Remove("acceptTerms");
                }
                player.PlayerObject.Save(() => { player.Send("acceptTerms"); });
                return;
            }

            case "checkUsername":
            {
                var name = message.GetString(0);

                this.playerIo.BigDB.Load("Usernames", name.ToLower(),
                                         result =>
                    {
                        player.Send("checkUsername", name,
                                    result == null || (!result.Contains("owner") && !result.Contains("oldowner")));
                    },
                                         error => { player.Send("checkUsername", name, false); });
                return;
            }

            case "setUsername":
            {
                var username = player.PlayerObject.GetString("name", null);
                if (username == null && player.ConnectUserId != "simpleguest")
                {
                    var newname = message.GetString(0).ToLower();
                    var test    = new Regex("[^0-9a-z]");
                    if (test.IsMatch(newname))
                    {
                        player.Send("error",
                                    "Your username contains invalid charaters. Valid charaters are 0-9 and A-Z");
                    }
                    else if (newname.Length > 20)
                    {
                        player.Send("error", "Your username cannot be more than 20 characters long.");
                    }
                    else if (newname.Length < 3)
                    {
                        player.Send("error", "Your username must be atleast 3 characters long.");
                    }

                    else if (BadWords.ContainsBadWord(newname))
                    {
                        player.Send("error", "Your username contains inappropriate words.");
                    }
                    else
                    {
                        var obj = new DatabaseObject();
                        obj.Set("owner", player.ConnectUserId);
                        this.playerIo.BigDB.CreateObject("Usernames", newname, obj,
                                                         delegate
                            {
                                Console.WriteLine("Set Username " + newname + " --> " + player.Name);
                                player.PlayerObject.Set("name", newname);
                                player.PlayerObject.Save(delegate
                                {
                                    Console.WriteLine("Username saved " + player.Name);
                                    player.Send("username", newname);
                                    player.Send("setUsername");
                                });
                            },
                                                         delegate
                            {
                                player.Send("error", "The username " + newname.ToUpper() + " is already taken!");
                            }
                                                         );
                    }
                }
                else
                {
                    player.Send("username", username);
                }
                return;
            }

            case "changeUsername":
            {
                if (player.PayVault.Has("changeusername") && player.PlayerObject.GetBool("changename", false))
                {
                    var username = player.PlayerObject.GetString("name", null);
                    if (username != null && player.ConnectUserId != "simpleguest")
                    {
                        var newname = message.GetString(0).ToLower();
                        var test    = new Regex("[^0-9a-z]");
                        if (test.IsMatch(newname))
                        {
                            player.Send("error",
                                        "Your new username contains invalid charaters. Valid charaters are 0-9 and A-Z");
                        }
                        else if (newname.Length > 20)
                        {
                            player.Send("error", "Your new username cannot be more than 20 characters long.");
                        }
                        else if (newname.Length < 3)
                        {
                            player.Send("error", "Your new username must be atleast 3 characters long.");
                        }
                        else if (BadWords.ContainsBadWord(newname))
                        {
                            player.Send("error", "Your username contains inappropriate words.");
                        }
                        else
                        {
                            var obj = new DatabaseObject();
                            obj.Set("owner", player.ConnectUserId);
                            this.playerIo.BigDB.CreateObject("Usernames", newname, obj,
                                                             delegate
                                {
                                    this.playerIo.BigDB.Load("Usernames", username,
                                                             delegate(DatabaseObject o)
                                    {
                                        o.Set("oldowner", player.ConnectUserId);
                                        o.Set("owner", "none");
                                        o.Save();

                                        Console.WriteLine("Change Username " + username + " --> " + newname);
                                        player.PlayerObject.Set("name", newname);
                                        player.PlayerObject.Set("oldname", username);
                                        player.PlayerObject.Set("changename", false);
                                        player.PlayerObject.Save(delegate
                                        {
                                            Console.WriteLine("Username saved " + player.Name);
                                            player.Send("username", newname);
                                            player.Send("changeUsername");
                                        });
                                    }
                                                             );
                                },
                                                             delegate
                                {
                                    player.Send("error", "The username " + newname.ToUpper() + " is already taken!");
                                }
                                                             );
                        }
                    }
                    else
                    {
                        player.Send("username", username);
                    }
                }
                else
                {
                    player.Send("error",
                                "You didn't purchase your name change yet!");
                    return;
                }

                return;
            }


            default:
                return;
            }
        }
Example #2
0
        public void HandleMessage(LobbyPlayer player, Message m)
        {
            var rtn = Message.Create(m.Type);

            switch (m.Type)
            {
            case "getCrew":
            {
                this.LoadCrew(Regex.Replace(m.GetString(0), @"\s+", "").ToLower(),
                              crew => crew.SendGetMessage(player));
                break;
            }

            case "getMyCrews":
            {
                this.GetMyCrews(rtn, player);
                break;
            }

            case "createCrew":
            {
                this.CreateCrew(rtn, player, m.GetString(0).Trim());
                break;
            }

            case "getCrewInvites":
            {
                InvitationHelper.GetInvitationsTo(this.client.BigDB, InvitationType.Crew,
                                                  player.Name, invites =>
                    {
                        var invitesIds =
                            invites.Where(it => it.Status == InvitationStatus.Pending)
                            .Select(it => it.Sender)
                            .ToArray();
                        if (invitesIds.Length > 0)
                        {
                            this.client.BigDB.LoadKeys("Crews", invitesIds, crews =>
                            {
                                foreach (var crew in crews.Where(crew => crew != null))
                                {
                                    rtn.Add(crew.Key);
                                    rtn.Add(crew.GetString("Name"));
                                    rtn.Add(crew.GetString("LogoWorld", ""));
                                }

                                player.Send(rtn);
                            });
                        }
                        else
                        {
                            player.Send(rtn);
                        }
                    });
                break;
            }

            case "blockCrewInvites":
            {
                var crewId      = m.GetString(0).ToLower();
                var shouldBlock = m.GetBoolean(1);

                InvitationBlocking.BlockCrew(this.client.BigDB, player.ConnectUserId, crewId, shouldBlock, () =>
                    {
                        if (shouldBlock)
                        {
                            InvitationHelper.GetInvitation(this.client.BigDB, InvitationType.Crew, crewId, player.Name,
                                                           invitation =>
                            {
                                if (invitation.Exists)
                                {
                                    invitation.Status = InvitationStatus.Rejected;
                                    invitation.Save(() => player.Send(m.Type, true));
                                }
                                else
                                {
                                    player.Send(m.Type, true);
                                }
                            });
                        }
                        else
                        {
                            player.Send(m.Type, true);
                        }
                    });
                break;
            }

            case "blockAllCrewInvites":
            {
                var shouldBlock = m.GetBoolean(0);

                InvitationBlocking.BlockAllCrews(this.client.BigDB, player.ConnectUserId, shouldBlock);
                player.Send(m.Type, shouldBlock);
                break;
            }
            }
        }
Example #3
0
        private void CreateCrew(Message rtn, LobbyPlayer player, string crewName)
        {
            player.PayVault.Refresh(() =>
            {
                this.client.BigDB.LoadRange("Crews", "ByCreator", new object[] { player.ConnectUserId }, null, null, 100,
                                            ownedCrews =>
                {
                    var canCreateNewCrew = player.PayVault.Count("crew") >
                                           ownedCrews.Count(it => !it.GetBool("Disbanded", false));
                    if (!canCreateNewCrew)
                    {
                        this.SendErrorReply(rtn, player, "Cannot create crew. First buy crew item in shop.");
                        return;
                    }

                    // Replace repeating spaces with one space
                    crewName = new Regex(@"[ ]{2,}").Replace(crewName, " ");

                    var test   = new Regex("^[A-Za-z0-9 ]*$");
                    var crewId = Regex.Replace(crewName, @"\s+", "").ToLower();
                    if (!test.IsMatch(crewName))
                    {
                        this.SendErrorReply(rtn, player,
                                            "Selected name contains invalid charaters. Valid charaters are 0-9, A-Z, a-z and space.");
                    }
                    else if (crewName.Length > 25)
                    {
                        this.SendErrorReply(rtn, player, "Crew name cannot be more than 25 characters long.");
                    }
                    else if (crewName.Length < 2)
                    {
                        this.SendErrorReply(rtn, player, "Crew name must be at least 2 characters long.");
                    }
                    else if (BadWords.ContainsBadWord(crewName) || BadWords.ContainsBadWord(crewId))
                    {
                        this.SendErrorReply(rtn, player, "Crew name contains inappropriate words.");
                    }
                    else
                    {
                        Console.WriteLine("Id for \"{0}\" crew = {1}", crewName, crewId);

                        var dbo = new DatabaseObject()
                                  .Set("Creator", player.ConnectUserId)
                                  .Set("Name", crewName)
                                  .Set("Subscribers", (uint)1)
                                  .Set("Ranks", new DatabaseArray()
                                       .Add(new DatabaseObject()
                                            .Set("Name", "Owner"))
                                       .Add(new DatabaseObject()
                                            .Set("Name", "Member")
                                            .Set("Powers", "0")))
                                  .Set("Members", new DatabaseObject()
                                       .Set(player.ConnectUserId, new DatabaseObject()
                                            .Set("Rank", 0)));

                        this.client.BigDB.CreateObject("Crews", crewId, dbo, newDbo =>
                        {
                            rtn.Add(true);
                            rtn.Add(newDbo.Key);

                            NotificationHelper.AddSubscription(this.client.BigDB, player.ConnectUserId,
                                                               "crew" + crewId);

                            this.client.BigDB.LoadOrCreate("CrewMembership", player.ConnectUserId, membership =>
                            {
                                membership.Set(crewId, crewName);
                                membership.Save(() => player.PlayerObject.Save(() => player.Send(rtn)));
                            });
                        }, error =>
                        {
                            // TODO: Improve error messages ?
                            this.SendErrorReply(rtn, player, error.Message);
                        });
                    }
                });
            });
        }
Example #4
0
 private void SendErrorReply(Message rtn, LobbyPlayer player, string error)
 {
     rtn.Add(false);
     rtn.Add(error);
     player.Send(rtn);
 }
Example #5
0
        public void ProcessMessages(QueueItem item, LobbyPlayer player)
        {
            switch (item.Method)
            {
            case "getFriends":
            {
                this.GetFriendKeys(player.ConnectUserId, keys =>
                    {
                        if (keys.Count <= 0)
                        {
                            player.Send(item.Method);
                            return;
                        }

                        OnlineStatus.GetOnlineStatus(this.client, keys.ToArray(), status =>
                        {
                            var rtn = Message.Create(item.Method);
                            foreach (var stat in status.Where(stat => stat != null))
                            {
                                stat.ToMessage(rtn);
                            }
                            player.Send(rtn);
                        });
                    });
                break;
            }

            case "getPending":
            {
                InvitationHelper.GetInvitationsFrom(this.client.BigDB, InvitationType.Friend,
                                                    this.name, invites =>
                    {
                        var rtn = Message.Create(item.Method);
                        foreach (var invite in invites)
                        {
                            rtn.Add(invite.Recipient);
                            rtn.Add((int)invite.Status);
                        }
                        player.Send(rtn);
                    });
                break;
            }

            case "getInvitesToMe":
            {
                InvitationHelper.GetInvitationsTo(this.client.BigDB, InvitationType.Friend,
                                                  this.name, invites =>
                    {
                        var rtn = Message.Create(item.Method);
                        foreach (var invite in invites.Where(it => it.Status == InvitationStatus.Pending))
                        {
                            rtn.Add(invite.Sender);
                        }
                        player.Send(rtn);
                    });
                break;
            }

            case "getBlockedUsers":
            {
                InvitationBlocking.GetBlockedUsers(this.client.BigDB, this.connectUserId, blockedUsers =>
                    {
                        var rtn = Message.Create(item.Method);
                        foreach (var blockedUser in blockedUsers)
                        {
                            rtn.Add(blockedUser);
                        }
                        player.Send(rtn);
                    });
                break;
            }

            case "createInvite":
            {
                if (!player.HasFriendFeatures)
                {
                    player.Send(item.Method, false);
                    return;
                }

                var friendName = item.Message.GetString(0).ToLower();

                this.CreateInvitation(friendName,
                                      () => player.Send(item.Method, true),
                                      error =>
                    {
                        switch (error)
                        {
                        case InvitationError.PlayerNotFound:
                            {
                                player.Send(item.Method, false,
                                            "Unknown user. Please check your spelling.");
                                break;
                            }

                        case InvitationError.AlreadyAdded:
                            {
                                player.Send(item.Method, false,
                                            "This user is already on your friendslist!");
                                break;
                            }

                        case InvitationError.AlreadySent:
                            {
                                player.Send(item.Method, false,
                                            "You already have a pending invitation for this user.");
                                break;
                            }

                        case InvitationError.LimitReached:
                            {
                                player.Send(item.Method, false,
                                            "You cannot have more than " +
                                            this.MaxFriendsAllowed +
                                            " friends and invites.");
                                break;
                            }

                        case InvitationError.Blocked:
                            {
                                player.Send(item.Method, false,
                                            "This user is blocking friend requests.");
                                break;
                            }

                        case InvitationError.SendingToSelf:
                            {
                                player.Send(item.Method, false,
                                            "You cannot add yourself.");
                                break;
                            }
                        }
                    });
                break;
            }

            case "answerInvite":
            {
                if (!player.HasFriendFeatures)
                {
                    player.Send(item.Method, false);
                    return;
                }

                var invitedBy = item.Message.GetString(0).ToLower();
                var accept    = item.Message.GetBoolean(1);

                this.AnswerInvitation(invitedBy, accept,
                                      senderId =>
                    {
                        OnlineStatus.GetOnlineStatus(this.client, senderId, onlineStatus =>
                        {
                            var rtn = Message.Create(item.Method, true);
                            player.Send(onlineStatus.ToMessage(rtn));
                        });
                    },
                                      error =>
                    {
                        switch (error)
                        {
                        case InvitationError.PlayerNotFound:
                            {
                                player.Send(item.Method, false,
                                            "Sorry, the sender of this friend request does not exist.");
                                break;
                            }

                        case InvitationError.InvitationNotFound:
                            {
                                player.Send(item.Method, false,
                                            "Sorry, the friend request does not exist anymore.");
                                break;
                            }

                        case InvitationError.LimitReached:
                            {
                                player.Send(item.Method, false,
                                            "You cannot have more than " +
                                            this.MaxFriendsAllowed +
                                            " friends.");
                                break;
                            }
                        }
                    });
                break;
            }

            case "deleteInvite":
            {
                var recipientName = item.Message.GetString(0).ToLower();

                InvitationHelper.DeleteInvitation(this.client.BigDB, InvitationType.Friend,
                                                  this.name, recipientName, success =>
                    {
                        if (!success)
                        {
                            this.client.ErrorLog.WriteError(
                                "Error deleting invitation from " + player.Name + " to " +
                                recipientName, "Invite not found",
                                "Error deleting pending invitation", null);
                        }
                        player.Send(item.Method, success);
                    });
                break;
            }

            case "blockUserInvites":
            {
                var invitedByName = item.Message.GetString(0).ToLower();
                var shouldBlock   = item.Message.GetBoolean(1);

                InvitationBlocking.BlockUser(this.client.BigDB, this.connectUserId, invitedByName, shouldBlock,
                                             () =>
                    {
                        if (shouldBlock)
                        {
                            InvitationHelper.GetInvitation(this.client.BigDB, InvitationType.Friend, invitedByName,
                                                           this.name, invitation =>
                            {
                                if (invitation.Exists)
                                {
                                    invitation.Status = InvitationStatus.Rejected;
                                    invitation.Save(() => player.Send(item.Method, true));
                                }
                                else
                                {
                                    player.Send(item.Method, true);
                                }
                            });
                        }
                        else
                        {
                            player.Send(item.Method, true);
                        }
                    });
                break;
            }

            case "getBlockStatus":
            {
                InvitationBlocking.IsBlockingAllUsers(this.client.BigDB, this.connectUserId,
                                                      isBlocking => { player.Send(item.Method, isBlocking); });
                break;
            }

            case "blockAllInvites":
            {
                var shouldBlock = item.Message.GetBoolean(0);

                InvitationBlocking.BlockAllFriends(this.client.BigDB, this.connectUserId, shouldBlock);
                player.Send(item.Method, shouldBlock);
                break;
            }

            case "deleteFriend":
            {
                CommonPlayer.GetId(this.client.BigDB, item.Message.GetString(0).ToLower(), friendId =>
                    {
                        this.AddOrRemoveFriend(friendId, false,
                                               delegate { player.Send(item.Method, true); });
                    });
                break;
            }

            case "GetOnlineStatus":
            {
                var id = item.Message.Count > 0 ? item.Message.GetString(0) : player.ConnectUserId;
                OnlineStatus.GetOnlineStatus(this.client, id,
                                             onlineStatus => player.Send(onlineStatus.ToMessage(item.Method)));
                break;
            }
            }
        }