Ejemplo n.º 1
0
def loan(current_player):
    # clients = get_clients()
    current_player.player_amount += 100000
    print("{} {}".format(current_player.player_name, ' loaned.'))
    broadcast(current_player.player_name + ' loaned some money.')
    print_name_amt_shares(current_player)
    card_discard(current_player, 'Loan')
Ejemplo n.º 2
0
def debenture(current_player, company):
    # clients = get_clients()
    if company.company_current_price == 0 and current_player.player_shares[
            company.company_name]:
        current_player.player_amount += \
            current_player.player_shares[company.company_name] * company.company_starting_price
        current_player.player_shares[company.company_name] = 0
        company.company_total_buy_shares += current_player.player_shares[
            company.company_name]
        print("{} {}".format(current_player.player_name, ' debenture'))
        broadcast(current_player.player_name + ' played debenture.')
        print_name_amt_shares(current_player)
        card_discard(current_player, 'Debenture')
Ejemplo n.º 3
0
	def locate(self,phoneNum) :
		s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
		s.bind(('',BROADCAST_PORT))
		s.settimeout(3)
		helper.broadcast('192.168.',THREAD_CONTROLLER,phoneNum,BROADCAST_PORT)
		try :
			data , address = s.recvfrom(MAX)
			if data == self.phoneNumber :
				location = address[0]
			else :
				location = 'NULL'
		except socket.error as error :
			location = 'NULL'
		return location
Ejemplo n.º 4
0
def player_choice(current_player, lp):
    clients = get_clients()
    com_name_list = get_cnl()
    current_turn = get_current_turn()
    global list_of_players
    list_of_players = lp
    choice = current_player.player_connection.recv(1024).decode().split(',')
    try:
        if choice[0] in ['pass', '']:
            print("{} {}".format(current_player.player_name, choice[0]))
            broadcast(current_player.player_name + " passed.")
            time.sleep(1)
            return
        elif choice[0] in ['buy', 'sell', 'sbuy', 'ssell']:
            company = com_name_list[int(choice[1]) - 1]
            if choice[0] == 'buy':
                buy(clients, current_player, company, int(choice[2]))
            elif choice[0] == 'sell':
                sell(clients, current_player, company, int(choice[2]))
            elif choice[0] == 'ssell':
                ssell(clients, current_player, company, int(choice[2]))
            else:
                sbuy(clients, current_player, company, int(choice[2]))
            return
        elif choice[0] == 'loan' and card_check(current_player, choice[0]):
            loan(current_player)
        elif choice[0] == 'debenture' and card_check(current_player,
                                                     choice[0]):
            company = com_name_list[int(choice[1]) - 1]
            debenture(current_player, company)
        elif choice[0] == 'rights' and card_check(current_player, choice[0]):
            company = com_name_list[int(choice[1]) - 1]
            print("Current player before rights  {}".format(
                current_player.player_name))
            rights(company, lp, current_turn, current_player)
            print("Current player after rights  {}".format(
                current_player.player_name))
            print("{} {}".format(current_player.player_name, choice))
            for player in lp:
                if player == current_player:
                    continue
                else:
                    print_name_amt_shares(player)
        return
    except Exception as e:
        print(str(e))
        current_player.player_connection.send(str.encode('play'))
        time.sleep(1)
        player_choice(current_player, lp)
Ejemplo n.º 5
0
def sbuy(clients, current_player, company, shares):
    if current_player.player_shares.get(company.company_name, 0) <= shares:
        current_player.player_shares[company.company_name] = \
            current_player.player_shares.get(company.company_name,0) + shares
        current_player.player_amount += shares * company.company_current_price * 2
        company.company_total_sell_shares += shares
        broadcast(current_player.player_name + ',' + ','.join(["does not short sell ",\
            company.company_name, str(shares)]))
        print_name_amt_shares(current_player)
        print("{} {}".format(current_player.player_name, 'buys short sold'))
    else:
        current_player.player_connection.send(
            str.encode("Not enough shares to buy. \n"))
        time.sleep(1)
        current_player.player_connection.send(str.encode('play'))
        time.sleep(1)
        player_choice(current_player, list_of_players)
Ejemplo n.º 6
0
def connections(num_of_players):
    ServerSocket = get_serversocket()
    count = 1
    clients = get_clients()
    while True:
        if count < num_of_players:
            Client, address = ServerSocket.accept()
            address
            count += 1
            start_new_thread(threaded_client, (Client, ))
        else:
            if count == num_of_players and count == len(clients):
                num_of_players = 0
                broadcast('Players: ' + ', '.join(clients.keys()))
                time.sleep(2)
                gameplay()
            else:
                continue
Ejemplo n.º 7
0
def sell(clients, current_player, company, shares):
    if current_player.player_shares.get(company.company_name, 0) >= shares:
        current_player.player_amount += shares * company.company_current_price
        company.company_total_buy_shares += shares
        current_player.player_shares[company.company_name] = \
            current_player.player_shares.get(company.company_name,0) - shares
        broadcast(', '.join([
            current_player.player_name, "sold", company.company_name,
            str(shares)
        ]))
        print_name_amt_shares(current_player)
        print("{} {}".format(current_player.player_name, 'sells'))
    else:
        current_player.player_connection.send(
            str.encode("Not enough shares to sell. \n"))
        time.sleep(1)
        current_player.player_connection.send(str.encode('play'))
        time.sleep(1)
        player_choice(current_player, list_of_players)
Ejemplo n.º 8
0
def buy(clients, current_player, company, shares):
    if current_player.player_amount >= shares * company.company_current_price and \
            shares <= company.company_total_buy_shares:
        current_player.player_amount -= shares * company.company_current_price
        company.company_total_buy_shares -= shares
        current_player.player_shares[company.company_name] = \
            current_player.player_shares.get(company.company_name,0) + shares
        broadcast(', '.join([
            current_player.player_name, "bought", company.company_name,
            str(shares)
        ]))
        print_name_amt_shares(current_player)
        print("{} {}".format(current_player.player_name, 'buys'))
    else:
        current_player.player_connection.send(str.encode("Not enough money to"\
            " buy the shares or not enough shares available. \n"))
        time.sleep(1)
        current_player.player_connection.send(str.encode('play'))
        time.sleep(1)
        player_choice(current_player, list_of_players)
Ejemplo n.º 9
0
def ssell(clients, current_player, company, shares):
    cost = shares * company.company_current_price * 2
    if current_player.player_shares.get(company.company_name,0) <= 0 and \
        current_player.player_amount >= cost:
        current_player.player_amount -= cost
        current_player.player_shares[company.company_name] = \
            current_player.player_shares.get(company.company_name,0) - shares
        company.company_total_sell_shares -= shares
        broadcast(', '.join([
            current_player.player_name, "shorts", company.company_name,
            str(shares)
        ]))
        print_name_amt_shares(current_player)
        print("{} {}".format(current_player.player_name, 'shorts'))
    else:
        current_player.player_connection.send(str.encode("Not enough shares to sell or not "\
            + "enough money. \n"))
        time.sleep(1)
        current_player.player_connection.send(str.encode('play'))
        time.sleep(1)
        player_choice(current_player, list_of_players)
Ejemplo n.º 10
0
def rights(company, lp, name, cp):
    # clients = get_clients()
    broadcast(', '.join(
        [cp.player_name, " invoked rights in ", company.company_name]))
    for _ in range(len(lp)):
        available_shares = min((cp.player_shares.get(company.company_name)//2000) * \
            1000, company.company_total_buy_shares)
        check_amount = cp.player_amount - available_shares * 10
        if check_amount < 0:
            max_shares = (cp.player_amount // 10000)
            cp.player_shares[company.company_name] = cp.player_shares.get(company.company_name) \
                + max_shares * 1000
            cp.player_amount -= max_shares * 10000
            company.company_total_buy_shares -= max_shares * 1000
            print(cp.player_name, cp.player_amount, cp.player_shares)
        else:
            cp.player_shares[company.company_name] += available_shares
            cp.player_amount = check_amount
            company.company_total_buy_shares -= available_shares
        print_name_amt_shares(cp)
        cp = lp[next(name)]
    card_discard(cp, 'Rights')
    return