-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildingserver.py
79 lines (62 loc) · 2.56 KB
/
buildingserver.py
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
from pyspades.server import buildingserver, Protocol, grenade_exploded
from pyspades.common import SColor
from twisted.internet import reactor
class BuildingServer(Protocol):
def __init__(self, *args, **kwargs):
Protocol.__init__(self, *args, **kwargs)
self.balances = {} # Player balances (crypto currency)
self.transaction_fee = 0.05 # Fee for each transaction (5%)
def on_load(self):
pass
def on_save(self):
pass
def on_block_build(self, x, y, z, mode, player, value):
# Charge a fee for building blocks
cost = 1
if self.can_afford(player, cost):
self.subtract_balance(player, cost)
return True
def on_kill(self, victim, attacker, type, grenade):
# Reward the attacker for killing a player
if attacker is not None:
reward = 2
self.add_balance(attacker, reward)
def on_pickup(self, player, pickup_entity):
# Reward players for picking up items
if pickup_entity.value == 1: # Assume PICKUP_NORMAL has a value of 1
reward = 1
self.add_balance(player, reward)
def can_afford(self, player, amount):
return self.get_balance(player) >= amount
def get_balance(self, player):
return self.balances.get(player.name, 0)
def add_balance(self, player, amount):
player_name = player.name
self.balances[player_name] = self.get_balance(player) + amount
def subtract_balance(self, player, amount):
player_name = player.name
current_balance = self.get_balance(player)
if current_balance >= amount:
self.balances[player_name] = current_balance - amount
return True
else:
return False
def on_trade(self, sender, receiver, amount):
# Implement a basic trading system
if amount <= 0 or not self.can_afford(sender, amount):
return False
# Apply transaction fee
fee = amount * self.transaction_fee
amount_after_fee = amount - fee
if self.subtract_balance(sender, amount):
self.add_balance(receiver, amount_after_fee)
return True
else:
return False
def apply_building_server_script(protocol, connection, config):
protocol.grenade_exploded = grenade_exploded
protocol.apply_script(building_server_script)
return protocol
if __name__ == "__main__":
apply_building_server_script(BuildingServer)
reactor.run()