summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-01-17 03:13:39 +0300
committerAndrei Karas <akaras@inbox.ru>2016-01-17 14:19:17 +0300
commit41141188d70dd7d6e0b8667bda9d0f6569c0b97b (patch)
tree4ed6186d9805fe3240fd3f574faa4455ca9e3fa9
parentfcf4c08536267a7be624b057c28586f510c0af2c (diff)
downloadmv-41141188d70dd7d6e0b8667bda9d0f6569c0b97b.tar.gz
mv-41141188d70dd7d6e0b8667bda9d0f6569c0b97b.tar.bz2
mv-41141188d70dd7d6e0b8667bda9d0f6569c0b97b.tar.xz
mv-41141188d70dd7d6e0b8667bda9d0f6569c0b97b.zip
Add support for adding cards into item links in chat.
-rw-r--r--src/utils/stringutils.cpp41
-rw-r--r--src/utils/stringutils_unittest.cc32
2 files changed, 67 insertions, 6 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 830b0346a..dadfd35f5 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -970,9 +970,11 @@ std::string timeDiffToString(int timeDiff)
return str;
}
+#include "logger.h"
+
+#ifndef DYECMD
void replaceItemLinks(std::string &msg)
{
-#ifndef DYECMD
// Check for item link
size_t start2 = msg.find('[');
size_t sz = msg.size();
@@ -998,11 +1000,34 @@ void replaceItemLinks(std::string &msg)
{
std::string itemStr = msg.substr(start2 + 1, end - start2 - 1);
- const ItemInfo &itemInfo = ItemDB::get(itemStr);
- if (itemInfo.getId() != 0)
+ StringVect parts;
+ splitToStringVector(parts, itemStr, ',');
+ if (parts.empty())
+ return;
+
+ const ItemInfo &itemInfo = ItemDB::get(parts[0]);
+ const int itemId = itemInfo.getId();
+ if (itemId != 0)
{
- std::string temp = strprintf("@@%d|", itemInfo.getId());
- msg.insert(end, "@@");
+ std::string temp = strprintf("@@%d", itemId);
+ std::string name = parts[0];
+ msg.erase(start2 + 1, end - start2 - 1);
+ parts.erase(parts.begin());
+ if (!parts.empty())
+ name.clear();
+
+ FOR_EACH (StringVectCIter, it, parts)
+ {
+ std:: string str = *it;
+ trim(str);
+ const ItemInfo &itemInfo2 = ItemDB::get(str);
+ const int cardId = itemInfo2.getId();
+ if (cardId != 0)
+ temp.append(strprintf(",%d", cardId));
+ }
+ temp.append("|");
+ temp.append(name);
+ temp.append("@@");
msg.insert(start2 + 1, temp);
sz = msg.size();
}
@@ -1010,5 +1035,9 @@ void replaceItemLinks(std::string &msg)
}
start2 = msg.find('[', start2 + 1);
}
-#endif
}
+#else
+void replaceItemLinks(std::string &msg A_UNUSED)
+{
+}
+#endif
diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc
index e2db8eed1..d2dfcdf62 100644
--- a/src/utils/stringutils_unittest.cc
+++ b/src/utils/stringutils_unittest.cc
@@ -625,6 +625,14 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[qqq]";
replaceItemLinks(str);
REQUIRE(str == "[qqq]");
+
+ str = "[,]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[,]");
+
+ str = "[, ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[, ]");
}
SECTION("simple")
@@ -653,6 +661,14 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[test name 1] [no link]";
replaceItemLinks(str);
REQUIRE(str == "[@@123456|test name 1@@] [no link]");
+
+ str = "[test name 1,test name 2]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456,123|test name 1@@]");
+
+ str = "[test name 1, test name 2 ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456,123|test name 1@@]");
}
SECTION("broken")
@@ -770,5 +786,21 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[[test name 1[]test name 1]";
replaceItemLinks(str);
REQUIRE(str == "[[test name 1[]test name 1]");
+
+ str = "[[test name 1],test name2,test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@],test name2,test name 1]");
+
+ str = "[[ test name 1], test name2,test name 1 ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@], test name2,test name 1 ]");
+
+ str = "[[test name 1,test name2[]test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test name 1,test name2[]test name 1]");
+
+ str = "[[test name 1 ,test name2[] test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test name 1 ,test name2[] test name 1]");
}
}