remoteplayer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2014-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "remoteplayer.h"
  18. #include <json/json.h>
  19. #include "content_sao.h"
  20. #include "filesys.h"
  21. #include "gamedef.h"
  22. #include "porting.h" // strlcpy
  23. #include "server.h"
  24. #include "settings.h"
  25. #include "convert_json.h"
  26. /*
  27. RemotePlayer
  28. */
  29. // static config cache for remoteplayer
  30. bool RemotePlayer::m_setting_cache_loaded = false;
  31. float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
  32. u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0;
  33. RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
  34. Player(name, idef)
  35. {
  36. if (!RemotePlayer::m_setting_cache_loaded) {
  37. RemotePlayer::m_setting_chat_message_limit_per_10sec =
  38. g_settings->getFloat("chat_message_limit_per_10sec");
  39. RemotePlayer::m_setting_chat_message_limit_trigger_kick =
  40. g_settings->getU16("chat_message_limit_trigger_kick");
  41. RemotePlayer::m_setting_cache_loaded = true;
  42. }
  43. movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS;
  44. movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS;
  45. movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS;
  46. movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS;
  47. movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS;
  48. movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS;
  49. movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS;
  50. movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS;
  51. movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS;
  52. movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS;
  53. movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS;
  54. movement_gravity = g_settings->getFloat("movement_gravity") * BS;
  55. // copy defaults
  56. m_cloud_params.density = 0.4f;
  57. m_cloud_params.color_bright = video::SColor(229, 240, 240, 255);
  58. m_cloud_params.color_ambient = video::SColor(255, 0, 0, 0);
  59. m_cloud_params.height = 120.0f;
  60. m_cloud_params.thickness = 16.0f;
  61. m_cloud_params.speed = v2f(0.0f, -2.0f);
  62. }
  63. void RemotePlayer::serializeExtraAttributes(std::string &output)
  64. {
  65. assert(m_sao);
  66. Json::Value json_root;
  67. const PlayerAttributes &attrs = m_sao->getExtendedAttributes();
  68. for (const auto &attr : attrs) {
  69. json_root[attr.first] = attr.second;
  70. }
  71. output = fastWriteJson(json_root);
  72. m_sao->setExtendedAttributeModified(false);
  73. }
  74. void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
  75. PlayerSAO *sao)
  76. {
  77. Settings args;
  78. if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
  79. throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
  80. }
  81. m_dirty = true;
  82. //args.getS32("version"); // Version field value not used
  83. const std::string &name = args.get("name");
  84. strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
  85. if (sao) {
  86. try {
  87. sao->setHPRaw(args.getS32("hp"));
  88. } catch(SettingNotFoundException &e) {
  89. sao->setHPRaw(PLAYER_MAX_HP_DEFAULT);
  90. }
  91. try {
  92. sao->setBasePosition(args.getV3F("position"));
  93. } catch (SettingNotFoundException &e) {}
  94. try {
  95. sao->setPitch(args.getFloat("pitch"));
  96. } catch (SettingNotFoundException &e) {}
  97. try {
  98. sao->setYaw(args.getFloat("yaw"));
  99. } catch (SettingNotFoundException &e) {}
  100. try {
  101. sao->setBreath(args.getS32("breath"), false);
  102. } catch (SettingNotFoundException &e) {}
  103. try {
  104. const std::string &extended_attributes = args.get("extended_attributes");
  105. std::istringstream iss(extended_attributes);
  106. Json::CharReaderBuilder builder;
  107. builder.settings_["collectComments"] = false;
  108. std::string errs;
  109. Json::Value attr_root;
  110. Json::parseFromStream(builder, iss, &attr_root, &errs);
  111. const Json::Value::Members attr_list = attr_root.getMemberNames();
  112. for (const auto &it : attr_list) {
  113. Json::Value attr_value = attr_root[it];
  114. sao->setExtendedAttribute(it, attr_value.asString());
  115. }
  116. } catch (SettingNotFoundException &e) {}
  117. }
  118. inventory.deSerialize(is);
  119. if (inventory.getList("craftpreview") == NULL) {
  120. // Convert players without craftpreview
  121. inventory.addList("craftpreview", 1);
  122. bool craftresult_is_preview = true;
  123. if(args.exists("craftresult_is_preview"))
  124. craftresult_is_preview = args.getBool("craftresult_is_preview");
  125. if(craftresult_is_preview)
  126. {
  127. // Clear craftresult
  128. inventory.getList("craftresult")->changeItem(0, ItemStack());
  129. }
  130. }
  131. }
  132. void RemotePlayer::serialize(std::ostream &os)
  133. {
  134. // Utilize a Settings object for storing values
  135. Settings args;
  136. args.setS32("version", 1);
  137. args.set("name", m_name);
  138. // This should not happen
  139. assert(m_sao);
  140. args.setS32("hp", m_sao->getHP());
  141. args.setV3F("position", m_sao->getBasePosition());
  142. args.setFloat("pitch", m_sao->getPitch());
  143. args.setFloat("yaw", m_sao->getYaw());
  144. args.setS32("breath", m_sao->getBreath());
  145. std::string extended_attrs;
  146. serializeExtraAttributes(extended_attrs);
  147. args.set("extended_attributes", extended_attrs);
  148. args.writeLines(os);
  149. os<<"PlayerArgsEnd\n";
  150. inventory.serialize(os);
  151. }
  152. const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
  153. {
  154. // Rate limit messages
  155. u32 now = time(NULL);
  156. float time_passed = now - m_last_chat_message_sent;
  157. m_last_chat_message_sent = now;
  158. // If this feature is disabled
  159. if (m_setting_chat_message_limit_per_10sec <= 0.0) {
  160. return RPLAYER_CHATRESULT_OK;
  161. }
  162. m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
  163. if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
  164. m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
  165. }
  166. if (m_chat_message_allowance < 1.0f) {
  167. infostream << "Player " << m_name
  168. << " chat limited due to excessive message amount." << std::endl;
  169. // Kick player if flooding is too intensive
  170. m_message_rate_overhead++;
  171. if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
  172. return RPLAYER_CHATRESULT_KICK;
  173. }
  174. return RPLAYER_CHATRESULT_FLOODING;
  175. }
  176. // Reinit message overhead
  177. if (m_message_rate_overhead > 0) {
  178. m_message_rate_overhead = 0;
  179. }
  180. m_chat_message_allowance -= 1.0f;
  181. return RPLAYER_CHATRESULT_OK;
  182. }