· 8 years ago · May 03, 2018, 06:24 PM
1local ranks = {
2 ["superadmin"] = true,
3 ["senioradmin"] = true,
4 ["admin"] = true,
5 ["mod"] = true
6}
7
8util.AddNetworkString("pointshop")
9util.AddNetworkString("getRewards")
10hook.Add("PlayerSay", "openshop", function(ply, text, sender)
11 if text != "/shop" then return end
12 if !ranks[ply:GetUserGroup()] then
13 PrintMessage(HUD_PRINTTALK, "Access Denied.")
14 return ""
15 end
16 // if text == "/shop" && ply:IsValid() && ranks[ply:GetUserGroup()] then
17 if text == "/shop" then
18 net.Start("pointshop")
19 net.WriteTable(staffPoints.CRewards)
20 net.Send(ply)
21 return ""
22 else
23 util.AddNetworkString("dnd")
24 net.Start("dnd")
25 net.Send(ply)
26 return ""
27 end
28end)
29
30util.AddNetworkString("staffBuy")
31net.Receive("staffBuy", function(len, ply)
32 print("ORANGES") // Debug
33 local reward = staffPoints.Rewards[net.ReadInt(9)]
34 if ply:GetNWInt("staffPoints") >= reward.price then
35 ply:PrintMessage( HUD_PRINTTALK, "Bought ".. reward.id .." for ".. reward.price .." pts")
36 staffPoints.addPoints(ply, -reward.price)
37 reward.func(ply)
38 return
39 end
40 ply:PrintMessage( HUD_PRINTTALK, "Sorry, you can't afford the "..reward.id.."!")
41end)
42
43local hranks = {
44 ["superadmin"] = true,
45 ["senioradmin"] = true
46}
47
48hook.Add("PlayerSay", "gpoints", function(ply, text, sender)
49 if text == "/gpoints" && ply:IsValid() && hranks[ply:GetUserGroup()] then
50 startingpoints = startingpoints + 100
51 return ""
52 end
53end)
54
55startingpoints = 50
56
57/// Colours's Stuffs ///
58
59staffPoints = {}
60
61
62staffPoints.Rewards = {}
63staffPoints.CRewards = {}
64
65function staffPoints.addReward(idx, pricex, funcx)
66 table.insert(staffPoints.Rewards, {id = idx, price = pricex, func = funcx})
67 table.insert(staffPoints.CRewards, idx)
68end
69
70--[[
71util.AddNetworkString("point")
72function staffPoints.updateClient(ply)
73
74 net.Start("point")
75 net.WriteInt( ply:GetNWInt("staffPoints") , 9 )
76 net.Send(ply)
77
78end ]]--
79
80function staffPoints.addPoints(ply, amt) // Add (or remove) points to a staff member
81
82 points = ply:GetNWInt("staffPoints")+amt
83 ply:SetNWInt("staffPoints", points)
84 staffPoints.Save(ply)
85
86end
87
88
89function staffPoints.resetPoints(ply) // Reset a staff member's points to 0
90
91 ply:SetNWInt("staffPoints", 0)
92 staffPoints.Save(ply)
93
94end
95
96function staffPoints.tableExists() // Check that our database exists, and create it if not
97
98 if (sql.TableExists("staff_points")) then
99 Msg("This table already exists!")
100 else
101 if (!sql.TableExists("staff_points")) then
102 query = "CREATE TABLE staff_points ( unique_id varchar(255), points int )"
103 result = sql.Query(query)
104 if (sql.TableExists("staff_points")) then
105 Msg("staff_points table created \n")
106 else
107 Msg("staff_points table failed to create! \n")
108 Msg( sql.LastError( result ) .. "\n" )
109 end
110 end
111 end
112
113end
114
115
116function staffPoints.staffExists( ply ) // Check that the player exists, add them to the table if not
117
118 steamID = ply:SteamID()
119
120 result = sql.Query("SELECT unique_id, points FROM staff_points WHERE unique_id = '"..steamID.."'")
121 if (result) then
122 staffPoints.Retrieve(ply)
123 else
124 staffPoints.newStaff( steamID, ply )
125 end
126
127end
128
129
130function staffPoints.newStaff( SteamID, ply ) // Add a new staff member to our table
131
132 steamID = SteamID
133 sql.Query( "INSERT INTO staff_points (`unique_id`, `points`)VALUES ('"..steamID.."', '100')" )
134 result = sql.Query( "SELECT unique_id, points FROM staff_points WHERE unique_id = '"..steamID.."'" )
135 if !(result) then
136 Msg("Failed to create staff data! :( )\n")
137 end
138
139end
140
141
142function staffPoints.Retrieve( ply ) // Retrieve a staff member's pointcount from database
143
144 id = sql.QueryValue("SELECT unique_id FROM staff_points WHERE unique_id = '"..ply:SteamID().."'")
145 points = tonumber(sql.QueryValue("SELECT points FROM staff_points WHERE unique_id = '"..ply:SteamID().."'"))
146
147 ply:SetNWInt("staffPoints", points)
148 //staffPoints.updateClient(ply)
149 print("Yo' Data: "..id.." "..points) // For debug
150 ply:PrintMessage( HUD_PRINTTALK, "Yo' Data: "..id.." "..points)
151
152end
153
154
155function staffPoints.Save ( ply ) // Save a staff member's pointcount to database
156
157 points = ply:GetNWInt("staffPoints")
158 unique_id = ply:SteamID()
159 sql.Query("UPDATE staff_points SET points = "..points.." WHERE unique_id = '"..unique_id.."'")
160 //staffPoints.updateClient(ply)
161 ply:ChatPrint("Stats updated !") // For debug
162
163end
164
165
166function PlayerInitialSpawn( ply )
167
168 timer.Create("id_delay", 1, 1, function()
169 ID = ply:SteamID()
170 staffPoints.staffExists( ply )
171 end)
172
173end
174
175
176function Initialize()
177
178 staffPoints.tableExists()
179
180end
181
182
183
184concommand.Add( "getMyData", staffPoints.Retrieve) // For debug
185concommand.Add( "createMyData", staffPoints.staffExists) // For debug
186concommand.Add( "giveMePoints", function(ply, cmd, args)
187 if args[1] == nil then return end
188 staffPoints.addPoints(ply, args[1])
189 end) // For debug
190concommand.Add( "clearMyPoints", staffPoints.resetPoints) // For debug
191
192hook.Add( "Initialize", "Initialize", Initialize )
193hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
194
195
196
197
198
199/// Add/Edit Rewards Below Here ///
200// I forgot which SWEP you actually had to begin with, so I just used two example placeholders
201// To add new things, just copy and paste one of the functions below. The first argument is the name
202// of the reward (appears in the shop), the second argument is the price, and the third argument is the
203// function that wil run when the player buys it. Currently it only supports a player argument, but that
204// can be adjusted in future to suit your needs. Each reward you add will automatically have a button
205// created for it on the shop menu (These things are a bit messy right now and far from being done, but they sorta work)
206// I can explain a bit more tomorrow.
207
208staffPoints.addReward("Crowbar", 50, function(args)
209 ply = args
210 ply:Give("weapon_crowbar")
211end)
212
213staffPoints.addReward("Crossbow", 75, function(args)
214 ply = args
215 ply:Give("weapon_crossbow")
216end)
217
218staffPoints.addReward("Popcorn", 50, function(args)
219 ply = args
220 ply:Give("weapon_popcorn")
221end)
222
223staffPoints.addReward("SpamPopcorn", 50, function(args)
224 ply = args
225 ply:Give("weapon_popcorn_spam")
226end)