This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsPlayer.lua
794 lines (690 loc) · 20.9 KB
/
sPlayer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
ClassStash.save("Player")
local MtaPlayer = ClassStash.get("Player")
Player = {}
setmetatable(Player, {__index = Element}) --Player inherits from Element
local super = Element
local player_mt = {__index = Player}
local instances = {}
local count = 0
Player.MAX_HEALTH = 100
Player.MAX_ARMOR = 100
Player.DEFAULT_SKIN = 0
Player.DEFAULT_INVENTORY_SIZE = 70*100
function Player.new()
local remote = Remote.new(client)
local newInst =
{
type = "player",
name = client.name,
remote = remote,
account = Account.new(remote),
inventory = Inventory.new(remote),
backpack = false,
match = false,
lobby = false,
health = Player.MAX_HEALTH,
skin = Player.DEFAULT_SKIN,
currentWeapon,
weapon = {},
ammoSlot = {},
armor = 0,
kills = 0,
headshots = 0,
}
setmetatable(newInst, player_mt)
instances[newInst] = true
count = count + 1
newInst.remote:setSuper(newInst)
newInst:sendLanguage()
newInst:askCredentials()
newInst.inventory:setCapacity(Player.DEFAULT_INVENTORY_SIZE)
newInst:giveInitialItems()
return newInst
end
addEvent("onJoin", true)
addEventHandler("onJoin", resourceRoot, Player.new)
function Player:destroy(isResourceStopping)
if self.account:getInHashQueue() then
Hash.removePlayerFromQueue(self)
end
if not isResourceStopping then
iprint(self.name.. " left the game")
if self:getInLobby() then
self.lobby:removePlayer(self)
end
if self:getInMatch() then
local match = self:getMatch()
match:removePlayer(self)
match:endIfWinner()
match:sendAliveCount()
end
end
self.account:save()
self.account:destroy()
self.inventory:destroy()
self.remote:destroy()
instances[self] = nil
count = count - 1
end
function Player.getAll()
return instances
end
function Player.isPlayer(element)
return element and element.type == "player"
end
function Player.getNameOrNil(player)
return (player and player.name) and player.name or false
end
function Player:getInLobby()
return self:getLobby() and true or false
end
function Player:getLobby()
return self.lobby
end
function Player:setLobby(_lobby)
self.lobby = _lobby
self:showChat(self:getInLobby())
self.remote:send("onSetInLobby", self:getInLobby())
end
local function handleSendPlayerToLobby(selectedSkin)
local player = Remote.getSuperFromRemote(client)
lobby:addPlayer(player, selectedSkin)
end
addEvent("onSendPlayerToLobby", true)
addEventHandler("onSendPlayerToLobby", root, handleSendPlayerToLobby)
function Player:getInMatch()
return self:getMatch() and true or false
end
function Player:getMatch()
return self.match
end
function Player:setMatch(_match)
self.match = _match
self.remote:send("onSetInMatch", self:getInMatch())
end
function Player:clearMatchData()
self:removeBackpack()
self.inventory:setEmpty()
self:giveInitialItems()
self:getMatch():getZone():sendNoZoneToPlayer(self)
self:setMatch(false)
self:takeAllWeapons()
self.health = Player.MAX_HEALTH
self.skin = Player.DEFAULT_SKIN
self.currentWeapon = nil
self.weapon = {}
self.ammoSlot = {}
self.armor = 0
self.kills = 0
self.headshots = 0
end
function Player:giveInitialItems()
self.inventory:giveItem("Bandage")
end
function Player:askCredentials()
self:showChat(false)
if self.account:getRegistered() then
self:openLoginPanel(true)
else
self:openLoginPanel(false)
end
end
function Player:spawn(x, y, z)
self.remote:getRemote():spawn(x, y, z)
end
function Player:showChat(_showChat)
showChat(self.remote:getRemote(), _showChat)
end
function Player:openLoginPanel(_isRegistered)
self.remote:send("onOpenLoginPanel", self.account:getSerial(), _isRegistered)
end
function Player:authenticate(password)
if password == self.account:getPassword() then
iprint("Player " ..self.name.. " entered the right password")
self.remote:send("onSendLoginStatus", "success")
else
iprint("Player " ..self.name.. " entered the wrong password")
self.remote:send("onSendLoginStatus", "wrongPass")
end
end
function Player:login(plainPassword)
if self.account:getRegistered() then
local salt = Hash.getSalt(self.account:getPassword())
Hash.getHash(plainPassword, salt, Player.authenticate, self)
else
iprint("Warning: Player " ..self.name.. " tried to login without an account!")
end
end
local function handlePlayerLogin(plainPassword)
if Remote.isLegitPlayer(client, source) then
local player = Remote.getSuperFromRemote(client)
player:login(plainPassword)
end
end
addEvent("onLogin", true)
addEventHandler("onLogin", root, handlePlayerLogin)
function Player:createAccount(password)
self.account:register(password)
self.remote:send("onSendLoginStatus", "success")
end
function Player:register(plainPassword)
if not self.account:getRegistered() then
if string.len(plainPassword) > 0 then
local salt = Hash.generateSalt()
Hash.getHash(plainPassword, salt, Player.createAccount, self)
else
self.remote:send("onSendLoginStatus", "noPassword")
end
else
iprint("Warning: Player " ..self.name.. " tried to register twice!")
end
end
local function handlePlayerRegister(plainPassword)
if Remote.isLegitPlayer(client, source) then
local player = Remote.getSuperFromRemote(client)
player:register(plainPassword)
end
end
addEvent("onRegister", true)
addEventHandler("onRegister", root, handlePlayerRegister)
function Player:getModel()
return self.skin
end
function Player:setModel(skin)
self.skin = skin
--FIXME: Element is manipulating an attribute that it does not
--have: remote. Maybe move it to the parent class
super.setModel(self, skin)
end
function Player:getWearingArmor()
return self.armor > 0
end
function Player:getArmor()
return self.armor
end
function Player:setArmor(_armor)
if self.armor ~= _armor then
self.armor = math.constrain(_armor, 0, Player.MAX_ARMOR)
--TODO: sync armor
end
end
function Player:giveArmor(_armor)
self:setArmor(self:getArmor() + _armor)
end
function Player:takeArmor(_armor)
self:setArmor(self:getArmor() - _armor)
end
function Player:getAlive() --NOTE: unused
return self:getHealth() > 0 and self:getInMatch()
end
function Player:getHealth()
return self.health
end
function Player:setHealth(_health, responsible, isHeashot)
_health = math.constrain(_health, 0, Player.MAX_HEALTH)
self.health = _health
self.remote:send("onSendPlayerHealth", _health)
if _health == 0 then
self:kill(responsible, isHeashot)
end
end
function Player:heal(_heal, responsible)
if _heal > 0 then
iprint("Heal " ..self.name.. " for " .._heal)
self:setHealth(self:getHealth() + _heal, responsible)
end
end
function Player:harm(_harm, responsible, isHeashot)
if _harm > 0 then
iprint("Harm " ..self.name.. " for " .._harm)
self:setHealth(self:getHealth() - _harm, responsible, isHeashot)
end
end
function Player:isFullHealth()
return self:getHealth() == Player.MAX_HEALTH
end
function Player:showFullHealthMessage()
self.remote:send("onShowInventoryMessage", "fullHealth")
end
function Player:showUseMessage(itemName)
self.remote:send("onShowInventoryMessage", "use", itemName)
end
function Player:giveParachute()
giveWeapon(self.remote:getRemote(), 46, 1, true)
end
function Player:takeParachute()
takeWeapon(self.remote:getRemote(), 46)
end
function Player:getBackpack()
return self.backpack
end
function Player:setBackpack(itemName)
self:removeBackpack()
if isItemBackpack(itemName) then
self.backpack = Backpack.new(itemName, self)
self.inventory:setCapacity(getBackpackSlotCount(itemName))
end
end
function Player:removeBackpack()
local backpack = self:getBackpack()
if backpack then
backpack:destroy()
self.inventory:setCapacity(Player.DEFAULT_INVENTORY_SIZE)
self.backpack = false
end
end
function Player:transferBackpackTo(ped)
if self:getBackpack() then
self:getBackpack():transferTo(ped)
end
end
function Player:setWeaponAmmo(itemName, ammoCount, ammoInClip)
iprint("Setting ammo for weapon " ..itemName.. " as " ..ammoCount)
setWeaponAmmo(self.remote:getRemote(), getWeaponID(itemName), ammoCount, ammoInClip)
end
function Player:updateAmmo(itemName)
iprint("Ammo " ..itemName.. " in use: " ..tostring(self:isAmmoInUse(itemName)))
if self:isAmmoInUse(itemName) then
local ammoSlot = self:getAmmoSlot(itemName)
local ammoCount = self.inventory:getItemCount(itemName)
self:setWeaponAmmo(self:getWeapon(ammoSlot), ammoCount)
if not self.inventory:doesHaveItem(itemName) then
self:takeWeapon(self:getWeapon(ammoSlot))
end
end
end
function Player:isAmmoInUse(itemName)
if self:getAmmoSlot(itemName) then
return true
else
return false
end
end
function Player:getAmmoSlot(itemName)
return self.ammoSlot[itemName]
end
function Player:setAmmoSlot(itemName, slot)
iprint(itemName .." is now on slot " ..tostring(slot))
self.ammoSlot[itemName] = slot
end
function Player:getWeapons()
return self.weapon
end
function Player:getWeapon(slot)
return self.weapon[slot]
end
function Player:setWeapon(itemName, slot)
self.weapon[slot] = itemName
end
function Player:isWeaponEquipped(itemName)
local weaponSlot = getWeaponSlot(itemName)
if self:getWeapon(weaponSlot) == itemName then
return true
else
return false
end
end
function Player:giveWeapon(itemName)
iprint("Equipping " ..itemName.. " weapon")
local weaponSlot = getWeaponSlot(itemName)
local ammoType = getWeaponAmmoType(itemName)
local ammoCount = self.inventory:getItemCount(ammoType)
self:setWeapon(itemName, weaponSlot)
self:setAmmoSlot(ammoType, weaponSlot)
self.remote:send("onEquipWeapon", itemName)
self.remote:getRemote():giveWeapon(getWeaponID(itemName), ammoCount, true)
end
function Player:takeWeapon(itemName)
iprint("Unequipping " ..itemName.. " weapon")
local weaponSlot = getWeaponSlot(itemName)
local ammoType = getWeaponAmmoType(itemName)
self:setAmmoSlot(ammoType, nil)
self:setWeapon(nil, weaponSlot)
self.remote:send("onUnequipWeapon", itemName)
self:setWeaponAmmo(itemName, 0)
end
function Player:takeAllWeapons()
for _, itemName in pairs(self:getWeapons()) do
self:takeWeapon(itemName)
end
self:takeParachute()
end
function Player:equipWeapon(itemName)
if self.inventory:doesHaveItem(getWeaponAmmoType(itemName)) then
if not self:isWeaponEquipped(itemName) then
local weaponSlot = getWeaponSlot(itemName)
if self:getWeapon(weaponSlot) then
self:takeWeapon(self:getWeapon(weaponSlot))
end
self:giveWeapon(itemName)
end
else
self.remote:send("onShowInventoryMessage", "noAmmo")
end
end
function Player:getWeaponDamage(slot)
local weapon = self:getWeapon(slot)
if weapon then
return getWeaponDamage(weapon)
end
end
function Player:getHeadshots()
return self.headshots
end
function Player:getCurrentWeapon()
return self.currentWeapon
end
function Player:setCurrentWeapon(itemName)
iprint(self.name.. "'s current weapon is " ..tostring(itemName))
self.currentWeapon = itemName
end
function Player:handleWeaponFire()
local currentWeapon = self:getCurrentWeapon()
local ammoType = getWeaponAmmoType(currentWeapon)
if currentWeapon then
self.inventory:takeItem(ammoType)
if not self.inventory:doesHaveItem(ammoType) then
self:takeWeapon(currentWeapon)
end
end
end
local function handlePlayerWeaponFire()
local player = Remote.getSuperFromRemote(source)
player:handleWeaponFire()
end
addEventHandler("onPlayerWeaponFire", root, handlePlayerWeaponFire)
function Player:sendLanguage()
if self.account:getRegistered() then
self.remote:send("onSetUserLanguage", self.account:getLanguage())
end
end
local function handleLanguageChange(language)
local player = Remote.getSuperFromRemote(client)
iprint(player.name.. " changed language from "
..player.account:getLanguage().. " to " ..tostring(language))
player.account:setLanguage(language)
end
addEvent("onPlayerLanguageChange", true)
addEventHandler("onPlayerLanguageChange", resourceRoot, handleLanguageChange)
function Player:handleWeaponThrow()
local currentWeapon = self:getCurrentWeapon()
if currentWeapon then
self.inventory:takeItem(currentWeapon)
if not self.inventory:doesHaveItem(currentWeapon) then
self:takeWeapon(currentWeapon)
end
end
end
local function handlePlayerWeaponThrow()
local player = Remote.getSuperFromRemote(client)
player:handleWeaponThrow()
end
addEvent("onPlayerWeaponThrow", true)
addEventHandler("onPlayerWeaponThrow", resourceRoot, handlePlayerWeaponThrow)
function Player:setHeadshots(_headshots)
self.headshots = _headshots
end
function Player:getKills()
return self.kills
end
function Player:setKills(_kills)
self.kills = _kills
end
function Player:increaseKills()
self:setKills(self:getKills() + 1)
end
function Player:setWeather(weather)
self.remote:send("onSetClientWeather", weather)
end
function Player:getFinalDamageAndDeduceArmor(damage)
if self:getWearingArmor() then
local armor = self:getArmor()
if damage >= armor then
self:setArmor(0)
damage = damage - armor
else
self:takeArmor(damage)
damage = 0
end
end
return damage
end
local GRENADE_MAX_DAMAGE = 82.5 --or so it seems
function Player:handleDamage(attacker, weapon, bodypart, loss)
iprint(tostring(Player.getNameOrNil(attacker)).. " attacked " ..self.name)
if self:getInMatch() then
local damage = 0
local headshot = false
if weapon == Damage.FALL then
damage = loss
iprint(self.name.. " fell. Damage: " ..damage)
elseif weapon == 16 then --grenade
damage = getWeaponDamage("Grenade")*(loss/GRENADE_MAX_DAMAGE)
iprint(tostring(attacker.name).. " blew " ..tostring(self.name)
.. " up using grenade. Damage: " ..tostring(damage))
elseif Player.isPlayer(attacker) and attacker:getCurrentWeapon()
and getWeaponID(attacker:getCurrentWeapon()) == weapon then
damage = getWeaponDamage(attacker:getCurrentWeapon())
if bodypart == Body.HEAD then
headshot = true
damage = damage*2
end
damage = self:getFinalDamageAndDeduceArmor(damage)
iprint(tostring(attacker.name).. " shot " ..tostring(self.name)
.. " using " ..tostring(attacker:getCurrentWeapon())
.. ". Damage: " ..tostring(damage)
.. ". Headshot: " ..tostring(headshot))
end
self:harm(damage, attacker, headshot)
end
end
local function handlePlayerDamage(attacker, weapon, bodypart, loss)
local player = Remote.getSuperFromRemote(client)
local attacker = Remote.getSuperFromRemote(attacker)
Player.handleDamage(player, attacker, weapon, bodypart, loss)
end
addEvent("onDamagePlayer", true) --onPlayerDamage is taken by MTA
addEventHandler("onDamagePlayer", resourceRoot, handlePlayerDamage)
function Player:getMatchReward()
return self:getKills()*10 + self:getHeadshots()*5 + 10
end
function Player:kill(killer, headshot)
iprint(self.name.. " died. Killed by: " ..tostring(killer and killer.name or killer).. ". Headshot: " ..tostring(headshot))
if Player.isPlayer(killer) and self ~= killer then
killer:increaseKills()
end
self:accountLoss()
self:showEndScreen()
self:createDeadBody()
self:moveToNowhere()
local match = self.match
match:removePlayer(self)
match:sendAliveCount()
match:endIfWinner()
match:pushDeathNotification(self.name, Player.getNameOrNil(killer))
end
function Player:moveToNowhere()
self:setPosition(6000, 6000, 0) --inheritance works! :D
end
function Player:moveCameraAround() --NOTE: unused --FIXME: camera moves should be done clientside
local x, y, z = getElementPosition(self)
setCameraMatrix(self, x, y, z + 10, x, y, z)
fadeCamera(self, false, 5)
Timer(
function(self)
if isElement(self) then
fadeCamera(self, true)
end
end, 6000, 1, self)
end
function Player:accountLoss()
self.account:increaseLosses()
self.account:increaseDeaths()
self:accountMatch()
end
function Player:accountWin()
self.account:increaseWins()
self:accountMatch()
end
function Player:accountMatch()
self.account:increaseGamesplayed()
self.account:addHeadshots(self:getHeadshots())
self.account:addKills(self:getKills())
self.account:addCoins(self:getMatchReward())
end
function Player:showEndScreen()
iprint("Showing endscreen for " ..self.name.. ". Rank: " ..self.match:getPlayerCount())
self.remote:send("onShowEndScreen", self.match:getPlayerCount())
end
local function handlerWeaponChange(_, currentWeaponID)
local player = Remote.getSuperFromRemote(source)
if player then --player happens to be nil when resource stops
for _, itemName in pairs(player:getWeapons()) do
if getWeaponID(itemName) == currentWeaponID then
return player:setCurrentWeapon(itemName)
end
end
player:setCurrentWeapon(nil)
end
end
addEventHandler("onPlayerWeaponSwitch", root, handlerWeaponChange)
function Player:useItem(itemName)
iprint(self.name.." used " ..tostring(itemName))
if self.inventory:doesHaveItem(itemName) and canItemBeUsed(itemName) then
if isItemWeapon(itemName) then
self:equipWeapon(itemName)
elseif isItemMedicine(itemName) then
if self:isFullHealth() then
self:showFullHealthMessage()
else
self:heal(getMedicineHealValue(itemName))
self:showUseMessage(itemName)
self.inventory:takeItem(itemName)
self:sendInventoryItems()
end
elseif isItemBackpack(itemName) then
self:setBackpack(itemName)
self:showUseMessage(itemName)
self.inventory:takeItem(itemName)
self:sendInventoryItems()
elseif isItemArmor(itemName) then
self:giveArmor(getArmorAbsorption(itemName))
self:showUseMessage(itemName)
self.inventory:takeItem(itemName)
self:sendInventoryItems()
--TODO: send armor picture to client
end
else
iprint("Warning: " ..self.name.. " failed to use" ..tostring(itemName)
..": HasItem: " ..tostring(self.inventory:doesHaveItem(itemName)
.." CanBeUsed: " ..tostring(canItemBeUsed(itemName))))
end
end
local function handleItemUse(itemName)
local player = Remote.getSuperFromRemote(client)
player:useItem(itemName)
end
addEvent("onPlayerUseItem", true)
addEventHandler("onPlayerUseItem", root, handleItemUse)
function Player:sendInventoryItems()
local self = Remote.getSuperFromRemote(client)
self.remote:send("onClearInventory")
for itemName in pairs(self.inventory:getItemList()) do
self.remote:send("onPopulateInventory", itemName, self.inventory:getItemCount(itemName))
if self.inventory:getItemCount(itemName) < 0 then
outputDebugString("Item with negative amount in " ..tostring(self.remote:getRemote()).. "'s inventory: " ..itemName, 2)
end
end
end
addEvent("onRefreshInventory", true)
addEventHandler("onRefreshInventory", root, Player.sendInventoryItems)
local function sendLootItems(loot)
local player = Remote.getSuperFromRemote(client)
local loot = Remote.getSuperFromRemote(loot)
if loot then
player.remote:send("onClearInventoryLoot")
for itemName in pairs(loot.inventory:getItemList()) do
player.remote:send("onPopulateLoot", itemName, loot.inventory:getItemCount(itemName))
--FIXME: destroy worldModels for taken items
end
end
end
addEvent("onRefreshLoot", true)
addEventHandler("onRefreshLoot",root,sendLootItems)
local function giveItemToLoot(itemName, loot)
local self = Remote.getSuperFromRemote(client)
local lootSpot = Remote.getSuperFromRemote(loot)
local amountToTransfer = 1
if isItemAmmo(itemName) then
local itemCount = self.inventory:getItemCount(itemName)
amountToTransfer = itemCount <= 10 and itemCount or 10
end
if lootSpot then
self.inventory:transferItemTo(lootSpot.inventory, itemName, amountToTransfer)
self:sendInventoryItems()
sendLootItems(loot)
end
--FIXME: implement dropping items to the ground
if isItemAmmo(itemName) then
self:updateAmmo(itemName)
elseif isItemWeapon(itemName)
and self:isWeaponEquipped(itemName)
and not self.inventory:doesHaveItem(itemName) then
self:takeWeapon(itemName)
end
end
addEvent("onItemFromInventoryToLoot", true)
addEventHandler("onItemFromInventoryToLoot", root, giveItemToLoot)
local function takeItemFromLoot(itemName, loot)
local self = Remote.getSuperFromRemote(client)
local lootSpot = Remote.getSuperFromRemote(loot)
if isItemAmmo(itemName) then
local itemCount = lootSpot.inventory:getItemCount(itemName)
lootSpot.inventory:transferItemTo(self.inventory, itemName, itemCount)
self:updateAmmo(itemName)
else
lootSpot.inventory:transferItemTo(self.inventory, itemName, 1)
end
self:sendInventoryItems()
sendLootItems(loot)
end
addEvent("onItemFromLootToInventory", true)
addEventHandler("onItemFromLootToInventory", root, takeItemFromLoot)
function Player:createDeadBody()
if not self:isInWater() then
local deadPlayer = DeadPlayer.new(self)
iprint("Created body for " ..self.name)
end
end
function handleResourceStop()
for player in pairs(Player.getAll()) do
player:destroy(true)
end
end
addEventHandler("onResourceStop", resourceRoot, handleResourceStop)
local function handlePlayerLeave()
local player = Remote.getSuperFromRemote(source)
player:destroy()
end
addEventHandler("onPlayerQuit", root, handlePlayerLeave)
function Player:sendStatistics()
local data = self.account.data
local statistics =
{
gamesPlayed = data.gamesPlayed,
wins = data.wins,
losses = data.losses,
kills = data.kills,
deaths = data.deaths,
headshots = data.headshots,
coins = data.coins,
coinsSpent = data.coinsSpent,
}
self.remote:send("onGetAccountStatsFromServer", statistics)
end
local function handleSendStatistics()
local player = Remote.getSuperFromRemote(client)
player:sendStatistics()
end
addEvent("onAskServerForAccountStats", true)
addEventHandler("onAskServerForAccountStats", root, handleSendStatistics)