add warning messages when certain words are found in comments

This commit is contained in:
jacob1 2017-06-09 22:18:20 -04:00
parent 4242e38968
commit 25b3244d6c
2 changed files with 97 additions and 4 deletions

View File

@ -1,6 +1,7 @@
#include <sstream>
#include <vector>
#include <cmath>
#include <algorithm>
#include "PreviewView.h"
#include "gui/dialogues/TextPrompt.h"
#include "simulation/SaveRenderer.h"
@ -45,6 +46,7 @@ class PreviewView::AutoCommentSizeAction: public ui::TextboxAction
public:
AutoCommentSizeAction(PreviewView * v): v(v) {}
virtual void TextChangedCallback(ui::Textbox * sender) {
v->CheckComment();
v->commentBoxAutoHeight();
}
};
@ -68,12 +70,15 @@ PreviewView::PreviewView():
savePreview(NULL),
submitCommentButton(NULL),
addCommentBox(NULL),
commentWarningLabel(NULL),
userIsAuthor(false),
doOpen(false),
doError(false),
doErrorMessage(""),
showAvatars(true),
prevPage(false),
commentBoxHeight(20)
commentBoxHeight(20),
commentHelpText(false)
{
class FavAction: public ui::ButtonAction
{
@ -200,6 +205,15 @@ PreviewView::PreviewView():
commentsPanel = new ui::ScrollPanel(ui::Point((XRES/2)+1, 1), ui::Point((Size.X-(XRES/2))-2, Size.Y-commentBoxHeight));
AddComponent(commentsPanel);
swearWords.insert("fuck");
swearWords.insert("shit ");
swearWords.insert("asshole");
swearWords.insert("dick");
swearWords.insert("cunt");
swearWords.insert(" nigger");
swearWords.insert("faggot");
swearWords.insert("dumbass");
}
void PreviewView::AttachController(PreviewController * controller)
@ -225,7 +239,7 @@ void PreviewView::commentBoxAutoHeight()
if(!addCommentBox)
return;
int textWidth = Graphics::textwidth(addCommentBox->GetText().c_str());
if(textWidth+15 > Size.X-(XRES/2)-48)
if (commentHelpText || textWidth+15 > Size.X-(XRES/2)-48)
{
addCommentBox->Appearance.VerticalAlign = ui::Appearance::AlignTop;
@ -239,6 +253,11 @@ void PreviewView::commentBoxAutoHeight()
commentBoxPositionY = Size.Y-(newSize+21);
commentBoxSizeX = Size.X-(XRES/2)-8;
commentBoxSizeY = newSize;
if (commentWarningLabel && commentHelpText && !commentWarningLabel->Visible && addCommentBox->Position.Y+addCommentBox->Size.Y < Size.Y-14)
{
commentWarningLabel->Visible = true;
}
}
else
{
@ -248,9 +267,63 @@ void PreviewView::commentBoxAutoHeight()
commentBoxPositionX = (XRES/2)+4;
commentBoxPositionY = Size.Y-19;
commentBoxSizeX = Size.X-(XRES/2)-48;
commentBoxSizeY = 16;
commentBoxSizeY = 17;
if (commentWarningLabel && commentWarningLabel->Visible)
{
commentWarningLabel->Visible = false;
}
}
}
bool PreviewView::CheckSwearing(std::string text)
{
for (std::set<std::string>::iterator iter = swearWords.begin(), end = swearWords.end(); iter != end; iter++)
{
if (text.find(*iter) != text.npos)
return true;
}
return false;
}
void PreviewView::CheckComment()
{
if (!commentWarningLabel)
return;
std::string text = addCommentBox->GetText();
std::transform(text.begin(), text.end(), text.begin(), ::tolower);
if (!userIsAuthor && (text.find("stolen") != text.npos || text.find("copied") != text.npos))
{
if (!commentHelpText)
{
if (rand()%2)
commentWarningLabel->SetText("Stolen? Report the save instead");
else
commentWarningLabel->SetText("Please report stolen saves");
commentHelpText = true;
}
}
else if (userIsAuthor && text.find("vote") != text.npos)
{
commentWarningLabel->SetText("Do not ask for votes");
commentHelpText = true;
}
else if (CheckSwearing(text))
{
if (!commentHelpText)
{
if (rand()%2)
commentWarningLabel->SetText("Please do not swear");
else
commentWarningLabel->SetText("Bad language may be deleted");
commentHelpText = true;
}
}
else
{
commentHelpText = false;
commentWarningLabel->Visible = false;
}
commentsPanel->Size.Y = Size.Y-commentBoxHeight;
}
void PreviewView::DoDraw()
@ -365,6 +438,7 @@ void PreviewView::OnTick(float dt)
addCommentBox->Size.Y += ydiff;
addCommentBox->Invalidate();
}
commentsPanel->Size.Y = addCommentBox->Position.Y-1;
}
c->Update();
@ -431,6 +505,10 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender)
{
authorDateLabel->SetText("\bgAuthor:\bw " + save->userName + " \bgDate:\bw " + format::UnixtimeToDateMini(save->date));
}
if (Client::Ref().GetAuthUser().ID && save->userName == Client::Ref().GetAuthUser().Username)
userIsAuthor = true;
else
userIsAuthor = false;
viewsLabel->SetText("\bgViews:\bw " + format::NumberToString<int>(save->Views));
saveDescriptionLabel->SetText(save->Description);
if(save->Favourite)
@ -531,6 +609,12 @@ void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender)
submitCommentButton->SetActionCallback(new SubmitCommentAction(this));
//submitCommentButton->Enabled = false;
AddComponent(submitCommentButton);
commentWarningLabel = new ui::Label(ui::Point((XRES/2)+4, Size.Y-19), ui::Point(Size.X-(XRES/2)-48, 16), "If you see this it is a bug");
commentWarningLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
commentWarningLabel->SetTextColour(ui::Colour(255, 0, 0));
commentWarningLabel->Visible = false;
AddComponent(commentWarningLabel);
}
else
{

View File

@ -2,6 +2,8 @@
#define PREVIEWVIEW_H_
#include <vector>
#include <set>
#include <string>
#include "Comment.h"
#include "gui/interface/Window.h"
#include "gui/preview/PreviewController.h"
@ -33,6 +35,7 @@ class PreviewView: public ui::Window {
ui::Button * reportButton;
ui::Button * submitCommentButton;
ui::Textbox * addCommentBox;
ui::Label * commentWarningLabel;
ui::Label * saveNameLabel;
ui::Label * authorDateLabel;
ui::AvatarButton * avatarButton;
@ -47,6 +50,7 @@ class PreviewView: public ui::Window {
std::vector<ui::Component*> commentTextComponents;
int votesUp;
int votesDown;
bool userIsAuthor;
bool doOpen;
bool doError;
std::string doErrorMessage;
@ -58,10 +62,15 @@ class PreviewView: public ui::Window {
float commentBoxPositionY;
float commentBoxSizeX;
float commentBoxSizeY;
bool commentHelpText;
std::set<std::string> swearWords;
void displayComments();
void commentBoxAutoHeight();
void submitComment();
bool CheckSwearing(std::string text);
void CheckComment();
public:
void AttachController(PreviewController * controller);
PreviewView();