Beispiel #1
0
def initTrade(gID, uID, playerNumber, jOffer, jFor):
    alert = {}
    alert["boolean"] = False
    game = r.getGame(gID)
    players = r.getPlayers(gID)
    trader = players[uID]
    receiver = None
    _uID = None
    for ID in players:
        if players[ID]['public']['number'] == playerNumber:
            receiver = players[ID]
            _uID = ID

    alert = mCannotTrade(gID, trader, receiver, jOffer, jFor)["boolean"]
    if not alert["boolean"]:
        alert = pCannotTrade(gID, jOffer, jFor)

    if not alert["boolean"]:
        tID = helpers.genID()
        offer = {}
        offer["from"] = {}
        offer["to"] = {}
        offer["from"]["number"] = trader['public']['number']
        offer["from"]["name"] = trader['public']['name']
        offer["to"]["number"] = receiver['public']['number']
        offer["to"]["name"] = receiver['public']['name']
        offer['offered'] = jOffer
        offer['for'] = jFor
        game['trade'][tID] = offer
        trader["trade"]["sent"][tID] = {"pending": receiver["public"]["name"]}
        receiver["trade"]["recieved"] += tID
        r.setPlayer(gID, _uID, receiver)
        r.setPlayer(gID, uID, trader)
        r.setGame(gID, game)
    return alert
Beispiel #2
0
def giveGOOJF(gID, uID, deckType):
    player = r.getPlayers(gID)[uID]
    game = r.getGame(gID)
    game['takenGOOJF'][deckType] = True
    player['public']['GOOJF'][deckType] = True
    r.setPlayer(gID, uID, player)
    r.setGame(gID, game)
Beispiel #3
0
def analysePosition(gID, uID, board, pos):
    # analyses the player's position to see what operations need to be made on a square
    alert = {}
    alert['boolean'] = False
    square = board[pos]
    if square['category'] == 'tax':
        alert = pay(gID, uID, 0, square['amount']
                    )  #pay the bank the tax amount required on this square
        return alert

    if square["category"] == "special":
        if square['name'] == 'goToJail':
            alert = updateLocation(gID, uID, "GTJ")

    elif square['category'] == 'card':
        alert = drawCard(gID, uID)

    elif square['category'] == 'property':
        deed = r.getDeeds(gID)[square['pID']]
        if deed['status'] == "owned":
            rent = rentCalculator(gID, deed)
            alert = pay(gID, uID, deed['owner'], rent)
            alert["activity"] = "rent"
            return alert
        elif deed['status'] == "mortgaged":
            pass
        elif deed['status'] == "notOwned":
            player = r.getPlayers(gID)[uID]
            player['options'] += ["BUY"]
            player['options'] += ["AUCTION"]
            player["canBuy"] = square["pID"]
            r.setPlayer(gID, uID, player)
    return alert
Beispiel #4
0
def giveProperty(gID, uID, deed, status="owned"):
    # method for giving a player a property (deed signing)
    player = r.getPlayers(gID)[uID]
    deed['status'] = status
    deed['owner'] = player['public']['number']
    player['public']['properties'][deed['group']]['owned'].append(deed['pID'])
    r.setDeed(gID, deed["pID"], deed)
    r.setPlayer(gID, uID, player)
Beispiel #5
0
def roll(json):
    # rolling a dice
    gID = json['gID']
    uID = json['uID']
    roll = double_roll()
    game = r.getGame(gID)
    game['lastRoll'] = roll
    r.setGame(gID, game)

    if inJail(gID, uID) and not roll['double']:
        player = r.getPlayers(gID)[uID]
        if player["public"]["jail"]["turn"] == 4:
            alert = payToGOOJ(gID, uID, player)
            if alert["boolean"]:
                #kill player
                pass
            else:
                getOutOfJail(gID, uID)
        if player["public"]["jail"]["turn"] == 3:
            alert = payToGOOJ(gID, uID, player)
            if alert["boolean"]:
                ret = helpers.getReturnData(gID, uID, ["ROLLED"], ['ROLL'],
                                            alert['alert'])
                return ret
            else:
                getOutOfJail(gID, uID)
        if inJail(gID, uID):
            player['public']['jail']['turn'] += 1
            incrementTurn(gID)
            r.setPlayer(gID, uID, player)
            ret = helpers.getReturnData(gID, uID, [], ['ROLL'])
            return ret

    elif inJail(gID, uID) and roll['double']:
        getOutOfJail(gID, uID)

    alert = updateLocation(gID, uID, roll['value'])
    if "card" in alert:
        card = alert["card"]
    else:
        card = False
    if alert['boolean']:
        ret = helpers.getReturnData(gID, uID, [], ['ROLL'], alert['alert'],
                                    card)
        if alert["alert"] == "IN JAIL":
            incrementTurn(gID)
            return ret
        else:
            return ret
    elif "activity" in alert:
        if alert['activity'] == "rent":
            ret = helpers.getReturnData(gID, uID, [], ['ROLL'],
                                        alert['activity'], card)
            incrementTurn(gID)
            return ret
    elif card == False:
        ret = helpers.getReturnData(gID, uID, ["ROLLED"], ['ROLL'])
        return ret
Beispiel #6
0
def updateLocation(gID,
                   uID,
                   value,
                   card=False):  #moves a player to new position
    #value can be value of a roll, or a special card e.g. jail, go, free parking
    player = r.getPlayers(gID)[uID]
    board = r.getBoard(gID)
    prevPos = player['public']['position']
    GO = False
    if value == "GTJ":
        newPos = 10
        player['public']['jail']['boolean'] = True
        alert = {}

        alert['boolean'] = True
        alert['alert'] = "IN JAIL"

        board[prevPos]['playersOn'].remove(player["public"]['number'])
        board[newPos]['playersOn'].append(player["public"]['number'])

        r.setBoard(gID, board)
        player['public']['position'] = newPos
        r.setPlayer(gID, uID, player)
        return alert

    if card:
        newPos = int(value)
        if newPos > 39:
            game = r.getGame(gID)
            amount = game['GO']
            incrementMoney(gID, uID, amount)
            newPos = newPos - 40
            GO = True
    else:
        newPos = prevPos + value
        if newPos > 39:
            game = r.getGame(gID)
            amount = game['GO']
            incrementMoney(gID, uID, amount)
            newPos = newPos - 40
            GO = True

    board[prevPos]['playersOn'].remove(player['public']['number'])
    board[newPos]['playersOn'].append(player['public']['number'])

    r.setBoard(gID, board)
    player['public']['position'] = newPos
    r.setPlayer(gID, uID, player)

    alert = analysePosition(gID, uID, board, newPos)  #checking position
    if GO:
        alert["GO"] = GO
    return alert
Beispiel #7
0
def declineTrade(gID, uID, tID):
    game = r.getGame(gID)
    players = r.getPlayers(gID)
    player = r.getPlayers(gID)[uID]
    sender = game["trade"][tID]["from"]["number"]
    game["trade"].pop(tID, None)
    r.setGame(gID, game)
    _uID = None
    for ID in players:
        if players[ID]['public']["number"] == sender:
            sender = players[ID]
            _uID = ID
    sender["trade"]["sent"][tID] = {"declined": player["public"]["name"]}
    r.setPlayer(gID, _uID, sender)
Beispiel #8
0
def payToGOOJ(gID, uID, player):
    alert = {}
    alert['boolean'] = False
    if player['money'] > 50:
        player['money'] - 50
        r.setPlayer(gID, uID, player)
    else:
        tempAlert = measureAssets(gID, uID, 50)
        if not tempAlert["boolean"]:
            alert['boolean'] = True
            alert["alert"] = "INSUFFICIENT FUNDS"
        else:
            alert = tempAlert
    return alert
Beispiel #9
0
def acceptTrade(gID, uID, tID):
    alert = {}
    alert["boolean"] = False
    game = r.getGame(gID)
    trade = game['trade'][tID]
    players = r.getPlayers(gID)
    player = players[uID]
    _uID = None
    sendingPlayer = trade[tID]["from"]["number"]
    for ID in players:
        if players[ID]['public']["number"] == sendingPlayer:
            sendingPlayer = players[ID]
            _uID = ID
            break

    alert = mCannotTrade(gID, sendingPlayer, player, trade["offered"],
                         trade["for"])
    if not alert["boolean"]:
        sTotal = alert["sTotal"]
        rTotal = alert["rTotal"]
        decrementMoney(gID, uID, alert["rMortgage"])
        decrementMoney(gID, _uID, alert["sMortgage"])
        if sTotal > rTotal:
            alert = pay(gID, _uID, sendingPlayer["public"]["number"],
                        sTotal - rTotal)
        elif rTotal > sTotal:
            alert = pay(gID, uID, player["public"]["number"], rTotal - sTotal)

    if "property" in trade["offered"]:
        for pID in trade["offered"]["property"]:
            tradeProp(gID, uID, _uID, pID, player, sendingPlayer)
    if "property" in trade["for"]:
        for pID in trade["for"]["property"]:
            tradeProp(gID, _uID, uID, pID, sendingPlayer, player)

    if "GOOJF" in trade["offered"]:
        sendingPlayer["public"]["GOOJF"][trade["offered"]["GOOJF"]] = False
        giveGOOJF(gID, uID, trade["offered"]["GOOJF"])
    if "GOOJF" in trade["for"]:
        player["public"]["GOOJF"][trade["for"]["GOOJF"]] = False
        giveGOOJF(gID, _uID, trade["for"]["GOOJF"])

    if not alert["boolean"]:
        sendingPlayer["trade"]["sent"][tID] = {
            "accepted": player["public"]["name"]
        }
        r.setPlayer(gID, _uID, sendingPlayer)
    return alert
Beispiel #10
0
def join(json, gID=None):
    #sets a player
    ret = {}
    if gID == None:
        if "gID" in json:
            gID = json['gID']
        else:
            gID = helpers.getfirstPublicGame()
    try:
        game = r.getGame(gID)
        if game['playersNo'] == 8:
            ret['error'] = 'Game is full. Try again.'
        else:
            game['playersNo'] += 1
            uID = helpers.genID()
            player = {}
            player['uID'] = uID
            player["state"] = "active"
            player['money'] = getInitJson()['startMoney']
            player['options'] = []
            player["trade"] = {}
            player["trade"]["sent"] = {}
            player["trade"]["recieved"] = []
            player['public'] = {}
            player['public']['name'] = json['name']
            player['public']['number'] = game['playersNo']
            player['public']['properties'] = getPlayerPropertiesDict()
            player['public']['GOOJF'] = {
                "CommunityChest": False,
                "Chance": False,
            }
            player['public']['position'] = 0
            player['public']['figurine'] = None
            player['public']['jail'] = {}
            player['public']['jail']['boolean'] = False
            player['public']['jail']['turn'] = 0
            player["canBuy"] = None

            r.setPlayer(gID, uID, player)
            r.setGame(gID, game)
            ret = helpers.getReturnData(gID, uID, ["FIGURINE"])

    except:
        ret['error'] = 'join'
    return ret
Beispiel #11
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
Beispiel #12
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
Beispiel #13
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
Beispiel #14
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
Beispiel #15
0
def getOutOfJail(gID, uID, card=False):
    ret = None
    if card:
        player = r.getPlayers(gID)[uID]
        if inJail(gID, uID) and (player['public']['GOOJF']['CommunityChest']
                                 or player['public']['GOOJF']['Chance']):
            deck = None
            if player['public']['GOOJF']['CommunityChest']:
                deck = "CommunityChest"
            elif player['public']['GOOJF']['Chance']:
                deck = "Chance"
            player['public']['GOOJF'][deck] = False
            game = r.getGame(gID)
            game['takenGOOJF'][deck] = False
            r.setGame(gID, game)
            ret = helpers.getReturnData(gID, uID)
        else:
            ret = helpers.getReturnData(gID, uID, [], [], "INVALID USE")
    player = r.getPlayers(gID)[uID]
    player['public']['jail']['boolean'] = False
    player['public']['jail']['turn'] = 0
    r.setPlayer(gID, uID, player)
    if card:
        return ret
Beispiel #16
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)
Beispiel #17
0
def tradeProp(gID, aUID, bUID, pID, aPlayer, bPlayer):
    deed = r.getDeeds(gID)[pID]
    bPlayer['public']["properties"][deed["group"]].remove(pID)
    r.setPlayer(gID, bUID, bPlayer)
    giveProperty(gID, aUID, aPlayer, deed, deed["status"])
Beispiel #18
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
Beispiel #19
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)