Пример #1
0
        public IHttpActionResult DeleteCountry(int countryId)
        {
            CountryRequest request = new CountryRequest
            {
                CountryDto = new CountryDto {
                    CountryId = countryId
                }
            };
            List <string>  errors  = ValidateDeleteCountry(request);
            CountryMessage message = new CountryMessage();

            if (errors != null && errors.Any())
            {
                message.ErrorMessage     = CountryMessageResource.ValidationErrors;
                message.ErrorType        = ErrorType.ValidationError;
                message.Errors           = new List <string>();
                message.OperationSuccess = false;
                message.Errors.AddRange(errors);
            }
            else
            {
                message = _serviceCountryClient.DeleteCountry(request);
            }
            return(Json(message));
        }
Пример #2
0
        /// <summary>
        /// Get list of Country
        /// </summary>
        /// <returns>Country message.</returns>
        public CountryMessage GetAllCountries()
        {
            CountryMessage message = new CountryMessage();

            try
            {
                message = _serviceCountry.GetAllCountries().ToMessage();
                message.OperationSuccess = true;
            }
            catch (Exception e)
            {
                message.ErrorType    = ErrorType.TechnicalError;
                message.ErrorMessage = e.Message;
            }
            return(message);
        }
Пример #3
0
        /// <summary>
        /// Delete Country
        /// </summary>
        /// <param name="request">country request.</param>
        /// <returns>Country message.</returns>
        public CountryMessage DeleteCountry(CountryRequest request)
        {
            CountryMessage message = new CountryMessage();

            try
            {
                _serviceCountry.DeleteCountry(request.ToPivot());
                message.OperationSuccess = true;
            }
            catch (Exception e)
            {
                message.ErrorType    = ErrorType.TechnicalError;
                message.ErrorMessage = e.Message;
            }
            return(message);
        }
Пример #4
0
        public IHttpActionResult FindCountries(CountryRequest request)
        {
            List <string>  errors  = ValidateFindCountries(request);
            CountryMessage message = new CountryMessage();

            if (errors != null && errors.Any())
            {
                message.ErrorMessage     = CountryMessageResource.ValidationErrors;
                message.ErrorType        = ErrorType.ValidationError;
                message.Errors           = new List <string>();
                message.OperationSuccess = false;
                message.Errors.AddRange(errors);
            }
            else
            {
                message = _serviceCountryClient.FindCountries(request);
            }
            return(Json(message));
        }
Пример #5
0
        public async Task SetPromotionStatusAsync(
            [FromBody] ChatMessage message)
        {
            Character          newCharacter      = null;
            CharacterLog       charalog          = null;
            CharacterLog       senderCharalog    = null;
            MapLog             maplog            = null;
            Town               newTown           = null;
            Town               oldTown           = null;
            CountryMessage     commanders        = null;
            IEnumerable <uint> oldTownCharacters = null;
            IEnumerable <uint> newTownCharacters = null;

            using (var repo = MainRepository.WithReadAndWrite())
            {
                var system = await repo.System.GetAsync();

                var chara = await repo.Character.GetByIdAsync(this.AuthData.CharacterId).GetOrErrorAsync(ErrorCode.CharacterNotFoundError);

                var old = await repo.ChatMessage.GetByIdAsync(message.Id).GetOrErrorAsync(ErrorCode.InvalidParameterError);

                var country = await repo.Country.GetAliveByIdAsync(chara.CountryId);

                if (country.HasData)
                {
                    // 無所属以外は実行できない
                    ErrorCode.InvalidOperationError.Throw();
                }
                if (old.Type != ChatMessageType.Promotion)
                {
                    // 登用文ではない
                    ErrorCode.InvalidOperationError.Throw();
                }

                if (message.Type == ChatMessageType.PromotionAccepted)
                {
                    var sender = await repo.Character.GetByIdAsync(old.TypeData).GetOrErrorAsync(ErrorCode.CharacterNotFoundError);

                    var senderCountry = await repo.Country.GetAliveByIdAsync(old.CharacterCountryId).GetOrErrorAsync(ErrorCode.CountryNotFoundError);

                    if (senderCountry.IntEstablished + Config.CountryBattleStopDuring > system.IntGameDateTime)
                    {
                        var characterCount = await repo.Country.CountCharactersAsync(senderCountry.Id, true);

                        if (characterCount >= Config.CountryJoinMaxOnLimited)
                        {
                            // 戦闘解除前の国に士官できない
                            ErrorCode.CantJoinAtSuchCountryhError.Throw();
                        }
                    }

                    var blockActions = await repo.BlockAction.GetAvailableTypesAsync(chara.Id);

                    if (blockActions.Contains(BlockActionType.StopJoin) && !system.IsWaitingReset)
                    {
                        ErrorCode.BlockedActionError.Throw();
                    }

                    oldTown = await repo.Town.GetByIdAsync(chara.TownId).GetOrErrorAsync(ErrorCode.TownNotFoundError);

                    newTown = await repo.Town.GetByIdAsync(senderCountry.CapitalTownId).GetOrErrorAsync(ErrorCode.TownNotFoundError);

                    oldTownCharacters = (await repo.Town.GetCharactersWithIconAsync(oldTown.Id)).Select(c => c.Character.Id);
                    newTownCharacters = (await repo.Town.GetCharactersWithIconAsync(newTown.Id)).Select(c => c.Character.Id);
                    commanders        = (await repo.Country.GetMessageAsync(senderCountry.Id, CountryMessageType.Commanders)).Data;

                    var reinforcements = await repo.Reinforcement.GetByCharacterIdAsync(chara.Id);

                    if (reinforcements.Any(r => r.Status == ReinforcementStatus.Active))
                    {
                        ErrorCode.NotPermissionError.Throw();
                    }

                    await CharacterService.ChangeTownAsync(repo, senderCountry.CapitalTownId, chara);

                    await CharacterService.ChangeCountryAsync(repo, senderCountry.Id, new Character[] { chara, });

                    charalog = new CharacterLog
                    {
                        CharacterId  = chara.Id,
                        DateTime     = DateTime.Now,
                        GameDateTime = system.GameDateTime,
                        Message      = $"<character>{sender.Name}</character> の登用に応じ、 <country>{senderCountry.Name}</country> に仕官しました",
                    };
                    senderCharalog = new CharacterLog
                    {
                        CharacterId  = old.TypeData,
                        DateTime     = DateTime.Now,
                        GameDateTime = system.GameDateTime,
                        Message      = $"<character>{chara.Name}</character> があなたの登用に応じ、 <country>{senderCountry.Name}</country> に仕官しました",
                    };
                    maplog = new MapLog
                    {
                        Date            = DateTime.Now,
                        ApiGameDateTime = system.GameDateTime,
                        IsImportant     = false,
                        EventType       = EventType.PromotionAccepted,
                        Message         = $"<character>{chara.Name}</character> は <country>{senderCountry.Name}</country> に仕官しました",
                    };

                    old.Character    = new CharacterChatData(sender, null);
                    old.ReceiverName = chara.Name;
                    old.Type         = message.Type;
                    newCharacter     = chara;
                }
                else if (message.Type == ChatMessageType.PromotionRefused)
                {
                    var sender = await repo.Character.GetByIdAsync(old.TypeData).GetOrErrorAsync(ErrorCode.CharacterNotFoundError);

                    charalog = new CharacterLog
                    {
                        CharacterId  = chara.Id,
                        DateTime     = DateTime.Now,
                        GameDateTime = system.GameDateTime,
                        Message      = $"<character>{sender.Name}</character> の登用を断りました",
                    };
                    senderCharalog = new CharacterLog
                    {
                        CharacterId  = old.TypeData,
                        DateTime     = DateTime.Now,
                        GameDateTime = system.GameDateTime,
                        Message      = $"<character>{chara.Name}</character> は、あなたの登用を断りました",
                    };

                    old.Character    = new CharacterChatData(sender, null);
                    old.ReceiverName = chara.Name;
                    old.Type         = message.Type;
                }
                else
                {
                    ErrorCode.InvalidParameterError.Throw();
                }

                if (maplog != null)
                {
                    await repo.MapLog.AddAsync(maplog);
                }
                if (charalog != null)
                {
                    await repo.Character.AddCharacterLogAsync(charalog);
                }
                if (senderCharalog != null)
                {
                    await repo.Character.AddCharacterLogAsync(senderCharalog);
                }

                message = old;
                await repo.SaveChangesAsync();
            }

            if (newCharacter != null)
            {
                StatusStreaming.Default.UpdateCache(new Character[] { newCharacter, });
                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(newCharacter), newCharacter.Id);
            }
            if (charalog != null)
            {
                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(charalog), charalog.CharacterId);

                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(message), charalog.CharacterId);
            }
            if (senderCharalog != null)
            {
                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(senderCharalog), senderCharalog.CharacterId);

                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(message), senderCharalog.CharacterId);
            }
            if (maplog != null)
            {
                await StatusStreaming.Default.SendAllAsync(ApiData.From(maplog));

                await AnonymousStreaming.Default.SendAllAsync(ApiData.From(maplog));
            }
        }
Пример #6
0
        public IHttpActionResult GetAllCountries()
        {
            CountryMessage message = _serviceCountryClient.GetAllCountries();

            return(Json(message));
        }
Пример #7
0
        public static async Task OverThrowAsync(MainRepository repo, Country country, Country winnerCountry, bool isLog = true)
        {
            var system = await repo.System.GetAsync();

            country.HasOverthrown      = true;
            country.OverthrownGameDate = system.GameDateTime;

            if (isLog)
            {
                await LogService.AddMapLogAsync(repo, true, EventType.Overthrown, $"<country>{country.Name}</country> は滅亡しました");
            }

            // 戦争勝利ボーナス

            /*
             * var wars = (await repo.CountryDiplomacies.GetAllWarsAsync()).Where(w => w.IsJoin(country.Id));
             * foreach (var war in wars)
             * {
             * var targetId = war.GetEnemy(country.Id);
             * var target = winnerCountry?.Id == targetId ? winnerCountry : (await repo.Country.GetAliveByIdAsync(targetId)).Data;
             * if (target != null)
             * {
             *  var characterCount = war.RequestedCountryId == country.Id ? war.RequestedCountryCharacterMax : war.InsistedCountryCharacterMax;
             *  target.SafeMoney += characterCount * 16_0000;
             *  target.SafeMoney = Math.Min(target.SafeMoney, GetCountrySafeMax((await repo.Country.GetPoliciesAsync(target.Id)).Select(p => p.Type)));
             *  await StatusStreaming.Default.SendCountryAsync(ApiData.From(target), target.Id);
             * }
             * }
             */

            var targetCountryCharacters = await repo.Character.RemoveCountryAsync(country.Id);

            repo.Unit.RemoveUnitsByCountryId(country.Id);
            repo.Reinforcement.RemoveByCountryId(country.Id);
            repo.ChatMessage.RemoveByCountryId(country.Id);
            repo.CountryDiplomacies.RemoveByCountryId(country.Id);
            repo.Country.RemoveDataByCountryId(country.Id);

            // 玉璽
            if (winnerCountry != null)
            {
                if (country.GyokujiStatus != CountryGyokujiStatus.NotHave && country.GyokujiStatus != CountryGyokujiStatus.Refused)
                {
                    if (winnerCountry.GyokujiStatus == CountryGyokujiStatus.NotHave || winnerCountry.GyokujiStatus == CountryGyokujiStatus.Refused)
                    {
                        winnerCountry.IntGyokujiGameDate = system.IntGameDateTime;
                        if (!system.IsBattleRoyaleMode)
                        {
                            await LogService.AddMapLogAsync(repo, true, EventType.Gyokuji, $"<country>{winnerCountry.Name}</country> は玉璽を手に入れました");
                        }
                    }

                    if (winnerCountry.GyokujiStatus != CountryGyokujiStatus.HasGenuine)
                    {
                        winnerCountry.GyokujiStatus = country.GyokujiStatus;
                    }
                    country.GyokujiStatus = CountryGyokujiStatus.NotHave;
                    await StatusStreaming.Default.SendAllExceptForCountryAsync(ApiData.From(new CountryForAnonymous(winnerCountry)), winnerCountry.Id);

                    await StatusStreaming.Default.SendCountryAsync(ApiData.From(winnerCountry), winnerCountry.Id);
                }
            }

            await StatusStreaming.Default.SendAllAsync(ApiData.From(country));

            await AnonymousStreaming.Default.SendAllAsync(ApiData.From(country));

            await AiService.CheckManagedReinforcementsAsync(repo, country.Id);

            // 援軍データをいじって、無所属武将一覧で援軍情報を表示できるようにする
            var reinforcements = await repo.Reinforcement.GetByCountryIdAsync(country.Id);

            foreach (var rein in reinforcements.Where(r => r.RequestedCountryId == country.Id))
            {
                rein.RequestedCountryId = 0;
                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(rein), rein.CharacterId);
            }

            // 滅亡国武将に通知
            var commanders = new CountryMessage
            {
                Type      = CountryMessageType.Commanders,
                Message   = string.Empty,
                CountryId = 0,
            };

            foreach (var targetCountryCharacter in await repo.Country.GetCharactersAsync(country.Id))
            {
                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(targetCountryCharacter), targetCountryCharacter.Id);

                await StatusStreaming.Default.SendCharacterAsync(ApiData.From(commanders), targetCountryCharacter.Id);
            }
            await PushNotificationService.SendCountryAsync(repo, "滅亡", "あなたの国は滅亡しました。どこかの国に仕官するか、登用に応じることでゲームを続行できます", country.Id);

            // 登用分を無効化
            await ChatService.DenyCountryPromotions(repo, country);

            StatusStreaming.Default.UpdateCache(targetCountryCharacters);
            await repo.SaveChangesAsync();

            var allTowns = await repo.Town.GetAllAsync();

            var allCountries = await repo.Country.GetAllAsync();

            var townAiMap    = allTowns.Join(allCountries, t => t.CountryId, c => c.Id, (t, c) => new { CountryId = c.Id, c.AiType, });
            var humanCountry = townAiMap.FirstOrDefault(t => t.AiType != CountryAiType.Terrorists);

            if (allTowns.All(t => t.CountryId > 0) &&
                townAiMap.All(t => t.CountryId == humanCountry.CountryId || t.AiType == CountryAiType.Terrorists))
            {
                if (!system.IsWaitingReset)
                {
                    var unifiedCountry = humanCountry != null?allCountries.FirstOrDefault(c => c.Id == humanCountry.CountryId) : allCountries.FirstOrDefault(c => !c.HasOverthrown);

                    if (unifiedCountry != null)
                    {
                        await UnifyCountryAsync(repo, unifiedCountry);
                    }
                }
            }
        }
Пример #8
0
        public static async Task ResetAsync(MainRepository repo)
        {
            var now    = DateTime.Now;
            var system = await repo.System.GetAsync();

            var history = await repo.History.GetAsync(system.Period, system.BetaVersion);

            if (history.HasData)
            {
                var countries = await repo.Country.GetAllAsync();

                var messages = await repo.ChatMessage.GetAllAsync();

                history.Data.ChatMessages = messages.Select(m => HistoricalChatMessage.FromChatMessage(m)).ToArray();
                await repo.History.RecordChatMessagesAndSaveAsync(history.Data);

                var            unifiedCountry        = countries.FirstOrDefault(c => !c.HasOverthrown);
                CountryMessage unifiedCountryMessage = null;
                if (unifiedCountry != null)
                {
                    var msgs = await repo.Country.GetMessagesAsync(CountryMessageType.Unified);

                    unifiedCountryMessage = msgs.FirstOrDefault();
                }
                if (unifiedCountryMessage != null)
                {
                    history.Data.UnifiedCountryMessage = unifiedCountryMessage.Message;
                }
            }

            await OnlineService.ResetAsync();

            await repo.AuthenticationData.ResetAsync();

            await repo.BattleLog.ResetAsync();

            await repo.CharacterItem.ResetAsync();

            await repo.CharacterCommand.ResetAsync();

            await repo.Character.ResetAsync();

            await repo.EntryHost.ResetAsync();

            await repo.ChatMessage.ResetAsync();

            await repo.CountryDiplomacies.ResetAsync();

            await repo.AiCountry.ResetAsync();

            await repo.AiActionHistory.ResetAsync();

            await repo.Country.ResetAsync();

            await repo.MapLog.ResetAsync();

            await repo.ScoutedTown.ResetAsync();

            await repo.ThreadBbs.ResetAsync();

            await repo.Town.ResetAsync();

            await repo.Unit.ResetAsync();

            await repo.Reinforcement.ResetAsync();

            await repo.DelayEffect.ResetAsync();

            await repo.Mute.ResetAsync();

            // await repo.PushNotificationKey.ResetAsync();
            await repo.BlockAction.ResetAsync();

            // ファイル削除
            try
            {
                foreach (var file in System.IO.Directory.GetFiles(Config.Game.UploadedIconDirectory).Where(f => !f.EndsWith(".txt", StringComparison.Ordinal)))
                {
                    System.IO.File.Delete(file);
                }
            }
            catch
            {
                // Loggerがない!
            }

            await ResetTownsAndSaveAsync(repo, system.RuleSetNextPeriod);

            system.GameDateTime = new GameDateTime
            {
                Year  = Config.StartYear,
                Month = Config.StartMonth,
            };
            system.CurrentMonthStartDateTime = new DateTime(now.Year, now.Month, now.Day, 20, 0, 0, 0);
            system.IsWaitingReset            = false;
            system.IntResetGameDateTime      = 0;
            system.TerroristCount            = 0;
            system.ManagementCountryCount    = 0;
            system.IsBattleRoyaleMode        = false;
            system.RuleSet           = system.RuleSetNextPeriod;
            system.RuleSetNextPeriod = system.RuleSetAfterNextPeriod;
            system.IsSoftStoped      = false;

            if (system.BetaVersion == 0 && (system.Period + 3) % 6 == 0)
            {
                system.RuleSetAfterNextPeriod =
                    RandomService.Next(new GameRuleSet[] {
                    GameRuleSet.SimpleBattle,
                    GameRuleSet.Wandering,
                    GameRuleSet.BattleRoyale,
                    GameRuleSet.Gyokuji,
                    GameRuleSet.Religion,
                });
            }
            else
            {
                system.RuleSetAfterNextPeriod = GameRuleSet.Normal;
            }

            if (system.IsNextPeriodBeta)
            {
                system.BetaVersion++;
            }
            else
            {
                system.BetaVersion = 0;
                system.Period++;
            }

            if (Config.Game.IsGenerateAdminCharacter)
            {
                var admin = new Character
                {
                    Name                = Config.Admin.Name,
                    AiType              = CharacterAiType.Administrator,
                    LastUpdated         = DateTime.Now,
                    LastUpdatedGameDate = system.GameDateTime,
                    TownId              = (await repo.Town.GetAllAsync()).First().Id,
                    AliasId             = Config.Admin.AliasId,
                    Money               = 1000_0000,
                };
                admin.SetPassword(Config.Admin.Password);
                await repo.Character.AddAsync(admin);

                await repo.SaveChangesAsync();

                var adminIcon = new CharacterIcon
                {
                    CharacterId = admin.Id,
                    IsAvailable = true,
                    IsMain      = true,
                    Type        = CharacterIconType.Gravatar,
                    FileName    = Config.Admin.GravatarMailAddressMD5,
                };
                await repo.Character.AddCharacterIconAsync(adminIcon);
            }


            var ruleSetName = system.RuleSet == GameRuleSet.Normal ? "標準" :
                              system.RuleSet == GameRuleSet.Wandering ? "放浪" :
                              system.RuleSet == GameRuleSet.SimpleBattle ? "原理" :
                              system.RuleSet == GameRuleSet.BattleRoyale ? "全国戦争" :
                              system.RuleSet == GameRuleSet.Gyokuji ? "玉璽" :
                              system.RuleSet == GameRuleSet.Religion ? "宗教" : "";
            await repo.MapLog.AddAsync(new MapLog
            {
                EventType       = EventType.Reset,
                Date            = DateTime.Now,
                ApiGameDateTime = system.GameDateTime,
                IsImportant     = true,
                Message         = $"ゲームプログラムを開始しました。今期のルールセットは {ruleSetName} です",
            });

            await repo.SaveChangesAsync();

            await StatusStreaming.Default.SendAllAsync(ApiData.From(new ApiSignal
            {
                Type = SignalType.Reseted,
            }));

            await AnonymousStreaming.Default.SendAllAsync(ApiData.From(new ApiSignal
            {
                Type = SignalType.Reseted,
            }));
        }