· 7 years ago · Dec 14, 2018, 02:36 AM
1Server Side:
2
3mysql = exports.mysql
4
5local machinePos = {
6 [1] = "2card",
7 [2] = "69",
8 [3] = "1card",
9 [4] = "piramid",
10 [5] = "cherry",
11 [6] = "strawberry",
12 [7] = "cherry",
13 [8] = "strawberry",
14 [9] = "piramid",
15 [10] = "69",
16 [11] = "piramid",
17 [12] = "1card",
18 [13] = "cherry",
19 [14] = "strawberry",
20 [15] = "69",
21 [16] = "strawberry",
22 [17] = "piramid",
23 [18] = "cherry",
24}
25
26local posCoords = {
27 [1] = 0,
28 [2] = 20,
29 [3] = 40,
30 [4] = 60,
31 [5] = 80,
32 [6] = 100,
33 [7] = 120,
34 [8] = 140,
35 [9] = 160,
36 [10] = 180,
37 [11] = 200,
38 [12] = 220,
39 [13] = 240,
40 [14] = 260,
41 [15] = 280,
42 [16] = 300,
43 [17] = 320,
44 [18] = 340
45}
46
47local wheelPos = {
48 [1] = -0.1,
49 [2] = 0,
50 [3] = 0.1
51}
52
53function randomMovement ( nr, object, wheel )
54 local rand = math.random(1,18)
55 attachElements ( wheel, object, wheelPos[nr], 0, 0, posCoords[rand] )
56 return rand
57end
58
59function randomMovementAll ( object, wheel1, wheel2, wheel3 )
60 local rand1 = randomMovement ( 1, object, wheel1 )
61 local rand2 = randomMovement ( 2, object, wheel2 )
62 local rand3 = randomMovement ( 3, object, wheel3 )
63 local msg1 = machinePos[rand1]
64 local msg2 = machinePos[rand2]
65 local msg3 = machinePos[rand3]
66 if msg1 == msg2 and msg3 ~= msg1 then
67 return math.random(50,90)
68 elseif msg1 == msg3 and msg2 ~= msg1 then
69 return math.random(50,90)
70 elseif msg2 == msg3 and msg2 ~= msg1 then
71 return math.random(50,90)
72 elseif msg1 == msg2 and msg2 == msg3 then
73 return math.random(100,250)
74 else
75 return 0
76 end
77end
78
79function createSlothMachine ( id, x, y, z, int, dim )
80 local sloth = createElement("slotmachine", "slot"..tonumber(id))
81 local object = createObject ( 2325, x, y, z-0.2, 0, 0, 0 )
82 local wheel1 = createObject ( 2347, x, y, z, 0, 0, 0 )
83 local wheel2 = createObject ( 2347, x, y, z, 0, 0, 0 )
84 local wheel3 = createObject ( 2347, x, y, z, 0, 0, 0 )
85 attachElements ( wheel1, object, -0.1, 0, 0 )
86 attachElements ( wheel2, object, 0, 0, 0 )
87 attachElements ( wheel3, object, 0.1, 0, 0 )
88 setElementParent(object, sloth)
89 setElementParent(wheel1, sloth)
90 setElementParent(wheel2, sloth)
91 setElementParent(wheel3, sloth)
92 setElementID(object,tonumber(id))
93 setElementInterior(object, int)
94 setElementInterior(wheel1, int)
95 setElementInterior(wheel2, int)
96 setElementInterior(wheel3, int)
97 setElementDimension(object, dim)
98 setElementDimension(wheel1, dim)
99 setElementDimension(wheel2, dim)
100 setElementDimension(wheel3, dim)
101 randomMovementAll ( object, wheel1, wheel2, wheel3 )
102end
103
104function doRandomMachine ( id, thePlayer )
105 local sloth = getElementByID("slot"..tonumber(id))
106 if not sloth then return end
107 local money = exports.global:getMoney(thePlayer)
108 if money < 50 then
109 outputChatBox("You dont have enough money (50)!", thePlayer, 255, 0, 0)
110 return
111 end
112 local object = nil
113 local wheel = { }
114 local nr = 1
115 for i, v in pairs (getElementChildren(sloth)) do
116 if getElementModel(v) == 2325 then
117 object = v
118 else
119 wheel[nr] = v
120 nr = nr +1
121 end
122 end
123 exports.global:takeMoney(thePlayer, 40)
124 local money = randomMovementAll ( object, wheel[1], wheel[2], wheel[3] )
125 if money == 0 then
126 outputChatBox("You paid 40 and you won nothing!", thePlayer, 255, 0, 0)
127 else
128 outputChatBox("You paid 40 and you won:"..exports.global:formatMoney(money).." from the slotmachine!", thePlayer, 0, 255, 0)
129 exports.global:giveMoney(thePlayer, money)
130 end
131end
132
133function doRand(thePlayer)
134 doRandomMachine ( 1, thePlayer )
135end
136addCommandHandler("random", doRand)
137
138function elementClicked( theButton, theState, thePlayer )
139 if theButton == "left" and theState == "down" then
140 local model = getElementModel(source)
141 if model == 2325 then
142 doRandomMachine ( getElementID(source), thePlayer )
143 end
144 end
145end
146addEventHandler( "onElementClicked", getRootElement(), elementClicked )
147
148function loadAllPacanele()
149 local ticks = getTickCount( )
150 local result = mysql:query("SELECT id, x, y, z, interior, dimension FROM slotmachines")
151 local count = 0
152 if (result) then
153 local continue = true
154 while continue do
155 local row = mysql:fetch_assoc(result)
156 if not row then break end
157 local id = tonumber(row["id"])
158 local x = tonumber(row["x"])
159 local y = tonumber(row["y"])
160 local z = tonumber(row["z"])
161 local interior = tonumber(row["interior"])
162 local dimension = tonumber(row["dimension"])
163 createSlothMachine ( id, x, y, z, interior, dimension )
164 count = count + 1
165 end
166 mysql:free_result(result)
167 end
168 outputDebugString("" .. count .. " SlothMachines loaded in " .. ( getTickCount( ) - ticks ) .. "ms")
169end
170addEventHandler("onResourceStart", getResourceRootElement(), loadAllPacanele)
171
172function createSlothMachine2(thePlayer, commandName)
173 if (exports.integration:isPlayerAdmin(thePlayer)) then
174 local x, y, z = getElementPosition(thePlayer)
175 local interior = getElementInterior(thePlayer)
176 local dimension = getElementDimension(thePlayer)
177 local id = mysql:query_insert_free("INSERT INTO slotmachines SET x='" .. mysql:escape_string(x) .. "', y='" .. mysql:escape_string(y) .. "', z='" .. mysql:escape_string(z) .. "', interior='" .. mysql:escape_string(interior) .. "', dimension='" .. mysql:escape_string(dimension) .. "'")
178 if (id) then
179 if createSlothMachine ( id, x, y, z, interior, dimension ) then
180 outputChatBox("SlothMachine created with ID #" .. id .. ".", thePlayer, 0, 255, 0)
181 end
182 else
183 outputChatBox("Error 200000 - Report on mantis.", thePlayer, 255, 0, 0)
184 end
185 end
186end
187addCommandHandler("createslotmachine", createSlothMachine2, false, false)
188
189function getNearbySlothMachine(thePlayer, commandName)
190 if (exports.integration:isPlayerAdmin(thePlayer)) then
191 local x, y, z = getElementPosition(thePlayer)
192 outputChatBox("Nearby slothmachines:", thePlayer, 255, 126, 0)
193 for key, element in pairs (getElementsByType("object")) do
194 if getElementModel(element) == 2325 then
195 local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( element ) )
196 if distance < 20 then
197 outputChatBox( " (( Slothmachine with ID: " .. tostring( getElementID(element) ).." ))", thePlayer, 255, 255, 0 )
198 end
199 end
200 end
201 end
202end
203addCommandHandler("slotmachines", getNearbySlothMachine, false, false)
204
205function delSlothMachine(thePlayer, commandName, id)
206 if not exports.integration:isPlayerAdmin(thePlayer) then return end
207 local id = tonumber(id)
208 if not id then return outputChatBox("You have not written the id of the slotmachine.", thePlayer, 255, 0, 0) end
209 local sloth = getElementByID("slot"..tonumber(id))
210 if not sloth then return end
211 mysql:query_free("DELETE FROM slotmachines WHERE id='" .. mysql:escape_string(id) .. "'")
212 outputChatBox("Slothmachine with id #" .. id .. " deleted.", thePlayer)
213 destroyElement(sloth)
214end
215addCommandHandler("delslotmachine", delSlothMachine, false, false)
216
217DB:
218CREATE TABLE IF NOT EXISTS `slotmachines` (
219`id` int(11) NOT NULL,
220 `x` float NOT NULL,
221 `y` float NOT NULL,
222 `z` float NOT NULL,
223 `interior` int(11) NOT NULL,
224 `dimension` int(11) NOT NULL
225) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;