Esempio n. 1
0
    def post(self):
        """
        login
        """
        print("*************this is join.py*****************")
        data = request.json
        session_id = data['session_id']
        roomID = data['roomID']
        caller = data['caller']
        print(caller)
        username = redis_store.get_item(session_id)

        if not username:
            return ({'Status': 'Please Login'})

        #generate caller ssesion id
        caller = str(caller)
        callerSession_id = hashlib.sha256(caller.encode()).hexdigest()
        print("sdfdf  ", caller + 'callerEvent')
        #check caller is login and send chat request
        if (redis_store.get_item(callerSession_id) != None):

            sse.publish({
                "state": "accept",
                "message": " accept your invite"
            },
                        type=caller + 'callerEvent')
            return render_template("join.html")
Esempio n. 2
0
    def post(self):
        """
        create chatRoom
        """
        print("this is CreateRoom")

        data = request.json
        session_id = data['session_id']
        calleeUsername = data['calleeUsername']

        print(data)

        username = redis_store.get_item(session_id)
        if not username:
            return ({'Status': 'Please Login'})

        username = username.decode('utf-8')

        #generate random Room ID
        chars = string.ascii_uppercase + string.digits
        roomID = ''.join(random.choice(chars) for _ in range(8))

        #generate callee ssesion id
        calleeUsername = str(calleeUsername)
        calleeSession_id = hashlib.sha256(calleeUsername.encode()).hexdigest()

        print(1)
        #save roomid in chat_store
        #value = {'caller':username, 'message':''}
        #chat_store.hmsetall(roomID, value)
        #chat_store.hmset(roomID, 'message', 'hy')

        #check callee is login and send chat request\

        print(calleeUsername)
        print(2)
        if (redis_store.get_item(calleeSession_id) != None):
            print(3)
            sse.publish(
                {
                    "state": "invite",
                    'roomID': roomID,
                    "callerUsername": username
                },
                type=calleeUsername + 'calleeEvent')
            return jsonify({
                'status': 200,
                'message': ' send your invitation to ' + calleeUsername,
                'roomID': roomID
            })

        #sse.publish({"message": "Hello!"}, type='greeting')

        return jsonify({'status': 400})
Esempio n. 3
0
    def post(self):
        """
        start game
        """

        # get info from user
        data = request.json
        hero1 = data['hero1']
        hero2 = data['hero2']
        hero3 = data['hero3']
        session_id = data['session_id']

        hero1 = str(hero1)
        hero2 = str(hero2)
        hero3 = str(hero3)
        username = redis_store.get_item(session_id)
        if not username:
            return ({'Status': 'Please Login'})
        username = username.decode('utf-8')

        active_hero = Player_Hero.start_game(username, hero1, hero2, hero3)
        if active_hero != None:
            return active_hero
        return ({'Status': 'Try Again'})
        '''
Esempio n. 4
0
    def get(self, session_id):

        username = redis_store.get_item(session_id)
        if not username:
            return ({'Status': 'please login'})
        username = username.decode('utf-8')

        coin = User.get_coin(username)
        if coin:
            return ({"user's coin": int(coin), 'session_id': session_id}), 200
        return ({'Status': 'Not Found'})
Esempio n. 5
0
    def get(self, session_id):
        """
        get active hero
        """
        username = redis_store.get_item(session_id)
        if not username:
            return ({'Status': 'Please Login'})
        username = username.decode('utf-8')

        activeHero = Player_Hero.get_active_playerHero(username)
        if activeHero != None:
            return ({'active heros': activeHero, 'session_id': session_id})
        return ({'Status': 'Not Found', 'session_id': session_id})
Esempio n. 6
0
    def post(self):
        """
        buy hero
        """
        data = request.json
        session_id = data['session_id']
        hero_id = data['hero_id']

        hero_id = str(hero_id)
        username = redis_store.get_item(session_id)

        if not username:
            return ({'Status': 'Please Login'})
        username = username.decode('utf-8')

        hero = Hero.get_hero_by_heroID(hero_id)
        if not hero:
            return ({'Status': 'Hero Not Found'})

        user = User.get_user_by_username(username)
        if not User.check_coin(user.coin, hero.price):
            return ({'Status': 'Coin Not Enough'})

        if not User.check_level(user.level, hero.minimumRequiredLevel):
            return ({'Status': 'Level Is Low'})

        if Player_Hero.get_exist_playerHero(username, hero_id) != None:
            return ({'Status': 'Hero Already Selected'})

        new_player_hero = Player_Hero(hero.hero_id, hero.energy,
                                      hero.recovery_rate, username)
        player_hero = new_player_hero.save_to_db()

        user.update_coin(-hero.price)
        user.update_db()

        if player_hero:

            return ({
                'Status': 'The Hero Received Successfuly',
                'hero_id': hero.hero_id,
                'session_id': session_id
            }), 200
        else:
            return ({'Status': 'Not Found'})
Esempio n. 7
0
    def post(self):

        data = request.json
        session_id = data['session_id']
        hero_id = data['hero_id']

        hero_id = str(hero_id)
        username = redis_store.get_item(session_id)
        if not username:
            return ({'Status': 'please login'})
        username = username.decode('utf-8')

        if not username:
            return ({'Status': 'please login'})

        hero = Player_Hero.boost_energy(username, hero_id)

        if hero:
            return ({'Status': ' Hero Charged'}), 200
        else:
            return ({'Status': ' Not Found'})
Esempio n. 8
0
    def post(self):
        """
        buy Coin
        """
        data = request.json
        session_id = data['session_id']
        numCoin = data['numCoin']

        username = redis_store.get_item(session_id)

        if not username:
            return ({'Status': 'Please Login'})
        username = username.decode('utf-8')

        user = User.get_user_by_username(username)

        if user:
            user.update_coin(numCoin)
            user.update_db()
            return ({'Status': 'Success', 'coin': user.coin})

        return ({'Status': 'Try Again'})
Esempio n. 9
0
    def get(self, session_id):
        """
        get all heros
        """

        username = redis_store.get_item(session_id)

        if not username:
            return ({'Status': 'Please Login'})
        username = username.decode("utf-8")

        #find nickname
        nickname = User.get_nickname_by_username(username)

        hero_list = Player_Hero.get_playerHero(username)

        if hero_list != None:
            return ({
                'nickname': nickname,
                'hero': hero_list,
                'session_id': session_id
            }), 200

        return ({'Status': hero_list, 'session_id': session_id})