Example #1
0
def alert(request):
    if no_babies():
        return create_baby(request)
    print "Tear alert detected on server."
    send_email(request.user.username)
    print "Email sent to user."

    try:
        broadcast({"message": "Dr. Orwell sayz: YOUR BABY IS PISSED. GO LOVE IT."})
    except NoSocket:
        print "Broadcast not sent: No connected sockets."

    # Record cry
    cry = Cry()
    cry.length = random.uniform(0.1, 3.0) # in seconds
    if request.method == 'POST':
        if 'volume' in request.POST:
            cry.volume = int(request.POST['volume'])
        else:
            cry.volume = -1
    else:
        cry.volume = -1
    cry.save()

    print "Cry recorded at %s\nLength: %f\nVolume: %f" % \
        (cry.time, cry.length, cry.volume)

    return HttpResponse("We get it. you're crying. wa wa waaa")
Example #2
0
def currentTurn():
    """broadcasts information about current player and his tokens on hand"""
    player = cv.getCurrentPlayer()
    tokens = []
    for i in player["tokens"]:
        tokens.append(cv.getTokenHand(i, player["color"]))
        tokens[-1]["color"] = tokens[-1]["color"].real
    broadcast({"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]})
Example #3
0
def system_message(request, template="system_message.html"):
    context = {"rooms": ChatRoom.objects.all()}
    if request.method == "POST":
        room = request.POST["room"]
        data = {"action": "system", "message": request.POST["message"]}
        try:
            if room:
                broadcast_channel(data, channel="room-" + room)
            else:
                broadcast(data)
        except NoSocket, e:
            context["message"] = e
        else:
            context["message"] = "Message sent"
Example #4
0
def system_message(request, template="chat/system_message.html"):
    context = {"rooms": ChatRoom.objects.all()}
    if request.method == "POST":
        room = request.POST["room"]
        data = {"action": "system", "message": request.POST["message"]}
        try:
            if room:
                broadcast_channel(data, channel="room-" + room)
            else:
                broadcast(data)
        except NoSocket, e:
            context["message"] = e
        else:
            context["message"] = "Message sent"
Example #5
0
def system_message(request, template="system_message.html"):
    # timeの取得
    from datetime import datetime
    ymdt = datetime.now()
    year = ymdt.year
    month = ymdt.month
    day = ymdt.day
    hour = ymdt.hour
    minute = ymdt.minute
    filtered = ChatRoom.objects.filter(create_time>=datetime(year, month, day,hour,minute))
    cnt = filtered.annotate(Count('tweet'))
    data = {"action": "system", "message": cnt}
    try:
        broadcast(data)
    except NoSocket, e:
        context["message"] = e
Example #6
0
def system_message(request, template="system_message.html"):
    context = {"warehouses": Warehouse.objects.all()}
    if request.method == "POST":
        warehouse = request.POST["warehouse"]
        data = {"action": "system", "message": request.POST["message"]}
        try:
            if warehouse:
                broadcast_channel(data, channel="warehouse-" + warehouse)
                print "sending data" + data
                #arduino=ArduinoControl(data)
                #arduino.run()
            else:
                broadcast(data)
        except NoSocket, e:
            context["message"] = e
        else:
            context["message"] = "Message sent"
Example #7
0
def performBattle(tokenActionId=-1, color=Color.NONE):
    """handles battle and its effects"""
    if tokenActionId == -1:
        cv.performBattle()
    else:
        cv.actionTokenBattle(tokenActionId, color)
        currentTurn()
        # ustawic zycie graczom
    life = {}
    for col in (1, 2, 3, 4):
        life[col] = 0

    board = getBoard()

    # znalezienie hq i przypisanie ich zycia odpowiedniemu kolorowi
    for tok in board:
        # print tok['token']
        if "hq" in tok["token"]["name"]:
            life[tok["token"]["color"]] = tok["token"]["life"]

            # przypisanie kazdemu graczowi zycia na podstawie zycia hq
    for player in game["players"].itervalues():
        player["life"] = life[player["color"]]

        # sprawdzenie czy zyje tylko 1 gracz
    alive = 0
    best = 0
    for name, p in game["players"].iteritems():
        if p["life"] > 0:
            alive = alive + 1
        if p["life"] > best:
            best = p["life"]
            winner = name

    if alive == 1:
        game["state"] = 3

    broadcast({"action": "afterBattle", "board": board, "players": game["players"]})

    if game["state"] == 1:
        nextTurn()
    else:  # 2 lub 3
        # koniec gry
        game["state"] = 3
        broadcast({"action": "gameEnd", "winner": winner})
Example #8
0
def nextTurn():
    """changes turn for next player"""
    player = cv.getNextPlayer()
    game["current_player"] = player["name"]
    tokens = []
    for i in player["tokens"]:
        tokens.append(cv.getTokenHand(i, player["color"]))
        tokens[-1]["color"] = tokens[-1]["color"].real
    if tokens.__len__() == 0:
        # bitwa koncowa
        game["state"] = 2
        performBattle()
    elif isBoardFull():
        performBattle()
    elif game["players"][player["name"]]["life"] < 1:
        nextTurn()
    else:
        broadcast({"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]})
Example #9
0
def playerDisconnected(request, socket, context):
    """handles player disconnect event"""
    global game
    tmp_k = ""
    for k, v in game["players"].iteritems():
        if v["socket"] == socket.session.session_id:
            print ">>>> ", k, " disconnected"
            if game["state"] == 1:
                cv.killPlayer(Color.values[v["color"]])
                if game["current_player"] == k:
                    nextTurn()
            tmp_k = k

    if tmp_k != "":
        del game["players"][tmp_k]
        if game["players"].__len__() == 1 and game["state"] == 1:
            game["state"] = 3
            for i in game["players"].iterkeys():
                winner = i
            broadcast({"action": "gameEnd", "winner": winner})
    if game["players"].__len__() == 0:
        cv.restartGame()
        game["state"] = 0
    sendPlayersList()
Example #10
0
def messageFromPlayer(request, socket, context, message):
    """handles messages from players"""
    global game
    print ">>>> ", message
    act = message["action"]

    # oczekiwanie na graczy: join, getColor, getPlayers, ready
    if act == "join":
        if game["state"] != 0:
            # gra juz trwa
            socket.send({"action": "joinState", "joined": False, "reason": "gameInProgress"})
        elif game["players"].__len__() > 3:
            # za duzo graczy
            socket.send({"action": "joinState", "joined": False, "reason": "playersCount"})
        elif message["name"] in game["players"]:
            # gracz o podanym imieniu istnieje
            socket.send({"action": "joinState", "joined": False, "reason": "nameNotAvailable"})
        else:
            game["players"][message["name"]] = {
                "ready": False,
                "socket": socket.session.session_id,
                "color": Color.NONE.real,
                "life": 20,
            }
            socket.send({"action": "joinState", "joined": True, "playerName": message["name"]})
            sendPlayersList()
    elif act == "getColor":
        socket.send({"action": "setColor", "color": game["players"][getNameBySocket(socket)]["color"]})
    elif act == "getPlayers":
        socket.send({"action": "playersList", "list": game["players"]})
    elif act == "ready":
        all_ready = True
        for p in game["players"].itervalues():
            if p["socket"] == socket.session.session_id:
                p["ready"] = message["is_ready"]
            if not p["ready"]:
                all_ready = False
        sendPlayersList()
        if all_ready:
            if game["players"].__len__() > 1:
                # colors = (Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN)
                i = 1
                for name in game["players"].iterkeys():
                    game["players"][name]["color"] = i
                    cv.addPlayer(name)
                    i = i + 1
                game["state"] = 1
                broadcast({"action": "gameState", "state": 1})
                player = cv.getCurrentPlayer()
                tokens = []
                game["current_player"] = player["name"]
                for i in player["tokens"]:
                    tokens.append(cv.getTokenHand(i, player["color"]))
                    tokens[-1]["color"] = tokens[-1]["color"].real
                broadcast(
                    {"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]}
                )

                # wlasciwa rozgrywka
    elif act == "nextTurn":
        if getNameBySocket(socket) == game["current_player"]:
            nextTurn()
        else:
            sendError(socket, "notYourTurn")
    elif act == "putToken":
        if getNameBySocket(socket) == game["current_player"]:
            color = Color.values[game["players"][getNameBySocket(socket)]["color"]]
            added = cv.addToken(message["id"], color, message["q"], message["r"], message["angle"])
            if added:
                token = cv.getTokenBoard(message["q"], message["r"])
                broadcast(
                    {
                        "action": "tokenAdded",
                        "color": color.real,
                        "id": message["id"],
                        "name": token["name"],
                        "q": message["q"],
                        "r": message["r"],
                        "angle": message["angle"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "addFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "throw":
        if getNameBySocket(socket) == game["current_player"]:
            thrown = cv.throwToken(message["id"], Color.values[game["players"][getNameBySocket(socket)]["color"]])
            if thrown:
                currentTurn()
            else:
                sendError(socket, "throwFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "currentPlayer":
        currentTurn()
    elif act == "move":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            token = cv.getTokenBoard(message["src_q"], message["src_r"])
            if token.__len__() == 0:
                sendError(socket, "moveFailed")
                return
            moved = cv.actionTokenMove(
                message["token"],
                player["color"],
                message["src_q"],
                message["src_r"],
                message["dst_q"],
                message["dst_r"],
            )
            if moved:
                # broadcast: info o przesunietym tokenie
                broadcast(
                    {
                        "action": "tokenMoved",
                        "id": token["id"],
                        "color": token["color"].real,
                        "src_q": message["src_q"],
                        "src_r": message["src_r"],
                        "dst_q": message["dst_q"],
                        "dst_r": message["dst_r"],
                        "name": token["name"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "moveFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "push":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            token = cv.getTokenBoard(int(message["src_q"]), int(message["src_r"]))
            if token.__len__() == 0:
                sendError(socket, "pushFailed")
                return
            pushed = cv.getTokenBoard(int(message["dst_q"]), int(message["dst_r"]))
            if pushed.__len__() == 0:
                sendError(socket, "pushFailed")
                return
            moved = cv.actionTokenPush(
                int(message["token"]),
                player["color"],
                int(message["src_q"]),
                int(message["src_r"]),
                int(message["dst_q"]),
                int(message["dst_r"]),
            )
            if moved:
                pushed_q = 2 * int(message["dst_q"]) - int(message["src_q"])
                pushed_r = 2 * int(message["dst_r"]) - int(message["src_r"])
                broadcast(
                    {
                        "action": "tokenMoved",
                        "id": pushed["id"],
                        "color": pushed["color"].real,
                        "src_q": message["dst_q"],
                        "src_r": message["dst_r"],
                        "dst_q": pushed_q,
                        "dst_r": pushed_r,
                        "name": pushed["name"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "pushFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "battle":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            performBattle(int(message["token"]), player["color"])
        else:
            sendError(socket, "notYourTurn")
    elif act == "getPushes":
        socket.send({"action": "pushes", "pushes": cv.getPushes(int(message["q"]), int(message["r"]))})
    elif act == "getMoves":
        socket.send({"action": "moves", "moves": cv.getMoves(int(message["q"]), int(message["r"]))})
    elif act == "getBoard":
        socket.send({"action": "board", "board": getBoard()})
        print cv.getBoard()
Example #11
0
def sendPlayersList():
    """broadcasts updated list of players"""
    global game
    broadcast({"action": "playersList", "list": game["players"]})