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 "filesys.h"
24 #include "gamedef.h"
25 #include "porting.h"  // strlcpy
26 #include "server.h"
27 #include "settings.h"
28 #include "convert_json.h"
29 #include "server/player_sao.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 
RemotePlayer(const char * name,IItemDefManager * idef)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 	// Skybox defaults:
71 
72 	SkyboxDefaults sky_defaults;
73 
74 	m_skybox_params.sky_color = sky_defaults.getSkyColorDefaults();
75 	m_skybox_params.type = "regular";
76 	m_skybox_params.clouds = true;
77 	m_skybox_params.fog_sun_tint = video::SColor(255, 244, 125, 29);
78 	m_skybox_params.fog_moon_tint = video::SColorf(0.5, 0.6, 0.8, 1).toSColor();
79 	m_skybox_params.fog_tint_type = "default";
80 
81 	m_sun_params = sky_defaults.getSunDefaults();
82 	m_moon_params = sky_defaults.getMoonDefaults();
83 	m_star_params = sky_defaults.getStarDefaults();
84 }
85 
86 
canSendChatMessage()87 const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
88 {
89 	// Rate limit messages
90 	u32 now = time(NULL);
91 	float time_passed = now - m_last_chat_message_sent;
92 	m_last_chat_message_sent = now;
93 
94 	// If this feature is disabled
95 	if (m_setting_chat_message_limit_per_10sec <= 0.0) {
96 		return RPLAYER_CHATRESULT_OK;
97 	}
98 
99 	m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f);
100 	if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) {
101 		m_chat_message_allowance = m_setting_chat_message_limit_per_10sec;
102 	}
103 
104 	if (m_chat_message_allowance < 1.0f) {
105 		infostream << "Player " << m_name
106 				<< " chat limited due to excessive message amount." << std::endl;
107 
108 		// Kick player if flooding is too intensive
109 		m_message_rate_overhead++;
110 		if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) {
111 			return RPLAYER_CHATRESULT_KICK;
112 		}
113 
114 		return RPLAYER_CHATRESULT_FLOODING;
115 	}
116 
117 	// Reinit message overhead
118 	if (m_message_rate_overhead > 0) {
119 		m_message_rate_overhead = 0;
120 	}
121 
122 	m_chat_message_allowance -= 1.0f;
123 	return RPLAYER_CHATRESULT_OK;
124 }
125 
onSuccessfulSave()126 void RemotePlayer::onSuccessfulSave()
127 {
128 	setModified(false);
129 	if (m_sao)
130 		m_sao->getMeta().setModified(false);
131 }
132