# Path: chromabot/commands.py
# class Context(object):
#     def __init__(self, player, config, session, comment, reddit):
#         self.player = player    # a DB object
#         self.config = config
#         self.session = session
#         self.comment = comment  # a praw object
#         self.reddit = reddit    # root praw object
# 
#     @failable
#     def reply(self, reply, pm=True):
#         verbose = self.config.bot.get("verbose_logging")
#         was_comment = getattr(self.comment, 'was_comment', True)
#         header = ""
#         if was_comment and pm:
#             header = ("(In response to [this comment](%s))" %
#                       self.comment.permalink)
#         else:  # It wasn't a comment, or pm = False
#             if verbose:
#                 logging.info("Replying: %s" % reply)
#             return self.comment.reply(reply)
# 
#         full_reply = "%s\n\n%s" % (header, reply)
#         if verbose:
#             logging.info("PMing: %s" % full_reply)
#         self.reddit.send_message(self.player.name, "Chromabot reply",
#                                  full_reply)
# 
#     @failable
#     def submit(self, srname, title, text):
#         return self.reddit.submit(srname, title=title, text=text)
# 
#     def team_name(self):
#         return num_to_team(self.player.team, self.config)
# 
# class InvadeCommand(Command):
#     def __init__(self, tokens):
#         self.where = tokens["where"].lower()
# 
#     @staticmethod
#     @failable
#     def post_invasion(title, battle, reddit):
#         text = ("Negotiations have broken down, and the trumpets of "
#                 "war have sounded.  Even now, civilians are being "
#                 "evacuated and the able-bodied drafted.  The conflict "
#                 "will soon be upon you.\n\n"
#                 "Gather your forces while you can, for your enemy "
#                 "shall arrive at %s") % battle.begins_str()
#         submitted = reddit.submit(battle.region.srname,
#                                    title=title,
#                                    text=text)
#         return submitted
# 
#     def execute(self, context):
#         dest = Region.get_region(self.where, context)
#         if dest:
#             now = time.mktime(time.localtime())
#             begins = now + context.config["game"]["battle_delay"]
#             battle = None
# 
#             if dest.capital is not None:
#                 invade = context.config['game']['capital_invasion']
#                 if invade == 'none':
#                     context.reply("You cannot invade the enemy capital")
#                     return
# 
#             try:
#                 battle = dest.invade(context.player, begins)
#                 if "battle_lockout" in context.config["game"]:
#                     battle.lockout = context.config["game"]["battle_lockout"]
#                     context.session.commit()
#             except db.RankException:
#                 context.reply("You don't have the authority "
#                               "to invade a region!")
#             except db.TeamException:
#                 context.reply("You can't invade %s, you already own it!" %
#                               dest.markdown())
#             except db.NonAdjacentException:
#                 context.reply("%s is not next to any territory you control" %
#                               dest.markdown())
#             except db.InProgressException as ipe:
#                 context.reply("%s is %s being invaded!" % (dest.markdown(),
#                               ipe.other.markdown("already")))
#             except db.TimingException as te:
#                 context.reply(("%s is too fortified to be attacked.  "
#                               "These fortifications will break down by %s") %
#                               (dest.markdown(), timestr(te.expected)))
# 
#             if battle:
#                 context.reply("**Confirmed**  Battle will begin at %s" %
#                               battle.begins_str())
#                 title = ("[Invasion] The %s armies march on %s!" %
#                          (context.team_name(), dest.name))
#                 submitted = InvadeCommand.post_invasion(title, battle,
#                                                         context.reddit)
#                 if submitted:
#                     battle.submission_id = submitted.name
#                     context.session.commit()
#                 else:
#                     logging.warn("Couldn't submit invasion thread")
#                     context.session.rollback()


import code
import logging
import readline
import sys
from config import Config
from db import *
from utils import *
from chromabot.commands import Context, InvadeCommand
    return query(cls, **kw).all()


def all_as_dict(cls):
    result = {}
    for item in all(cls):
        result[item.name] = item
    return result


def battle_adopt(battle, postid):
    battle.ends = battle.begins + 10800
    battle.display_ends = battle.ends
    battle.submission_id="t3_%s" % postid
    sess.commit()


def cancel_battle(battle):
    global reddit

    post = reddit.get_submission(
        submission_id=name_to_id(battle.submission_id))
    post.edit("*This battle has been canceled*")
    sess.delete(battle)
    sess.commit()


def context(player=None, comment=None):
    if not player:
        player = by_id(User, 1)

# Path: chromabot/commands.py
# class Context(object):
#     def __init__(self, player, config, session, comment, reddit):
#         self.player = player    # a DB object
#         self.config = config
#         self.session = session
#         self.comment = comment  # a praw object
#         self.reddit = reddit    # root praw object
# 
#     @failable
#     def reply(self, reply, pm=True):
#         verbose = self.config.bot.get("verbose_logging")
#         was_comment = getattr(self.comment, 'was_comment', True)
#         header = ""
#         if was_comment and pm:
#             header = ("(In response to [this comment](%s))" %
#                       self.comment.permalink)
#         else:  # It wasn't a comment, or pm = False
#             if verbose:
#                 logging.info("Replying: %s" % reply)
#             return self.comment.reply(reply)
# 
#         full_reply = "%s\n\n%s" % (header, reply)
#         if verbose:
#             logging.info("PMing: %s" % full_reply)
#         self.reddit.send_message(self.player.name, "Chromabot reply",
#                                  full_reply)
# 
#     @failable
#     def submit(self, srname, title, text):
#         return self.reddit.submit(srname, title=title, text=text)
# 
#     def team_name(self):
#         return num_to_team(self.player.team, self.config)
# 
# class InvadeCommand(Command):
#     def __init__(self, tokens):
#         self.where = tokens["where"].lower()
# 
#     @staticmethod
#     @failable
#     def post_invasion(title, battle, reddit):
#         text = ("Negotiations have broken down, and the trumpets of "
#                 "war have sounded.  Even now, civilians are being "
#                 "evacuated and the able-bodied drafted.  The conflict "
#                 "will soon be upon you.\n\n"
#                 "Gather your forces while you can, for your enemy "
#                 "shall arrive at %s") % battle.begins_str()
#         submitted = reddit.submit(battle.region.srname,
#                                    title=title,
#                                    text=text)
#         return submitted
# 
#     def execute(self, context):
#         dest = Region.get_region(self.where, context)
#         if dest:
#             now = time.mktime(time.localtime())
#             begins = now + context.config["game"]["battle_delay"]
#             battle = None
# 
#             if dest.capital is not None:
#                 invade = context.config['game']['capital_invasion']
#                 if invade == 'none':
#                     context.reply("You cannot invade the enemy capital")
#                     return
# 
#             try:
#                 battle = dest.invade(context.player, begins)
#                 if "battle_lockout" in context.config["game"]:
#                     battle.lockout = context.config["game"]["battle_lockout"]
#                     context.session.commit()
#             except db.RankException:
#                 context.reply("You don't have the authority "
#                               "to invade a region!")
#             except db.TeamException:
#                 context.reply("You can't invade %s, you already own it!" %
#                               dest.markdown())
#             except db.NonAdjacentException:
#                 context.reply("%s is not next to any territory you control" %
#                               dest.markdown())
#             except db.InProgressException as ipe:
#                 context.reply("%s is %s being invaded!" % (dest.markdown(),
#                               ipe.other.markdown("already")))
#             except db.TimingException as te:
#                 context.reply(("%s is too fortified to be attacked.  "
#                               "These fortifications will break down by %s") %
#                               (dest.markdown(), timestr(te.expected)))
# 
#             if battle:
#                 context.reply("**Confirmed**  Battle will begin at %s" %
#                               battle.begins_str())
#                 title = ("[Invasion] The %s armies march on %s!" %
#                          (context.team_name(), dest.name))
#                 submitted = InvadeCommand.post_invasion(title, battle,
#                                                         context.reddit)
#                 if submitted:
#                     battle.submission_id = submitted.name
#                     context.session.commit()
#                 else:
#                     logging.warn("Couldn't submit invasion thread")
#                     context.session.rollback()


# Path: bin/cli.py
import code
import logging
import readline
import sys
from config import Config
from db import *
from utils import *
from chromabot.commands import Context, InvadeCommand

    return query(cls, **kw).all()


def all_as_dict(cls):
    result = {}
    for item in all(cls):
        result[item.name] = item
    return result


def battle_adopt(battle, postid):
    battle.ends = battle.begins + 10800
    battle.display_ends = battle.ends
    battle.submission_id="t3_%s" % postid
    sess.commit()


def cancel_battle(battle):
    global reddit

    post = reddit.get_submission(
        submission_id=name_to_id(battle.submission_id))
    post.edit("*This battle has been canceled*")
    sess.delete(battle)
    sess.commit()


def context(player=None, comment=None):
    if not player:
        player = by_id(User, 1)

    return Context(player, config, sess, comment, reddit)