Esempio n. 1
0
        public async Task <bool> SendAsync(string receiver, string title, string message)
        {
            // ToDo consume pen
            // ToDo check for ignore

            AccountEntity account;

            using (var db = _databaseService.Open <AuthContext>())
                account = await db.Accounts.FirstOrDefaultAsync(x => x.Nickname == receiver);

            if (account == null)
            {
                return(false);
            }

            using (var db = _databaseService.Open <GameContext>())
            {
                var entity = new PlayerMailEntity
                {
                    Id             = _idGeneratorService.GetNextId(IdKind.Mail),
                    PlayerId       = account.Id,
                    SenderPlayerId = (int)Player.Account.Id,
                    SentDate       = DateTimeOffset.Now.ToUnixTimeSeconds(),
                    Title          = title,
                    Message        = message,
                    IsMailNew      = true,
                    IsMailDeleted  = false
                };
                db.PlayerMails.Add(entity);

                var plr = _playerManager.GetByNickname(receiver);
                plr?.Mailbox.Add(new Mail(entity, receiver));
                return(true);
            }
        }
Esempio n. 2
0
        public async Task Save(GameContext db)
        {
            if (!_mailsToDelete.IsEmpty)
            {
                var idsToRemove = new List <long>();
                while (_mailsToDelete.TryPop(out var mailToDelete))
                {
                    idsToRemove.Add(mailToDelete.Id);
                }

                await db.PlayerMails.Where(x => idsToRemove.Contains(x.Id)).DeleteAsync();
            }

            foreach (var mail in _mails.Values.Where(x => x.IsDirty))
            {
                var entity = new PlayerMailEntity
                {
                    Id        = mail.Id,
                    IsMailNew = mail.IsNew
                };

                db.Attach(entity).Property(x => x.IsMailNew).IsModified = true;
                mail.SetDirtyState(false);
            }
        }