Skip to content

Commit

Permalink
Colourise messages on the server side
Browse files Browse the repository at this point in the history
Also add /self for listing rooms owned and invites
  • Loading branch information
LBPHacker committed Jul 4, 2021
1 parent c740f13 commit 6205f17
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 154 deletions.
3 changes: 1 addition & 2 deletions tptmp/client/colours.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ for key, value in pairs({
fpssyncenable = { 255, 100, 255 },
fpssyncdisable = { 130, 130, 255 },
lobby = { 0, 200, 200 },
normal = { 200, 200, 200 },
neutral = { 200, 200, 200 },
room = { 200, 200, 0 },
server = { 127, 255, 255 },
status = { 150, 150, 150 },
notif_normal = { 100, 100, 100 },
notif_important = { 255, 50, 50 },
Expand Down
7 changes: 7 additions & 0 deletions tptmp/client/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ local names = {
[ "guest" ] = "guest lobby",
[ "kicked" ] = "kicked lobby",
}

local function room(unformatted)
local name = names[unformatted]
return name and (colours.commonstr.lobby .. name) or (colours.commonstr.room .. unformatted)
end

local function troom(unformatted)
local name = names[unformatted]
return name and (colours.commonstr.lobby .. name) or ("room " .. colours.commonstr.room .. unformatted)
end

return {
nick = nick,
room = room,
troom = troom,
}
30 changes: 15 additions & 15 deletions tptmp/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ local function run()
win:backlog_bump_marker()
win.in_focus = true
end
win = window.new({
hide_window_func = hide_window,
window_hidden_func = window_hidden,
client_func = function()
return cli and cli:registered() and cli
end,
localcmd_parse_func = function(str)
return cmd:parse(str)
end,
placing_zoom_func = function(str)
return prof:placing_zoom()
end,
})
local cmd = localcmd.new({
client_func = function()
return cli and cli:registered() and cli
Expand All @@ -108,22 +121,9 @@ local function run()
should_reconnect = false
kill_client()
end,
})
win = window.new({
hide_window_func = hide_window,
window_hidden_func = window_hidden,
client_func = function()
return cli and cli:registered() and cli
end,
localcmd_parse_func = function(str)
return cmd:parse(str)
end,
placing_zoom_func = function(str)
return prof:placing_zoom()
end,
window = win,
})
win.localcmd = cmd
cmd.window = win
local sbtn = side_button.new({
notif_count_func = function()
return win:backlog_notif_count()
Expand Down Expand Up @@ -434,7 +434,7 @@ local function run()
end

win:set_subtitle("status", "Not connected")
win:backlog_push_neutral("* Type " .. colours.commonstr.error .. "/connect" .. colours.commonstr.normal .. " to join a server, " .. colours.commonstr.error .. "/list" .. colours.commonstr.normal .. " for a list of commands, or " .. colours.commonstr.error .. "/help" .. colours.commonstr.normal .. " for command help")
win:backlog_push_neutral("* Type " .. colours.commonstr.error .. "/connect" .. colours.commonstr.neutral .. " to join a server, " .. colours.commonstr.error .. "/list" .. colours.commonstr.neutral .. " for a list of commands, or " .. colours.commonstr.error .. "/help" .. colours.commonstr.neutral .. " for command help")
win:backlog_notif_reset()
end

Expand Down
48 changes: 27 additions & 21 deletions tptmp/client/localcmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local config = require("tptmp.client.config")
local format = require("tptmp.client.format")
local manager = require("tptmp.client.manager")
local command_parser = require("tptmp.common.command_parser")
local colours = require("tptmp.client.colours")

local localcmd_i = {}
local localcmd_m = { __index = localcmd_i }
Expand All @@ -23,7 +24,7 @@ local cmdp = command_parser.new({
if not width or not height then
return false
else
localcmd.window:set_size(width, height)
localcmd.window_:set_size(width, height)
end
return true
end,
Expand All @@ -34,9 +35,9 @@ local cmdp = command_parser.new({
local cli = localcmd.client_func_()
if cli then
cli:send_sync()
localcmd.window:backlog_push_neutral("* Simulation synchronized")
localcmd.window_:backlog_push_neutral("* Simulation synchronized")
else
localcmd.window:backlog_push_error("Not connected, cannot sync")
localcmd.window_:backlog_push_error("Not connected, cannot sync")
end
return true
end,
Expand All @@ -54,19 +55,19 @@ local cmdp = command_parser.new({
if cli then
cli:fps_sync(localcmd.fps_sync_)
end
localcmd.window:backlog_push_neutral("* FPS synchronization enabled")
localcmd.window:backlog_push_neutral("* Note: FPS synchronization is not currently implemented, this command is just a placeholder") -- * TODO[imm]: remove this
localcmd.window_:backlog_push_neutral("* FPS synchronization enabled")
localcmd.window_:backlog_push_neutral("* Note: FPS synchronization is not currently implemented, this command is just a placeholder") -- * TODO[imm]: remove this
return true
elseif words[2] == "check" or not words[2] then
if localcmd.fps_sync_ then
local cli = localcmd.client_func_()
if cli then
cli:push_fpssync()
else
localcmd.window:backlog_push_fpssync(true)
localcmd.window_:backlog_push_fpssync(true)
end
else
localcmd.window:backlog_push_fpssync(false)
localcmd.window_:backlog_push_fpssync(false)
end
return true
elseif words[2] == "off" then
Expand All @@ -75,7 +76,7 @@ local cmdp = command_parser.new({
if cli then
cli:fps_sync(localcmd.fps_sync_)
end
localcmd.window:backlog_push_neutral("* FPS synchronization disabled")
localcmd.window_:backlog_push_neutral("* FPS synchronization disabled")
return true
end
return false
Expand All @@ -94,7 +95,7 @@ local cmdp = command_parser.new({
reconnect = {
macro = function(localcmd, message, words, offsets)
if not localcmd.reconnect_ then
localcmd.window:backlog_push_error("No successful connection on record, cannot reconnect")
localcmd.window_:backlog_push_error("No successful connection on record, cannot reconnect")
return {}
end
return { "connectroom", localcmd.reconnect_.room, localcmd.reconnect_.host .. ":" .. localcmd.reconnect_.secr .. localcmd.reconnect_.port }
Expand All @@ -107,7 +108,7 @@ local cmdp = command_parser.new({
if not words[2] then
localcmd:print_help_("connectroom")
elseif cli then
localcmd.window:backlog_push_error("Already connected")
localcmd.window_:backlog_push_error("Already connected")
else
local host = words[3] or config.default_host
local host_without_port, port = host:match("^(.+):(%+?[^:]+)$")
Expand Down Expand Up @@ -139,7 +140,7 @@ local cmdp = command_parser.new({
if cli then
localcmd.kill_client_func_()
else
localcmd.window:backlog_push_error("Not connected")
localcmd.window_:backlog_push_error("Not connected")
end
return true
end,
Expand All @@ -160,16 +161,16 @@ local cmdp = command_parser.new({
if cli then
cli:push_names("Currently in ")
else
localcmd.window:backlog_push_error("Not connected")
localcmd.window_:backlog_push_error("Not connected")
end
return true
end,
help = "/names, no arguments: tells you which room you are in and lists users present",
},
clear = {
func = function(localcmd, message, words, offsets)
localcmd.window:backlog_reset()
localcmd.window:backlog_push_neutral("* Backlog cleared")
localcmd.window_:backlog_reset()
localcmd.window_:backlog_push_neutral("* Backlog cleared")
return true
end,
help = "/clear, no arguments: clears the chat window",
Expand All @@ -181,10 +182,10 @@ local cmdp = command_parser.new({
return false
elseif cli then
local msg = message:sub(offsets[2])
localcmd.window:backlog_push_say3rd(cli:formatted_nick(), msg)
localcmd.window_:backlog_push_say3rd(cli:formatted_nick(), msg)
cli:send_say3rd(msg)
else
localcmd.window:backlog_push_error("Not connected, message not sent")
localcmd.window_:backlog_push_error("Not connected, message not sent")
end
return true
end,
Expand All @@ -195,6 +196,7 @@ local cmdp = command_parser.new({
localcmd.nick_colour_seed_ = words[2] or tostring(math.random())
manager.set("nickColourSeed", tostring(localcmd.nick_colour_seed_))
local cli = localcmd.client_func_()
localcmd.window_:nick_colour_seed(localcmd.nick_colour_seed_)
if cli then
cli:nick_colour_seed(localcmd.nick_colour_seed_)
end
Expand All @@ -204,7 +206,7 @@ local cmdp = command_parser.new({
},
},
respond = function(localcmd, message)
localcmd.window:backlog_push_neutral("* " .. message)
localcmd.window_:backlog_push_neutral(message)
end,
cmd_fallback = function(localcmd, message)
local cli = localcmd.client_func_()
Expand All @@ -228,9 +230,10 @@ local cmdp = command_parser.new({
cli:send_say("/slist")
end
end,
alias_format = "/%s is an alias for /%s",
list_format = "Client commands: %s",
unknown_format = "No such command, try /list (maybe it is server-only, connect and try again)",
help_format = colours.commonstr.neutral .. "* %s",
alias_format = colours.commonstr.neutral .. "* /%s is an alias for /%s",
list_format = colours.commonstr.neutral .. "* Client commands: %s",
unknown_format = colours.commonstr.error .. "* No such command, try /list (maybe it is server-only, connect and try again)",
})

function localcmd_i:parse(str)
Expand Down Expand Up @@ -263,14 +266,17 @@ local function new(params)
if #reconnect.room == 0 or #reconnect.host == 0 or #reconnect.port == 0 then
reconnect = nil
end
return setmetatable({
local cmd = setmetatable({
fps_sync_ = manager.get("fpsSync", "") == "on",
reconnect_ = reconnect,
client_func_ = params.client_func,
new_client_func_ = params.new_client_func,
kill_client_func_ = params.kill_client_func,
nick_colour_seed_ = manager.get("nickColourSeed", "0"),
window_ = params.window,
}, localcmd_m)
cmd.window_:nick_colour_seed(cmd.nick_colour_seed_)
return cmd
end

return {
Expand Down
31 changes: 23 additions & 8 deletions tptmp/client/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function window_i:backlog_push_say3rd(formatted_nick, str, important)
end

function window_i:backlog_push_room(room, members, prefix)
local sep = colours.commonstr.normal .. ", "
local collect = { colours.commonstr.normal, "* ", prefix, format.room(room), sep }
local sep = colours.commonstr.neutral .. ", "
local collect = { colours.commonstr.neutral, "* ", prefix, format.troom(room), sep }
if next(members) then
table.insert(collect, "present: ")
local first = true
Expand All @@ -77,8 +77,8 @@ function window_i:backlog_push_room(room, members, prefix)
end

function window_i:backlog_push_fpssync(members)
local sep = colours.commonstr.normal .. ", "
local collect = { colours.commonstr.normal, "* " }
local sep = colours.commonstr.neutral .. ", "
local collect = { colours.commonstr.neutral, "* " }
if members == true then
table.insert(collect, "FPS synchronization is enabled")
elseif members then
Expand All @@ -103,15 +103,29 @@ function window_i:backlog_push_fpssync(members)
end

function window_i:backlog_push_registered(formatted_nick)
self:backlog_push_str(colours.commonstr.normal .. "* Connected as " .. formatted_nick, true)
self:backlog_push_str(colours.commonstr.neutral .. "* Connected as " .. formatted_nick, true)
end

local server_colours = {
n = colours.commonstr.neutral,
e = colours.commonstr.error,
j = colours.commonstr.join,
l = colours.commonstr.leave,
}
function window_i:backlog_push_server(str)
self:backlog_push_str(colours.commonstr.server .. str, true)
local formatted = str
:gsub("\au([A-Za-z0-9-_]+)", function(cap) return format.nick(cap, self.nick_colour_seed_) end)
:gsub("\ar([A-Za-z0-9-_]+)", function(cap) return format.room(cap) end)
:gsub("\a([nejl])" , function(cap) return server_colours[cap] end)
self:backlog_push_str(formatted, true)
end

function window_i:nick_colour_seed(seed)
self.nick_colour_seed_ = seed
end

function window_i:backlog_push_neutral(str)
self:backlog_push_str(colours.commonstr.normal .. str, true)
self:backlog_push_str(colours.commonstr.neutral .. str, true)
end

function window_i:backlog_wrap_(msg)
Expand Down Expand Up @@ -985,7 +999,7 @@ function window_i:set_subtitle(template, text)
if template == "status" then
self.subtitle_ = colours.commonstr.status .. text
elseif template == "room" then
self.subtitle_ = "In " .. format.room(text)
self.subtitle_ = "In " .. format.troom(text)
end
self:subtitle_update_()
end
Expand Down Expand Up @@ -1024,6 +1038,7 @@ local function new(params)
input_history_next_ = 1,
input_editing_ = {},
input_last_say_ = 0,
nick_colour_seed_ = 0,
}, window_m)
win:input_reset_()
win:backlog_reset()
Expand Down
3 changes: 2 additions & 1 deletion tptmp/common/command_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function command_parser_i:help_(ctx, from)
end
local cmd = self.commands_[from]
if cmd then
self.respond_(ctx, cmd.help)
self.respond_(ctx, self.help_format_:format(cmd.help))
return true
end
if self.help_fallback_ then
Expand All @@ -88,6 +88,7 @@ local function new(params)
respond_ = params.respond,
help_fallback_ = params.help_fallback,
list_extra_ = params.list_extra,
help_format_ = params.help_format,
alias_format_ = params.alias_format,
list_format_ = params.list_format,
unknown_format_ = params.unknown_format,
Expand Down
2 changes: 1 addition & 1 deletion tptmp/common/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return {
-- ***********************************************************************

-- * Protocol version, between 0 and 254. 255 is reserved for future use.
version = 16, -- * TODO[fin]: Give this a bump.
version = 17, -- * TODO[fin]: Give this a bump.

-- * Client-to-server message size limit, between 0 and 255, the latter
-- limit being imposted by the protocol.
Expand Down
4 changes: 2 additions & 2 deletions tptmp/server/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function client_i:check_message_(message)
local server = self:server()
local ok, err = server:phost():call_check_all("message_ok", self, message)
if not ok then
self:send_server("* Cannot send message: " .. err)
self:send_server("\ae* Cannot send message: " .. err)
return false
end
return true
Expand All @@ -176,7 +176,7 @@ function client_i:forward_message_(format, event, packet, message)
local server = self:server()
local ok, err, rconinfo = server:phost():call_check_all("content_ok", server, message)
if not ok then
self:send_server("* Cannot send message: " .. err)
self:send_server("\ae* Cannot send message: " .. err)
self:server():rconlog(util.info_merge({
event = event .. "_fail",
client_name = self.name_,
Expand Down
2 changes: 1 addition & 1 deletion tptmp/server/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,5 @@ return {
tpt_version_min = { 91, 4 }, -- * TODO[fin]: Bump to 96.0.

-- * Maximum accepted TPT version.
tpt_version_max = { 95, 0 }, -- * TODO[fin]: Bump to 96.0.
tpt_version_max = { 96, 0 },
}
2 changes: 1 addition & 1 deletion tptmp/server/plugins/back.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ return {
if client.back_target_ then
return { "join", client.back_target_ }
end
client:send_server("* No previous room")
client:send_server("\ae* No previous room")
return {}
end,
help = "/back, no arguments: joins the room you were previously in",
Expand Down
Loading

0 comments on commit 6205f17

Please sign in to comment.