public async Task LogMessageAsync(SocketUserMessage msg, bool isSuccessfulCommand) { RecentMessages.Add(await DbMessage.CreateAsync(msg, PlayerCollection, isSuccessfulCommand)); while (RecentMessages.Count > 5) { var oldest = RecentMessages.Aggregate((l, r) => l.CreateTime < r.CreateTime ? l : r); await oldest.DeleteAsync(); RecentMessages.Remove(oldest); } }
private async Task InitializeAsync(ulong id, CollectionReference playerCollection) { PlayerCollection = playerCollection; PlayerDoc = PlayerCollection.Document(id.ToString()); var playerDocSnap = await PlayerDoc.GetSnapshotAsync(); if (!playerDocSnap.Exists) { await PlayerDoc.CreateAsync(new Dictionary <string, object>() { { "boost", 0 }, { "demo-count", 0 }, { "death-count", 0 }, { "demo-rate", (DemoData.MinDemoChance + DemoData.MaxDemoChance) / 2 }, { "avoid-rate", (DemoData.MinAvoidChance + DemoData.MaxAvoidChance) / 2 }, { "avoid-count", 0 }, { "miss-count", 0 } }); playerDocSnap = await PlayerDoc.GetSnapshotAsync(); } if (!playerDocSnap.ContainsField("avoid-count")) { await PlayerDoc.UpdateAsync("avoid-count", 0); await PlayerDoc.UpdateAsync("miss-count", 0); } if (!playerDocSnap.TryGetValue("avoid-count", out int avoidCount)) { avoidCount = 0; } if (!playerDocSnap.TryGetValue("miss-count", out int missCount)) { missCount = 0; } if (!playerDocSnap.TryGetValue("boost-used", out int boostUsed)) { boostUsed = 0; } Data = new DemoData( playerDocSnap.GetValue <int>("boost"), playerDocSnap.GetValue <int>("demo-count"), playerDocSnap.GetValue <int>("death-count"), playerDocSnap.GetValue <double>("demo-rate"), playerDocSnap.GetValue <double>("avoid-rate"), avoidCount, missCount); RecentMessages = new List <DbMessage>(); var recentMsgSnap = await PlayerDoc.Collection("recent-messages").GetSnapshotAsync(); if (recentMsgSnap.Count > 0) { foreach (var message in recentMsgSnap) { //content, createTime if (!message.TryGetValue("content", out string content)) { content = ""; } if (!message.TryGetValue("create-time", out DateTime createTime)) { createTime = new DateTime(0); } if (!message.TryGetValue("is-successful-command", out bool isSuccessfulCommand)) { isSuccessfulCommand = false; } RecentMessages.Add(await DbMessage.CreateAsync(content, createTime, isSuccessfulCommand, id, PlayerCollection, message.Id)); } } }