Package lacewing :: Package moo :: Module client
[frames] | no frames]

Source Code for Module lacewing.moo.client

  1  from twisted.internet.protocol import ClientFactory 
  2   
  3  from lacewing.moo.protocol import MooProtocol 
  4   
  5  from lacewing.moo.packet import ServerPacket, ClientPacket 
  6   
  7  from lacewing.moo.packetloaders.server import * 
  8  from lacewing.moo.packetloaders.client import * 
  9   
 10  from lacewing.multidict import MultikeyDict 
 11   
 12  from lacewing.moo.packetloaders.message import * 
 13   
14 -class Player(object):
15 name = None 16 id = None 17 ip = None 18 channel = None 19
20 - def __init__(self, name, id, ip, channel):
21 self.name = name 22 self.id = id 23 self.ip = ip 24 self.channel = channel
25
26 - def isPeer(self):
27 return self.channel.player != self
28
29 - def isMaster(self):
30 return self.channel.getMaster() == self
31
32 - def sendMessage(self, *arg, **kw):
33 self.channel.sendMessage(toClient = self, *arg, **kw)
34
35 -class Channel(object):
36 name = None 37 id = None 38 connection = None 39 player = None 40 connections = None 41 42 masterId = None 43
44 - def __init__(self, name, id, connection):
45 self.name = name 46 self.id = id 47 self.connection = connection 48 49 self.connections = MultikeyDict()
50
51 - def addConnection(self, connection):
53
54 - def removeConnection(self, connection):
55 del self.connections[connection]
56
57 - def sendMessage(self, value, subchannel, type = None, toClient = None):
58 newValue = Message(**self.connection.settings) 59 newValue.type = type or detectType(value) 60 newValue.value = value 61 if toClient: 62 newMessage = PrivateMessage() 63 newMessage.setConnection(toClient) 64 else: 65 newMessage = ToChannelMessage() 66 67 newMessage.setChannel(self) 68 newMessage.message = newValue 69 newMessage.subchannel = subchannel 70 self.connection.sendLoader(newMessage)
71
72 - def setMaster(self, id):
73 self.masterId = id
74
75 - def getMaster(self):
76 if not self.masterId in self.connections: 77 return None 78 return self.connections[self.masterId]
79
80 -class MooClientProtocol(MooProtocol):
81 _sendPacket = ClientPacket 82 _receivePacket = ServerPacket 83 84 motd = None 85 86 playerClass = Player 87
88 - def __init__(self, **settings):
89 self.settings = settings 90 MooProtocol.__init__(self)
91
92 - def loaderReceived(self, loader):
93 if isinstance(loader, MOTD): 94 self.motd = loader.motd 95 self.motdReceived(loader.motd) 96 97 elif isinstance(loader, AssignedID): 98 self.id = loader.playerId 99 100 newName = SetName() 101 newName.setConnection(self) 102 self.sendLoader(newName) 103 104 self.connectionAccepted() 105 106 elif isinstance(loader, ChannelJoined): 107 channelName = loader.channelName 108 channelId = loader.channelId 109 newChannel = Channel(channelName, channelId, self) 110 newPlayer = self.playerClass(loader.playerName, loader.playerId, 111 loader.playerIp, newChannel) 112 newChannel.player = newPlayer 113 newChannel.addConnection(newPlayer) 114 newChannel.setMaster(loader.masterId) 115 self.channels[channelName, channelId] = newChannel 116 self.channelJoined(newChannel) 117 118 elif isinstance(loader, (PlayerExists, PlayerJoined)): 119 playerName = loader.playerName 120 playerId = loader.playerId 121 playerIp = loader.playerIp 122 channel, = self.channels[loader.channelId] 123 newPlayer = self.playerClass(playerName, playerId, playerIp, channel) 124 channel.addConnection(newPlayer) 125 channel.setMaster(loader.masterId) 126 if isinstance(loader, PlayerExists): 127 self.channelUserExists(channel, newPlayer) 128 elif isinstance(loader, PlayerJoined): 129 self.channelUserJoined(channel, newPlayer) 130 131 elif isinstance(loader, FromChannelMessage): 132 if loader.isServer(): 133 self.messageReceived(loader.message, loader.subchannel) 134 else: 135 channel, = self.channels[loader.channelId] 136 player, = channel.connections[loader.playerId] 137 self.channelMessageReceived(channel, player, loader.message, 138 loader.subchannel) 139 140 elif isinstance(loader, PlayerChanged): 141 playerId = loader.playerId 142 playerName = loader.playerName 143 for channel in self.channels.values(): 144 if playerId in channel.connections: 145 player, = channel.connections[playerId] 146 oldName = player.name 147 player.name = playerName 148 if player.isPeer(): 149 self.channelUserChanged(player, oldName, player.name) 150 else: 151 self.nameChanged(channel, oldName, player.name) 152 153 elif isinstance(loader, PlayerLeft): 154 channel, = self.channels[loader.channelId] 155 player, = channel.connections[loader.playerId] 156 if player.isPeer(): 157 channel.removeConnection(player) 158 channel.setMaster(loader.masterId) 159 self.channelUserLeft(channel, player) 160 else: 161 del self.channels[channel] 162 self.channelLeft(channel) 163 164 else: 165 raise NotImplementedError
166 167 # event-like 168
169 - def motdReceived(self, value):
170 """ 171 Called upon receiving the MOTD 172 """
173
174 - def connectionAccepted(self):
175 """ 176 Called when the connection has been accepted, and the 177 client notified. 178 """
179
180 - def messageReceived(self, message, subchannel):
181 """ 182 Called upon receiving a server message 183 184 @type message: L{Message} object 185 """
186
187 - def channelMessageReceived(self, channel, connection, message, subchannel):
188 """ 189 Called when a channel message has been received 190 191 @type message: lacewing.moo.packetloaders.message.Message object 192 """
193
194 - def channelJoined(self, channel):
195 """ 196 Called when the server has accepted a channel join request. 197 @arg channel: The channel the client has joined. 198 """
199
200 - def channelLeft(self, channel):
201 """ 202 Called when the server has accepted a channel leave request. 203 @arg channel: The channel the client has left. 204 """
205
206 - def nameChanged(self, channel, oldName, newName):
207 """ 208 Called when the server has told us we've changed 209 our name 210 """
211
212 - def channelUserJoined(self, channel, client):
213 """ 214 Called when a client has joined the channel. 215 @arg channel: The channel the client has joined. 216 """
217
218 - def channelUserExists(self, channel, client):
219 """ 220 Called when a client exists in the channel. 221 @arg channel: The channel the client exists in. 222 """
223
224 - def channelUserLeft(self, channel, client):
225 """ 226 Called when a client has left the channel. 227 @arg channel: The channel the client has left. 228 """
229
230 - def channelUserChanged(self, client, oldName, newName):
231 """ 232 Called when a client in the channel (including has changed name. 233 """
234 235 # action-like 236
237 - def changeName(self, name):
238 newChange = ChangeName() 239 newChange.newName = name 240 self.sendLoader(newChange)
241
242 - def sendMessage(self, value, subchannel, type = None):
243 """ 244 Sends a message to the other end 245 """ 246 newValue = Message(**self.settings) 247 newValue.type = type or detectType(value) 248 newValue.value = value 249 newMessage = ClientMessage() 250 newMessage.message = newValue 251 newMessage.subchannel = subchannel 252 self.sendLoader(newMessage)
253
254 - def sendChannelMessage(self, channel, *arg, **kw):
255 channel.sendMessage(self, *arg, **kw)
256
257 - def joinChannel(self, channelName):
258 newJoin = JoinChannel() 259 newJoin.channelName = channelName 260 self.sendLoader(newJoin)
261
262 - def leaveChannel(self, channel):
263 newLeave = LeaveChannel() 264 newLeave.setChannel(channel) 265 self.sendLoader(newLeave)
266