Skip to content

Commit ab143ea

Browse files
author
Snowy
committed
Astron: Remove MsgType wildcard imports
1 parent 34ba7e6 commit ab143ea

File tree

3 files changed

+95
-99
lines changed

3 files changed

+95
-99
lines changed

direct/src/distributed/AstronClientRepository.py

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from direct.directnotify import DirectNotifyGlobal
44
from .ClientRepositoryBase import ClientRepositoryBase
5-
from .MsgTypes import *
5+
from . import MsgTypes
66
from direct.distributed.PyDatagram import PyDatagram
77
from panda3d.direct import STUint16, STUint32
88

9+
910
class AstronClientRepository(ClientRepositoryBase):
1011
"""
1112
The Astron implementation of a clients repository for
@@ -22,7 +23,7 @@ class AstronClientRepository(ClientRepositoryBase):
2223
* LOST_CONNECTION ()
2324
"""
2425

25-
notify = DirectNotifyGlobal.directNotify.newCategory("ClientRepository")
26+
notify = DirectNotifyGlobal.directNotify.newCategory("AstronClientRepository")
2627

2728
# This is required by DoCollectionManager, even though it's not
2829
# used by this implementation.
@@ -31,37 +32,35 @@ class AstronClientRepository(ClientRepositoryBase):
3132
def __init__(self, *args, **kwargs):
3233
ClientRepositoryBase.__init__(self, *args, **kwargs)
3334
base.finalExitCallbacks.append(self.shutdown)
34-
self.message_handlers = {CLIENT_HELLO_RESP: self.handleHelloResp,
35-
CLIENT_EJECT: self.handleEject,
36-
CLIENT_ENTER_OBJECT_REQUIRED: self.handleEnterObjectRequired,
37-
CLIENT_ENTER_OBJECT_REQUIRED_OWNER: self.handleEnterObjectRequiredOwner,
38-
CLIENT_OBJECT_SET_FIELD: self.handleUpdateField,
39-
CLIENT_OBJECT_SET_FIELDS: self.handleUpdateFields,
40-
CLIENT_OBJECT_LEAVING: self.handleObjectLeaving,
41-
CLIENT_OBJECT_LOCATION: self.handleObjectLocation,
42-
CLIENT_ADD_INTEREST: self.handleAddInterest,
43-
CLIENT_ADD_INTEREST_MULTIPLE: self.handleAddInterestMultiple,
44-
CLIENT_REMOVE_INTEREST: self.handleRemoveInterest,
45-
CLIENT_DONE_INTEREST_RESP: self.handleInterestDoneMessage,
46-
}
35+
self.message_handlers = {
36+
MsgTypes.CLIENT_HELLO_RESP: self.handleHelloResp,
37+
MsgTypes.CLIENT_EJECT: self.handleEject,
38+
MsgTypes.CLIENT_ENTER_OBJECT_REQUIRED: self.handleEnterObjectRequired,
39+
MsgTypes.CLIENT_ENTER_OBJECT_REQUIRED_OWNER: self.handleEnterObjectRequiredOwner,
40+
MsgTypes.CLIENT_OBJECT_SET_FIELD: self.handleUpdateField,
41+
MsgTypes.CLIENT_OBJECT_SET_FIELDS: self.handleUpdateFields,
42+
MsgTypes.CLIENT_OBJECT_LEAVING: self.handleObjectLeaving,
43+
MsgTypes.CLIENT_OBJECT_LOCATION: self.handleObjectLocation,
44+
MsgTypes.CLIENT_ADD_INTEREST: self.handleAddInterest,
45+
MsgTypes.CLIENT_ADD_INTEREST_MULTIPLE: self.handleAddInterestMultiple,
46+
MsgTypes.CLIENT_REMOVE_INTEREST: self.handleRemoveInterest,
47+
MsgTypes.CLIENT_DONE_INTEREST_RESP: self.handleInterestDoneMessage,
48+
}
4749

4850
#
4951
# Message Handling
5052
#
5153

5254
def handleDatagram(self, di):
5355
msgType = self.getMsgType()
54-
# self.handleMessageType(msgType, di)
55-
#
56-
#def handleMessageType(self, msgType, di):
5756
if msgType in self.message_handlers:
5857
self.message_handlers[msgType](di)
5958
else:
6059
self.notify.error("Got unknown message type %d!" % (msgType,))
6160

6261
self.considerHeartbeat()
6362

64-
def handleHelloResp(self, di):
63+
def handleHelloResp(self, _):
6564
messenger.send("CLIENT_HELLO_RESP", [])
6665

6766
def handleEject(self, di):
@@ -127,19 +126,7 @@ def generateWithRequiredFieldsOwner(self, dclass, doId, di):
127126
return distObj
128127

129128
def handleUpdateFields(self, di):
130-
# Can't test this without the server actually sending it.
131129
self.notify.error("CLIENT_OBJECT_SET_FIELDS not implemented!")
132-
# # Here's some tentative code and notes:
133-
# do_id = di.getUint32()
134-
# field_count = di.getUint16()
135-
# for i in range(0, field_count):
136-
# field_id = di.getUint16()
137-
# field = self.get_dc_file().get_field_by_index(field_id)
138-
# # print(type(field))
139-
# # print(field)
140-
# # FIXME: Get field type, unpack value, create and send message.
141-
# # value = di.get?()
142-
# # Assemble new message
143130

144131
def handleObjectLeaving(self, di):
145132
do_id = di.get_uint32()
@@ -201,35 +188,33 @@ def deleteObject(self, doId):
201188
self.freeDoId(doId)
202189
else:
203190
# Otherwise, ignore it
204-
self.notify.warning(
205-
"Asked to delete non-existent DistObj " + str(doId))
191+
self.notify.warning("Asked to delete non-existent DistObj " + str(doId))
206192

207193
#
208194
# Sending messages
209195
#
210196

211197
def sendUpdate(self, distObj, fieldName, args):
212198
""" Sends a normal update for a single field. """
213-
dg = distObj.dclass.clientFormatUpdate(
214-
fieldName, distObj.doId, args)
199+
dg = distObj.dclass.clientFormatUpdate(fieldName, distObj.doId, args)
215200
self.send(dg)
216201

217202
# FIXME: The version string should default to a .prc variable.
218203
def sendHello(self, version_string):
219204
dg = PyDatagram()
220-
dg.add_uint16(CLIENT_HELLO)
205+
dg.add_uint16(MsgTypes.CLIENT_HELLO)
221206
dg.add_uint32(self.get_dc_file().get_hash())
222207
dg.add_string(version_string)
223208
self.send(dg)
224209

225210
def sendHeartbeat(self):
226211
datagram = PyDatagram()
227-
datagram.addUint16(CLIENT_HEARTBEAT)
212+
datagram.addUint16(MsgTypes.CLIENT_HEARTBEAT)
228213
self.send(datagram)
229214

230215
def sendAddInterest(self, context, interest_id, parent_id, zone_id):
231216
dg = PyDatagram()
232-
dg.add_uint16(CLIENT_ADD_INTEREST)
217+
dg.add_uint16(MsgTypes.CLIENT_ADD_INTEREST)
233218
dg.add_uint32(context)
234219
dg.add_uint16(interest_id)
235220
dg.add_uint32(parent_id)
@@ -238,7 +223,7 @@ def sendAddInterest(self, context, interest_id, parent_id, zone_id):
238223

239224
def sendAddInterestMultiple(self, context, interest_id, parent_id, zone_ids):
240225
dg = PyDatagram()
241-
dg.add_uint16(CLIENT_ADD_INTEREST_MULTIPLE)
226+
dg.add_uint16(MsgTypes.CLIENT_ADD_INTEREST_MULTIPLE)
242227
dg.add_uint32(context)
243228
dg.add_uint16(interest_id)
244229
dg.add_uint32(parent_id)
@@ -249,7 +234,7 @@ def sendAddInterestMultiple(self, context, interest_id, parent_id, zone_ids):
249234

250235
def sendRemoveInterest(self, context, interest_id):
251236
dg = PyDatagram()
252-
dg.add_uint16(CLIENT_REMOVE_INTEREST)
237+
dg.add_uint16(MsgTypes.CLIENT_REMOVE_INTEREST)
253238
dg.add_uint32(context)
254239
dg.add_uint16(interest_id)
255240
self.send(dg)

direct/src/distributed/AstronDatabaseInterface.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from panda3d.direct import DCPacker
2-
from .MsgTypes import *
2+
from . import MsgTypes
33
from direct.directnotify import DirectNotifyGlobal
44
from .ConnectionRepository import ConnectionRepository
55
from .PyDatagram import PyDatagram
66
from .PyDatagramIterator import PyDatagramIterator
77

8+
89
class AstronDatabaseInterface:
910
"""
1011
This class is part of Panda3D's new MMO networking framework.
@@ -22,7 +23,7 @@ def __init__(self, air):
2223
self._callbacks = {}
2324
self._dclasses = {}
2425

25-
def createObject(self, databaseId, dclass, fields={}, callback=None):
26+
def createObject(self, databaseId, dclass, fields=None, callback=None):
2627
"""
2728
Create an object in the specified database.
2829
@@ -31,6 +32,8 @@ def createObject(self, databaseId, dclass, fields={}, callback=None):
3132
fields is a dict with any fields that should be stored in the object on creation.
3233
callback will be called with callback(doId) if specified. On failure, doId is 0.
3334
"""
35+
if fields is None:
36+
fields = {}
3437

3538
# Save the callback:
3639
ctx = self.air.getContext()
@@ -53,7 +56,7 @@ def createObject(self, databaseId, dclass, fields={}, callback=None):
5356

5457
# Now generate and send the datagram:
5558
dg = PyDatagram()
56-
dg.addServerHeader(databaseId, self.air.ourChannel, DBSERVER_CREATE_OBJECT)
59+
dg.addServerHeader(databaseId, self.air.ourChannel, MsgTypes.DBSERVER_CREATE_OBJECT)
5760
dg.addUint32(ctx)
5861
dg.addUint16(dclass.getNumber())
5962
dg.addUint16(fieldCount)
@@ -92,18 +95,15 @@ def queryObject(self, databaseId, doId, callback, dclass=None, fieldNames=()):
9295
dg = PyDatagram()
9396

9497
if not fieldNames:
95-
dg.addServerHeader(databaseId, self.air.ourChannel,
96-
DBSERVER_OBJECT_GET_ALL)
98+
dg.addServerHeader(databaseId, self.air.ourChannel, MsgTypes.DBSERVER_OBJECT_GET_ALL)
9799
else:
98100
# We need a dclass in order to convert the field names into field IDs:
99101
assert dclass is not None
100102

101103
if len(fieldNames) > 1:
102-
dg.addServerHeader(databaseId, self.air.ourChannel,
103-
DBSERVER_OBJECT_GET_FIELDS)
104+
dg.addServerHeader(databaseId, self.air.ourChannel, MsgTypes.DBSERVER_OBJECT_GET_FIELDS)
104105
else:
105-
dg.addServerHeader(databaseId, self.air.ourChannel,
106-
DBSERVER_OBJECT_GET_FIELD)
106+
dg.addServerHeader(databaseId, self.air.ourChannel, MsgTypes.DBSERVER_OBJECT_GET_FIELD)
107107

108108
dg.addUint32(ctx)
109109
dg.addUint32(doId)
@@ -123,7 +123,7 @@ def handleQueryObjectResp(self, msgType, di):
123123

124124
if ctx not in self._callbacks:
125125
self.notify.warning('Received unexpected %s'
126-
' (ctx %d)' % (MsgId2Names[msgType], ctx))
126+
' (ctx %d)' % (MsgTypes.MsgId2Names[msgType], ctx))
127127
return
128128

129129
try:
@@ -132,7 +132,7 @@ def handleQueryObjectResp(self, msgType, di):
132132
self._callbacks[ctx](None, None)
133133
return
134134

135-
if msgType == DBSERVER_OBJECT_GET_ALL_RESP:
135+
if msgType == MsgTypes.DBSERVER_OBJECT_GET_ALL_RESP:
136136
dclassId = di.getUint16()
137137
dclass = self.air.dclassesByNumber.get(dclassId)
138138
else:
@@ -142,7 +142,7 @@ def handleQueryObjectResp(self, msgType, di):
142142
self.notify.error('Received bad dclass %d in'
143143
' DBSERVER_OBJECT_GET_ALL_RESP' % (dclassId))
144144

145-
if msgType == DBSERVER_OBJECT_GET_FIELD_RESP:
145+
if msgType == MsgTypes.DBSERVER_OBJECT_GET_FIELD_RESP:
146146
fieldCount = 1
147147
else:
148148
fieldCount = di.getUint16()
@@ -224,18 +224,18 @@ def updateObject(self, databaseId, doId, dclass, newFields, oldFields=None, call
224224
self._callbacks[ctx] = callback
225225
if fieldCount == 1:
226226
dg.addServerHeader(databaseId, self.air.ourChannel,
227-
DBSERVER_OBJECT_SET_FIELD_IF_EQUALS)
227+
MsgTypes.DBSERVER_OBJECT_SET_FIELD_IF_EQUALS)
228228
else:
229229
dg.addServerHeader(databaseId, self.air.ourChannel,
230-
DBSERVER_OBJECT_SET_FIELDS_IF_EQUALS)
230+
MsgTypes.DBSERVER_OBJECT_SET_FIELDS_IF_EQUALS)
231231
dg.addUint32(ctx)
232232
else:
233233
if fieldCount == 1:
234234
dg.addServerHeader(databaseId, self.air.ourChannel,
235-
DBSERVER_OBJECT_SET_FIELD)
235+
MsgTypes.DBSERVER_OBJECT_SET_FIELD)
236236
else:
237237
dg.addServerHeader(databaseId, self.air.ourChannel,
238-
DBSERVER_OBJECT_SET_FIELDS)
238+
MsgTypes.DBSERVER_OBJECT_SET_FIELDS)
239239
dg.addUint32(doId)
240240
if fieldCount != 1:
241241
dg.addUint16(fieldCount)
@@ -295,13 +295,15 @@ def handleUpdateObjectResp(self, di, multi):
295295
del self._callbacks[ctx]
296296

297297
def handleDatagram(self, msgType, di):
298-
if msgType == DBSERVER_CREATE_OBJECT_RESP:
298+
if msgType == MsgTypes.DBSERVER_CREATE_OBJECT_RESP:
299299
self.handleCreateObjectResp(di)
300-
elif msgType in (DBSERVER_OBJECT_GET_ALL_RESP,
301-
DBSERVER_OBJECT_GET_FIELDS_RESP,
302-
DBSERVER_OBJECT_GET_FIELD_RESP):
300+
elif msgType in (
301+
MsgTypes.DBSERVER_OBJECT_GET_ALL_RESP,
302+
MsgTypes.DBSERVER_OBJECT_GET_FIELDS_RESP,
303+
MsgTypes.DBSERVER_OBJECT_GET_FIELD_RESP
304+
):
303305
self.handleQueryObjectResp(msgType, di)
304-
elif msgType == DBSERVER_OBJECT_SET_FIELD_IF_EQUALS_RESP:
306+
elif msgType == MsgTypes.DBSERVER_OBJECT_SET_FIELD_IF_EQUALS_RESP:
305307
self.handleUpdateObjectResp(di, False)
306-
elif msgType == DBSERVER_OBJECT_SET_FIELDS_IF_EQUALS_RESP:
308+
elif msgType == MsgTypes.DBSERVER_OBJECT_SET_FIELDS_IF_EQUALS_RESP:
307309
self.handleUpdateObjectResp(di, True)

0 commit comments

Comments
 (0)