vectozavr-shooter/network/Chat.cpp

43 lines
1005 B
C++
Raw Normal View History

2022-07-22 22:52:54 +03:00
#include <string>
2023-08-03 01:48:04 +03:00
#include "Chat.h"
void ChatManager::addNewMessage(const std::string& author, const std::string& message) {
hide = 7.0;
2022-07-22 22:52:54 +03:00
messages.push_back(message);
authors.push_back(author);
isChatUpdate = true;
if (messages.size() > 20) {
messages.erase(messages.begin());
}
2022-07-22 22:52:54 +03:00
}
int ChatManager::update(double delta) {
hide = std::max(hide-delta, 0.0);
return std::min((int)(hide * 255.0), 255);
}
std::string ChatManager::getChat() {
2023-08-03 01:48:04 +03:00
updateChat();
return chatStr;
}
std::string ChatManager::getChatPreview() {
2023-08-03 01:48:04 +03:00
updateChat();
return chatStrPrev;
}
void ChatManager::updateChat() {
2022-07-22 22:52:54 +03:00
if (isChatUpdate) {
isChatUpdate = false;
2023-08-03 01:48:04 +03:00
size_t size = messages.size();
2022-07-22 22:52:54 +03:00
chatStr = "";
chatStrPrev = "";
for (int messageIndex = size - 1; messageIndex >= 0; messageIndex--)
2022-07-22 22:52:54 +03:00
{
if (messageIndex > size - 6) {
2022-07-24 20:22:47 +03:00
chatStrPrev += authors[messageIndex] + ": " + messages[messageIndex] + "\n";
}
2022-07-24 20:22:47 +03:00
chatStr += authors[messageIndex] + ": " + messages[messageIndex] + "\n";
2022-07-22 22:52:54 +03:00
}
}
}