def sendMove(mx, my): move = PyDatagram() move.addUint16(PACKET_MOVE) sgn = (mode == MODE_2P or MY_POSITION > 1) and -1 or 1 move.addFloat64(sgn * mx) move.addFloat64(sgn * my) cWriter.send(move, serverConnection)
def sendMove(mx, my): move = PyDatagram() move.addUint16(PACKET_MOVE) sgn = (mode == MODE_2P or MY_POSITION>1) and -1 or 1 move.addFloat64(sgn*mx) move.addFloat64(sgn*my) cWriter.send(move, serverConnection)
def pingTask(task): if (task.frame % 1000) > 0: #determine network delay every now and then (every 1000 frames) return Task.cont ping = PyDatagram() ping.addUint16(PACKET_PING) ping.addFloat64(time.time()) toAll(ping, activeConnections) return Task.cont
def makeLaser(self, start, end, color, scale): dg = PyDatagram() CIGlobals.putVec3(dg, start) CIGlobals.putVec3(dg, end) CIGlobals.putVec3(dg, Vec3(color[0], color[1], color[2])) dg.addFloat64(scale) self.sendTempEnt(TE_LASER, dg)
def makeDecalTrace(self, material, scale, rot, start, end): dg = PyDatagram() dg.addString(material) dg.addFloat64(scale) dg.addFloat64(rot) CIGlobals.putVec3(dg, start) CIGlobals.putVec3(dg, end) self.sendTempEnt(TE_DECAL_TRACE, dg)
def pingTask(task): if (task.frame % 1000 ) > 0: #determine network delay every now and then (every 1000 frames) return Task.cont ping = PyDatagram() ping.addUint16(PACKET_PING) ping.addFloat64(time.time()) toAll(ping, activeConnections) return Task.cont
def enterValidate(self): self.infoLbl = canvas.create_text(287, 210, text="Validating...", fill="white") self.tk.update() self.badLauncherMsg = None dg = PyDatagram() dg.addUint16(LAUNCHER_VERSION) dg.addFloat64(self.version) self.cWriter.send(dg, self.Connection)
def makeBulletRicochet(self, pos, bulletDirection, surfaceNormal, scale=3): dg = PyDatagram() CIGlobals.putVec3(dg, pos) CIGlobals.putVec3( dg, CIGlobals.reflect(bulletDirection, surfaceNormal) + Vec3(random.uniform(-0.15, 0.15), random.uniform(-0.15, 0.15), random.uniform(-0.15, 0.15))) dg.addFloat64(scale * random.uniform(0.15, 1.3)) self.sendTempEnt(TE_BULLET_RICOCHET, dg)
def reportDownloadTimes(self): print "----------DOWNLOAD TIMES----------" totalTime = 0 for k, v in self.downloadTime.iteritems(): print files2Download[k] + ": " + str( self.downloadTime[k]) + " seconds." totalTime += self.downloadTime[k] print "Total time: " + str(totalTime) + " seconds." dg = PyDatagram() dg.addUint16(DL_TIME_REPORT) dg.addFloat64(totalTime) self.cWriter.send(dg, self.Connection)
def tower_train(self,tower_id,build_object): order = PyDatagram() if base.client == True: order.addUint16(REQUEST_TOWER_TRAIN) else: order.addUint16(TOWER_TRAIN) order.addInt16(army_id) order.addFloat64(tx) order.addFloat64(ty) if base.client == True: self.cWriter.send(order,base.server_connection) else: self.send_package(order) base.towers[tower_id].train_counter()
def send(self, kwargs): try: pkg = PyDatagram() pkg.addUint16(Constants.CMSG_MOVE) pkg.addFloat64(kwargs['x']) pkg.addFloat64(kwargs['y']) pkg.addFloat64(kwargs['z']) pkg.addFloat64(kwargs['h']) pkg.addString(kwargs['keys']) self.cWriter.send(pkg, self.connection) self.log('Sent [' + str(Constants.CMSG_MOVE) + '] RequestMove') except: self.log('Bad [' + str(Constants.CMSG_MOVE) + '] RequestMove') print_exc()
def makeExplosion(self, pos=(0, 0, 0), scale=1, sound=True, shakeCam=True, duration=1.0, soundVol=1.0): dg = PyDatagram() CIGlobals.putVec3(dg, pos) dg.addFloat64(scale) dg.addUint8(int(sound)) dg.addUint8(int(shakeCam)) dg.addFloat64(duration) dg.addFloat64(soundVol) self.sendTempEnt(TE_EXPLOSION, dg)
def army_move(self,army_id,tx,ty): order = PyDatagram() if base.client == True: order.addUint16(ARMY_MOVE_REQUEST) else: order.addUint16(ARMY_MOVE) ax = base.armies[army_id].node_path.getX() ay = base.armies[army_id].node_path.getY() order.addInt16(army_id) order.addFloat64(ax) order.addFloat64(ay) order.addFloat64(tx) order.addFloat64(ty) if base.client == True: self.cWriter.send(order,base.server_connection) else: self.send_package(order) base.armies[army_id].move_to_point(tx,ty)
def send(self, args = {}): try: pkg = PyDatagram() pkg.addUint16(Constants.CMSG_MOVE) #print(args) pkg.addFloat64(args["x"]) pkg.addFloat64(args["y"]) pkg.addFloat64(args["h"]) pkg.addInt8(args["isMoving"]) self.cWriter.send(pkg, self.connection) #self.log('Sent [' + str(Constants.RAND_STRING) + '] Int Request') except: self.log('Bad [' + str(Constants.REQ_HEARTBEAT) + '] Heartbeat Request') print_exc()
def initialize_player(self): conn = self.player_list[-1].conn_id datagram = PyDatagram() datagram.addString("init") newest = self.player_list[-1].player_id datagram.addUint8(newest) logging.debug( f"{self.active_players} players and {newest} is the newest player") datagram.addUint8(self.active_players) if len(self.player_list) > 1: for k in self.player_list: datagram.addString(k.username) datagram.addFloat64(k.pos_and_or['x']) datagram.addFloat64(k.pos_and_or['y']) datagram.addFloat64(k.pos_and_or['z']) self.cWriter.send(datagram, conn)
class World(DirectObject): #This class will control anything related to the virtual world def __init__(self): self.timeSinceLastUpdate = 0 def UpdateWorld(self,meClass,clientClass): #get the time since the last framerate self.elapsed = globalClock.getDt() #add it to the time since we last set our position to where the server thinks we are #add the elapsed time to the time since the last update sent to the server self.timeSinceLastUpdate += self.elapsed if (self.timeSinceLastUpdate > 0.1): self.datagram = PyDatagram() self.datagram.addString("positions") self.datagram.addFloat64(meClass.model.getX()) self.datagram.addFloat64(meClass.model.getY()) self.datagram.addFloat64(meClass.model.getZ()) self.datagram.addFloat64(meClass.model.getH()) self.datagram.addFloat64(meClass.model.getP()) self.datagram.addFloat64(meClass.model.getR()) try: clientClass.cWriter.send(self.datagram,clientClass.Connection) except: print "No connection to the server. You are in stand alone mode." return Task.done self.timeSinceLastUpdate = 0 return Task.cont
class World(DirectObject ): #This class will control anything related to the virtual world def __init__(self): self.timeSinceLastUpdate = 0 def UpdateWorld(self, meClass, clientClass): #get the time since the last framerate self.elapsed = globalClock.getDt() #add it to the time since we last set our position to where the server thinks we are #add the elapsed time to the time since the last update sent to the server self.timeSinceLastUpdate += self.elapsed if (self.timeSinceLastUpdate > 0.1): self.datagram = PyDatagram() self.datagram.addString("positions") self.datagram.addFloat64(meClass.model.getX()) self.datagram.addFloat64(meClass.model.getY()) self.datagram.addFloat64(meClass.model.getZ()) self.datagram.addFloat64(meClass.model.getH()) self.datagram.addFloat64(meClass.model.getP()) self.datagram.addFloat64(meClass.model.getR()) try: clientClass.cWriter.send(self.datagram, clientClass.Connection) except: #print "No connection to the server. You are in stand alone mode." return Task.done self.timeSinceLastUpdate = 0 return Task.cont
def update_world(self, arg): self.Δt = globalClock.getDt() self.Δt_update += self.Δt if self.Δt_update > 0.05: datagram = PyDatagram() datagram.addString("position") datagram.addFloat64(me.model.getX()) datagram.addFloat64(me.model.getY()) datagram.addFloat64(me.model.getZ()) datagram.addFloat64(me.model.getH()) datagram.addFloat64(me.model.getP()) datagram.addFloat64(me.model.getR()) try: world_client.cWriter.send(datagram, world_client.conn) except: logging.info("No connection to the server. You are in stand alone mode.") return Task.done self.Δt_update = 0 return Task.cont
def update_positions(self, data): # send self.Δt = globalClock.getDt() self.Δt_update += self.Δt if self.Δt_update > 0.05: if self.active_players > 1: datagram = PyDatagram() datagram.addString("update") datagram.addInt8(self.active_players) for p in self.player_list: datagram.addString(p.username) datagram.addFloat64(p.pos_and_or['x']) datagram.addFloat64(p.pos_and_or['y']) datagram.addFloat64(p.pos_and_or['z']) datagram.addFloat64(p.pos_and_or['h']) datagram.addFloat64(p.pos_and_or['p']) datagram.addFloat64(p.pos_and_or['r']) for p in self.player_list: self.cWriter.send(datagram, p.conn_id) self.Δt_update = 0 return Task.cont
def myProcessDataFunction(datagram): global status, P1NAME, P2NAME, lastResetRequest global MY_POSITION, kicker, texKicker, texKicker2, mode if (role == ROLE_SERVER): sender = activeConnections.index(datagram.getConnection()) data = PyDatagramIterator(datagram) #try: if True: #just to keep the indent, in case we need "try" again. pktType = data.getUint16() if role == ROLE_SERVER: # packets received only by server if pktType == PACKET_MOVE: setOpponentMove(data, sender + 1) elif pktType == PACKET_MSG: timer = data.getUint16() msg = data.getString() sendMessage(msg, timer) #broadcast to all elif pktType == PACKET_HELLO: magic = data.getString() proto = data.getUint16() soft = data.getString() if magic != MAGIC_WORD: print "Connecting party did not identify as netkickern client." sys.exit(1) if proto != PROTOCOL_VERSION: print "Connecting party used incompatible protocol version " + str( proto) + "." print "We are using " + str(PROTOCOL_VERSION) + "." sys.exit(1) if soft != SOFTWARE_VERSION: print "WARNING: Client is using software " + soft + "." print "Ok, client " + str(sender + 1) + " connected." status = STATUS_INIT qpref = PyDatagram() #query for client preferences qpref.addUint16(PACKET_QPREF) qpref.addString(SOFTWARE_VERSION) cWriter.send(qpref, activeConnections[sender]) elif pktType == PACKET_PREF: if status != STATUS_INIT: return prefs = {} teamname = data.getString() prefs['team'] = teamname place = -1 if (mode == MODE_4P): #assign the last free position, and P1 only if the teamname matches or everything else is given away if not POSITION[3]: place = 3 if not POSITION[2]: place = 2 if (teamname == TEAMNAME) or (place == -1): if not POSITION[1]: place = 1 print "placed player ", sender + 1, " at position ", place POSITION[place] = sender + 1 prefs['pos'] = place playerPrefs[sender] = prefs if (mode == MODE_2P) or (0 in playerPrefs and 1 in playerPrefs and 2 in playerPrefs): P1NAME = TEAMNAME P2NAME = playerPrefs[0]['team'] rename = PyDatagram() rename.addUint16(PACKET_NAME) rename.addString(P1NAME) rename.addString(P2NAME) toAll(rename, activeConnections) if (mode == MODE_4P ): #inform players of the position they play in for i in range(0, 3): placing = PyDatagram() placing.addUint16(PACKET_PLACE) placing.addUint16(playerPrefs[i]['pos']) cWriter.send(placing, activeConnections[i]) resetNames() welcome = PyDatagram() welcome.addUint16(PACKET_START) toAll(welcome, activeConnections) startGame() elif pktType == PACKET_PONG: stime = data.getFloat64() now = time.time() deltatime = now - stime # TODO: use this to delay mouse movements by avg(deltatime)/2 print "player " + str(sender) + " network delay: " + str( deltatime * 500) + "ms " #rtt/2 elif pktType == PACKET_RESET: lastResetRequest = time.time() playerPrefs[sender]['lastReset'] = lastResetRequest if not isResetConfirmed(): resetRequest = PyDatagram() #forward to all clients resetRequest.addUint16(PACKET_RESET) #resetRequest.addUint16(sender) # TODO: tell everyone which player sent the request toAll(resetRequest, activeConnections) setMessage( "Player " + str(sender + 1) + " wishes to reset the ball.\nPress Space to confirm.", 3) else: # packets received only by clients if pktType == PACKET_SET: setGameStatus(data) elif pktType == PACKET_SCORE: setScore(data) elif pktType == PACKET_QPREF: soft = data.getString() if soft != SOFTWARE_VERSION: print "WARNING: Server is using software " + soft + "." status = STATUS_INIT pref = PyDatagram() pref.addUint16(PACKET_PREF) pref.addString(TEAMNAME) cWriter.send(pref, serverConnection) elif pktType == PACKET_START: print "connection to game host confirmed." startGame() elif pktType == PACKET_MSG: timer = data.getUint16() msg = data.getString() setMessage(msg, timer) elif pktType == PACKET_NAME: P1NAME = data.getString() P2NAME = data.getString() resetNames() elif pktType == PACKET_PLACE: #4P only MY_POSITION = data.getUint16() mode = MODE_4P resetGameColours(kicker, texKicker, texKicker2) elif pktType == PACKET_PING: stime = data.getFloat64() pong = PyDatagram() pong.addUint16(PACKET_PONG) pong.addFloat64(stime) cWriter.send(pong, serverConnection) elif pktType == PACKET_RESET: setMessage( "Another wishes to reset the ball.\nPress Space to confirm.", 3) #except Exception, e: # print e # sys.exit(1) #wow, this is what I call exception handling.. return
class PlayerReg(DirectObject): #This class will hold anything that is related to regulating clients def __init__(self): self.PlayerList = [] self.active = 0 self.timeSinceLastUpdate = 0 self.elapsed = 0 def updatePlayers(self,serverClass,data,type): if (type == "positions"): #keep players updated on their position #self.elapsed = globalClock.getDt() self.elapsed = 0.0001 self.timeSinceLastUpdate += self.elapsed if (self.timeSinceLastUpdate > 0.1): self.timeSinceLastUpdate = 0 if (self.active): self.datagram = PyDatagram() self.datagram.addString("update") #add the number of players self.datagram.addFloat64(self.active) #add every players current position for k in range(self.active): self.datagram.addFloat64(self.PlayerList[k].currentPos['x']) self.datagram.addFloat64(self.PlayerList[k].currentPos['y']) self.datagram.addFloat64(self.PlayerList[k].currentPos['z']) self.datagram.addFloat64(self.PlayerList[k].currentPos['h']) self.datagram.addFloat64(self.PlayerList[k].currentPos['p']) self.datagram.addFloat64(self.PlayerList[k].currentPos['r']) for k in self.PlayerList: self.conn = k.connectionID serverClass.cWriter.send(self.datagram,self.conn) return Task.cont if(type == "chat"): #Keep players up to date with all the chat thats goin on self.iterator = data self.datagram = PyDatagram() self.datagram.addString("chat") self.text = self.iterator.getString() self.datagram.addString(self.text) print self.text,' ',str(serverClass) for k in self.PlayerList: serverClass.cWriter.send(self.datagram,k.connectionID) def updateData(self,connection, datagram,serverClass): self.iterator = PyDatagramIterator(datagram) self.type = self.iterator.getString() if (self.type == "positions"): for k in self.PlayerList: if (k.connectionID == connection): k.currentPos['x'] = self.iterator.getFloat64() k.currentPos['y'] = self.iterator.getFloat64() k.currentPos['z'] = self.iterator.getFloat64() k.currentPos['h'] = self.iterator.getFloat64() k.currentPos['p'] = self.iterator.getFloat64() k.currentPos['r'] = self.iterator.getFloat64() if (self.type == "chat"): self.updatePlayers(serverClass,self.iterator,"chat") def sendInitialInfo(self,i,server): #Initialize the new Player self.con = self.PlayerList[i].connectionID #set the connection to the player's connection self.datagram = PyDatagram() #create a datagram instance self.datagram.addString("init") #specify to the client that this is an init type packet self.datagram.addUint8(self.active) #specify the player's number (not sure why this is here) self.datagram.addFloat64(i) #specify number of players (same as player's number) for k in self.PlayerList: #Add the current position of everyone in the game world and send it self.datagram.addString(k.username) self.datagram.addFloat64(k.currentPos['x']) self.datagram.addFloat64(k.currentPos['y']) self.datagram.addFloat64(k.currentPos['z']) server.cWriter.send(self.datagram,self.con)
def sendGameStatus(): px, py, pz = ballBody.getPosition() prot = ballBody.getRotation() r1, r2, r3, r4 = row1.getZ(), row2.getZ(), row3.getZ(), row4.getZ() o1, o2, o3, o4 = rrow1.getZ(), rrow2.getZ(), rrow3.getZ(), rrow4.getZ() rot = [None] * 4 for i in range(0, 4): rot[i] = kicker[i].getH() status = PyDatagram() status.addUint16(PACKET_SET) status.addFloat64(px) status.addFloat64(py) status.addFloat64(pz) for r in range(9): #just to be explicit status.addFloat64(prot[i]) status.addFloat64(r1) status.addFloat64(r2) status.addFloat64(r3) status.addFloat64(r4) status.addFloat64(o1) status.addFloat64(o2) status.addFloat64(o3) status.addFloat64(o4) for i in range(0, 4): status.addFloat64(rot[i]) toAll(status, activeConnections)
def myProcessDataFunction(datagram): global status, P1NAME, P2NAME, lastResetRequest global MY_POSITION, kicker, texKicker, texKicker2, mode if (role==ROLE_SERVER): sender = activeConnections.index(datagram.getConnection()) data = PyDatagramIterator(datagram) #try: if True: #just to keep the indent, in case we need "try" again. pktType = data.getUint16() if role==ROLE_SERVER: # packets received only by server if pktType==PACKET_MOVE: setOpponentMove(data, sender+1) elif pktType==PACKET_MSG: timer = data.getUint16() msg = data.getString() sendMessage(msg, timer) #broadcast to all elif pktType==PACKET_HELLO: magic = data.getString() proto = data.getUint16() soft = data.getString() if magic != MAGIC_WORD: print "Connecting party did not identify as netkickern client." sys.exit(1) if proto != PROTOCOL_VERSION: print "Connecting party used incompatible protocol version "+str(proto)+"." print "We are using "+str(PROTOCOL_VERSION)+"." sys.exit(1) if soft != SOFTWARE_VERSION: print "WARNING: Client is using software "+soft+"." print "Ok, client "+str(sender+1)+" connected." status = STATUS_INIT qpref = PyDatagram() #query for client preferences qpref.addUint16(PACKET_QPREF) qpref.addString(SOFTWARE_VERSION) cWriter.send(qpref, activeConnections[sender]) elif pktType==PACKET_PREF: if status != STATUS_INIT: return prefs = {} teamname = data.getString() prefs['team']=teamname place = -1 if (mode == MODE_4P): #assign the last free position, and P1 only if the teamname matches or everything else is given away if not POSITION[3]: place = 3 if not POSITION[2]: place = 2 if (teamname == TEAMNAME) or (place==-1): if not POSITION[1]: place = 1 print "placed player ", sender+1, " at position ", place POSITION[place]=sender+1 prefs['pos'] = place playerPrefs[sender]=prefs if (mode == MODE_2P) or (0 in playerPrefs and 1 in playerPrefs and 2 in playerPrefs): P1NAME = TEAMNAME P2NAME = playerPrefs[0]['team'] rename = PyDatagram() rename.addUint16(PACKET_NAME) rename.addString(P1NAME) rename.addString(P2NAME) toAll(rename, activeConnections) if (mode == MODE_4P): #inform players of the position they play in for i in range(0,3): placing = PyDatagram() placing.addUint16(PACKET_PLACE) placing.addUint16(playerPrefs[i]['pos']) cWriter.send(placing, activeConnections[i]) resetNames() welcome = PyDatagram() welcome.addUint16(PACKET_START) toAll(welcome, activeConnections) startGame() elif pktType==PACKET_PONG: stime = data.getFloat64() now = time.time() deltatime = now-stime # TODO: use this to delay mouse movements by avg(deltatime)/2 print "player "+str(sender)+" network delay: "+str(deltatime*500)+"ms " #rtt/2 elif pktType==PACKET_RESET: lastResetRequest = time.time() playerPrefs[sender]['lastReset']=lastResetRequest if not isResetConfirmed(): resetRequest=PyDatagram() #forward to all clients resetRequest.addUint16(PACKET_RESET) #resetRequest.addUint16(sender) # TODO: tell everyone which player sent the request toAll(resetRequest, activeConnections) setMessage("Player "+str(sender+1)+" wishes to reset the ball.\nPress Space to confirm.", 3) else: # packets received only by clients if pktType==PACKET_SET: setGameStatus(data) elif pktType==PACKET_SCORE: setScore(data) elif pktType==PACKET_QPREF: soft = data.getString() if soft != SOFTWARE_VERSION: print "WARNING: Server is using software "+soft+"." status = STATUS_INIT pref = PyDatagram() pref.addUint16(PACKET_PREF) pref.addString(TEAMNAME) cWriter.send(pref, serverConnection) elif pktType==PACKET_START: print "connection to game host confirmed." startGame() elif pktType==PACKET_MSG: timer = data.getUint16() msg = data.getString() setMessage(msg, timer) elif pktType==PACKET_NAME: P1NAME = data.getString() P2NAME = data.getString() resetNames() elif pktType==PACKET_PLACE: #4P only MY_POSITION = data.getUint16() mode = MODE_4P resetGameColours(kicker, texKicker, texKicker2) elif pktType==PACKET_PING: stime = data.getFloat64() pong = PyDatagram() pong.addUint16(PACKET_PONG) pong.addFloat64(stime) cWriter.send(pong, serverConnection) elif pktType==PACKET_RESET: setMessage("Another wishes to reset the ball.\nPress Space to confirm.", 3) #except Exception, e: # print e # sys.exit(1) #wow, this is what I call exception handling.. return
class PlayerReg( DirectObject ): #This class will hold anything that is related to regulating clients def __init__(self): self.PlayerList = [] self.active = 0 self.timeSinceLastUpdate = 0 def updatePlayers(self, serverClass, data, type): if (type == "positions"): #keep players updated on their position self.elapsed = globalClock.getDt() self.timeSinceLastUpdate += self.elapsed if (self.timeSinceLastUpdate > 0.1): if (self.active): self.datagram = PyDatagram() self.datagram.addString("update") #add the number of players self.datagram.addFloat64(self.active) #add every players current position for k in range(self.active): self.datagram.addFloat64( self.PlayerList[k].currentPos['x']) self.datagram.addFloat64( self.PlayerList[k].currentPos['y']) self.datagram.addFloat64( self.PlayerList[k].currentPos['z']) self.datagram.addFloat64( self.PlayerList[k].currentPos['h']) self.datagram.addFloat64( self.PlayerList[k].currentPos['p']) self.datagram.addFloat64( self.PlayerList[k].currentPos['r']) for k in self.PlayerList: self.conn = k.connectionID serverClass.cWriter.send(self.datagram, self.conn) self.timeSinceLastUpdate = 0 return Task.cont if (type == "chat"): #Keep players up to date with all the chat thats goin on self.iterator = data self.datagram = PyDatagram() self.datagram.addString("chat") self.text = self.iterator.getString() self.datagram.addString(self.text) print self.text, ' ', str(serverClass) for k in self.PlayerList: serverClass.cWriter.send(self.datagram, k.connectionID) def updateData(self, connection, datagram, serverClass): self.iterator = PyDatagramIterator(datagram) self.type = self.iterator.getString() if (self.type == "positions"): for k in self.PlayerList: if (k.connectionID == connection): k.currentPos['x'] = self.iterator.getFloat64() k.currentPos['y'] = self.iterator.getFloat64() k.currentPos['z'] = self.iterator.getFloat64() k.currentPos['h'] = self.iterator.getFloat64() k.currentPos['p'] = self.iterator.getFloat64() k.currentPos['r'] = self.iterator.getFloat64() if (self.type == "chat"): self.updatePlayers(serverClass, self.iterator, "chat") def sendInitialInfo(self, i, server): #Initialize the new Player self.con = self.PlayerList[ i].connectionID #set the connection to the player's connection self.datagram = PyDatagram() #create a datagram instance self.datagram.addString( "init") #specify to the client that this is an init type packet self.datagram.addUint8( self.active ) #specify the player's number (not sure why this is here) self.datagram.addFloat64( i) #specify number of players (same as player's number) for k in self.PlayerList: #Add the current position of everyone in the game world and send it self.datagram.addString(k.username) self.datagram.addFloat64(k.currentPos['x']) self.datagram.addFloat64(k.currentPos['y']) self.datagram.addFloat64(k.currentPos['z']) server.cWriter.send(self.datagram, self.con)
def sendGameStatus(): px,py,pz = ballBody.getPosition() prot = ballBody.getRotation() r1,r2,r3,r4 = row1.getZ(), row2.getZ(), row3.getZ(), row4.getZ() o1,o2,o3,o4 = rrow1.getZ(), rrow2.getZ(), rrow3.getZ(), rrow4.getZ() rot = [None]*4 for i in range(0,4): rot[i] = kicker[i].getH() status = PyDatagram() status.addUint16(PACKET_SET) status.addFloat64(px) status.addFloat64(py) status.addFloat64(pz) for r in range(9): #just to be explicit status.addFloat64(prot[i]) status.addFloat64(r1) status.addFloat64(r2) status.addFloat64(r3) status.addFloat64(r4) status.addFloat64(o1) status.addFloat64(o2) status.addFloat64(o3) status.addFloat64(o4) for i in range(0,4): status.addFloat64(rot[i]) toAll(status, activeConnections)
class PlayerReg(DirectObject): def __init__(self): self.PlayerList = [] self.active = 0 self.timeSinceLastUpdate = 0 def updatePlayers(self, serverClass, data, type): if (type == "positions"): self.elapsed = globalClock.getDt() self.timeSinceLastUpdate += self.elapsed if (self.timeSinceLastUpdate > 0.1): if (self.active): self.datagram = PyDatagram() self.datagram.addString("update") self.datagram.addFloat64(self.active) for k in range(self.active): self.datagram.addFloat64( self.PlayerList[k].currentPos['x']) self.datagram.addFloat64( self.PlayerList[k].currentPos['y']) self.datagram.addFloat64( self.PlayerList[k].currentPos['z']) self.datagram.addFloat64( self.PlayerList[k].currentPos['h']) self.datagram.addFloat64( self.PlayerList[k].currentPos['p']) self.datagram.addFloat64( self.PlayerList[k].currentPos['r']) for k in self.PlayerList: self.conn = k.connectionID serverClass.cWriter.send(self.datagram, self.conn) self.timeSinceLastUpdate = 0 return Task.cont if (type == "chat"): self.iterator = data self.datagram = PyDatagram() self.datagram.addString("chat") self.text = self.iterator.getString() self.datagram.addString(self.text) for k in self.PlayerList: serverClass.cWriter.send(self.datagram, k.connectionID) def updateData(self, connection, datagram, serverClass): self.iterator = PyDatagramIterator(datagram) self.type = self.iterator.getString() if (self.type == "positions"): for k in self.PlayerList: if (k.connectionID == connection): k.currentPos['x'] = self.iterator.getFloat64() k.currentPos['y'] = self.iterator.getFloat64() k.currentPos['z'] = self.iterator.getFloat64() k.currentPos['h'] = self.iterator.getFloat64() k.currentPos['p'] = self.iterator.getFloat64() k.currentPos['r'] = self.iterator.getFloat64() if (self.type == "chat"): self.updatePlayers(serverClass, self.iterator, "chat") def sendInitialInfo(self, i, server): self.con = self.PlayerList[i].connectionID self.datagram = PyDatagram() self.datagram.addString("init") self.datagram.addUint8(self.active) self.datagram.addFloat64(i) for k in self.PlayerList: self.datagram.addString(k.username) self.datagram.addFloat64(k.currentPos['x']) self.datagram.addFloat64(k.currentPos['y']) self.datagram.addFloat64(k.currentPos['z']) server.cWriter.send(self.datagram, self.con)