Exemple #1
0
    def on_ask_fight(self, eid):
        logging.debug("On ask fight from {}".format(self.fbid))
        if self.fbid == eid:
            self.emit('error', "You can't fight against yourself...")
            return True

        with app.app_context():
            if User.query.filter_by(fb_id=eid).first() is None:
                self.emit('error', "The user asked is not in the database. Ask him on Facebook ?")
                return True

        if self.in_a_fight:
            self.emit('error', "You're already in a fight.")
            return True

        if self.is_in_a_fight(eid):
            self.emit('error', "Your opponent is already in a fight")
            return True

        info_dict = {}

        monster_1 = Monster.query.filter_by(belong_to=self.fbid).first()
        monster_2 = Monster.query.filter_by(belong_to=eid).first()

        first_player = self.fbid if monster_1.speed >= monster_2.speed else eid

        with app.app_context():
            info_dict = {
                'current_player': first_player,
                self.fbid: monster_1.get_stats(),
                eid: monster_2.get_stats()
            }

        self.r.add_new_fight(self.fbid, eid, info_dict)

        self.emit("ok_fight")

        s = self.get_socket_with_fbid(eid)
        if s:
            s.emit("new_fight", self.fbid)

        return True
Exemple #2
0
    def add_new_fight(self, fbid, eid, info_dict):
        self.r.set("f:c:{fbid}".format(fbid=fbid), eid)
        self.r.set("f:c:{eid}".format(eid=eid), fbid)

        with app.app_context():
            f = Fight(fbid, eid, None)
            db.session.add(f)
            db.session.commit()

        fid = self._get_formated_id(fbid, eid)

        self.r.set("f:c:b:{fid}".format(fid=fid), json.dumps(info_dict))
Exemple #3
0
    def on_get_monsters(self):
        logging.debug("On get monsters from {}".format(self.fbid))
        if not self.fbid:
            self.emit("error", "Hello has not been sent, I can't fetch your monsters book :(")
            return True

        with app.app_context():
            user = User.query.filter_by(fb_id=self.fbid).first()
            monsters = map(lambda e: e.get_stats(complete=True), user.monsters) or []
        if user is None:
            self.emit("error", "You're not in our database. This is a problem. Please contact us.")
            return False

        self.emit("monsters", json.dumps(monsters))
Exemple #4
0
    def end_fight(self, fbid, result, eid=None):
        if eid is None:
            eid = self.r.get("f:c:{fbid}".format(fbid))

        fid = self._get_formated_id(fbid, eid)

        self.r.delete("f:c:{fbid}".format(fbid=fbid))
        self.r.delete("f:c:b:{id}".format(id=fid))
        self.r.delete("f:c:{eid}".format(eid=eid))

        with app.app_context():
            f = Fight.get_last_fight_of(fbid, eid)
            if f and f.result is None:
                f.set_winner(fbid if result else eid)
                db.session.commit()
Exemple #5
0
    def on_hello(self, fbid):
        logging.debug("Receive hello from {}".format(fbid))

        if fbid is None:
            self.emit("error", "I need a Facebook ID to work !")
            return True

        self.fbid = fbid
        with app.app_context():
            u = User.query.filter_by(fb_id=self.fbid).first()
            if u is None:
                u = User(self.fbid)
                db.session.add(u)
                db.session.commit()
        self.eid = self.in_a_fight
        self.emit("welcome")
        return True
Exemple #6
0
    def get_last_history(self, fbid):

        with app.app_context():
            u = User.query.filter_by(fb_id=fbid).first()
            if u is not None:
                return u.fights.order_by(desc(Fight.id)).limit(10).all()