diff --git a/common.lua b/common.lua index ee29f34..71a7e0d 100644 --- a/common.lua +++ b/common.lua @@ -1,4 +1,25 @@ +beerchat.get_player_channel = function(name) + if type(name) == "string" then + local channel = beerchat.currentPlayerChannel[name] + if channel and beerchat.channels[channel] then + return channel + end + end +end + +beerchat.fix_player_channel = function(name, notify) + if notify or notify == nil then + minetest.chat_send_player( + name, + "Channel "..beerchat.currentPlayerChannel[name].." does not exist, switching back to ".. + beerchat.main_channel_name..". Please resend your message" + ) + end + beerchat.currentPlayerChannel[name] = beerchat.main_channel_name + minetest.get_player_by_name(name):get_meta():set_string("beerchat:current_channel", beerchat.main_channel_name) +end + beerchat.has_player_muted_player = function(name, other_name) local cb_result = beerchat.execute_callbacks('before_check_muted', name, other_name) if cb_result ~= nil then @@ -14,12 +35,12 @@ beerchat.has_player_muted_player = function(name, other_name) local key = "beerchat:muted:" .. other_name local meta = player:get_meta() return "true" == meta:get_string(key) -end -- has_player_muted_player +end beerchat.is_player_subscribed_to_channel = function(name, channel) return (nil ~= beerchat.playersChannels[name]) and (nil ~= beerchat.playersChannels[name][channel]) -end -- is_player_subscribed_to_channel +end beerchat.send_message = function(name, message, channel) if not beerchat.execute_callbacks('before_send', name, message, channel) then @@ -27,8 +48,8 @@ beerchat.send_message = function(name, message, channel) end minetest.chat_send_player(name, message) --- TODO: read player settings for channel sounds + -- TODO: read player settings for channel sounds if beerchat.enable_sounds and channel ~= beerchat.main_channel_name then minetest.sound_play(beerchat.channel_message_sound, { to_player = name, gain = beerchat.sounds_default_gain } ) end -end -- send_message +end diff --git a/init.lua b/init.lua index 1d2385f..ef5a568 100644 --- a/init.lua +++ b/init.lua @@ -7,7 +7,7 @@ local http = QoS and QoS(minetest.request_http_api(), 1) or minetest.request_htt beerchat = { -- The main channel is the one you send messages to when no channel is specified - main_channel_name = minetest.settings:get("beerchat.main_channel_name"), + main_channel_name = minetest.settings:get("beerchat.main_channel_name") or "main", -- The default color of channels when no color is specified default_channel_color = "#ffffff", @@ -43,10 +43,6 @@ beerchat = { remote_username_map = {} } -if nil == beerchat.main_channel_name or "" == beerchat.main_channel_name then - beerchat.main_channel_name = "main" -end - local MP = minetest.get_modpath("beerchat") dofile(MP.."/router.lua") diff --git a/plugin/me.lua b/plugin/me.lua index be3d75c..bfc1449 100644 --- a/plugin/me.lua +++ b/plugin/me.lua @@ -7,34 +7,36 @@ local me_override = { beerchat.main_channel_name.."| * Player01 eats pizza", func = function(name, param) local msg = param - local channel_name = beerchat.main_channel_name - if not beerchat.channels[channel_name] then - minetest.chat_send_player(name, "Channel "..channel_name.." does not exist.") + local channel = beerchat.get_player_channel(name) + if not channel then + beerchat.fix_player_channel(name, true) + elseif not beerchat.channels[channel] then + minetest.chat_send_player(name, "Channel "..channel.." does not exist.") elseif msg == "" then minetest.chat_send_player(name, "Please enter the message you would like to send.") - elseif not beerchat.playersChannels[name][channel_name] then - minetest.chat_send_player(name, "You need to join channel " .. channel_name + elseif not beerchat.playersChannels[name][channel] then + minetest.chat_send_player(name, "You need to join channel " .. channel .. " in order to be able to send messages to it") else - local cb_result, cb_message = beerchat.execute_callbacks('before_send_me', name, msg, channel_name) - beerchat.on_me_message(channel_name, name, msg) + local cb_result, cb_message = beerchat.execute_callbacks('before_send_me', name, msg, channel) + beerchat.on_me_message(channel, name, msg) if not cb_result then if cb_message then return false, cb_message else return false end end for _,player in ipairs(minetest.get_connected_players()) do local target = player:get_player_name() -- Checking if the target is in this channel - if beerchat.is_player_subscribed_to_channel(target, channel_name) then + if beerchat.is_player_subscribed_to_channel(target, channel) then if not beerchat.has_player_muted_player(target, name) then beerchat.send_message( target, beerchat.format_message(me_message_string, { to_player = target, - channel_name = channel_name, + channel_name = channel, from_player = name, message = msg }), - channel_name + channel ) end end diff --git a/router.lua b/router.lua index 1a38930..ac2ca06 100644 --- a/router.lua +++ b/router.lua @@ -14,22 +14,16 @@ local function default_message_handler(name, message) return true end - local channel_name = beerchat.currentPlayerChannel[name] - if not beerchat.channels[channel_name] then - minetest.chat_send_player( - name, - "Channel "..channel_name.." does not exist, switching back to ".. - beerchat.main_channel_name..". Please resend your message" - ) - beerchat.currentPlayerChannel[name] = beerchat.main_channel_name - minetest.get_player_by_name(name):get_meta():set_string("beerchat:current_channel", beerchat.main_channel_name) + local channel = beerchat.get_player_channel(name) + if not channel then + beerchat.fix_player_channel(name, true) elseif message == "" then minetest.chat_send_player(name, "Please enter the message you would like to send to the channel") - elseif not beerchat.is_player_subscribed_to_channel(name, channel_name) then + elseif not beerchat.is_player_subscribed_to_channel(name, channel) then minetest.chat_send_player(name, "You need to join this channel in order to be able to send messages to it") else - beerchat.on_channel_message(channel_name, name, message) - beerchat.send_on_channel(name, channel_name, message) + beerchat.on_channel_message(channel, name, message) + beerchat.send_on_channel(name, channel, message) end return true end diff --git a/spec/init_spec.lua b/spec/init_spec.lua index cfde6dd..8ed046a 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -1,6 +1,8 @@ require("mineunit") mineunit("core") +mineunit("player") +mineunit("server") describe("Mod initialization", function() @@ -9,3 +11,46 @@ describe("Mod initialization", function() end) end) + +describe("Chatting", function() + + local SX = Player("SX", { shout = 1 }) + + setup(function() + mineunit:execute_on_joinplayer(SX) + end) + + teardown(function() + mineunit:execute_on_leaveplayer(SX) + end) + + it("sends messages", function() + spy.on(minetest, "chat_send_player") + SX:send_chat_message("Everyone ignore me, this is just a test") + assert.spy(minetest.chat_send_player).was.called() + end) + + it("creates channel", function() + SX:send_chat_message("/cc foo") + assert.not_nil(beerchat.channels["foo"]) + end) + + it("switches channels", function() + SX:send_chat_message("#foo") + assert.equals("foo", SX:get_meta():get_string("beerchat:current_channel")) + SX:send_chat_message("Everyone ignore me, this is just a test") + end) + + it("deletes channel", function() + SX:send_chat_message("/dc foo") + assert.is_nil(beerchat.channels["foo"]) + end) + + it("whispers", function() + pending("Spy is not working correctly here, possibly it cannot track indirect calls") + spy.on(beerchat, "whisper") + SX:send_chat_message("$ Everyone ignore me, this is just a test") + assert.spy(beerchat.whisper).was.called() + end) + +end)