[virtualcommons-svn] commit/vcweb: alllee: reducing number of redundant queries for player data val
Status: Beta
Brought to you by:
alllee
|
From: <com...@bi...> - 2013-06-20 02:29:37
|
1 new commit in vcweb: https://bitbucket.org/virtualcommons/vcweb/commits/b0a68cb398a6/ Changeset: b0a68cb398a6 User: alllee Date: 2013-06-20 04:29:23 Summary: reducing number of redundant queries for player data values within the group (last harvest decision, storage, and player status / alive / dead) minor improvements to participant UI for saving selected harvest decisions Affected #: 3 files diff -r 2a077522e3c42ed7a228d08984cb101d6903285a -r b0a68cb398a694c33115fb43ce8fcd8fb1a82f76 vcweb/bound/models.py --- a/vcweb/bound/models.py +++ b/vcweb/bound/models.py @@ -1,7 +1,7 @@ from django.db.models import Sum from django.dispatch import receiver from vcweb.core import signals, simplecache -from vcweb.core.models import (ExperimentMetadata, Parameter, ParticipantRoundDataValue, GroupRelationship, +from vcweb.core.models import (DefaultValue, ExperimentMetadata, Parameter, ParticipantRoundDataValue, GroupRelationship, GroupCluster, GroupClusterDataValue, RoundData, RoundConfiguration) from vcweb.forestry.models import (get_harvest_decision_parameter, get_harvest_decision, get_harvest_decision_dv, get_regrowth_rate_parameter, get_group_harvest_parameter, get_reset_resource_level_parameter, get_resource_level as get_unshared_resource_level, @@ -9,6 +9,7 @@ get_resource_level_parameter, get_resource_level_dv as get_unshared_resource_level_dv, set_group_harvest, set_regrowth, set_harvest_decision) +from collections import defaultdict import logging logger = logging.getLogger(__name__) @@ -197,6 +198,39 @@ def get_number_alive(group, round_data): return ParticipantRoundDataValue.objects.for_group(group, parameter=get_player_status_parameter(), round_data=round_data, boolean_value=True).count() +def get_player_data(group, previous_round_data, current_round_data, self_pgr): + prdvs = ParticipantRoundDataValue.objects.for_group(group=group, + round_data__in=[previous_round_data, current_round_data], + parameter__in=(get_player_status_parameter(), get_storage_parameter(), get_harvest_decision_parameter()), + ) + player_dict = defaultdict(lambda: defaultdict(lambda: None)) + for prdv in prdvs: + player_dict[prdv.participant_group_relationship][prdv.parameter] = prdv + player_data = [] + for pgr, pgrdv_dict in player_dict.iteritems(): + # FIXME: figure out a way to handle default values elegantly in this case since we aren't using the accessor + # methods + for int_parameter in (get_harvest_decision_parameter(), get_storage_parameter()): + if pgrdv_dict[int_parameter] is None: + pgrdv_dict[int_parameter] = DefaultValue(0) + if pgrdv_dict[get_player_status_parameter()] is None: + pgrdv_dict[get_player_status_parameter()] = DefaultValue(True) + player_data.append({ + 'id': pgr.pk, + 'number': pgr.participant_number, + 'lastHarvestDecision': pgrdv_dict[get_harvest_decision_parameter()].int_value, + 'alive': pgrdv_dict[get_player_status_parameter()].boolean_value, + 'storage': pgrdv_dict[get_storage_parameter()].int_value, + }) + own_player = player_dict[self_pgr] + return (player_data, { + 'lastHarvestDecision': own_player[get_harvest_decision_parameter()].int_value, + 'alive': own_player[get_player_status_parameter()].boolean_value, + 'storage': own_player[get_storage_parameter()].int_value, + }) + + + def set_player_status(participant_group_relationship, round_data, value): status_dv = get_player_status_dv(participant_group_relationship, round_data) status_dv.boolean_value = value diff -r 2a077522e3c42ed7a228d08984cb101d6903285a -r b0a68cb398a694c33115fb43ce8fcd8fb1a82f76 vcweb/bound/templates/bound/participate.html --- a/vcweb/bound/templates/bound/participate.html +++ b/vcweb/bound/templates/bound/participate.html @@ -404,7 +404,12 @@ <h3>Harvest</h3><div data-bind='if: submitted'><div class='alert alert-success'> - <i class='icon-leaf'></i> You have currently selected a harvest decision of <span class='badge badge-info' data-bind='text: harvestDecision'></span> trees. Click the submit button to finalize your decision. + <i class='icon-leaf'></i> You have already submitted a harvest decision of <span class='badge badge-info' data-bind='text: harvestDecision'></span> trees. + </div> + </div> + <div data-bind='if: selectedHarvestDecision'> + <div class='alert alert-success'> + <i class='icon-leaf'></i> You have currently selected a harvest decision of <span class='badge badge-info' data-bind='text: harvestDecision'></span> trees. Click the Save button to finalize your decision. </div></div><div data-bind='ifnot: alive'> @@ -418,7 +423,7 @@ {% csrf_token %} <div id='harvestDecisionDiv' class='control-group'><div data-bind='template: { name: "harvest-decision-multiselect-template" }'></div> - <button id='submitDecisionButton' data-bind='click: $root.submitDecision' type='submit' class='btn btn-primary'>Submit</button> + <button disabled id='submitDecisionButton' data-bind='click: $root.submitDecision, text: submitted() ? "Continue" : "Save"' type='submit' class='btn btn-primary'></button></div></form></div> @@ -688,6 +693,7 @@ console.debug(formData); $.post('submit-harvest-decision', formData, function(response) { console.debug(response); + $('#submitDecisionButton').prop('disabled', false); }); return false; } @@ -737,7 +743,6 @@ model.chatEnabled(! chatDisabled); } model.afterRenderTemplate = function(elements) { - console.debug("after render template: " + model.harvestDecision()); if (model.templateId() === "REGULAR") { model.startRound() } diff -r 2a077522e3c42ed7a228d08984cb101d6903285a -r b0a68cb398a694c33115fb43ce8fcd8fb1a82f76 vcweb/bound/views.py --- a/vcweb/bound/views.py +++ b/vcweb/bound/views.py @@ -10,8 +10,8 @@ from vcweb.bound.models import (get_experiment_metadata, get_regrowth_rate, get_max_allowed_harvest_decision, get_cost_of_living, get_resource_level, get_initial_resource_level, get_total_storage, get_storage, get_all_session_storages, get_last_harvest_decision, get_harvest_decision_dv, get_harvest_decision_parameter, - set_harvest_decision, can_observe_other_group, is_player_alive, get_average_harvest, get_average_storage, - get_total_harvest, get_number_alive) + set_harvest_decision, can_observe_other_group, get_average_harvest, get_average_storage, + get_total_harvest, get_number_alive, get_player_data) from urllib import urlencode import logging @@ -73,16 +73,18 @@ 'maximumResourcesToDisplay': 20, 'warningCountdownTime': 10, 'harvestDecision': 0, + 'storage': 0, 'roundDuration': 60, 'chatMessages': [], 'canObserveOtherGroup': False, + 'selectedHarvestDecision': False, 'isInstructionsRound': False, 'waitThirtySeconds': False, 'totalHarvest': 0, 'sessionOneStorage': 0, 'sessionTwoStorage': 0, } -# FIXME: need to distinguish between instructions / welcome rounds and practice/regular rounds +# FIXME: bloated method with too many special cases, try to refactor def get_view_model_json(experiment, participant_group_relationship, **kwargs): ec = experiment.experiment_configuration current_round = experiment.current_round @@ -139,23 +141,15 @@ # instructions rounds to practice rounds. own_group = participant_group_relationship.group own_resource_level = get_resource_level(own_group) - last_harvest_decision = get_last_harvest_decision(participant_group_relationship, round_data=previous_round_data) - # FIXME: redundancy with playerData and direct values - experiment_model_dict['playerData'] = [{ - 'id': pgr.pk, - 'number': pgr.participant_number, - 'lastHarvestDecision': get_last_harvest_decision(pgr, round_data=previous_round_data), - 'alive': is_player_alive(pgr, current_round_data), - 'storage': get_storage(pgr, current_round_data), - } for pgr in own_group.participant_group_relationship_set.all()] - experiment_model_dict['lastHarvestDecision'] = last_harvest_decision - experiment_model_dict['storage'] = get_storage(participant_group_relationship, current_round_data) + if current_round.is_playable_round: + player_data, own_data = get_player_data(own_group, previous_round_data, current_round_data, participant_group_relationship) + experiment_model_dict.update(own_data) + experiment_model_dict['playerData'] = player_data + experiment_model_dict['averageHarvest'] = get_average_harvest(own_group, previous_round_data) + experiment_model_dict['averageStorage'] = get_average_storage(own_group, current_round_data) + c = Counter(map(itemgetter('alive'), experiment_model_dict['playerData'])) + experiment_model_dict['numberAlive'] = "%s out of %s" % (c[True], sum(c.values())) experiment_model_dict['resourceLevel'] = own_resource_level - experiment_model_dict['alive'] = is_player_alive(participant_group_relationship, current_round_data) - experiment_model_dict['averageHarvest'] = get_average_harvest(own_group, previous_round_data) - experiment_model_dict['averageStorage'] = get_average_storage(own_group, current_round_data) - c = Counter(map(itemgetter('alive'), experiment_model_dict['playerData'])) - experiment_model_dict['numberAlive'] = "%s out of %s" % (c[True], sum(c.values())) # participant group data parameters are only needed if this round is a data round or the previous round was a data round if previous_round.is_playable_round or current_round.is_playable_round: harvest_decision = get_harvest_decision_dv(participant_group_relationship, current_round_data) Repository URL: https://bitbucket.org/virtualcommons/vcweb/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. |