public static async Task <CharacterLog> AddCharacterLogAsync(MainRepository repo, GameDateTime current, uint id, string message) { var log = new CharacterLog { GameDateTime = current, DateTime = DateTime.Now, CharacterId = id, Message = message, }; await repo.Character.AddCharacterLogAsync(log); await StatusStreaming.Default.SendCharacterAsync(ApiData.From(log), id); return(log); }
public async Task DischargeUnitMemberAsync( [FromRoute] uint unitId, [FromRoute] uint charaId) { using (var repo = MainRepository.WithReadAndWrite()) { var old = await repo.Unit.GetByIdAsync(unitId); if (!old.HasData) { ErrorCode.UnitNotFoundError.Throw(); } var unit = old.Data; var members = await repo.Unit.GetMembersAsync(unitId); if (!members.Any(u => u.Post == UnitMemberPostType.Leader && u.CharacterId == this.AuthData.CharacterId)) { ErrorCode.NotPermissionError.Throw(); } if (this.AuthData.CharacterId == charaId) { ErrorCode.InvalidOperationError.Throw(); } UnitService.Leave(repo, charaId); var log = new CharacterLog { CharacterId = charaId, DateTime = DateTime.Now, GameDateTime = (await repo.System.GetAsync()).GameDateTime, Message = $"部隊 {unit.Name} から除隊されました", }; await repo.Character.AddCharacterLogAsync(log); await StatusStreaming.Default.SendCharacterAsync(ApiData.From(log), charaId); await StatusStreaming.Default.SendCharacterAsync(ApiData.From(new ApiSignal { Type = SignalType.UnitDischarged, }), charaId); await repo.SaveChangesAsync(); } }
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)); } }