Приложению Angular Firebase, которое использует Firestore как форму сохранения, необходимо взаимодействовать с ботом Discord. Я создал бот-синхронизатор, который является посредником между существующим внешним ботом и веб-приложением.
Информации достаточно для поиска и обновления документа.
< h2>Проблема
Обновление не происходит из-за проблемы с конвертацией.
Исключение: невозможно создать конвертер для введите Модели.Участник
Вопрос
После нескольких попыток решения, в основном с использованием преобразования json, я упростил код по порядку. чтобы лучше понять ситуацию. Я предполагаю, что чего-то не хватает, но из-за моей неопытности в Firebase (Firestore) на данный момент я не могу понять, чего именно.
Код: Выделить всё
public async Task NextTurn(string encounterName)
{
var encounterSnapshotQuery = await _encountersCollection.WhereEqualTo("name", encounterName).GetSnapshotAsync();
foreach (DocumentSnapshot encounterSnapshot in encounterSnapshotQuery.Documents)
{
Dictionary data = encounterSnapshot.ToDictionary();
var name = data["name"].ToString();
if (name == encounterName)
{
var participants = data["participants"].ToParticipants();
var orderedParticipants = participants.OrderByDescending(x => x.initiative + x.roll).ToList();
var current = orderedParticipants.Single(x => x.isCurrent != null && x.isCurrent is bool && (bool)x.isCurrent);
var currentIndex = orderedParticipants.FindIndex(x => x.characterName == current.characterName);
var next = orderedParticipants[currentIndex + 1];
current.hasPlayedThisTurn = true;
current.isCurrent = false;
next.isCurrent = true;
var updates = new Dictionary
{
{ new FieldPath("participants"), orderedParticipants }
};
try
{
await encounterSnapshot.Reference.UpdateAsync(updates);
}
catch (Exception ex)
{
_logger.LogError(new EventId(), ex, "Update failed.");
}
}
}
return true;
}
Обновить
Полное сообщение об исключении:
Код: Выделить всё
at Google.Cloud.Firestore.Converters.ConverterCache.CreateConverter(Type targetType)
at Google.Cloud.Firestore.Converters.ConverterCache.c.b__1_0(Type t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Google.Cloud.Firestore.Converters.ConverterCache.GetConverter(Type targetType)
at Google.Cloud.Firestore.SerializationContext.GetConverter(Type targetType)
at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
at Google.Cloud.Firestore.Converters.ListConverterBase.Serialize(SerializationContext context, Object value)
at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value)
at Google.Cloud.Firestore.WriteBatch.c__DisplayClass12_0.b__1(KeyValuePair`2 pair)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
at Google.Cloud.Firestore.WriteBatch.Update(DocumentReference documentReference, IDictionary`2 updates, Precondition precondition)
at Google.Cloud.Firestore.DocumentReference.UpdateAsync(IDictionary`2 updates, Precondition precondition, CancellationToken cancellationToken)
Код: Выделить всё
public class Participant
{
public string playerName { get; set; }
public int experience { get; set; }
public int level { get; set; }
public string characterName { get; set; }
public string playerUid { get; set; }
public object joined { get; set; }
public string type { get; set; }
public object abilities { get; set; }
public int roll { get; set; }
public bool? isCurrent { get; set; }
public int sizeModifier { get; set; }
public int initiative { get; set; }
public bool? hasPlayedThisTurn { get; set; }
public string portraitUrl { get; set; }
}
Код: Выделить всё
export interface Participant {
playerName: string,
characterName: string,
initiative: number,
roll: number,
playerUid: string,
joined: Date,
portraitUrl: string,
level: number,
experience: number,
isCurrent: boolean,
sizeModifier: number,
type: string,
abilities: {
strength: number,
dexterity: number,
constitution: number,
intelligence: number,
wisdom: number,
charisma: number
},
hasPlayedThisTurn: boolean
}
Подробнее здесь: https://stackoverflow.com/questions/597 ... onsole-app
Мобильная версия