-
Notifications
You must be signed in to change notification settings - Fork 0
/
managing_players.py
52 lines (38 loc) · 1.43 KB
/
managing_players.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
import pyinsim
# Store the connection and player lists in dictionaries.
connections = {}
players = {}
def new_connection(insim, ncn):
# Add a new connection to the connections dict.
connections[ncn.UCID] = ncn
print ('New connection: %s' % ncn.UName)
def connection_left(insim, cnl):
# Get connection from connections dict.
ncn = connections[cnl.UCID]
# Delete the connection from the dict.
del connections[cnl.UCID]
print ('Connection left: %s' % ncn.UName)
# This function will also be called, when player leaves pit,
# Not only, when joining the race.
def new_player(insim, npl):
# Add the new player to the players dict.
players[npl.PLID] = npl
print ('New player: %s' % str(npl.PName))
def player_left(insim, pll):
# Get player from the players dict.
npl = players[pll.PLID]
# Delete them from the dict.
del players[pll.PLID]
print('Player left: %s' % (npl.PName))
# Init new InSim object.
insim = pyinsim.insim(b'127.0.0.1', 29999, Admin=b'')
# Bind events for the connection and player packets.
insim.bind(pyinsim.ISP_NCN, new_connection)
insim.bind(pyinsim.ISP_CNL, connection_left)
insim.bind(pyinsim.ISP_NPL, new_player)
insim.bind(pyinsim.ISP_PLL, player_left)
# Request for LFS to send all connections and players.
insim.send(pyinsim.ISP_TINY, ReqI=255, SubT=pyinsim.TINY_NCN)
insim.send(pyinsim.ISP_TINY, ReqI=255, SubT=pyinsim.TINY_NPL)
# Start pyinsim.
pyinsim.run()