classroom/api.lua

155 lines
3.1 KiB
Lua
Raw Permalink Normal View History

2019-07-09 15:55:56 -07:00
local _groups = {}
local _action_by_name = {}
2019-07-09 21:53:01 -07:00
local _actions = {}
2019-07-09 15:55:56 -07:00
2019-11-24 12:40:42 -08:00
-- Load data from MetaDataRef
function classroom.load_from(meta)
local groups_str = meta:get("groups")
if not groups_str then
return
end
_groups = minetest.deserialize(groups_str)
end
-- Save data to MetaDataRef
function classroom.save_to(meta)
meta:set_string("groups", minetest.serialize(_groups))
end
function classroom.save()
-- Overridden in init.lua
end
2019-07-09 15:55:56 -07:00
function classroom.get_students()
local students = {}
2019-07-10 04:44:12 -07:00
for _, player in pairs(classroom.get_connected_players()) do
if not classroom.check_player_privs(player, { teacher = true }) then
students[#students + 1] = player:get_player_name()
end
2019-07-09 15:55:56 -07:00
end
return students
end
function classroom.get_group_students(name)
local group = classroom.get_group(name)
if not group then
return nil
end
local students = {}
for _, student in pairs(group.students) do
2019-07-10 04:44:12 -07:00
if classroom.get_player_by_name(student) then
2019-07-09 15:55:56 -07:00
students[#students + 1] = student
end
end
return students
end
2019-07-10 04:19:51 -07:00
function classroom.get_students_except(students)
local student_by_name = {}
for _, name in pairs(students) do
student_by_name[name] = true
end
local retval = {}
2019-07-10 04:44:12 -07:00
for _, player in pairs(classroom.get_connected_players()) do
2019-07-10 04:19:51 -07:00
if not student_by_name[player:get_player_name()] and
2019-07-10 04:44:12 -07:00
not classroom.check_player_privs(player, { teacher = true }) then
2019-07-10 04:19:51 -07:00
retval[#retval + 1] = player:get_player_name()
end
end
return retval
end
2019-07-09 15:55:56 -07:00
function classroom.get_all_groups()
return _groups
end
function classroom.get_group(name)
return _groups[name]
end
function classroom.create_group(name)
if _groups[name] or #name == 0 then
2019-07-09 15:55:56 -07:00
return nil
end
local group = {
name = name,
students = {},
}
_groups[name] = group
2019-11-24 12:40:42 -08:00
classroom.save()
2019-07-09 15:55:56 -07:00
return group
end
2019-07-10 04:19:51 -07:00
function classroom.add_student_to_group(name, student)
local group = classroom.get_group(name)
if group then
for i=1, #group.students do
if group.students[i] == student then
return
end
end
group.students[#group.students + 1] = student
2019-11-24 12:40:42 -08:00
classroom.save()
2019-07-10 04:19:51 -07:00
end
end
function classroom.remove_student_from_group(name, student)
local group = classroom.get_group(name)
if group then
for i=1, #group.students do
if group.students[i] == student then
table.remove(group.students, i)
2019-11-24 12:40:42 -08:00
classroom.save()
2019-07-10 04:19:51 -07:00
end
end
end
end
2019-07-09 15:55:56 -07:00
function classroom.register_action(name, def)
2019-07-09 21:53:01 -07:00
def.name = name
2019-07-09 15:55:56 -07:00
_action_by_name[name] = def
2019-07-09 21:53:01 -07:00
table.insert(_actions, def)
2019-07-09 15:55:56 -07:00
end
function classroom.get_actions()
2019-07-09 21:53:01 -07:00
return _actions
2019-07-09 15:55:56 -07:00
end
function classroom.get_students_by_selector(selector)
if selector == "*" then
return classroom.get_students()
elseif selector:sub(1, 6) == "group:" then
return classroom.get_group_students(selector:sub(7))
2019-07-09 21:04:20 -07:00
elseif selector:sub(1, 5) == "user:" then
local pname = selector:sub(6)
if classroom.get_player_by_name(pname) then
return { pname }
else
return {}
end
2019-07-09 15:55:56 -07:00
else
2019-07-09 21:04:20 -07:00
return {}
2019-07-09 15:55:56 -07:00
end
end
function classroom.run_action(aname, runner, selector, params)
2019-11-24 12:47:52 -08:00
local action = _action_by_name[aname]
local students = classroom.get_students_by_selector(selector)
if #students > 0 then
action.func(runner, students)
end
2019-07-09 15:55:56 -07:00
end