Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- nucescape 1.0
- =============
- A little RPG-themed IRC multiplayer game.
- """
- import cPickle
- from twisted.internet import reactor
- import botbase
- import maps
- # Configuration
- HOST = "irc.freenode.net"
- PORT = 6667
- NICK = "nucbot-rpg"
- CHANNELS = ["##nucular"]
- SHOUTCHANS = ["##nucular"]
- MAP = maps.map1
- PREFIX = "<"
- OWNERMASK = "*!*@unaffiliated/nucular"
- LOGLEVEL = botbase.DEBUG
- def fancyList(lst):
- if len(lst) == 0:
- return "None"
- elif len(lst) == 1:
- return lst[0]
- elif len(lst) == 2:
- return "%s and %s" % (lst[0], lst[1])
- else:
- msg = ", ".join(lst[:-1])
- msg += " and " + lst[-1]
- return msg
- class Character(object):
- def __init__(self, bot, nick, user, god=False):
- self.bot = bot
- self.nick = nick
- self.user = user
- self.god = god
- self.inventory = []
- def msg(self, msg):
- self.bot.msg(self.nick, msg)
- def command(self, cmd):
- hm = botbase.getHostmask(self.user)
- if cmd == "quit":
- self.msg("You leave the game.")
- del self.bot.activeCharacters[self.user]
- def introduction(self):
- self.msg("Welcome to the world of %s!" % NICK)
- self.msg("Currently, you can only quit and join :<")
- class RPGBot(botbase.BotBase):
- """This is our main bot class."""
- def __init__(self, factory, gamemap):
- botbase.BotBase.__init__(self, factory)
- # This dict maps hostmasks to Character objects.
- # It can be pickled to store the game state.
- self.characters = {}
- # This one maps actual hosts to Character objects.
- self.activeCharacters = {}
- self.gamemap = gamemap
- def channelMsg(self, user, nick, channel, msg):
- """Will be called on receiving a channel message."""
- botbase.BotBase.channelMsg(self, user, nick, channel, msg)
- if msg.startswith(PREFIX):
- self.command(channel, user, nick, msg[1:])
- def privateMsg(self, user, nick, msg):
- botbase.BotBase.privateMsg(self, user, nick, msg)
- hm = botbase.getHostmask(user)
- if hm in self.characters:
- self.characters[hm].command(msg)
- else:
- if msg.startswith(PREFIX):
- self.command(nick, user, nick, msg[1:])
- def saveState(self):
- pass
- def command(self, target, user, nick, cmd):
- hm = botbase.getHostmask(user)
- self.logger(botbase.INFO, "Command! "+cmd)
- if cmd == "panic" and self.isGod(user):
- self.saveState()
- for i in CHANNELS:
- self.msg(i, "Panic quit requested by %s!" % nick)
- for i in self.activeCharacters:
- self.msg(i.nick, "Panic quit requested by %s! Your game was saved." % nick)
- self.factory.reconnect = False
- connector.disconnect()
- elif cmd == "join":
- if user in self.activeCharacters:
- self.msg(user, "You are already active.")
- elif hm in self.characters:
- self.activeCharacters[user] = self.characters[hm]
- self.msg(user, "Welcome back!")
- else:
- newchar = Character(self, nick, user, self.isGod(user))
- self.activeCharacters[user] = newchar
- self.characters[hm] = newchar
- newchar.introduction()
- print self.characters
- elif cmd == "active":
- msg = "These users are currently playing: "
- msg += fancyList([self.activeCharacters[i].nick for i in self.activeCharacters])
- self.msg(target, msg)
- elif cmd.startswith("help"):
- if cmd.endswith("panic"):
- self.msg(target, "panic: Saves the game and halts the bot. Only for gods.")
- elif cmd.endswith("join"):
- self.msg(target, "join: Join the game. Creates a new character or loads the last one.")
- elif cmd.endswith("active"):
- self.msg(target, "active: Shows a list of active users.")
- else:
- self.msg(target, "These commands are available here: panic, join, active, help.")
- def isGod(self, user):
- hm = botbase.getHostmask(user)
- if hm in self.characters:
- return self.characters[hm].god
- else:
- return hm == OWNERMASK
- # Let there be bot!
- if __name__ == "__main__":
- logger = botbase.Logger(LOGLEVEL)
- f = botbase.BotBaseFactory(RPGBot, NICK, CHANNELS, [MAP], logger)
- connector = reactor.connectTCP(HOST, PORT, f)
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement