Esempio n. 1
0
def mortgage(json):
    # used to mortgage a property
    gID = json['gID']
    uID = json['uID']
    pID = json['pID']
    deeds = r.getDeeds(gID)
    deed = deeds[pID]
    player = r.getPlayers(gID)[uID]
    mortgageable = isMortgageable(gID, pID, player['public']['number'])
    if mortgageable['value'] == False:
        deed["status"] = "mortgaged"
        player['money'] += deed['mortgage']
        r.setPlayer(gID, uID, player)
        r.setDeeds(gID, deeds)
        ret = helpers.getReturnData(gID, uID)
    else:
        ret = helpers.getReturnData(gID, uID, [], [], mortgageable['value'])
    return ret
Esempio n. 2
0
def selectFigurine(json):
    #used in lobby to select players figurine
    game = r.getGames()[json['gID']]

    if json['figurine'] in game['figurines']:

        player = r.getPlayers(json['gID'])[json['uID']]
        player['public']['figurine'] = json['figurine']

        game['figurines'].remove(json['figurine'])
        r.setPlayer(json['gID'], json['uID'], player)
        r.setGame(json['gID'], game)

        ret = helpers.getReturnData(json['gID'], json['uID'], [], ["FIGURINE"])
    else:
        ret = {}
        ret['error'] = 'figurine'
    return ret
Esempio n. 3
0
def measureAssets(gID, uID, amountNeeded):
    alert = {}
    alert["boolean"] = False
    players = r.getPlayers(gID)
    deeds = r.getDeeds(gID)
    assetsTotal = 0
    if players[uID]['money'] < amountNeeded:
        for group in players[uID]["public"]["properties"]:
            for pID in players[uID]["public"]["properties"][group]["owned"]:
                if deeds[pID]['status'] != "mortgaged":
                    assetsTotal += (deeds[pID]["house1"] *
                                    deeds[pID]['buildings']) / 2
                    assetsTotal += deeds[pID]["mortgage"]
        assetsTotal += players[uID]['money']
        if assetsTotal < amountNeeded:
            alert["boolean"] = True
            alert["alert"] = "BANKRUPT"
            killPlayer(gID, uID)
            return alert
    return alert
Esempio n. 4
0
def buy(json):
    # if player has option to buy a certain property and chooses to buy
    gID = json['gID']
    uID = json['uID']
    pID = json["pID"]
    players = r.getPlayers(gID)
    player = players[uID]
    if player["canBuy"] == pID:
        deed = r.getDeeds(gID)[pID]
        alert = pay(gID, uID, 0, deed['price'])
        if not alert['boolean']:
            giveProperty(gID, uID, deed)
            if incrementTurn(gID):
                ret = helpers.getReturnData(gID, uID, [],
                                            ["BUY", "AUCTION", "ROLLED"])
            else:
                ret = helpers.getReturnData(gID, uID, ["ROLL"],
                                            ["BUY", "AUCTION", "ROLLED"])
        else:
            ret = helpers.getReturnData(gID, uID, [], [], alert['alert'])
        return ret
Esempio n. 5
0
def start(json):
    #initialises the board and starts the game
    gID = json['gID']
    uID = json['uID']
    if canStart(gID, uID):
        game = r.getGame(gID)
        game['state'] = "PLAYING"
        r.setGame(gID, game)
        _start(gID)

        players = r.getPlayers(
            gID)  #add the default options for the PLAYING state to all players
        for _uID in players:
            players[_uID]["options"].append("TRADE")
            players[_uID]["options"].append("MORTGAGE")
        r.setPlayers(gID, players)

        ret = helpers.getReturnData(gID, uID, ["ROLL"], ["START"])
    else:
        ret = {}
        ret['error'] = "START"
    return ret
Esempio n. 6
0
def auction(json):
    # moves the game state into an auction for 30 seconds
    gID = json['gID']
    uID = json['uID']
    players = r.getPlayers(gID)
    player = players[uID]
    pID = json["pID"]
    if player["canBuy"] == pID:
        deed = r.getDeeds(gID)[pID]
        for _uID in players:
            players[_uID]['options'] += ["BID"]
            r.setPlayer(gID, _uID, players[_uID])
        game = r.getGame(gID)
        game['auction'] = {}
        game['auction']['pID'] = pID
        game['auction']['highest'] = {0: deed['price'] / 2}
        game['auction']['out'] = []
        game['lastActivity'] = helpers.getTime()
        game['state'] = "AUCTION"
        r.setGame(gID, game)
        ret = helpers.getReturnData(gID, uID, [], ["AUCTION", "BUY"])
        return ret
Esempio n. 7
0
def pay(gID, uID, rec, amount):
    #pay to a player/bank (rec == 0 for bank), !can return an alert!
    alert = {}
    alert['boolean'] = False
    players = r.getPlayers(gID)
    if players[uID]['money'] >= amount:
        players[uID]['money'] = players[uID]['money'] - amount
        if rec != 0:
            for _uID in players:
                if players[_uID]['public']['number'] == rec:
                    players[_uID]['money'] += amount
                    r.setPlayer(gID, _uID, players[_uID])
        r.setPlayer(gID, uID, players[uID])
    else:
        tempAlert = measureAssets(gID, uID, amount)
        if not tempAlert["boolean"]:
            alert['boolean'] = True
            alert[
                'alert'] = "INSUFFICIENT FUNDS"  # alert; must be dealt with or player loses
        else:
            alert = tempAlert
    return alert
Esempio n. 8
0
def drawCard(gID, uID):
    alert = {}
    alert['boolean'] = False
    game = r.getGame(gID)
    board = r.getBoard(gID)
    player = r.getPlayers(gID)[uID]
    pos = player['public']['position']
    cardType = board[pos]['name']
    cardNumber = drawFromDeck()
    takenGOOJF = game['takenGOOJF'][cardType]
    if takenGOOJF and cardNumber == 0:
        cardNumber += 1
    if cardType == "CommunityChest":
        deck = getCommChestJson()
    else:
        deck = getChanceJson()

    deck = helpers.keyStringtoInt(deck)
    card = deck[cardNumber - 1]
    alert = processCard(gID, uID, card, cardType)
    alert['card'] = card

    return alert
Esempio n. 9
0
def getReturnData(gID,
                  uID,
                  optionsToAdd=[],
                  optionsToRemove=[],
                  alert={},
                  card=False):
    ret = {}
    game = r.getGame(gID)
    players = r.getPlayers(gID)
    player = players[uID]

    ret['players'] = {}
    for _uID in players:
        ret['players'][players[_uID]['public']
                       ['name']] = players[_uID]['public']

    if game['state'] != "LOBBY":
        ret['board'] = r.getBoard(gID)
        ret['deeds'] = r.getDeeds(gID)

        ret["chat"] = r.getChat(gID)

        ret["trade"] = {}
        if len(player["trade"]["recieved"]) > 0:
            ret["trade"]["recieved"] = player["trade"]["recieved"][0]

        if len(player["trade"]["sent"]) > 0:
            for tID in player["trade"]["sent"]:
                if player["trade"]["sent"][tID] == "declined":
                    game["trade"].pop(tID, None)
                    ret["trade"]["sent"] = player["trade"]["sent"][tID][
                        "declined"] + "declined your trade offer"
                    player["trade"]["sent"].pop(tID, None)
                    r.setPlayer(gID, uID, player)
                    r.setGame(gID, game)
                    break
                if player["trade"]["sent"][tID] == "accepted":
                    game["trade"].pop(tID, None)
                    ret["trade"]["sent"] = player["trade"]["sent"][tID][
                        "accepted"] + "accepted your trade offer"
                    player["trade"]["sent"].pop(tID, None)
                    r.setPlayer(gID, uID, player)
                    r.setGame(gID, game)
                    break

    if len(optionsToAdd) + len(optionsToRemove) > 0:
        for option in optionsToAdd:
            if option not in player['options']:
                player['options'] += [option]
        for option in optionsToRemove:
            try:
                if option == "BUY":
                    player["canBuy"] = None
                player['options'].remove(option)
            except:
                pass
        r.setPlayer(gID, uID, player)

    ret['options'] = player['options']
    if len(alert) != 0:
        ret['alert'] = alert
    if card:
        ret['card'] = card

    ret['game'] = game
    ret['player'] = player
    # if game['state'] != "LOBBY" and (game["turn"] == player["public"]["number"]):
    #     print ("Return Data")
    #     print("name: " + player["public"]["name"])
    #     print("turn: " + str(game["turn"]))
    #     print("options: " + str(ret['options']))
    #     if "card" in ret:
    #         print (card['type'])
    #     if 'alert' in ret:
    #         print (alert)
    #     print("")
    return ret
Esempio n. 10
0
def isHost(gID, uID):
    player = r.getPlayers(gID)[uID]
    return player['public']['number'] == 1
Esempio n. 11
0
def incrementMoney(gID, uID, amount):
    # increments money for a player
    player = r.getPlayers(gID)[uID]
    player['money'] = int(player['money']) + int(amount)
    r.setPlayer(gID, uID, player)
Esempio n. 12
0
def bid(json):
    gID = json['gID']
    uID = json['uID']
    if r.validateUID(json['gID'], json['uID']):
        game = r.getGame(gID)
        players = r.getPlayers(gID)
        player = players[uID]
        hBidder = None
        for key in game['auction']['highest']:
            hBidder = key
        hBidAmount = game['auction']['highest'][hBidder]

        def close():
            if hBidder == None or int(hBidder) == 0:
                for _uID in players:
                    players[_uID]['options'].remove("BID")
                    if "AUCTION" in players[_uID]["options"]:
                        players[_uID]['options'].remove("AUCTION")
                    if "BUY" in players[_uID]["options"]:
                        players[_uID]['options'].remove("BUY")
                        players[_uID]["canBuy"] = None
                r.setPlayers(gID, players)
                game['state'] = "PLAYING"
                r.setGame(gID, game)
                incrementTurn(gID)
                return helpers.getReturnData(gID, uID)
            if int(hBidder) != 0:
                payUID = None
                for _uID in players:
                    if players[_uID]["public"]["number"] == int(hBidder):
                        payUID = _uID
                        break
                alert = pay(gID, payUID, 0, hBidAmount)
                if not alert['boolean']:
                    deed = r.getDeeds(gID)[game['auction']['pID']]
                    giveProperty(gID, payUID, deed)
                    for _uID in players:
                        players[_uID]['options'].remove("BID")
                        if "AUCTION" in players[_uID]["options"]:
                            players[_uID]['options'].remove("AUCTION")
                        if "BUY" in players[_uID]["options"]:
                            players[_uID]['options'].remove("BUY")
                            players[_uID]["canBuy"] = None
                    r.setPlayers(gID, players)
                    game['state'] = "PLAYING"
                    r.setGame(gID, game)
                    incrementTurn(gID)
                    return helpers.getReturnData(gID, uID)
                else:
                    return helpers.getReturnData(gID, uID, [], [],
                                                 alert['alert'])

        if not helpers.checkTime(game['lastActivity'], 30, True):
            return close()

        elif json['request'] == "PING":
            pass

        elif json['request'] == "BID" and player['public'][
                'number'] not in game['auction']['out']:
            if (json['amount'] > hBidAmount) and (json['amount'] <=
                                                  player['money']):
                newBid = {player['public']['number']: json['amount']}
                game['auction']['highest'] = newBid
                r.setGame(json['gID'], game)
            else:
                return helpers.getReturnData(gID, uID, [], [],
                                             "INSUFFICIENT FUNDS")

        elif json['request'] == "OUT":
            if hBidder != player['public']['number']:
                game['auction']['out'].append(player['public']['number'])
                r.setGame(json['gID'], game)
                if len(game['auction']['out']) >= game['playersNo'] - 1 - len(
                        game['spect']):
                    return close()

        return helpers.getReturnData(gID, uID)
Esempio n. 13
0
def inJail(gID, uID):
    # returns True if player is in jail
    return r.getPlayers(gID)[uID]['public']['jail']['boolean']
Esempio n. 14
0
def hasMonopoly(gID, uID, group):
    # returns True if player has a monopoly on group
    player = r.getPlayers(gID)[uID]
    return len(player['public']['properties'][group]
               ['owned']) == player['public']['properties'][group]['groupSize']
Esempio n. 15
0
def decrementMoney(gID, uID, amount):
    # decrements money from player
    player = r.getPlayers(gID)[uID]
    player['money'] = int(player['money']) - int(amount)
    r.setPlayer(gID, uID, player)