You've already forked ComfyFactorio
mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-11-25 22:32:18 +02:00
Poll: parse when sending to backend
This commit is contained in:
@@ -4,7 +4,7 @@ local Public = {}
|
|||||||
|
|
||||||
local notification = Discord.channel_names.scenario_notifications
|
local notification = Discord.channel_names.scenario_notifications
|
||||||
|
|
||||||
--- Send a parsed message to the connected channel.
|
--- Send a parsed message to the named discord channel.
|
||||||
--- Requires at least a title and a description
|
--- Requires at least a title and a description
|
||||||
---@param data table
|
---@param data table
|
||||||
function Public.send_notification(data)
|
function Public.send_notification(data)
|
||||||
@@ -19,6 +19,21 @@ function Public.send_notification(data)
|
|||||||
Server.to_discord_named_parsed_embed(notification, data)
|
Server.to_discord_named_parsed_embed(notification, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Send a parsed message to the connected channel.
|
||||||
|
--- Requires at least a title and a description
|
||||||
|
---@param data table
|
||||||
|
function Public.send_notification_connected_channel(data)
|
||||||
|
if not data or not data.title or not data.description then
|
||||||
|
return error("Title and description is required.", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
if game.tick < 10 then return end
|
||||||
|
|
||||||
|
data.tick = game.tick
|
||||||
|
|
||||||
|
Server.to_discord_embed_parsed(data)
|
||||||
|
end
|
||||||
|
|
||||||
--- Send a message to the connected channel.
|
--- Send a message to the connected channel.
|
||||||
--- Requires a title and a description
|
--- Requires a title and a description
|
||||||
---@param scenario_name string|nil
|
---@param scenario_name string|nil
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ local session = require 'utils.datastore.session_data'
|
|||||||
local Config = require 'utils.gui.config'
|
local Config = require 'utils.gui.config'
|
||||||
local SpamProtection = require 'utils.spam_protection'
|
local SpamProtection = require 'utils.spam_protection'
|
||||||
local Math = require 'utils.math.math'
|
local Math = require 'utils.math.math'
|
||||||
|
local Discord = require 'utils.discord_handler'
|
||||||
local Public = {}
|
local Public = {}
|
||||||
|
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
@@ -116,48 +117,65 @@ local function do_remaining_time(poll, remaining_time_label)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function send_poll_result_to_discord(poll)
|
local function send_poll_result_to_discord(poll, complete)
|
||||||
local result = { 'Poll #', poll.id }
|
local fields = {}
|
||||||
|
|
||||||
local created_by_player = poll.created_by
|
-- Created by
|
||||||
if created_by_player then
|
if poll.created_by then
|
||||||
insert(result, ' Created by ')
|
table.insert(fields,
|
||||||
insert(result, created_by_player)
|
{
|
||||||
|
title = "Created by",
|
||||||
|
description = poll.created_by,
|
||||||
|
inline = "false"
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Edited by
|
||||||
local edited_by_players = poll.edited_by
|
local edited_by_players = poll.edited_by
|
||||||
if next(edited_by_players) then
|
if next(edited_by_players) then
|
||||||
insert(result, ' Edited by ')
|
local editors = {}
|
||||||
for pi, _ in pairs(edited_by_players) do
|
for pi, _ in pairs(edited_by_players) do
|
||||||
local p = game.players[pi]
|
local p = game.players[pi]
|
||||||
if p and p.valid then
|
if p and p.valid then
|
||||||
insert(result, p.name)
|
table.insert(editors, p.name)
|
||||||
insert(result, ', ')
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.remove(result)
|
|
||||||
end
|
|
||||||
|
|
||||||
insert(result, '\\n**Question: ')
|
if #editors > 0 then
|
||||||
insert(result, poll.question)
|
table.insert(fields,
|
||||||
insert(result, '**\\n')
|
{
|
||||||
|
title = "Edited by",
|
||||||
|
description = table.concat(editors, ", "),
|
||||||
|
inline = "false"
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Answers (each as inline field)
|
||||||
local answers = poll.answers
|
local answers = poll.answers
|
||||||
local answers_count = #answers
|
if answers and #answers > 0 then
|
||||||
for i, a in pairs(answers) do
|
for _, a in ipairs(answers) do
|
||||||
insert(result, '[')
|
table.insert(fields,
|
||||||
insert(result, a.voted_count)
|
{
|
||||||
insert(result, '] - ')
|
title = a.text,
|
||||||
insert(result, a.text)
|
description = "**" .. a.voted_count .. "** votes",
|
||||||
if i ~= answers_count then
|
inline = "true"
|
||||||
insert(result, '\\n')
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local message = table.concat(result)
|
-- Send to Discord
|
||||||
Server.to_discord_embed(message)
|
Discord.send_notification_connected_channel(
|
||||||
|
{
|
||||||
|
title = complete and "Poll #" .. poll.id .. " has finished" or "Poll #" .. poll.id,
|
||||||
|
description = "**Question:** " .. poll.question,
|
||||||
|
color = complete and "success" or "info",
|
||||||
|
fields = fields,
|
||||||
|
tick = game.tick
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function redraw_poll_viewer_content(data)
|
local function redraw_poll_viewer_content(data)
|
||||||
local poll_viewer_content = data.poll_viewer_content
|
local poll_viewer_content = data.poll_viewer_content
|
||||||
local remaining_time_label = data.remaining_time_label
|
local remaining_time_label = data.remaining_time_label
|
||||||
@@ -871,7 +889,7 @@ local function tick()
|
|||||||
local poll = running_polls[i]
|
local poll = running_polls[i]
|
||||||
if poll_complete(poll) then
|
if poll_complete(poll) then
|
||||||
table.remove(running_polls, i)
|
table.remove(running_polls, i)
|
||||||
send_poll_result_to_discord(poll)
|
send_poll_result_to_discord(poll, true)
|
||||||
|
|
||||||
local message = table.concat { 'Poll finished: Poll #', poll.id, ': ', poll.question }
|
local message = table.concat { 'Poll finished: Poll #', poll.id, ': ', poll.question }
|
||||||
for _, p in pairs(game.connected_players) do
|
for _, p in pairs(game.connected_players) do
|
||||||
@@ -1491,7 +1509,7 @@ end
|
|||||||
|
|
||||||
function Public.send_poll_result_to_discord(id)
|
function Public.send_poll_result_to_discord(id)
|
||||||
if type(id) ~= 'number' then
|
if type(id) ~= 'number' then
|
||||||
Server.to_discord_embed('poll-id must be a number')
|
Server.output_script_data('poll-id must be a number')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1503,7 +1521,7 @@ function Public.send_poll_result_to_discord(id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local message = table.concat { 'poll #', id, ' not found' }
|
local message = table.concat { 'poll #', id, ' not found' }
|
||||||
Server.to_discord_embed(message)
|
Server.output_script_data(message)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Public.poll_complete(id)
|
function Public.poll_complete(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user