def fight(self):
     t = MUDTime.getTime()
     if t < self.nextattacktime:
         return
     if not self.room.hasPlayerRef(self.opponent):
         self.memory = self.opponent
         self.isfighting = False
         self.opponent = None
         self.room.sendAll(self.name + " calms down.\r\n")
     else:
         p = self.opponent
         if int(random.random()*100) < self.getAcc():
             self.room.sendAll(yellow + self.name + " swings at "+p.name+" but misses!" + reset+nl)
             self.nextattacktime = t + self.swingtime
             return
         if int(random.random()*100) < p.getDod():
             self.room.sendAll(yellow +p.name+" dodges "+self.name+"'s attack!" + reset+nl)
             self.nextattacktime = t + self.swingtime
             return
         dam = int(random.random()*3+1)
         dam = dam + self.getStrDam() - p.getDamAbs()
         if dam < 0:
             dam = 0
         self.room.sendAll(yellow+bold+self.name + " pounds "+p.name + " real hard!\r\n" +reset)
         p.hp = p.hp - dam
         if p.hp <= 0:
             p.die()
             self.room.sendAll("You shiver as you hear "+p.name+"'s death cry.\r\n")
             self.room.sendAll(p.name + " dissolves into nothing as the gods reclaim the soul...\r\n\r\n")
             self.isfighting = False
             self.opponent  = None
         else:
             p.sendString(yellow+bold+"Your HP is " + str(p.hp) + "\r\n"+reset)
     self.nextattacktime = t + self.swingtime
 def fight(self):
     t = MUDTime.getTime()
     if t < self.nextattacktime:
         return
     m = self.opponent
     hitchance = self.getAcc()
     if hitchance < int(random.random()*100):
         self.room.sendAll(yellow + nl + self.name + " swings at "+m.name+" but misses!" + reset+nl)
         self.nextattacktime = t + self.swingtime
         return
     if m.getDod() > int(random.random()*100):
         self.room.sendAll(yellow + nl + m.name+" dodges " + self.name+"'s attack!"+ reset+nl)
         self.nextattacktime = t + self.swingtime
         return
     if self.weapon:
         dam = self.weapon.min + int((self.weapon.max-self.weapon.min)*random.random()+1)
     else:
         dam = int(random.random()*3+1)
     dam = dam + self.getStrDam() - m.getDamAbs()
     if dam < 0:
         dam = 0
     m.hp = m.hp - dam
     if m.hp <= 0:
         self.room.sendAll(yellow + self.name + " delivers the death blow to "+m.name + "!"+nl )
         self.room.sendAll("You shiver as you hear "+m.name+"'s death cry."+nl)
         self.room.sendAll(m.name + " dissolves into nothing..."+nl+reset)
         m.die()
         self.opponent = None
         self.isfighting = False
     else:
         self.room.sendAll(yellow + self.name + " pounds "+m.name + " real hard!"+nl+reset )
         percent = int(float((m.hp*100))/m.getMaxHP())
         self.sendString(m.name + ": "+str(percent) + "%\r\n")
     self.nextattacktime = t + self.swingtime
 def trySpawn(self):
     t = MUDTime.getTime()
     if t >= self.nextspawn:
         self.hp = self.coreSet[MAXHP]
         self.isdead = False
         self.room = rdb.getByID(self.spawnroomid)
         self.roomid = self.spawnroomid
         self.room.addMob(self)
         self.room.sendAll(self.name + " suddenly appears, as if spawned by some greater power!\r\n")       
 def act(self):
     if self.isfighting:
         self.fight()
         return
     t = MUDTime.getTime()
     if t < self.nextact:
         return
     else:
         aggro = self.wanderlust + self.aggro
         emote = aggro + self.emote
         die = int(random.random()*100+1)
         if die < self.wanderlust:
             self.actWander()
         elif die < aggro:
             self.actAggro()
         elif die < emote:
             self.actEmote()
     self.nextact = t + self.acttime
pdb = PlayerDatabase()
pdb.load()
Handlers.pdb = pdb

mdb = MobDatabase()
mdb.load()

lm = ListeningManager()
cm = ConnectionManager()
lm.setConnectionManager(cm)

playerRegenTime = 60000
nextPlayerRegen = 10000

while True:
    lm.listen()
    cm.manage()
    pdb.fightRound()
    mdb.spawnRound()
    mdb.actRound()
    t = MUDTime.getTime()
    if t >= nextPlayerRegen:
        pdb.regen()
        nextPlayerRegen = t+playerRegenTime
    time.sleep(0.02)




 def die(self):
     self.isdead = True
     self.isfighting = False
     self.opponent = None
     self.room.removeMob(self)
     self.nextspawn = MUDTime.getTime()+self.spawntime
 def handle(self, commandstring):
     if "r" != commandstring:
         self.p.lastcommand = commandstring
     temp = commandstring
     cwlist = commandstring.split()
     command = cwlist[0] #commandwordlist, of course
     if "r" == command:
         if self.p.lastcommand:
             self.handle(self.p.lastcommand)
         else:
             self.p.sendString(nl+"There is no command to repeat!"+nl)
         return
     if "whoami" == command:
         self.p.sendString("You are "+self.p.name+"\r\n")
         return
     if "time" == command:
         self.p.sendString("The time is " + str(MUDTime.getTime())+"\r\n")
         return
     if "quit" == command:
         if self.p.isfighting:
             self.p.sendString(dnl+"You can't quit in the middle of a fight!"+dnl)
         self.p.conn.sendImmediate("You exit the realm... \r\n")
         self.p.room.removePlayer(self.p)
         self.p.room.sendAll(self.p.name + " waves his hand and disappears into thin air.\r\n")
         self.p.conn.close()
         self.p.save()
         self.p.loggedin = False
         self.p.conn.handler = 0
         return
     if "t" == command:
         self.p.room.sendAll(self.p.name + " says: "+temp[2:])
         return
     if "help" == command:
         self.p.sendString("Available commands:\r\n"+
                           "exits, emote [mess], whoami,"+
                           " time, quit, t [message], help,"+
                           " look, e, w, s, n"+
                           "drop, use [item], rem/remove armor/a/weapon/w/item\r\n")
         return
     if "look" == command or "l" == command:
         if len(cwlist) == 1:
             self.p.sendString(self.p.room.getLook())
         else:
             arg = cwlist[1]
             if self.p.room.hasItemName(arg):
                 entity = self.p.room.getStored()
                 self.p.sendString(nl+entity.name+":" + nl + entity.getLook() + nl)
             elif self.p.room.hasMobName(arg):
                 entity = self.p.room.getStored()
                 self.p.sendString(nl+entity.name+":" + nl + entity.getLook() + nl)
             elif self.p.room.hasPlayerName(arg):
                 entity = self.p.room.getStored()
                 self.p.sendString(nl+entity.name+":" + nl + entity.getLook() + nl)
             else:
                 self.p.sendString(nl+"You don't see that here. Hmm..."+nl)
         return
     if "flee" == command:
         if self.p.isfighting:
             if random.random()*100 < self.p.getDod():
                 self.p.sendString(dnl+"You manage to escape!"+dnl)
             else:
                 self.p.sendString(dnl+"Your attempt to flee did not succeed!"+dnl)
                 return
         exitlist = []
         for k in self.p.room.exits:
             if self.p.room.exits[k]:
                 exitlist.append(k)
         direction = exitlist[int(random.random()*len(exitlist))]
         newroomid = self.p.room.exits[direction]
         newroom = rdb.getByID(newroomid)
         exit_str = {"e":"east","w":"west","n":"north","s":"south"}
         self.p.room.removePlayer(self.p)
         self.p.room.sendAll(yellow + self.p.name + "flees to the " + exit_str[direction]+nl+reset)
         newroom.sendAll(yellow + self.p.name + " enters hastily."+nl+reset)
         self.p.sendString(newroom.getLook())
         newroom.addPlayer(self.p)
         self.p.room = newroom
         self.p.roomid = newroom.id
         self.p.isfighting = False
         self.p.opponent = None
         return
     if "emote" == command:
         self.p.room.sendAll(self.p.name + temp[5:] +"\r\n")
         return
     if "w" == command or "e" == command or "n" == command or "s" == command:
         if self.p.isfighting:
             self.p.sendString(dnl+"You can't just walk away from a fight!"+dnl)
             return
         if self.p.room.exits[command]:
             exit_str = {"e":"east","w":"west","n":"north","s":"south"}
             nroom = rdb.getByID(self.p.room.exits[command])
             self.p.room.removePlayer(self.p)
             self.p.room.sendAll(self.p.name + " leaves to the " + exit_str[command]+".\r\n")
             self.p.room = nroom
             self.p.roomid = nroom.id
             self.p.room.sendAll(self.p.name + " enters.\r\n")
             self.p.sendString(self.p.room.getLook())
             nroom.addPlayer(self.p)
         else:
             self.p.sendString("You cannot go that way!\r\n")
         return
     if "exits" == command:
         exit_str = {"e":"East","w":"West","n":"North","s":"South"}
         count = 0
         for k in exit_str:
             if self.p.room.exits[k]:
                 self.p.sendString(exit_str[k]+": ")
                 self.p.sendString(rdb.getByID(self.p.room.exits[k]).name + "\r\n")
                 count = count + 1
         if not count:
             self.p.sendString("There are no exits! AAARGH!!")
         self.p.sendString("\r\n")
         return
     if "save" == command:
         self.p.save()
         self.p.sendString("You have been saved!\r\n")
         return
     if "inv" == command or "i" == command:
         inv = self.p.inventory
         if not len(inv):
             self.p.sendString("\r\nYou have nothing in your inventory.\r\n")
         else:
             s = ""
             for k in inv:
                 s = s + nl + k.name
             s = s+ nl + nl
             self.p.sendString(s)
         return
     if "get" == command or "g" == command:
         temp = temp[4:]
         if not temp:
             self.p.sendString(dnl+"Get what?"+dnl)
             return
         if self.p.room.hasItemName(temp):
             i = self.p.room.getStored()
             self.p.addItem(i)
             self.p.room.removeItem(i)
             self.p.room.sendAll(yellow + nl + self.p.name + " picks up " + i.name+ reset + nl)
         else:
             self.p.sendString(dnl + "There is no "+temp +" here."+dnl)
             print "the room didnt have the item"
         return
     if "drop" == command or "d" == command:
         temp = temp[5:]
         if not temp:
             self.p.sendString(dnl+"Drop what?"+dnl)
             return
         if self.p.hasItemName(temp):
             i = self.p.getStored()
             self.p.dropItem(i)
             self.p.room.addItem(i)
             self.p.room.sendAll(self.p.name + " drops " + i.name + dnl)
         else:
             self.p.sendString(dnl+"You have no such item in your inventory."+dnl)
         return
     if "use" == command or "u" == command:
         temp = temp[4:]
         if not temp:
             self.p.sendString(dnl+"Use what?"+dnl)
             return
         if self.p.hasItemName(temp):
             i = self.p.getStored()
             if i.isWeapon():
                 if self.p.weapon:
                     self.p.sendString(dnl + "You are already using a weapon!"+dnl)
                     return
                 self.p.useWeapon(i)
                 self.p.room.sendAll(nl+self.p.name+" wields " + i.name)
                 return
             elif i.isArmor():
                 if self.p.armor:
                     self.p.sendString(dnl + "You are already wearing armor!"+dnl)
                     return
                 self.p.useArmor(i)
                 self.p.room.sendAll(nl+self.p.name + " wears " + i.name)
                 return
             elif i.isHealing():
                 self.p.sendString("not implemented (yet)\r\n")
                 print "not implemented"
                 
                 return
         else:
             self.p.sendString(dnl+"You have no such item!"+nl)
         return
     if "remove" == command or "rem" == command:
         if len(cwlist) < 2:
             self.p.sendString(nl+"Remove what?"+nl)
             return
         temp = cwlist[1]
         if self.p.armor:
             if self.p.armor.partialMatch(temp):
                 self.p.room.sendAll(yellow + self.p.name + " removes " + self.p.armor.name+nl+reset)
                 self.p.removeArmor()
                 return
         if self.p.weapon:
             if self.p.weapon.partialMatch(temp):
                 self.p.room.sendAll(yellow + self.p.name + " removes " + self.p.weapon.name+reset+nl)
                 self.p.removeWeapon()
                 return
         self.p.sendString(nl+"You are not using any "+ temp+ "!"+nl)
         return
     if "stats" == command:
         self.p.sendString(dnl+"Your attributes are: "+nl)
         self.p.sendString(self.p.getStatsString()+nl)
         self.p.sendString("Your HP is: "+str(self.p.hp)+nl)
         if self.p.weapon:
             self.p.sendString("You wield "+self.p.weapon.name+nl)
         else:
             self.p.sendString("You wield nothing."+nl)
         if self.p.armor:
             self.p.sendString("You wear "+self.p.armor.name+nl)
         else:
             self.p.sendString("You wear no armor."+nl)
         return
     if "a" == command or "attack" == command or "k" == command or "kill" == command:
         if len(cwlist) < 2:
             self.p.sendString("Attack what?\r\n")
             return
         temp = cwlist[1]
         if self.p.room.hasPlayerName(temp):
             self.p.sendString(dnl+"You wouldn't attack another player, now would you?"+dnl)
             return
         if self.p.isfighting:
             if self.p.room.hasMobName(temp):
                 m = self.p.room.getStored()
                 if m == self.p.opponent:
                     self.p.sendString(dnl+"You are already fighting that enemy, stupid!"+dnl)
                 else:
                     self.p.sendString(dnl+"You switch targets!"+dnl)
                     m.notifyAboutAttack(self.p)
                     self.p.opponent = m
             else:
                 self.p.sendString(dnl+"You don't see that here!"+dnl)
         else:
             if self.p.room.hasMobName(temp):
                 m = self.p.room.getStored()
                 m.notifyAboutAttack(self.p)
                 self.p.isfighting = True
                 self.p.opponent = m
                 self.p.sendString(dnl+"You attack!"+dnl)
             else:
                 self.p.sendString("There is nothing here called "+temp+" to attack..."+dnl)
         return
            
     self.p.sendString("That is not a command, now is it..?\r\n")