· 8 years ago · Mar 05, 2018, 11:56 PM
1--require("mysqloo")
2
3
4-- local HPip = "sbscwrp.site.nfoservers.com" --Keep it unless the mysql server is not the same as the gmod server
5-- local HPuser = "sbscwrp"
6-- local HPpw = "J8XECzgUQlmds"
7-- local HPdb = "sbscwrp_hwrp_hp"
8-- local HPport = 3306 -- 3306 by default
9
10-- local hpointsDB = mysqloo.connect( HPip , HPuser , HPpw , HPdb , HPport)
11
12-- function hpointsDB:onConnected()
13-- print("MYSQL HOUSE POINTS ADDON CONNECTED SUCCESFULLY!")
14-- local HPtableQuery = self:query(" CREATE TABLE IF NOT EXISTS house_points ( house VARCHAR(255) not null , points INT UNSIGNED NOT NULL);")
15
16-- local HPhouseAddGryff = self:query([[ INSERT INTO house_points (house)
17-- SELECT 'gryffindor' FROM DUAL
18-- WHERE NOT EXISTS
19-- (SELECT house FROM house_points WHERE house='gryffindor');]])
20-- local HPhouseAddSly = self:query([[ INSERT INTO house_points (house)
21-- SELECT 'slytherin' FROM DUAL
22-- WHERE NOT EXISTS
23-- (SELECT house FROM house_points WHERE house='slytherin');]])
24-- local HPhouseAddHuf = self:query([[ INSERT INTO house_points (house)
25-- SELECT 'hufflepuff' FROM DUAL
26-- WHERE NOT EXISTS
27-- (SELECT house FROM house_points WHERE house='hufflepuff');]])
28-- local HPhouseAddRav = self:query([[ INSERT INTO house_points (house)
29-- SELECT 'ravenclaw' FROM DUAL
30-- WHERE NOT EXISTS
31-- (SELECT house FROM house_points WHERE house='ravenclaw');]])
32
33-- function HPtableQuery:onError( err , sql )
34-- print("HOUSE POINTS SYSTEM QUERY ERROR")
35-- print( "Query : ".. sql)
36-- print( "Error : " ..err)
37-- end
38-- function HPhouseAddGryff:onError( err , sql )
39-- print("HOUSE POINTS SYSTEM QUERY ERROR")
40-- print( "Query : ".. sql)
41-- print( "Error : " ..err)
42-- end
43
44-- HPtableQuery:start()
45-- HPhouseAddGryff:start()
46-- HPhouseAddSly:start()
47-- HPhouseAddHuf:start()
48-- HPhouseAddRav:start()
49
50-- timer.Create("Reset_data_sorting_houses", 10, 0, function()
51-- if not file.Exists( "reset_data.txt", "DATA" ) then
52-- file.Write("reset_data.txt", os.time())
53-- end
54
55-- if ( tonumber(os.time()) - tonumber(file.Read("reset_data.txt", "DATA" )) ) > 60*60*24*14 then
56-- (self:query("UPDATE `house_points` SET `points`=0 WHERE 1")):start()
57
58-- file.Write("reset_data.txt", os.time())
59-- local files, directories = file.Find( "sorting/*", "DATA" )
60-- for k,v in pairs(files) do
61-- file.Delete("sorting/" .. v)
62-- end
63-- for k,v in pairs(player.GetAll()) do
64-- local text = "You are not sorted. Head to the Great Hall and speak to the Sorting Hat."
65-- net.Start("send_reset_alert")
66-- net.WriteString(text)
67-- net.WriteString(os.date("!%c",file.Read("reset_data.txt", "DATA" )))
68-- net.Send(v)
69-- end
70
71-- end
72-- end)
73
74-- hook.Add("PlayerInitialSpawn", "tell_unsorted_people", function(ply)
75-- timer.Simple(5, function()
76-- if ply and not file.Exists("sorting/" .. ply:SteamID64() .. ".txt", "DATA") then
77-- local text = "You are not sorted. Head to the Great Hall and speak to the Sorting Hat."
78-- net.Start("send_reset_alert")
79-- net.WriteString(text)
80-- net.WriteString(os.date("!%c",file.Read("reset_data.txt", "DATA" )))
81-- net.Send(ply)
82-- end
83-- end)
84
85-- end)
86
87
88-- end
89
90-- function hpointsDB:onConnectionFailed( err )
91
92-- print( "[FVJohnny] Connection to database failed! Check your database parameters such as user,database name, password... " )
93-- print( "Error:", err )
94
95-- end
96
97-- hpointsDB:connect()
98
99HOUSE_POINTS = HOUSE_POINTS or {}
100HOUSE_POINTS.BaseData = {
101 ["gryffindor"] = 0,
102 ["slytherin"] = 0,
103 ["hufflepuff"] = 0,
104 ["ravenclaw"] = 0
105}
106
107util.AddNetworkString( "send_reset_alert" )
108
109
110function HOUSE_POINTS.SVInitialize()
111 local txtData = file.Read("housepoints.txt")
112 if not txtData then
113 HOUSE_POINTS.Points = HOUSE_POINTS.BaseData
114 file.Write("housepoints.txt", util.TableToJSON(HOUSE_POINTS.BaseData))
115 else
116 HOUSE_POINTS.Points = util.JSONToTable(txtData)
117 end
118
119
120 local ostime = os.time()
121 HOUSE_POINTS.ResetData = ostime
122
123 local reset_data_txt = file.Read("reset_data.txt", "DATA" )
124 if reset_data_txt == "" then reset_data_txt = nil end
125
126
127 if not reset_data_txt then
128 file.Write("reset_data.txt", ostime)
129 return
130 end
131
132 HOUSE_POINTS.ResetData = tonumber(reset_data_txt)
133
134 if ( tonumber(ostime) - tonumber(reset_data_txt) ) > 60*60*24*14 then
135 file.Write("housepoints.txt", util.TableToJSON(HOUSE_POINTS.BaseData))
136 HOUSE_POINTS.Points = HOUSE_POINTS.BaseData
137 file.Write("reset_data.txt", ostime)
138
139 end
140end
141HOUSE_POINTS.SVInitialize()
142
143function HOUSE_POINTS.SaveData()
144 file.Write("housepoints.txt", util.TableToJSON(HOUSE_POINTS.Points))
145end
146
147hook.Add("PlayerInitialSpawn", "Send_Sorted_Gui", function(ply)
148 if ply and not file.Exists("sorting/" .. ply:SteamID64() .. ".txt", "DATA") then
149 local text = "You are not sorted. Head to the Great Hall and speak to the Sorting Hat."
150 net.Start("send_reset_alert")
151 net.WriteString(text)
152 net.WriteString(os.date("!%c",HOUSE_POINTS.ResetData))
153 net.Send(ply)
154 end
155end)
156
157function HPchangeHousePoints(house,points)
158
159 HOUSE_POINTS.Points[house] = points
160 HOUSE_POINTS.SaveData()
161
162 timer.Simple(0.5,function()
163 HPsendBoard()
164 end)
165end
166
167function HPaddHousePoints(house,points)
168 HOUSE_POINTS.Points[house] = HOUSE_POINTS.Points[house] +points
169 HOUSE_POINTS.SaveData()
170
171 timer.Simple(0.5,function()
172 HPsendBoard()
173 end)
174end
175
176
177function HPsubHousePoints(house,points)
178 HOUSE_POINTS.Points[house] = HOUSE_POINTS.Points[house] - points
179 HOUSE_POINTS.SaveData()
180
181 timer.Simple(0.5,function()
182 HPsendBoard()
183 end)
184end
185
186
187
188function HPsendBoard()
189 net.Start("hp_board_send")
190 net.WriteTable(HOUSE_POINTS.Points)
191 net.Broadcast()
192
193 PrintTable(HOUSE_POINTS.Points)
194end
195
196
197function HPsendBoardTo(ply)
198 net.Start("hp_board_send")
199 net.WriteTable(HOUSE_POINTS.Points)
200 net.Send(ply)
201end