Chatcommands

This commit is contained in:
AliasAlreadyTaken 2023-05-30 14:02:45 +02:00
parent 25d2a1e76e
commit b73ad72286
5 changed files with 186 additions and 25 deletions

View File

@ -1,3 +0,0 @@
function yl_cinema.listmovies(searchterm)
return yl_cinema.search_movies(yl_cinema.movies, searchterm)
end

View File

@ -5,26 +5,7 @@ local chatcommand_definition = {
privs = {
[yl_cinema.settings.admin_priv] = true
}, -- Require the "privs" privilege to run
func = function(name, param)
local args = string.split(param, " ")
if (#args > 1) then
return false, "Usage: /listmovies [<searchstring>]"
end
local searchterm = args[1] or ""
core.log("action", "[yl_cinema] Player " .. name .. " searches for movie " .. searchterm)
local success, msg = yl_cinema.listmovies(searchterm)
if success then
return true, dump(msg)
else
return false, "No movies matching your criteria were found."
end
end
func = yl_cinema.cmd_list_movies
}
minetest.register_chatcommand(chatcommand_cmd, chatcommand_definition)

11
chatcommand_showmovie.lua Normal file
View File

@ -0,0 +1,11 @@
local chatcommand_cmd = "showmovie"
local chatcommand_definition = {
params = "<name> <movie>", -- Short parameter description
description = "Shows to the <player> the <movie>", -- Full description
privs = {
[yl_cinema.settings.admin_priv] = true
}, -- Require the "privs" privilege to run
func = yl_cinema.cmd_show_movie
}
minetest.register_chatcommand(chatcommand_cmd, chatcommand_definition)

View File

@ -1,2 +1,2 @@
--dofile(yl_cinema.modpath .. "chatcommand_showmovie.lua")
dofile(yl_cinema.modpath .. "chatcommand_showmovie.lua")
dofile(yl_cinema.modpath .. "chatcommand_listmovies.lua")

View File

@ -2,6 +2,8 @@
-- Those that do real work should be local and wrapped in public functions
-- Minetest connector
local json = yl_cinema.dependencies.json
local F = core.formspec_escape
yl_cinema.watching = {}
local function warn(text)
core.log("warning", "[yl_cinema] " .. text)
@ -22,6 +24,23 @@ end
-- internal functions
local function watches(playername, movie_id)
yl_cinema.watching[playername] = movie_id
end
local function watched_by(playername)
return yl_cinema.watching[playername] or ""
end
local function stops_watching(player)
local playername = player:get_player_name()
yl_cinema.watching[playername] = nil
end
local function orderpages(a, b)
return a.order < b.order
end
local function mediacallback(name)
local target_name = name or ""
action("mediacallback: File pushed to player " .. target_name)
@ -130,6 +149,159 @@ local function search_movies(movies, search_term)
end
end
local function get_movieformspec(movie_id, pagenum)
local movie = yl_cinema.get_movie(movie_id)
local screen_width = 12
local screen_height = 6.75
local screensize = screen_width .. "," .. 6.75
local formspec = {}
table.insert(formspec, "formspec_version[4]size[".. screensize.."]bgcolor[#000000]")
if not movie or not pagenum then
warn("Some error happened. movie=" .. dump(movie) .. ", page=" .. dump(pagenum))
table.insert(formspec, "[0.5,0.5;Some Error happened.]")
return table.concat(formspec, "")
end
if pagenum == 0 then
table.insert(formspec, "background[0,0;".. screensize..";" .. (F(movie.title_texture) or "") .. ";true]")
table.insert(formspec, "label[0.5,0.5;" .. (F(movie.name) or "") .. "]")
table.insert(formspec, "textarea[0.5,1.5;".. screensize ..";;" .. (F(movie.description) or "") .. ";]")
else
local page = movie.pages[pagenum]
if not page then warn("Page empty = "..dump(page)) return "" end
table.insert(formspec, "background[0,0;".. screensize..";" .. (F(page.texture) or "") .. ";true]")
table.insert(formspec, "label[" .. (F(page.captionposx) or 0) .. "," .. (F(page.captionposy) or 0) .. ";" .. (F(page.caption) or "") .. "]")
if pagenum == #movie.pages then
table.insert(formspec, "button[3,".. (F(screen_height) -1 ) ..";2,1;replay;Replay]")
table.insert(formspec, "button_exit[6,".. (F(screen_height) -1) .. ";2,1;exit;Exit]")
end
end
return table.concat(formspec, "")
end
local function showpage(target_player_name, movie_id, pagenum)
if not core.get_player_by_name(target_player_name) then return end
if watched_by(target_player_name) == "" then return end
local formspec = get_movieformspec(movie_id, pagenum)
local movie = get_movie(movie_id)
local pages = movie.pages
if pagenum <= #pages then
table.sort(pages,orderpages)
local page = pages[pagenum] or {}
local delay = page.duration or 3
local nextpage = pagenum + 1
core.after(delay,showpage,target_player_name, movie_id, nextpage)
watches(target_player_name,movie_id)
core.show_formspec(target_player_name, "yl_cinema:movie", formspec)
end
end
--
core.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "yl_cinema:movie" then return end
local playername = player:get_player_name()
if fields.exit or fields.quit then
stops_watching(player)
end
if player and fields.replay then
local movie_id = watched_by(playername)
showpage(player:get_player_name(), movie_id, 0)
end
action("fields=" .. dump(fields))
end)
core.register_on_leaveplayer(stops_watching)
core.register_on_dieplayer(stops_watching)
-- Chatcommands
function yl_cinema.cmd_list_movies(name, param)
local args = string.split(param, " ")
if (#args > 1) then
return false, "Usage: /listmovies [<searchstring>]"
end
local searchterm = args[1] or ""
core.log("action", "[yl_cinema] Player " .. name .. " searches for movie " .. searchterm)
local success, msg = yl_cinema.listmovies(searchterm)
if success then
return true, dump(msg)
else
return false, "No movies matching your criteria were found."
end
end
function yl_cinema.cmd_show_movie(name, param)
local args = string.split(param, " ")
if (#args ~= 2) then
return false, "Usage: /showmovie <playername> <movie>"
end
if not core.get_player_by_name(args[1]) then
return false, "Player not online"
end
local target_player_name = args[1] or ""
local movie_id = args[2] or ""
core.log("action", "[yl_cinema] Player " .. name .. " shows movie " .. movie_id .. " to " .. target_player_name)
local success, msg = yl_cinema.startmovie(movie_id, target_player_name)
if success then
return true, msg
else
return false, msg
end
end
function yl_cinema.listmovies(searchterm)
return search_movies(yl_cinema.movies, searchterm)
end
function yl_cinema.startmovie(movie_id, target_player_name)
local pagenum = 0
watches(target_player_name, movie_id)
-- Start to show a movie
if core.get_player_by_name(target_player_name) then
showpage(target_player_name, movie_id, pagenum)
return true, "Movie " .. movie_id .. " started for " .. target_player_name
else
return false, "Player " .. target_player_name .. " not found."
end
end
-- Called when command is run. Returns boolean success and text output.
-- Special case: The help message is shown to the player if `func`
-- returns false without a text output.
-- Public functions wrap the private ones, so they can be exchanged easily
function yl_cinema.load(filename, ...)
@ -191,4 +363,4 @@ function yl_cinema.get_movie(movie_id, ...)
end
yl_cinema.action = action
yl_cinema.warn = warn
yl_cinema.warn = warn