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