Exemplo n.º 1
0
def update_users_score(names, score):
    print score
    player1 = connections.fetch_one(constants.TABLE_USER, names[0])
    print player1
    player1["score"] += score
    connections.insert(constants.TABLE_USER, names[0], player1)
    player2 = connections.fetch_one(constants.TABLE_USER, names[1])
    player2["score"] += score
    connections.insert(constants.TABLE_USER, names[1], player2)
Exemplo n.º 2
0
def ws_message(message):
    """
    If a group already exists i.e A user is in queue, Then add the new user to the same Group
    If no group exists then create a Group, Add user to the group and tell him to wait until someone else joins the group
    """
    if constants.CONNECTED in message.content['text']:
        name = utils.get_name(message.content['text'].split("-")
                              [1])  # Get name of the user connected to socket
        # When user gets connected store his channel id
        constants.CONNECTED_USERS[str(message.reply_channel)] = name
        print constants.CONNECTED_USERS
        # Check if some other user is in queue
        users_in_queue = connections.fetch(constants.TABLE_QUEUE)
        # If other user is in queue means he is already in a Group, Add this user in the same group and Start the Game
        if users_in_queue:
            for key in users_in_queue:
                if key != name:
                    group_key = users_in_queue[key]["key"]
                    # Add this user to the Group in which user_in_queue is waiting
                    Group(group_key).add(message.reply_channel)
                    #Remove the user_in_queue from Queue
                    connections.delete(constants.TABLE_QUEUE, key)
                    #Create a group with both the Users as child
                    group = {name: "{}", key: "{}"}
                    connections.insert(constants.TABLE_GROUP, group_key, group)
                    #Send the message to both to Start the Game
                    utils.start_game(group_key)
                else:
                    Group(users_in_queue[key]["key"]).add(
                        message.reply_channel)
                break
        else:
            client = {
                "name":
                name,
                "channel":
                str(message.reply_channel),
                "key":
                ''.join(
                    random.choice(string.ascii_uppercase + string.digits)
                    for _ in range(5))
            }
            Group(client['key']).add(message.reply_channel)
            connections.insert(constants.TABLE_QUEUE, client['name'], client)

    #Logging of answers submited by users
    elif constants.LOG in message.content['text']:
        # Persist users answers in case server is not running
        utils.store_user_answers(message.content['text'].split("-")[1])
        # Log user activity
        connections.push(constants.TABLE_LOG, message.content['text'])

    # When one user want to leave the group, notify the other user also
    elif constants.LEAVE_GROUP in message.content['text']:
        reply = constants.LEAVE_GROUP
        send_message_to_group(message.content['text'].split("-")[1], reply)
Exemplo n.º 3
0
def check_for_consensus(data, question):
    result = {}

    if data["a"] >= (constants.REDUNDANCE) / 2:
        result = {"question": question, "actual_answer": "a"}
    elif data["b"] >= (constants.REDUNDANCE) / 2:
        result = {"question": question, "actual_answer": "b"}
    else:
        result = {"question": question, "actual_answer": "c"}
    connections.insert(constants.TABLE_RESULT, question, result)
    connections.delete(constants.TABLE_QUESTION, question)
Exemplo n.º 4
0
def store_user_answers(text):
    data = json.loads(text)
    player = get_name(data["user"])
    group = dict(
        connections.fetch_one(constants.TABLE_GROUP, data["group_key"]))
    temp = ast.literal_eval(group[player])
    # Store user answer in Group table
    temp.update({data["question"]: data["answer"]})
    group[player] = str(temp)
    connections.insert(constants.TABLE_GROUP, data["group_key"], group)
    # If user has submited all answers, calculate scores
    if len(temp.keys()) == 5: evaluate_score(data["group_key"])
Exemplo n.º 5
0
def update_attempt(question_answers):
    for q in question_answers:
        data = connections.fetch_one(constants.TABLE_QUESTION, q[0])

        data["redundancy"] += 2
        if str(q[1]) == '1':
            data["a"] += 2
        elif str(q[1]) == '2':
            data["b"] += 2
        else:
            data["c"] += 2
        connections.insert(constants.TABLE_QUESTION, q[0], data)
        if data["redundancy"] == constants.REDUNDANCE:
            check_for_consensus(data, q[0])
Exemplo n.º 6
0
def login(request):
    if "user" not in request.session:
        if request.POST:
            name =  utils.get_name(request.POST['email'])
            user = connections.fetch_one(constants.TABLE_USER, name)
            if user is None:        
                user = {
                    "email": request.POST['email'], 
                    "password": request.POST['password'],
                    "score": 0
                    }
                connections.insert(constants.TABLE_USER, name, user) # Insert record into DB
            request.session["user"] = user['email'] # Create session for user

            return HttpResponseRedirect(reverse('index')) 
        return render_to_response('login.html')  
    return HttpResponseRedirect(reverse('index'))