Last 3 sent messages against the newest while saving only the last 3 newest messages?
Currently I have a simple class for users, like the follow:
public class Users
{
public int ID { get; set; }
public string Name { get; set; }
public Groups Access { get; set; }
public IPAddress IP { get; set; }
public bool IsProtected { get; set; }
public bool IsInvisible { get; set; }
}
Now I would like to store the user last 3 sent messages and be able to compare if user new last message matchs with any of the previous 3 stores messages and if not remove the oldest message from the list and add the new one.
I was initially thinking of simple using a list of strings and go for something like this:
bool msg_match = user.LastMessages.Any(x => String.Equals(x, msg, StringComparison.OrdinalIgnoreCase));
if (msg_match)
{
// punish user
user.LastMessages.Clear();
}
else
{
// some way to remove oldest message and leave only the 3 newest messages
user.LastMessages.Add(mensagem);
}
The above method do work but I am a bit clueless on how to pick the oldest message to be removed resulting in the last the newest messages and also a not sure if this method would be ok or if there is a better way to approach this 开发者_如何学编程?
PS: My title sounds rather confusing I think if you have a better idea for the title let me know or change it pls.
You could store the messages in a LinkedList<string>
. This would make it simple to do you check:
bool msg_match = user.LastMessages.Any(x => String.Equals(x, msg, StringComparison.OrdinalIgnoreCase));
if (msg_match)
{
// punish user
user.LastMessages.Clear();
}
else
{
// some way to remove oldest message and leave only the 3 newest messages
user.LastMessages.AddFirst(mensagem);
if (user.LastMessages.Count > 3)
user.LastMessages.RemoveLast();
}
精彩评论