Ejemplo n.º 1
0
        public async Task CreateChat(Save.Request request, CancellationToken cancellationToken)
        {
            var advertisement = await _advertisementRepository.FindById(request.AdvertisementId, cancellationToken);

            if (advertisement == null)
            {
                throw new AdNotFoundException(request.AdvertisementId);
            }

            var userId = await _identityService.GetCurrentUserId(cancellationToken);

            var chatDuplicate = await _chatRepository.FindWhere(c => c.Advertisement.Id == advertisement.Id && c.BuyerId == userId, cancellationToken);

            if (chatDuplicate != null)
            {
                throw new ChatDuplicateException($"Чат с id {chatDuplicate.Id} уже создан");
            }

            if (userId == advertisement.OwnerId)
            {
                throw new ChatException($"Попытка создать чат с самим собой, пользователь с id {userId}, является владельцем объявления");
            }

            var chat = new Domain.Chat
            {
                Advertisement = advertisement,
                BuyerId       = userId,
                CreatedDate   = DateTime.UtcNow,
            };

            await _chatRepository.Save(chat, cancellationToken);
        }
        public async Task <IActionResult> Save([FromBody] Save.Request req)
        {
            var res = new Save.Response();

            evolib.Services.MasterData.ISeason currentSeason = MasterData.GetCurrentSeason();

            // プレイヤー毎
            foreach (var registerSaveData in req.playerRecordInfoList)
            {
                await SaveCareerRecordOfPlayer(req, registerSaveData, currentSeason);
            }

            return(Ok(res));
        }
        private async Task SaveCareerRecordOfPlayer(Save.Request req, Save.Request.PlayerRecordInfo registerSaveData, evolib.Services.MasterData.ISeason currentSeason)
        {
            //ランクマッチ時に現在のシーズンが取得出来なかったなら、処理を終了する
            //(カジュアルマッチ時は処理が行える為、呼び出し側ではなく内部でチェックする)
            if (req.matchType == evolib.Battle.MatchType.Rank && currentSeason == null)
            {
                return;
            }

            var playerId = registerSaveData.playerId;
            var db       = PDBSM.PersonalDBContext(playerId);

            int seasonNo = req.matchType == evolib.Battle.MatchType.Rank ? currentSeason.seasonNo : 0;

            var query = db.CareerRecords.Where(
                x => x.playerId == playerId &&
                x.matchType == req.matchType &&
                x.seasonNo == seasonNo);
            var records = await query.ToListAsync();

            foreach (var info in registerSaveData.recordList)
            {
                var careerRecord = MasterData.GetCareerRecord(info.id);

                if (careerRecord == null)
                {
                    continue;
                }

                foreach (var blockInfo in info.blocks)
                {
                    var blockRecord = new evolib.Databases.personal.CareerRecord();
                    blockRecord.playerId      = playerId;
                    blockRecord.recordItemId  = info.id;
                    blockRecord.matchType     = req.matchType;
                    blockRecord.seasonNo      = seasonNo;
                    blockRecord.mobileSuitId  = blockInfo.ms;
                    blockRecord.value         = double.Parse(blockInfo.val);
                    blockRecord.numForAverage = blockInfo.num;

                    // CareerRecordの1項目を特定
                    var recordData = records.Find(i =>
                                                  i.recordItemId == info.id &&
                                                  i.mobileSuitId == blockInfo.ms);

                    if (recordData == null)
                    {
                        await db.CareerRecords.AddAsync(blockRecord);
                    }
                    else
                    {
                        switch (careerRecord.valueType)
                        {
                        // 累積する場合
                        case evolib.CareerRecord.ValueType.Sum:
                        case evolib.CareerRecord.ValueType.SumAverage:
                        {
                            recordData.value         += blockRecord.value;
                            recordData.numForAverage += blockRecord.numForAverage;
                        }
                        break;

                        // 値が高い方を更新する場合
                        default:
                        {
                            // 平均値の場合
                            if (careerRecord.valueType == evolib.CareerRecord.ValueType.MaxAverage)
                            {
                                var averageValue       = blockRecord.numForAverage == 0 ? blockRecord.value : blockRecord.value / blockRecord.numForAverage;
                                var recordAverageValue = recordData.numForAverage == 0 ? recordData.value : recordData.value / recordData.numForAverage;
                                if (recordAverageValue < averageValue)
                                {
                                    recordData.value         = blockRecord.value;
                                    recordData.numForAverage = blockRecord.numForAverage;
                                }
                            }
                            // 平均値以外
                            else
                            {
                                if (recordData.value < blockRecord.value)
                                {
                                    recordData.value         = blockRecord.value;
                                    recordData.numForAverage = blockRecord.numForAverage;
                                }
                            }
                        }
                        break;
                        }

                        // 最大値を超えないように設定
                        recordData.value         = Math.Min(recordData.value, evolib.CareerRecord.RECORD_MAX_VALUE);
                        recordData.numForAverage = Math.Min(recordData.numForAverage, evolib.CareerRecord.RECORD_MAX_VALUE);
                    }
                }
            }

            await db.SaveChangesAsync();
        }