Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / game / packets.py
blobbdca0fd856e1af4a9b8e9a8218fe51f87a703740
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
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 # ###################################################
22 import game.main
23 import time
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
29 class Packet(object):
30 """
31 @param address:
32 @param port:
33 """
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
38 """
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
46 handled on a server
47 TIP: you can access (Server|Client)connection via game.main.connection
48 """
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
55 handled on a client
56 """
57 print "Warning: unhandled packet on client:",self
59 class TickPacket(Packet):
60 """
61 @param address:
62 @param port:
63 @param tick:
64 @param commands:
65 """
66 def __init__(self, address, port, tick, commands):
67 super(TickPacket, self).__init__(address, port)
68 self.tick = tick
69 self.commands = commands
71 class QueryPacket(Packet):
72 """Client sends this to discover servers
73 """
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):
79 """
80 @param address:
81 @param port:
82 @param text:
83 """
84 def __init__(self, address, port, text):
85 super(ChatPacket, self).__init__(address, port)
86 self.text = text
88 class ConnectPacket(Packet):
89 pass
91 class LobbyJoinPacket(Packet):
92 """Use this to join a game
93 """
94 def __init__(self, address, port, player):
95 super(LobbyJoinPacket, self).__init__(address, port)
96 self.player = player
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
108 def __init__(self):
109 pass
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)
122 self.player = player
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
130 break
131 game.main.connection.notifyClients()
133 class LobbyKeepAlivePacket(Packet):
134 """Sent regularly to master server to tell it that we are still there"""
135 def __init__(self):
136 pass
138 def handleOnServer(self):
139 game.main.connection.last_client_message[(self.address, self.port)] = time.time()
141 # MAYBE:
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):
150 pass
152 class MasterServerListQueryPacket(Packet):
153 pass
155 class MasterServerListAnswerPacket(Packet):
156 pass
158 class MasterVersionPacket(Packet):
159 pass
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)
167 self.myport = port
169 class InfoPacket(Packet):
171 @param address:
172 @param port:
173 @param map:
174 @param players:
175 @param bots:
176 @param maxplayers:
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
198 pass