Advertisement
Guest User

Untitled

a guest
May 4th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. // -----------------------------------------------
  2. //
  3. // SAPHIRA CORE                 Version 1.0
  4. // Developed by Aryel 'DfKimera' Tupinambá
  5. //
  6. // File: server/sv.core.lua
  7. // Purpose: Core script. Manages and propagates events and hooks.
  8. // Version: 1.0
  9. // Last updated: 18-Oct-2009
  10. //
  11. // -----------------------------------------------
  12.  
  13. // TO-DO: Relocate the config to its own place
  14. Config = {}
  15.  
  16. Config.Hostname = "localhost"
  17. Config.Username = "saphira"
  18. Config.Password = ""
  19. Config.Database = "saphira"
  20.  
  21. Saph = {}
  22.  
  23. // Includes enums
  24. include("Saphira/gamemode/shared/saph.usermessages.lua")
  25. include("Saphira/gamemode/shared/saph.uicodes.lua")
  26.  
  27. // Initializes global objects
  28. Saph.DB = nil
  29. Saph.Config = Config
  30.  
  31. // Initializes cache tables
  32. Saph.Characters = {}
  33. Saph.Items = {}
  34. Saph.PlayerData = {}
  35.  
  36. // ------------------------------
  37. // Function: Initialize
  38. // Called: When the server starts
  39. // Purpose: Initializes the database
  40. // ------------------------------
  41. function Saph:Initialize()
  42.  
  43.     Saph.DB = Database:New()
  44.     Saph.DB:OpenConnection(Saph.Config)
  45.     Saph:CacheData()
  46.    
  47. end
  48.  
  49. // ------------------------------
  50. // Function: CacheData
  51. // Purpose: Takes all current data from the database and caches locally for better speeds
  52. // ------------------------------
  53. function Saph:CacheData()
  54.    
  55.     local characters = Saph.DB:Get("characters", {'id','name','model','title'})
  56.     for k,v in pairs(characters) do
  57.         Saph.Characters[v['id']] = v
  58.     end
  59.    
  60.     // TO-DO: Cache items, etc.
  61.    
  62. end
  63.  
  64. // ------------------------------
  65. // Function: PlayerSpawn
  66. // Called: When the player spawns for the first time
  67. // Purpose: Manages newly arrived players
  68. // ------------------------------
  69. function Saph:PlayerInitialSpawn(ply)
  70.  
  71.     Saph:RegisterPlayer(ply) //timer.Create( "Saphira.PlayerInitialSpawn", 1, 1, self.RegisterPlayer, ply )
  72.    
  73. end
  74.  
  75. // ------------------------------
  76. // Function: InitializePlayer
  77. // Purpose:
  78. // ------------------------------
  79.  
  80. // TO-DO: Hook this event with cache transfer using the data exchanger.
  81. function Saph:InitializePlayer(ply)
  82.  
  83.     ply:Freeze()
  84.     ply:ShowUI(Saph.UI.CharacterSelection)
  85.    
  86. end
  87.  
  88. // ------------------------------
  89. // Function: RegisterPlayer
  90. // Purpose: Registers the player in the database
  91. // ------------------------------
  92. function Saph:RegisterPlayer(ply)
  93.    
  94.     trace("Logging player: " .. ply:SteamID())
  95.    
  96.     playerData = Saph.DB:GetSingle("players", {'id'}, ply:SteamID(), "steamid")
  97.    
  98.     if(!playerData) then
  99.    
  100.         trace("Player is new, creating account...")
  101.    
  102.         playerData = {
  103.             ip = ply:IPAddress(),
  104.             steamid = ply:SteamID(),
  105.             name = ply:Name()
  106.         }
  107.        
  108.         SaphID = Saph.DB:Add("players", playerData)
  109.         ply:SetNetworkedInt("SaphID", SaphID)
  110.        
  111.         // QUERY: Can networked ints be changed by the client?
  112.         // If so, we'll have to add networked variables support on the data exchanger, and
  113.         // make it integrate seamlessly with the Player object by adding a method to the metatable.
  114.        
  115.         trace("Account created! " .. ply:SteamID() .. " = " .. SaphID)
  116.        
  117.         Saph:InitializePlayer(ply)
  118.     else
  119.        
  120.         SaphID = playerData.id
  121.         ply:SetNetworkedInt("SaphID", SaphID)
  122.        
  123.         trace("Logged in! " .. ply:SteamID() .. " = " .. SaphID)
  124.    
  125.         Saph:InitializePlayer(ply)
  126.        
  127.     end
  128. end
  129.  
  130. // ------------------------------
  131. // Function: CreateCharacter
  132. // Purpose: Creates a character
  133. // ------------------------------
  134.  
  135. // TO-DO: Trigger the cache manager to update the clients cache. If the player just created a character, he's most likely going to use it.
  136. function Saph:CreateCharacter(ply, characterData)
  137.    
  138.     CharID = Saph.DB:Add("characters", characterData)
  139.     table.insert( Saph.Characters, CharID, Character:New(CharID, characterData) )
  140.    
  141.     return CharID
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement