· 8 years ago · Jan 23, 2018, 09:12 PM
1local PLUGIN = PLUGIN;
2
3-- Create the table for the datafile.
4function cwDatafile:ClockworkDatabaseConnected()
5 local CREATE_DATAFILE_TABLE = [[
6 CREATE TABLE IF NOT EXISTS `]]..Clockwork.config:Get("mysql_datafile_table"):Get()..[[` (
7 `_Key` smallint(11) unsigned NOT NULL AUTO_INCREMENT,
8 `_CharacterID` varchar(50) NOT NULL,
9 `_CharacterName` varchar(150) NOT NULL,
10 `_SteamID` varchar(60) NOT NULL,
11 `_Schema` text NOT NULL,
12 `_GenericData` text NOT NULL,
13 `_Datafile` text NOT NULL,
14 PRIMARY KEY (`_Key`),
15 KEY `_CharacterName` (`_CharacterName`),
16 KEY `_SteamID` (`_SteamID`))
17 ]];
18
19 mysql:RawQuery(string.gsub(CREATE_DATAFILE_TABLE, "%s", " "));
20end;
21
22-- Check if the player has a datafile or not. If not, create one.
23function cwDatafile:PostPlayerSpawn(player)
24 local bHasDatafile = self:HasDatafile(player);
25
26 -- Nil because the bHasDatafile is not in every player their character data.
27 if ((!bHasDatafile or bHasDatafile == nil) and self:IsRestrictedFaction(player)) then
28 self:CreateDatafile(player);
29 end;
30
31 -- load the datafile again with the new changes.
32 self:LoadDatafile(player);
33end;
34
35-- Function to load the datafile on the player's character. Used after updating something in the MySQL.
36function cwDatafile:LoadDatafile(player)
37 if (IsValid(player)) then
38 local schemaFolder = Clockwork.kernel:GetSchemaFolder();
39 local datafileTable = Clockwork.config:Get("mysql_datafile_table"):Get();
40 local character = player:GetCharacter();
41
42 local queryObj = mysql:Select(datafileTable);
43 queryObj:Where("_CharacterID", character.characterID);
44 queryObj:Where("_SteamID", player:SteamID());
45 queryObj:Where("_Schema", schemaFolder);
46 queryObj:Callback(function(result, status, lastID)
47 if (!IsValid(player)) then return; end;
48
49 if (mysql:IsResult(result)) then
50 player.cwDatafile = {
51 GenericData = Clockwork.json:Decode(result[1]._GenericData);
52 Datafile = Clockwork.json:Decode(result[1]._Datafile);
53 };
54 end;
55 end);
56 queryObj:ExecutePool(Clockwork.pool);
57 end;
58end;
59
60-- Create a datafile for the player.
61function cwDatafile:CreateDatafile(player)
62 if (IsValid(player)) then
63 local schemaFolder = Clockwork.kernel:GetSchemaFolder();
64 local datafileTable = Clockwork.config:Get("mysql_datafile_table"):Get();
65 local character = player:GetCharacter();
66 local steamID = player:SteamID();
67
68 local defaultDatafile = self.Default.CivilianData;
69
70 if (Schema:PlayerIsCombine(player)) then
71 defaultDatafile = self.Default.CombineData;
72 end;
73
74 -- Set all the values.
75 local insertObj = mysql:Insert(datafileTable);
76 insertObj:Insert("_CharacterID", character.characterID);
77 insertObj:Insert("_CharacterName", character.name);
78 insertObj:Insert("_SteamID", steamID);
79 insertObj:Insert("_Schema", schemaFolder);
80 insertObj:Insert("_GenericData", Clockwork.json:Encode(PLUGIN.Default.GenericData));
81 insertObj:Insert("_Datafile", Clockwork.json:Encode(defaultDatafile));
82 insertObj:Callback(function(result)
83 cwDatafile:SetHasDatafile(player, true);
84 end);
85 insertObj:ExecutePool(Clockwork.pool);
86 end;
87end;
88
89-- Sets whether as character has a datafile.
90function cwDatafile:SetHasDatafile(player, bhasDatafile)
91 player:SetCharacterData("HasDatafile", bhasDatafile);
92end;
93
94-- Returns true if the player has a datafile.
95function cwDatafile:HasDatafile(player)
96 return player:GetCharacterData("HasDatafile");
97end;
98
99-- Datafile handler. Decides what to do when a player types /Datafile John Doe.
100function cwDatafile:HandleDatafile(player, target)
101 local playerValue = self:ReturnPermission(player);
102 local targetValue = self:ReturnPermission(target);
103 local bTargetIsRestricted, restrictedText = self:IsRestricted(player);
104
105 if (playerValue >= targetValue) then
106 if (playerValue == DATAFILE_PERMISSION_NONE) then
107 Clockwork.player:Notify(player, "You are not authorized to access this datafile.");
108
109 return false;
110 end;
111
112 local GenericData = self:ReturnGenericData(target);
113 local datafile = self:ReturnDatafile(target);
114
115 if (playerValue == DATAFILE_PERMISSION_MINOR) then
116 if (bTargetIsRestricted) then
117 Clockwork.player:Notify(player, "This datafile has been restricted; access denied. REASON: " .. restrictedText);
118
119 return false;
120 end;
121
122 for k, v in pairs(datafile) do
123 if (v.category == "civil") then
124 table.remove(datafile, k);
125 end;
126 end;
127
128 Clockwork.datastream:Start(player, "CreateRestrictedDatafile", {target, GenericData, datafile});
129 else
130 Clockwork.datastream:Start(player, "CreateFullDatafile", {target, GenericData, datafile});
131 end;
132
133 elseif (playerValue < targetValue) then
134 Clockwork.player:Notify(player, "You are not authorized to access this datafile.");
135
136 return false;
137 end;
138end;
139
140-- Datastream
141
142-- Update the last seen.
143Clockwork.datastream:Hook("UpdateLastSeen", function(player, data)
144 local target = data[1];
145
146 cwDatafile:UpdateLastSeen(target);
147end);
148
149-- Update the civil status.
150Clockwork.datastream:Hook("UpdateCivilStatus", function(player, data)
151 local target = data[1];
152 local civilStatus = data[2];
153
154 cwDatafile:SetCivilStatus(target, player, civilStatus);
155end);
156
157-- Add a new entry.
158Clockwork.datastream:Hook("AddDatafileEntry", function(player, data)
159 local target = data[1];
160 local category = data[2];
161 local text = data[3];
162 local points = data[4];
163
164 cwDatafile:AddEntry(category, text, points, target, player, false);
165end);
166
167-- Add/remove a BOL.
168Clockwork.datastream:Hook("SetBOL", function(player, data)
169 local target = data[1];
170 local bHasBOL = cwDatafile:ReturnBOL(player);
171
172 if (bHasBOL) then
173 cwDatafile:SetBOL(false, "", target, player);
174 else
175 cwDatafile:SetBOL(true, "", target, player);
176 end;
177end);
178
179-- Send the points of the player back to the user.
180Clockwork.datastream:Hook("RequestPoints", function(player, data)
181 local target = data[1];
182
183 if (cwDatafile:ReturnPermission(player) == DATAFILE_PERMISSION_MINOR and (cwDatafile:ReturnPermission(target) == DATAFILE_PERMISSION_NONE or cwDatafile:ReturnPermission(target) == DATAFILE_PERMISSION_MINOR)) then
184 Clockwork.datastream:Start(player, "SendPoints", {cwDatafile:ReturnPoints(target)});
185 end;
186end);
187
188-- Remove a line from someone their datafile.
189Clockwork.datastream:Hook("RemoveDatafileLine", function(player, data)
190 local target = data[1];
191 local key = data[2];
192 local date = data[3];
193 local category = data[4];
194 local text = data[5];
195
196 cwDatafile:RemoveEntry(player, target, key, date, category, text);
197end);
198
199-- Refresh the active datafile panel of a player.
200Clockwork.datastream:Hook("RefreshDatafile", function(player, data)
201 local target = data[1];
202
203 cwDatafile:HandleDatafile(player, target);
204end);