1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
25 # There are two ways of sending packets without specifing target ip/port:
26 # 1. Server sends with address and port == None, then it gets sent to all players
27 # 2. Client uses sendToServer
34 def __init__(self
, address
, port
):
35 """ This can just set address and port
37 If you want to extend this function, you have to update most of the Packet-classes
39 (self
.address
,) = None if address
is None else str(address
),
40 self
.port
= None if port
is None else int(port
)
42 def handleOnServer(self
):
43 """Defines how packet is handled when received on game server
45 Gets overwritten in every packet subclass, that has to be
47 TIP: you can access (Server|Client)connection via game.main.connection
49 print "Warning: unhandled packet on server:",self
51 def handleOnClient(self
):
52 """Defines how packet is handled when received on game client
54 Gets overwritten in every packet subclass, that has to be
57 print "Warning: unhandled packet on client:",self
59 class TickPacket(Packet
):
66 def __init__(self
, address
, port
, tick
, commands
):
67 super(TickPacket
, self
).__init
__(address
, port
)
69 self
.commands
= commands
71 class QueryPacket(Packet
):
72 """Client sends this to discover servers
74 def handleOnServer(self
):
75 o
= game
.main
.connection
.mpoptions
76 game
.main
.connection
.send(InfoPacket(self
.address
, self
.port
, 'unknown map' if o
['selected_map'] == -1 else o
['maps'][1][o
['selected_map']], len(o
['players']), 0 if o
['bots'] is None else o
['bots'], 0 if o
['slots'] is None else o
['slots']))
78 class LobbyChatPacket(Packet
):
84 def __init__(self
, address
, port
, text
):
85 super(ChatPacket
, self
).__init
__(address
, port
)
88 class ConnectPacket(Packet
):
91 class LobbyJoinPacket(Packet
):
92 """Use this to join a game
94 def __init__(self
, address
, port
, player
):
95 super(LobbyJoinPacket
, self
).__init
__(address
, port
)
98 def handleOnServer(self
):
99 self
.player
.address
, self
.player
.port
= self
.address
, self
.port
100 game
.main
.connection
.mpoptions
['players'].append(self
.player
)
101 game
.main
.connection
.last_client_message
[(self
.player
.address
, self
.player
.port
)] = time
.time()
102 print 'JOIN BY', self
.player
.address
, self
.player
.port
103 game
.main
.connection
.notifyClients()
105 class LeaveServerPacket(Packet
):
106 """Use this to leave a server
111 def handleOnServer(self
):
112 for player
in game
.main
.connection
.mpoptions
['players']:
113 if player
.address
== self
.address
and player
.port
== self
.port
:
114 print 'LEAVE BY', self
.address
, self
.port
115 game
.main
.connection
.mpoptions
['players'].remove(player
)
117 class LobbyPlayerModifiedPacket(Packet
):
118 """Notifes server about changes to the local player
120 def __init__(self
, address
, port
, player
):
121 super(LobbyPlayerModifiedPacket
, self
).__init
__(address
, port
)
124 def handleOnServer(self
):
125 self
.player
.address
, self
.player
.port
= self
.address
, self
.port
126 players
= game
.main
.connection
.mpoptions
['players']
127 for i
in xrange(0, len(players
)):
128 if players
[i
].address
== self
.address
and players
[i
].port
== self
.port
:
129 players
[i
] = self
.player
131 game
.main
.connection
.notifyClients()
133 class LobbyKeepAlivePacket(Packet
):
134 """Sent regularly to master server to tell it that we are still there"""
138 def handleOnServer(self
):
139 game
.main
.connection
.last_client_message
[(self
.address
, self
.port
)] = time
.time()
142 # try to tell client when he got disconnected
143 # packet might not arrive
144 # but he could be kicked for another reason
145 # so include a parameter in the packet which tells the client why he got disconnected
147 # packet when server is closed. probably packet above can be used
149 class MasterRegisterPacket(Packet
):
152 class MasterServerListQueryPacket(Packet
):
155 class MasterServerListAnswerPacket(Packet
):
158 class MasterVersionPacket(Packet
):
161 class MasterRegisterPacket(Packet
):
163 @param port: port on which local game server runs
165 def __init__(self
, port
):
166 super(MasterRegisterPacket
, self
).__init
__(game
.main
.settings
.network
.url_master
, game
.main
.settings
.network
.port
)
169 class InfoPacket(Packet
):
178 def __init__(self
, address
, port
, map, players
, bots
, maxplayers
):
179 super(InfoPacket
, self
).__init
__(address
, port
)
180 self
.map, self
.players
, self
.bots
, self
.maxplayers
= map, players
, bots
, maxplayers
182 class LobbyServerInfoPacket(Packet
):
183 """ Contains info about multiplayer game
185 The game server sends this packet to clients
186 to notify them about game settings
187 NOTE: address & port are none, because this way the packet gets sent to all clients
189 def __init__(self
, mpoptions
, address
= None, port
= None):
190 super(LobbyServerInfoPacket
, self
).__init
__(address
, port
)
191 self
.mpoptions
= mpoptions
193 def handleOnClient(self
):
194 game
.main
.connection
.mpoptions
= self
.mpoptions
196 def handleOnServer(self
):
197 # server sent this, so it can ignore it