def do(self): # We need the original fleet fleet1 = Object(self.oid) # We need the other fleet if self.fleet != -1: fleet2 = Object(self.fleet) message = Message() message.slot = -1 message.bid = fleet1.owner message.subject = "Merge Fleet failed." # Check the other object is actually a fleet... if self.fleet == -1 or fleet2.type.endswith('Fleet'): # Send message about the owner not matching... message.body = """\ The merge failed (of %s) because the merge target wasn't a fleet! The merge order has been removed. """ % (fleet1.name) message.insert() self.remove() return # Check they have the same owner :) if fleet1.owner != fleet2.owner: # Send message about the owner not matching... message.body = """\ The merge between %s and %s failed because you didn't own both fleets. The merge order has been removed. """ % (fleet1.name, fleet2.name) message.insert() self.remove() return # Check they are at the same position if (fleet1.posx, fleet1.posy, fleet1.posz) != (fleet2.posx, fleet2.posy, fleet2.posz): return # Merge the fleets for type, number in fleet1.ships.items(): if fleet2.ships.has_key(type): fleet2.ships[type] += number else: fleet2.ships[type] = number del fleet1.ships[type] fleet1.save() # Remove the other fleet fleet2.remove() self.remove()
def do(self, action): # We are going to have to modify the object so lets load it obj = Object(self.oid) # Work out what the maximum speed of this object is speed = obj.speed() xd, yd, zd = self.pos[0] - obj.posx, self.pos[1] - obj.posy, self.pos[ 2] - obj.posz if action == 'finalise': # Make sure that we haven't missed the object if (obj.velx, obj.vely, obj.velz) != (0, 0, 0): if xd * obj.velx < 0 or yd * obj.vely < 0 or zd * obj.velz < 0: print "Object %i (%s) has overshot destination %s to (%i, %i, %i)" % \ (obj.id, obj.name, self.pos, obj.velx, obj.vely, obj.velz) obj.posx, obj.posy, obj.posz = self.pos ReparentOne(obj) obj.save() # Have we reached our destination? if self.pos == (obj.posx, obj.posy, obj.posz): print "Object %i (%s) has arrived at destination (%i, %i, %i)" % \ (obj.id, obj.name, obj.posx, obj.posy, obj.posz) obj.velx = obj.vely = obj.velz = 0 obj.save() self.remove() # Send a message to the owner that the object has arrived... message = Message() message.bid = obj.owner message.slot = -1 message.subject = "%s arrived" % obj.name message.body = """%s has arrive at it's destination (%i, %i, %i).""" % \ (obj.name, obj.posx, obj.posy, obj.posz) message.insert() return elif action == 'prepare': distance = math.sqrt(xd**2 + yd**2 + zd**2) if distance == 0: return # Set the velocity so we are moving towards self.pos at speed velx = away(closest(speed * xd / distance, xd)) vely = away(closest(speed * yd / distance, yd)) velz = away(closest(speed * zd / distance, zd)) if (velx, vely, velz) != (obj.velx, obj.vely, obj.velz): print "Setting velocity of object %i to %r currently at %r destination %r" % ( obj.id, (velx, vely, velz), (obj.posx, obj.posy, obj.posz), self.pos) obj.velx, obj.vely, obj.velz = velx, vely, velz obj.save() return else: raise Exception("Unknown action!")
def do(self, action): # We are going to have to modify the object so lets load it obj = Object(self.oid) # Work out what the maximum speed of this object is speed = obj.speed() xd, yd, zd = self.pos[0] - obj.posx, self.pos[1] - obj.posy, self.pos[2] - obj.posz if action == 'finalise': # Make sure that we haven't missed the object if (obj.velx, obj.vely, obj.velz) != (0,0,0): if xd*obj.velx < 0 or yd*obj.vely < 0 or zd*obj.velz < 0: print "Object %i (%s) has overshot destination %s to (%i, %i, %i)" % \ (obj.id, obj.name, self.pos, obj.velx, obj.vely, obj.velz) obj.posx, obj.posy, obj.posz = self.pos ReparentOne(obj) obj.save() # Have we reached our destination? if self.pos == (obj.posx, obj.posy, obj.posz): print "Object %i (%s) has arrived at destination (%i, %i, %i)" % \ (obj.id, obj.name, obj.posx, obj.posy, obj.posz) obj.velx = obj.vely = obj.velz = 0 obj.save() self.remove() # Send a message to the owner that the object has arrived... message = Message() message.bid = obj.owner message.slot = -1 message.subject = "%s arrived" % obj.name message.body = """%s has arrive at it's destination (%i, %i, %i).""" % \ (obj.name, obj.posx, obj.posy, obj.posz) message.insert() return elif action == 'prepare': distance = math.sqrt(xd**2 + yd**2 + zd**2) if distance == 0: return # Set the velocity so we are moving towards self.pos at speed velx = away(closest(speed * xd/distance, xd)) vely = away(closest(speed * yd/distance, yd)) velz = away(closest(speed * zd/distance, zd)) if (velx, vely, velz) != (obj.velx, obj.vely, obj.velz): print "Setting velocity of object %i to %r currently at %r destination %r" % (obj.id, (velx, vely, velz), (obj.posx, obj.posy, obj.posz), self.pos) obj.velx, obj.vely, obj.velz = velx, vely, velz obj.save() return else: raise Exception("Unknown action!")
def do(self): builder = Object(self.oid) if not hasattr(builder, "owner"): print "Could not do a build order because it was on an unownable object." self.remove() if self.turns() > 1: # Add another year to worked... self.worked += 1 print "Worked %s, %s left until built." % (self.worked, self.turns()) self.save() return # Build new fleet object fleet = Object(type='tp.server.rules.minisec.objects.Fleet') # Type Fleet fleet.parent = builder.id fleet.posx = builder.posx fleet.posy = builder.posy fleet.posz = builder.posz fleet.size = 1 fleet.owner = builder.owner fleet.ships = self.ships fleet.insert() fleet.name = self.name fleet.save() message = Message() message.slot = -1 message.bid = builder.owner message.subject = "Fleet built" message.body = """\ A new fleet (%s) has been built and is orbiting %s. It consists of: """ % (fleet.name, builder.name) for type, number in fleet.ships.items(): if number > 1: message.body += "%s %ss" % (number, Fleet.ship_types[type]) else: message.body += "%s %s" % (number, Fleet.ship_types[type]) message.insert() self.remove()
def player(self, username, password, email='Unknown', comment=''): """ --player <game> <username> <password> [<email>, <comment>] Create a player for this game. The default function creates a new user, a board for the user and adds a welcome message. It returns the newly created user object. """ dbconn.use(self.game) trans = dbconn.begin() try: user = User() user.username = username user.password = password user.email = email user.comment = comment user.save() board = Board() board.id = user.id board.name = "Private message board for %s" % username board.desc = """\ This board is used so that stuff you own (such as fleets and planets) \ can inform you of what is happening in the universe. \ """ board.insert() # Add the first message message = Message() message.bid = user.id message.slot = -1 message.subject = "Welcome to the Universe!" message.body = """\ Welcome, %s, to the python Thousand Parsec server. Hope you have fun! \ \ This game is currently playing version %s of %s. """ % (username, self.version, self.name) message.insert() trans.commit() return user except: trans.rollback() raise
def do(self): builder = Object(self.oid) # FIXME: Check that this is a planet # FIXME: Check that this planet has the headquarter resource if False: print "Could not do a build order because it was on a planet headquaters. (This should not happen.)" self.remove() # Check that there are enough components to build this ship... # Build new fleet object fleet = Object(type='tp.server.rules.minisec.objects.Fleet') # Check if there is a design which matches this amount of components. If not create it... # Type Fleet fleet.parent = builder.id fleet.posx = builder.posx fleet.posy = builder.posy fleet.posz = builder.posz fleet.size = 1 fleet.owner = builder.owner fleet.ships = self.ships fleet.insert() fleet.name = self.name fleet.save() message = Message() message.slot = -1 message.bid = builder.owner message.subject = "Fleet built" message.body = """\ A new ship (%s) has been built and is orbiting %s. """ % (fleet.name, builder.name) message.insert() self.remove()
def do(top): owners = {} def h(obj, owners=owners): if hasattr(obj, "owner") and obj.owner > 0: owners[obj.owner] = None WalkUniverse(top, "after", h) if len(owners.keys()) == 1: winner = owners.keys()[0] print "It seems that %i is the only player left so must be the winner!" % winner m = Message() m.bid = winner m.slot = -1 m.subject = "Yay!" m.body = """\ <h1>You are the undisputed ruler of the universe!</h1> You have crushed all your enemies and now only you remain. It's a lonely place at the top but you'll live.""" m.insert() else: print "The following players still have objects in the universe", owners.keys( )
def do(top): owners = {} def h(obj, owners=owners): if hasattr(obj, "owner") and obj.owner > 0: owners[obj.owner] = None WalkUniverse(top, "after", h) if len(owners.keys()) == 1: winner = owners.keys()[0] print "It seems that %i is the only player left so must be the winner!" % winner m = Message() m.bid = winner m.slot = -1 m.subject = "Yay!" m.body = """\ <h1>You are the undisputed ruler of the universe!</h1> You have crushed all your enemies and now only you remain. It's a lonely place at the top but you'll live.""" m.insert() else: print "The following players still have objects in the universe", owners.keys()
def do(self): # We are going to have to modify the object so lets load it fleet = Object(self.oid) planet = Object(self.target) # Do checks :) message = Message() message.slot = -1 message.bid = fleet.owner message.subject = "Colonise failed." if planet.posx != fleet.posx or planet.posy != fleet.posy or planet.posz != planet.posz: print "Colonise of Planet %s (%s) (by %s-%s) failed. The fleet was not orbiting the planet!" % (planet.id, planet.name, fleet.id, fleet.name) message.body = """\ Colonise of %s <b>failed</b> because %s was not orbiting the planet.<br> The order has been removed.""" % (planet.name, fleet.name) message.insert() self.remove() return if not planet.type.endswith('Planet'): print "Colonise of Planet %s (%s) (by %s-%s) failed. %s not a planet!" % (planet.id, planet.name, fleet.id, fleet.name, planet.name) message.body = """\ Colonise of %s <b>failed</b> because %s is not a Planet!<br> The order has been removed.""" % (planet.name, planet.name) message.insert() self.remove() return if not planet.owner in (-1, 0): print "Colonise of Planet %s (%s) (by %s-%s) failed. %s is owned by %s." % (planet.id, planet.name, fleet.id, fleet.name, planet.name, planet.owner) message.body = """\ Colonise of %s <b>failed</b> because %s is already colonised by %s!<br> You can decolonised the planet by bombing the bejesus out of it. The order has been removed.""" % (planet.name, planet.name, planet.owner) message.insert() self.remove() return if not fleet.ships.has_key(Frigate) or fleet.ships[Frigate] < 1: print "Colonise of Planet %s (%s) (by %s-%s) failed. %s has no frigates." % (planet.id, planet.name, fleet.id, fleet.name, fleet.name) message.body = """\ Colonise of %s <b>failed</b> because %s does not have any Frigates!<br> The order has been removed.""" % (planet.name, fleet.name) message.insert() self.remove() return print "Colonise of Planet %s (%s) (by %s-%s) succeeded." % (planet.id, planet.name, fleet.id, fleet.name) message.subject = "Colonise success." message.body = """\ Colonisation of %s <b>succeded</b>.""" % (planet.name,) message.insert() planet.owner = fleet.owner fleet.ships[Frigate] -= 1 planet.save() fleet.save() self.remove()