Esempio n. 1
0
        public async Task ClubCreateReq2(GameSession session, ClubCreateReq2Message message)
        {
            var plr = session.Player;

            if (plr == null)
            {
                return;
            }

            var ascii = Config.Instance.Game.NickRestrictions.AsciiOnly;

            if (GameServer.Instance.ClubManager.Any(c =>
                                                    c.ClanName == message.Name || c.Players.ContainsKey(plr.Account.Id)) ||
                !Namecheck.IsNameValid(message.Name, true) ||
                ascii && message.Name.Any(c => c > 127) ||
                !ascii && message.Name.Any(c => c > 255))
            {
                Logger.ForAccount(plr).Information($"Couldnt create Clan : {message.Name}");
                await session.SendAsync(new ClubCreateAck2Message(1));
            }
            else
            {
                var clubDto = new ClubDto
                {
                    Name = message.Name,
                    Icon = ""
                };

                using (var db = GameDatabase.Open())
                {
                    try
                    {
                        using (var transaction = DbUtil.BeginTransaction(db))
                        {
                            await DbUtil.InsertAsync(db, clubDto,
                                                     statement => statement.AttachToTransaction(transaction));

                            var clubPlayerInfo = new ClubPlayerInfo
                            {
                                AccountId = session.Player.Account.Id,
                                Account   = session.Player.Account.AccountDto,
                                State     = ClubState.Joined,
                                Rank      = ClubRank.Master
                            };

                            var club = new Club(clubDto, new[] { clubPlayerInfo });
                            GameServer.Instance.ClubManager.Add(club);
                            transaction.Commit();

                            var clubplrdto = new ClubPlayerDto
                            {
                                PlayerId = (int)session.Player.Account.Id,
                                ClubId   = club.Id,
                                Rank     = (byte)ClubRank.Master,
                                State    = (int)ClubState.Joined
                            };
                            await DbUtil.InsertAsync(db, clubplrdto);

                            session.Player.Club = club;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.ToString());
                        await session.SendAsync(new ClubCreateAck2Message(1));

                        return;
                    }

                    await session.SendAsync(new ClubCreateAck2Message(0));

                    await session.SendAsync(new ClubMyInfoAckMessage(plr.Map <Player, ClubMyInfoDto>()));

                    Club.LogOn(plr);
                }
            }
        }