public static async Task SendTownWarAndSaveAsync(MainRepository repo, TownWar war) { await repo.CountryDiplomacies.SetTownWarAsync(war); await repo.SaveChangesAsync(); await StatusStreaming.Default.SendCountryAsync(ApiData.From(war), war.RequestedCountryId); }
/// <summary> /// 攻略を設定する /// </summary> /// <param name="war">新しい戦争</param> public async Task SetTownWarAsync(TownWar war) { try { var old = await this.GetTownWarAsync(war.RequestedCountryId, war.InsistedCountryId, war.TownId); old.Some(o => { this.container.Context.TownWars.Remove(o); }); if (war.Status != TownWarStatus.None) { await this.container.Context.TownWars.AddAsync(war); } } catch (Exception ex) { this.container.Error(ex); } }
public async Task SetTownWarAsync( [FromRoute] uint townId) { using (var repo = MainRepository.WithReadAndWrite()) { var system = await repo.System.GetAsync(); if (system.RuleSet == GameRuleSet.SimpleBattle) { ErrorCode.RuleSetError.Throw(); } if (system.IsSoftStoped) { ErrorCode.SystemSoftStopedError.Throw(); } var chara = await repo.Character.GetByIdAsync(this.AuthData.CharacterId).GetOrErrorAsync(ErrorCode.LoginCharacterNotFoundError); var myPosts = await repo.Country.GetCharacterPostsAsync(chara.Id); if (!myPosts.Any(p => p.Type.CanDiplomacy())) { ErrorCode.NotPermissionError.Throw(); } var country = await repo.Country.GetAliveByIdAsync(chara.CountryId).GetOrErrorAsync(ErrorCode.CountryNotFoundError); var targetTown = await repo.Town.GetByIdAsync(townId).GetOrErrorAsync(ErrorCode.TownNotFoundError); var targetCountry = await repo.Country.GetAliveByIdAsync(targetTown.CountryId).GetOrErrorAsync(ErrorCode.CountryNotFoundError); if (targetTown.Id == targetCountry.CapitalTownId) { // 首都への攻略 ErrorCode.InvalidOperationError.Throw(); } if (await repo.Town.CountByCountryIdAsync(targetCountry.Id) <= 1) { // 残り1都市の国への攻略 ErrorCode.NotPermissionError.Throw(); } var alliance = await repo.CountryDiplomacies.GetCountryAllianceAsync(country.Id, targetCountry.Id); var countryWar = await repo.CountryDiplomacies.GetCountryWarAsync(country.Id, targetCountry.Id); if (alliance.HasData) { if (alliance.Data.Status == CountryAllianceStatus.Available || alliance.Data.Status == CountryAllianceStatus.InBreaking || alliance.Data.Status == CountryAllianceStatus.ChangeRequesting || alliance.Data.Status == CountryAllianceStatus.Requesting) { ErrorCode.NotPermissionError.Throw(); } } if (countryWar.HasData) { if (countryWar.Data.Status == CountryWarStatus.Available || countryWar.Data.Status == CountryWarStatus.StopRequesting) { ErrorCode.MeaninglessOperationError.Throw(); } if (countryWar.Data.Status == CountryWarStatus.InReady) { if (system.IntGameDateTime > countryWar.Data.IntStartGameDate - 6) { ErrorCode.NotPermissionError.Throw(); } } } else { // 他国同士の戦争には介入できない var wars = (await repo.CountryDiplomacies.GetAllWarsAsync()) .Where(w => w.RequestedCountryId == targetCountry.Id || w.InsistedCountryId == targetCountry.Id); if (wars.Any(w => w.Status == CountryWarStatus.InReady || w.Status == CountryWarStatus.Available || w.Status == CountryWarStatus.StopRequesting)) { ErrorCode.NotPermissionError.Throw(); } } var olds = (await repo.CountryDiplomacies.GetAllTownWarsAsync()) .Where(o => o.RequestedCountryId == country.Id); if (olds.Any(o => o.Status == TownWarStatus.Available || o.Status == TownWarStatus.InReady)) { // 重複して宣戦布告はできない ErrorCode.MeaninglessOperationError.Throw(); } else if (olds.Any(o => o.Status == TownWarStatus.Terminated && system.IntGameDateTime - o.IntGameDate < 12 * 10)) { ErrorCode.NotPermissionError.Throw(); } var war = new TownWar { RequestedCountryId = country.Id, InsistedCountryId = targetCountry.Id, IntGameDate = system.IntGameDateTime + 1, TownId = townId, Status = TownWarStatus.InReady, }; await CountryService.SendTownWarAndSaveAsync(repo, war); } }