Support handling vault-content events in the vault UI

This commit is contained in:
Trever Fischer
2012-11-17 21:33:42 -05:00
parent a306df9df0
commit 690ff8f21e
5 changed files with 124 additions and 33 deletions

View File

@@ -38,6 +38,10 @@ div[class|="inventory-item"] .quantity {
padding: 3px;
}
div[class|="inventory-item"] .damage.empty{
display: none;
}
div[class|="inventory-item"] .damage {
position: absolute;
bottom: 0;

81
static/js/vault.js Normal file
View File

@@ -0,0 +1,81 @@
var InventoryWidget = function(tableElement) {
this.e = $(tableElement);
this.slots = new Array();
this.e.find("td[data-position]").each((function (idx, td) {
var slot = new ItemSlot(td);
this.slots[slot.getPosition()] = slot;
}).bind(this));
Caminus.eventPoll.onEvent('vault-contents', this._vaultEvent.bind(this));
}
InventoryWidget.prototype._vaultEvent = function(evt, payload) {
$(payload.items).each((function(idx, item) {
var slot = this.slotByPosition(item.position);
console.log(slot);
slot.updateFromJSON(item);
}).bind(this));
};
InventoryWidget.prototype.slotByPosition = function(pos) {
console.log(this);
return this.slots[pos];
}
var ItemSlot = function(tdElement) {
this.e = $(tdElement);
this.item = $(this.e.find('div[class|="inventory-item"]')[0]);
}
ItemSlot.prototype.getPosition = function() {
return this.e.data('position');
}
ItemSlot.prototype.updateFromJSON = function(data) {
console.log(data.item);
this.setMaterial(data.item);
this.setDurability(data.durablility);
this.setDamage(data.damage);
this.setQuantity(data.quantity);
this.setName(data.name);
}
ItemSlot.prototype.setName = function(s) {
this.item.data('name', s);
this.item.find('.name').html(s);
}
ItemSlot.prototype.setMaterial = function(id) {
console.log(this.item);
this.item.data('material', id);
this.item.attr('class',"inventory-item-"+id);
}
ItemSlot.prototype.setQuantity = function(qty) {
this.item.data('quantity', qty);
this.item.find('.quantity').html(qty);
}
ItemSlot.prototype.setDamage = function(dmg) {
this.item.data('damage', dmg);
this.item.find('.damage', "50%");
if (dmg == 0)
this.item.find('.damage').addClass("empty");
else
this.item.find('.damage').removeClass("empty");
}
ItemSlot.prototype.getDamage = function() {
return this.item.data('damage');
}
ItemSlot.prototype.setDurability = function(d) {
this.item.data('durability', d);
this.setDamage(this.getDamage());
}
$(document).ready(function() {
$('table.inventory').each(function (idx, inventory) {
new InventoryWidget(inventory);
});
});

View File

@@ -1,12 +1,12 @@
<div class="inventory-item-{{item.material}}">
{% if quantity > 0 %}
<div class="quantity">{{quantity}}</div>
<div class="inventory-item-{{item.material}}" data-material="{{item.material}}"
data-quantity="{{quantity}}" data-damage="{{item.damage}}">
<div class="quantity">
{% if quantity > 0 %}{{quantity}}
</div>
{% endif %}
<div class="name">{{item.name}}</div>
{% if item.damage %}
<div class="damage">
<div class="damage {% if not item.damage %}empty{%endif%}">
<div class="current"
style="width:{{item.damagePct}}%"></div>
</div>
{% endif %}
</div>

View File

@@ -1,8 +1,14 @@
{% extends "base_simple.html" %}
{%load static %}
{% get_static_prefix as STATIC_PREFIX %}
{% block title %}Vault{% endblock %}
{% block sectiontitle %}Your Vault{% endblock %}
{% block extrahead %}
<script type="text/javascript" language="javascript" src="{{STATIC_PREFIX}}/js/vault.js"></script>
{% endblock %}
{% block content %}
<table class="inventory">
<tr>
@@ -11,10 +17,8 @@
</tr>
<tr>
{% endif %}
<td>
{% if slot.item %}
<td data-id="{{slot.id}}" data-position="{{slot.position}}">
{% include 'common/item.html' with quantity=slot.quantity item=slot.item %}
{% endif %}
</td>
{% endfor %}
</tr>

View File

@@ -1,7 +1,7 @@
from django.db import models
from minecraft.models import MinecraftProfile
from django.db.models.signals import post_save, post_delete
from api.events import VaultContentsEvent, broadcast_server_event
from api.events import VaultContentsEvent, broadcast_server_event, send_web_event
from minecraft.models import Item
from django.conf import settings
@@ -59,36 +59,38 @@ class VaultSlot(models.Model):
raise VaultError, "Insufficient available slots."
return slots
def send_vault_delete(sender, instance, *args, **kwargs):
slots = [
{
'item': None,
'quantity': -1,
'damage': None,
'data': None,
'position': instance.position
}
]
broadcast_server_event(VaultContentsEvent(instance.player.mc_username,
slots))
def send_vault_update(sender, instance, created, *args, **kwargs):
if created and instance.item:
if instance.item:
slots = [
{
'item': instance.item.material,
'quantity': instance.quantity,
'damage': instance.item.damage,
'data': instance.item.data,
'position': instance.position
'position': instance.position,
'name': instance.item.name(),
'durability': instance.item.metadata['durability']
}
]
broadcast_server_event(VaultContentsEvent(instance.player.mc_username, slots))
else:
slots = [
{
'item': None,
'quantity': -1,
'damage': 0,
'data': 0,
'position': instance.position,
'name': "",
'durability': 0
}
]
evt = VaultContentsEvent(instance.player.mc_username, slots)
broadcast_server_event(evt)
send_web_event(evt)
post_save.connect(send_vault_update, sender=VaultSlot, dispatch_uid='derp')
post_delete.connect(send_vault_delete, sender=VaultSlot, dispatch_uid='derp')
def create_initial_slots(sender, instance, created, *args, **kwargs):
def ensure_initial_slots(sender, instance, created, *args, **kwargs):
slots = instance.vault_slots.all()
if len(slots) < settings.CAMINUS_VAULT_SLOTS:
neededPositions = range(0, settings.CAMINUS_VAULT_SLOTS)
@@ -97,5 +99,5 @@ def create_initial_slots(sender, instance, created, *args, **kwargs):
for pos in neededPositions:
VaultSlot.objects.create(player=instance, position=pos)
post_save.connect(create_initial_slots, sender=MinecraftProfile)
post_save.connect(ensure_initial_slots, sender=MinecraftProfile)
post_delete.connect(ensure_initial_slots, sender=VaultSlot, dispatch_uid='derp')