Created
August 7, 2018 07:05
-
-
Save sam-thecoder/d578a02cfec06344b92a612608bf3a53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main_key = 0 | |
game_record = {} | |
##Add Reward Cycle last | |
def bitcoin_game(user_key=None, amount=1000, start_index=0, invest_amount=None, withdraw_amount=None): | |
global main_key, game_record | |
#print(user_key) | |
if user_key == None: | |
player_name = 'player_{0}'.format(main_key) | |
user_key = main_key | |
main_key += 1 | |
if player_name not in game_record.keys(): | |
game_record[player_name] = { | |
'dys_without_activity': 0, | |
'amount': amount, | |
'last_game': 0, | |
'game_history': {}, | |
'invested': 0, | |
'investment_break_down': {}, #buying price & amount investment, day_id | |
'withdraw_breakdown': {}, #day_id withdraw, withdraw amount, stock price | |
'start_index': start_index, | |
'current_date': start_index, | |
'invest_history': [], | |
'amount_history': [], | |
} | |
player_record = game_record[player_name] | |
else: | |
player_record = game_record[player_name] | |
#get day's info | |
day_info = df.iloc[0] | |
player_record['current_date'] += 1 | |
#player_name, day_info, amount, profit, loss, game end (boolean) | |
return (user_key, day_info, amount, 0, 0, 0, False) | |
elif user_key != None: | |
#print('found user key', user_key) | |
player_name = 'player_{0}'.format(user_key) | |
if player_name in game_record.keys(): | |
player_record = game_record[player_name] | |
else: | |
print('Error in player name not found, game terminating') | |
return (0, 0, 0, 0, 0, 0, True) | |
#move one day into the future | |
#print(player_record['current_date']) | |
player_record['current_date'] += 1 | |
#print(player_record['current_date']) | |
current_date = player_record['current_date'] | |
if current_date > df.shape[0]-1: #last row | |
print('Reached the final day, game ending') | |
day_info = df.iloc[current_date-1] | |
prev_day_info = df.iloc[current_date-2] | |
prev_price = prev_day_info[1] | |
today_price = day_info[1] | |
if player_record['invested']: | |
#pct_change = (today_price*100)/prev_price | |
new_invest = round( (today_price * player_record['invested'])/prev_price, 2) | |
if new_invest > player_record['invested']: | |
profit = new_invest - player_record['invested'] | |
loss = 0 | |
else: | |
loss = player_record['invested'] - new_invest | |
profit = 0 | |
player_record['last_game'] += 1 | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, True) | |
day_info = df.iloc[current_date] | |
prev_day_info = df.iloc[current_date-1] | |
prev_price = prev_day_info[1] | |
today_price = day_info[1] | |
if player_record['invested']: | |
#pct_change = (today_price*100)/prev_price | |
#new_invest = (player_record['invested'] * pct_change) | |
new_invest = round( (today_price * player_record['invested'])/prev_price, 2) | |
if new_invest > player_record['invested']: | |
profit = new_invest - player_record['invested'] | |
loss = 0 | |
else: | |
loss = player_record['invested'] - new_invest | |
profit = 0 | |
player_record['invest_history'].append(player_record['invested']) | |
player_record['invested'] = new_invest | |
else: | |
profit = 0 | |
loss = 0 | |
if player_record['amount'] == 0 and player_record['invested'] == 0: | |
print("Something went terribly wrong, you're broke... bye!") | |
player_record['last_game'] += 1 | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, True) | |
if invest_amount == None and withdraw_amount == None and player_record['dys_without_activity'] < 5: | |
player_record['dys_without_activity'] += 1 | |
if player_record['invested'] == 0 and player_record['dys_without_activity'] == 5: | |
print('You have not invested for 5 consecutive days and have 0 investments, game ending') | |
player_record['last_game'] += 1 | |
#end game | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, True) | |
player_record['amount_history'].append(player_record['amount']) | |
if invest_amount: | |
#1. Check amount to invest exists i.e. < player amount | |
#2. If above passes reset dys_without_activity | |
if player_record['amount'] > invest_amount: | |
player_record['amount'] -= invest_amount | |
player_record['invested'] += invest_amount | |
player_record['investment_break_down'][current_date] = { | |
'buying_price': today_price, | |
'amount_invested': invest_amount | |
} | |
player_record['dys_without_activity'] = 0 | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, False) | |
else: | |
print('You have {0} to invest, the invest amount {1} exceeds this, try again with a smaller amount'.format(player_record['amount'], invest_amount)) | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, False) | |
if withdraw_amount: | |
#1. If amount invested < withdraw amount | |
#2. Reset dys_without_activity | |
if player_record['invested'] > withdraw_amount: | |
player_record['amount'] += withdraw_amount | |
player_record['invested'] -= withdraw_amount | |
player_record['withdraw_breakdown'][current_date] = { | |
'withdraw_amount': withdraw_amount, | |
'stock_price': today_price | |
} | |
player_record['dys_without_activity'] = 0 | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, False) | |
else: | |
print('You have {0} invested, you want to withdraw {1} which exceeds the amount available, try again with a smaller amount'.format(player_record['invested'], withdraw_amount)) | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, False) | |
return (user_key, day_info, player_record['amount'], player_record['invested'], profit, loss, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment