Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarEmoteProcessor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/StarEmoteProcessor.cpp')
-rw-r--r--source/game/StarEmoteProcessor.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/source/game/StarEmoteProcessor.cpp b/source/game/StarEmoteProcessor.cpp
new file mode 100644
index 0000000..8bcf960
--- /dev/null
+++ b/source/game/StarEmoteProcessor.cpp
@@ -0,0 +1,61 @@
+#include "StarEmoteProcessor.hpp"
+#include "StarJsonExtra.hpp"
+#include "StarAssets.hpp"
+#include "StarRoot.hpp"
+
+namespace Star {
+
+EmoteProcessor::EmoteProcessor() {
+ auto assets = Root::singleton().assets();
+
+ m_emoteBindings.clear();
+ auto cfg = assets->json("/emotes.config");
+ for (auto binding : cfg.get("emoteBindings").iterateObject()) {
+ for (auto text : binding.second.toArray()) {
+ EmoteBinding emoteBinding;
+ emoteBinding.emote = HumanoidEmoteNames.getLeft(binding.first);
+ emoteBinding.text = text.toString();
+ m_emoteBindings.append(emoteBinding);
+ }
+ }
+}
+
+HumanoidEmote EmoteProcessor::detectEmotes(String const& chatter) const {
+ auto isShouty = [](String const& text) -> bool {
+ int caps = 0;
+ int noCaps = 0;
+ for (auto c : text) {
+ if (String::toUpper(c) != String::toLower(c)) {
+ if (String::toUpper(c) == c)
+ caps++;
+ else
+ noCaps++;
+ }
+ }
+ return caps > noCaps;
+ };
+
+ HumanoidEmote result = HumanoidEmote::Idle;
+ if (!chatter.empty()) {
+ if (isShouty(chatter))
+ result = HumanoidEmote::Shouting;
+ else
+ result = HumanoidEmote::Blabbering;
+ }
+
+ float bestMatch = -1;
+
+ for (auto option : m_emoteBindings) {
+ auto p = chatter.find(option.text);
+ if (p == NPos)
+ continue;
+ float r = p + (float)option.text.length() * 0.01f;
+ if (r > bestMatch) {
+ bestMatch = r;
+ result = option.emote;
+ }
+ }
+ return result;
+}
+
+}