remoteplayer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 StringMap &attrs = m_sao->getMeta().getStrings();
  68. for (const auto &attr : attrs) {
  69. json_root[attr.first] = attr.second;
  70. }
  71. output = fastWriteJson(json_root);
  72. }
  73. void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
  74. PlayerSAO *sao)
  75. {
  76. Settings args;
  77. if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
  78. throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
  79. }
  80. m_dirty = true;
  81. //args.getS32("version"); // Version field value not used
  82. const std::string &name = args.get("name");
  83. strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
  84. if (sao) {
  85. try {
  86. sao->setHPRaw(args.getU16("hp"));
  87. } catch(SettingNotFoundException &e) {
  88. sao->setHPRaw(PLAYER_MAX_HP_DEFAULT);
  89. }
  90. try {
  91. sao->setBasePosition(args.getV3F("position"));
  92. } catch (SettingNotFoundException &e) {}
  93. try {
  94. sao->setLookPitch(args.getFloat("pitch"));
  95. } catch (SettingNotFoundException &e) {}
  96. try {
  97. sao->setPlayerYaw(args.getFloat("yaw"));
  98. } catch (SettingNotFoundException &e) {}
  99. try {
  100. sao->setBreath(args.getU16("breath"), false);
  101. } catch (SettingNotFoundException &e) {}
  102. try {
  103. const std::string &extended_attributes = args.get("extended_attributes");
  104. std::istringstream iss(extended_attributes);
  105. Json::CharReaderBuilder builder;
  106. builder.settings_["collectComments"] = false;
  107. std::string errs;
  108. Json::Value attr_root;
  109. Json::parseFromStream(builder, iss, &attr_root, &errs);
  110. const Json::Value::Members attr_list = attr_root.getMemberNames();
  111. for (const auto &it : attr_list) {
  112. Json::Value attr_value = attr_root[it];
  113. sao->getMeta().setString(it, attr_value.asString());
  114. }
  115. sao->getMeta().setModified(false);
  116. } catch (SettingNotFoundException &e) {}
  117. }
  118. try {
  119. inventory.deSerialize(is);
  120. } catch (SerializationError &e) {
  121. errorstream << "Failed to deserialize player inventory. player_name="
  122. << name << " " << e.what() << std::endl;
  123. }
  124. if (!inventory.getList("craftpreview") && inventory.getList("craftresult")) {
  125. // Convert players without craftpreview
  126. inventory.addList("craftpreview", 1);
  127. bool craftresult_is_preview = true;
  128. if(args.exists("craftresult_is_preview"))
  129. craftresult_is_preview = args.getBool("craftresult_is_preview");
  130. if(craftresult_is_preview)
  131. {
  132. // Clear craftresult
  133. inventory.getList("craftresult")->changeItem(0, ItemStack());
  134. }
  135. }
  136. }
  137. void RemotePlayer::serialize(std::ostream &os)
  138. {
  139. // Utilize a Settings object for storing values
  140. Settings args;
  141. args.setS32("version", 1);
  142. args.set("name", m_name);
  143. // This should not happen
  144. assert(m_sao);
  145. args.setU16("hp", m_sao->getHP());
  146. args.setV3F("position", m_sao->getBasePosition());
  147. args.setFloat("pitch", m_sao->getLookPitch());
  148. args.setFloat("yaw", m_sao->getRotation().Y);
  149. args.setU16("breath", m_sao->getBreath());
  150. std::string extended_attrs;
  151. serializeExtraAttributes(extended_attrs);
  152. args.set("extended_attributes", extended_attrs);
  153. args.writeLines(os);
  154. os<<"PlayerArgsEnd\n";
  155. inventory.serialize(os);
  156. }
  157. const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
  158. {
  159. // Rate limit messages
  160. u32 now = time(NULL);
  161. float time_passed = now - m_last_chat_message_sent;
  162. m_last_chat_message_sent = now;
  163. // If this feature is disabled
  164. if (m_setting_chat_message_limit_per_10sec <= 0.0) {
  165. return RPLAYER_CHATRESULT_OK;
  166. }
  167. m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
  168. if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
  169. m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
  170. }
  171. if (m_chat_message_allowance < 1.0f) {
  172. infostream << "Player " << m_name
  173. << " chat limited due to excessive message amount." << std::endl;
  174. // Kick player if flooding is too intensive
  175. m_message_rate_overhead++;
  176. if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
  177. return RPLAYER_CHATRESULT_KICK;
  178. }
  179. return RPLAYER_CHATRESULT_FLOODING;
  180. }
  181. // Reinit message overhead
  182. if (m_message_rate_overhead > 0) {
  183. m_message_rate_overhead = 0;
  184. }
  185. m_chat_message_allowance -= 1.0f;
  186. return RPLAYER_CHATRESULT_OK;
  187. }
  188. void RemotePlayer::onSuccessfulSave()
  189. {
  190. setModified(false);
  191. if (m_sao)
  192. m_sao->getMeta().setModified(false);
  193. }