Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions instagram_web/blueprints/monopoly/templates/monopoly/index1.html
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ <h5 class="modal-title" id="payModalLabel">Funds Transfer.</h5>
</button>
</div>
<div class="modal-body">
Your Money: ${{current_user.money}}
<div id='money-in-modal'></div>
<form id='pay-form'>
<input type="hidden" name="csrf_token" value='{{csrf_token()}}'>
<br>
Expand Down Expand Up @@ -548,8 +548,8 @@ <h5 class="modal-title" id="{{prop.id}}ImageModalLabel">{{prop.name}}</h5>

<!-- Property modal -->
<div id='property-modal' class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-dialog modal-lg" role="document">
<div id='prop-modal-content' class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Owned Props</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
Expand Down Expand Up @@ -586,13 +586,11 @@ <h5 class="modal-title">Owned Props</h5>

<script type="text/javascript" charset="utf-8">
const socket = io();
const currentUsername = $('#current-username').text()
if (socket.connected) {
socket.emit('connect')
}

$(document).ready(() => {

$(() => {
const currentUsername = $('#current-username').text()
if (socket.connected) {
socket.emit('connect')
}
alertBox = $('#mon-alert')
hideAlert = () => {
alertBox.removeClass('show')
Expand Down Expand Up @@ -635,10 +633,13 @@ <h5 class="modal-title">Owned Props</h5>
${item}
<hr>
</div>

`)
})
})
socket.on('money_update', (data) => {
moneyDiv = $('#money-in-modal')
moneyDiv.html(` Your Money: $${data}`)
})
positions = ($('summary'))
reloadPage = () => {
location.reload()
Expand Down Expand Up @@ -731,18 +732,37 @@ <h5 class="modal-title">Owned Props</h5>
const propButton = $('#prop-button')
propButton.click(() => {
socket.emit('prop_request')
console.log('wanker')
})


socket.on('prop_response', (data) => {
propData = JSON.parse(data)
propRow = $('#prop-row')
propRow.html('')
propData.forEach((x, i) => {
propRow.append(`
<div class="col-md-4"><img src="${x.image_url}" alt="${i} prop"><br> Number of houses: ${x.houses}</div>
<div class="col-md-6">
<img src="${x.image_url}" alt="${i} prop">
<br>
Number of houses: ${x.houses}
<div id='${x.name}' class = '${x.house_price}'>
<button type="button" class=" transfer-btn btn btn-warning">Transfer Ownership</button>
</div>
</div>
`)
})
$('.0').prepend('<button type="button" class="house-btn btn btn-success">Buy house/hotel</button>')
})
$('#prop-row').on('click', '.house-btn', (e) => {
propName = e.target.parentElement.id
if (confirm(`Confirm house purchase for ${propName}?`)) {
socket.emit('house_buy', propName)
console.log('in if')
}
})
$('#prop-row').on('click', '.transfer-btn', (e) => {
console.log('wanek')
socket.emit('tester')
})
})
</script>
Expand Down
42 changes: 40 additions & 2 deletions instagram_web/blueprints/monopoly/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def update_positions():
user_positions.append(
f'{user.username} @ {locations[user.position]} | ${user.money}')
socketio.emit('position_update', user_positions)
emit('current_update', )


@socketio.on('connect')
Expand All @@ -56,6 +55,8 @@ def activity_create(txt):
for old_act in old_activities:
old_act.delete_instance()
update_activities()
emit('money_update', current_user.money)
update_positions()


def jail_free():
Expand Down Expand Up @@ -212,6 +213,8 @@ def pay(data):
if current_user.save() and recipient.save():
activity_create(
f'{current_user.username} payed ${amount} to {recipient_username}')
else:
print('failed saving at pay func.')


@monopoly_blueprint.route("/house", methods=['POST'])
Expand Down Expand Up @@ -256,7 +259,8 @@ def create_property():
@socketio.on('prop_request')
def prop_show():
if current_user.is_authenticated:
owned_props = Property.select().where(Property.user == current_user.id)
owned_props = Property.select().where(
Property.user == current_user.id).order_by(Property.created_at.desc())
prop_data = []
for each in owned_props:
image_url = each.image_url
Expand All @@ -271,3 +275,37 @@ def prop_show():
})
data = json.dumps(prop_data)
emit('prop_response', data)


@socketio.on('house_buy')
def house_create(prop_name):
if current_user.is_authenticated:
current_prop = Property.get_or_none(Property.name == prop_name)
if not current_prop:
print('not prop')
flash('No such property exists! Trouble. Contact shen.', 'warning')
return redirect(url_for('users.index'))
if current_prop.user_id != current_user.id:
print('not user')
flash('You are not authorized to do that', 'danger')
return redirect(url_for('users.index'))

if current_user.money < current_prop.house_price:
print('broke')
send('broke')
else:
current_user.money -= current_prop.house_price
current_prop.houses += 1
if not current_prop.save():
print('prop did not save')

if not current_user.save():
print('user did not save')

activity_create(
f'{current_user.username} bought a house for {prop_name} | ${current_prop.house_price}')


@socketio.on('tester')
def tester():
print('fuck')