Skip to content

Instantly share code, notes, and snippets.

@Kamshak Kamshak/cl_myview.lua
Last active Feb 20, 2018

Embed
What would you like to do?
LibK Networking
MyView = class( "MyView" )
MyView:include( BaseView )
-- Set the controller to use
MyView.static.controller = "MyController"
function MyView:OnMapChanged(map)
hook.Run("ServerMapChanged", map)
-- This is a useful pattern for derma updating:
--[[
function PANEL:Init()
self.title = vgui.Create("DLabel")
-- The first arg is always self, then hook args if using this style
hook.Add("ServerMapChanged", self, function(self, map)
self.title:SetText(map.title)
end)
end
]]--
end
function MyView:GetAllMaps()
-- Call function GetAllMaps on the server and return the promise.
-- The promise will resolve with the maps once all maps have been loaded from the database.
-- To call this e.g. from a derma frame do:
-- MyView:getInstance():GetAllMaps():Then(function(maps)
-- PrintTable(maps)
-- end, function(err)
-- Derma_Message("Loading Failed", tostring(err))
-- end)
return self:controllerTransaction("GetAllMaps")
end
function MyView:ShowLoading()
if IsValid(self.LoadingWindow) then
return
end
local Window = vgui.Create("DFrame")
Window:SetTitle("Loading")
Window:SetDraggable(false)
Window:ShowCloseButton(false)
Window:SetBackgroundBlur(true)
Window:SetDrawOnTop(true)
local InnerPanel = vgui.Create("Panel", Window)
local Text = vgui.Create("DLabel", InnerPanel)
Text:SetText("Loading, please wait...")
Text:SizeToContents()
Text:SetContentAlignment(5)
Text:SetTextColor(color_white)
local w, h = Text:GetSize()
Window:SetSize( w + 50, h + 25 + 45 + 10 )
Window:Center()
Text:StretchToParent( 5, 5, 5, 5 )
Window:MakePopup()
Window:DoModal()
self.LoadingWindow = Window
end
function MyView:HideLoading()
if IsValid(self.LoadingWindow) then
self.LoadingWindow:Remove()
end
end
function MyView:SaveMap(map)
-- Save the map.
self:ShowLoading()
return self:controllerTransaction("SaveMap", map)
:Then(function(newMap)
print("The map has been updated on the server!")
end, function(error)
-- Saving failed. Could be SQL error
ErrorNoHalt("Updating failed")
end)
:Always(function()
-- The always callback is called on both, success and error
self:HideLoading()
end)
end
-- Set up the class
MyController = class( "MyController" )
MyController:include( BaseController )
-- This controlls which methods of the class can be used.
-- Use this for permission checks.
-- ply is the player who is trying to call the method, action is the name of the method
function MyController:canDoAction( ply, action )
if action == "GetAllMaps" then
return Promise.Resolve( )
elseif action == "SaveMap" then
-- This is a LibK Interface for checking permissions across many admin mods.
-- It falls back to IsSuperAdmin() if the permission is not defined.
-- you could also return ply:IsAdmin()
return PermissionInterface.query(ply, "savemap")
end
return Promise.Reject( )
end
-- You always get the ply who called the function as first argument
function MyController:GetAllMaps(ply)
return Maps.getAll()
end
-- You can add as many additional arguments as you like
function MyController:SaveMap(ply, map)
return map:save():Then(function(map)
self:BroadcastMapChanged(map)
end)
end
function MyController:BroadcastMapChanged(map)
-- startView sends SV -> CL, here we call MyView:OnMapChanged(map) on all clients.
-- BaseController:startView(viewName, functionName, player or table of players, ...arguments)
self:startView("MyView", "OnMapChanged", player.GetAll(), map)
end
function MyController:PlayerJoined(ply)
print(ply:Nick() .. " has joined.")
end
hook.Add("LibK_PlayerInitialSpawn", "MyController_PlayerJoined", function(ply)
-- This is how you get a controller from outside
MyController:getInstance():PlayerJoined(ply)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.