Skip to content

Commit

Permalink
Merge pull request #92 from jeffreykirchner/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jeffreykirchner authored Sep 7, 2023
2 parents 1229a1b + 82d87ac commit 2f5bcf3
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 55 deletions.
64 changes: 57 additions & 7 deletions main/consumers/staff/session_consumer_mixins/subject_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,13 @@ def sync_move_fruit_to_avatar(session_id, player_id, target_player_id, good_one_

with transaction.atomic():
session = Session.objects.select_for_update().get(id=session_id)

current_period_id = str(session.get_current_session_period().id)

parameter_set = session.parameter_set.json()
parameter_set_player_id = str(session.world_state['avatars'][str(player_id)]['parameter_set_player_id'])

summary_data = session.summary_data[current_period_id][str(player_id)]

good_one = parameter_set['parameter_set_players'][parameter_set_player_id]['good_one']
good_two = parameter_set['parameter_set_players'][parameter_set_player_id]['good_two']
good_three = parameter_set['parameter_set_players'][parameter_set_player_id]['good_three']
Expand All @@ -1005,6 +1008,11 @@ def sync_move_fruit_to_avatar(session_id, player_id, target_player_id, good_one_
session.world_state['avatars'][str(target_player_id)][good_two] += good_two_move
session.world_state['avatars'][str(target_player_id)][good_three] += good_three_move

#data
summary_data["send_avatar_to_avatar_" + target_player_id + "_good_" + good_one] += good_one_move
summary_data["send_avatar_to_avatar_" + target_player_id + "_good_" + good_two] += good_two_move
summary_data["send_avatar_to_avatar_" + target_player_id + "_good_" + good_three] += good_three_move

session.save()

world_state = session.world_state
Expand All @@ -1021,7 +1029,9 @@ def sync_move_fruit_to_house(session_id, player_id, target_house_id, good_one_mo

with transaction.atomic():
session = Session.objects.select_for_update().get(id=session_id)
current_period_id = str(session.get_current_session_period().id)
world_state = session.world_state
summary_data = session.summary_data[current_period_id][str(player_id)]

parameter_set = session.parameter_set.json()
parameter_set_player_id = str(world_state['avatars'][str(player_id)]['parameter_set_player_id'])
Expand Down Expand Up @@ -1082,6 +1092,11 @@ def sync_move_fruit_to_house(session_id, player_id, target_house_id, good_one_mo
house[good_one] += good_one_move
house[good_two] += good_two_move
house[good_three] += good_three_move

#data
summary_data["send_avatar_to_house_" + target_house_id + "_good_" + good_one] += good_one_move
summary_data["send_avatar_to_house_" + target_house_id + "_good_" + good_two] += good_two_move
summary_data["send_avatar_to_house_" + target_house_id + "_good_" + good_three] += good_three_move
else:
avatar[good_one] += good_one_move
avatar[good_two] += good_two_move
Expand Down Expand Up @@ -1110,10 +1125,14 @@ def sync_attack_avatar(session_id, player_id, target_house_id):

with transaction.atomic():
session = Session.objects.select_for_update().get(id=session_id)
session_period_id = str(session.get_current_session_period().id)
parameter_set = session.parameter_set.json()

source_player = session.world_state['avatars'][str(player_id)]
target_player = session.world_state['avatars'][str(target_house_id)]
player_id_s = str(player_id)
target_house_id_s = str(target_house_id)

source_player = session.world_state['avatars'][player_id_s]
target_player = session.world_state['avatars'][target_house_id_s]

if Decimal(source_player["health"]) < Decimal(parameter_set["attack_cost"]):
status = "fail"
Expand All @@ -1124,13 +1143,32 @@ def sync_attack_avatar(session_id, player_id, target_house_id):
error_message.append({"id":"attack_avatar_button", "message": "Target player already has zero health."})

if status == "success":
source_player["health"] = Decimal(source_player["health"]) - Decimal(parameter_set["attack_cost"])
target_player["health"] = Decimal(target_player["health"]) - Decimal(parameter_set["attack_damage"])
#data for summary
summary_data = session.summary_data[session_period_id]
summary_data_source = summary_data[player_id_s]
summary_data_target = summary_data[target_house_id_s]

attack_cost = Decimal(parameter_set["attack_cost"])
attack_damage = Decimal(parameter_set["attack_damage"])

source_player["health"] = Decimal(source_player["health"]) - attack_cost
target_player["health"] = Decimal(target_player["health"]) - attack_damage

#data for summary
summary_data_source["attacks_cost_at_" + target_house_id_s] = str(Decimal(summary_data_source["attacks_cost_at_" + target_house_id_s]) + attack_cost)
summary_data_target["attacks_damage_from_" + player_id_s] = str(Decimal(summary_data_target["attacks_damage_from_" + player_id_s]) + attack_damage)

summary_data_source["attacks_at_" + target_house_id_s] += 1
summary_data_target["attacks_from_" + player_id_s] += 1

if Decimal(source_player["health"]) < 0:
#handle underage
summary_data_source["attacks_cost_at_" + target_house_id_s] = str(Decimal(summary_data_source["attacks_cost_at_" + target_house_id_s]) + Decimal(source_player["health"]))
source_player["health"] = 0

if Decimal(target_player["health"]) < 0:
#handle underage
summary_data_target["attacks_damage_from_" + player_id_s] = str(Decimal(summary_data_target["attacks_damage_from_" + player_id_s]) + Decimal(target_player["health"]))
target_player["health"] = 0

source_player["health"] = str(source_player["health"])
Expand Down Expand Up @@ -1187,9 +1225,14 @@ def sync_grove_harvest(session_id, player_id, grove_id):

with transaction.atomic():
session = Session.objects.select_for_update().get(id=session_id)
session_period_id = str(session.get_current_session_period().id)
parameter_set = session.parameter_set.json()
player = session.world_state['avatars'][str(player_id)]
grove = session.world_state['groves'][str(grove_id)]

player_id_s = str(player_id)
grove_id_s = str(grove_id)

player = session.world_state['avatars'][player_id_s]
grove = session.world_state['groves'][grove_id_s]

status = "fail"

Expand All @@ -1212,8 +1255,15 @@ def sync_grove_harvest(session_id, player_id, grove_id):
error_message.append({"id":"grove_harvest", "message": "No harvests remaining this period."})

if status == "success":
summary_data = session.summary_data[session_period_id][player_id_s]

player[grove["good"]] += harvest_amount
player["period_grove_harvests"] += 1

summary_data["grove_harvests_count_" + grove_id_s] += 1
summary_data["grove_harvests_total_" + grove_id_s] += harvest_amount
summary_data["harvest_total_" + grove["good"]] += harvest_amount

session.save()

world_state = session.world_state
Expand Down
48 changes: 43 additions & 5 deletions main/consumers/staff/session_consumer_mixins/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,25 @@ def sync_continue_timer(event, session_id):
#check session over
if session.world_state["current_period"] >= parameter_set["period_count"] and \
session.world_state["time_remaining"] <= 1:

world_state = session.world_state
summary_data = session.summary_data

session.world_state["current_period"] = parameter_set["period_count"]
session.world_state["time_remaining"] = 0
session.world_state["timer_running"] = False
world_state["current_period"] = parameter_set["period_count"]
world_state["time_remaining"] = 0
world_state["timer_running"] = False

current_period_id = session.get_current_session_period().id

#store data
for i in world_state["avatars"]:
sd_player = summary_data[str(current_period_id)][i]
sd_player["end_health"] = world_state["avatars"][i]["health"]

#inventory
for k in main.globals.Goods.choices:
sd_player["house_" + k[0]] = world_state["houses"][i][k[0]]
sd_player["avatar_" + k[0]] = world_state["avatars"][i][k[0]]

session.save()

Expand Down Expand Up @@ -258,18 +273,41 @@ def sync_continue_timer(event, session_id):

#check if period over
if period_is_over:

summary_data = session.summary_data
world_state = session.world_state
current_period_id = session.get_current_session_period().id

#store data
for i in world_state["avatars"]:
sd_player = summary_data[str(last_period_id)][i]

#health
sd_player["end_health"] = world_state["avatars"][i]["health"]
sd_player["start_health"] = world_state["avatars"][i]["health"]

#inventory
for k in main.globals.Goods.choices:
sd_player["house_" + k[0]] = world_state["houses"][i][k[0]]
sd_player["avatar_" + k[0]] = world_state["avatars"][i][k[0]]


session = session.session_periods.get(id=last_period_id).do_consumption()
session = session.get_current_session_period().do_timer_actions(time_remaining)
session = session.get_current_session_period().do_production()
session = session.get_current_session_period().do_grove_growth()

for i in session.world_state["avatars"]:
for i in world_state["avatars"]:
# session.world_state["session_players"][i]["earnings"] += session.world_state["session_players"][i]["inventory"][current_period_id]

earnings[i] = {}
earnings[i]["total_earnings"] = 0
earnings[i]["period_earnings"] = 0

#store starting health


session.save()

else:
session = session.get_current_session_period().do_timer_actions(time_remaining)

Expand Down
2 changes: 1 addition & 1 deletion main/forms/parameter_set_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ParameterSetForm(forms.ModelForm):
"step":"1",
"min":"1"}))

max_grove_harvests = forms.IntegerField(label='Max Grove Harvests',
max_grove_harvests = forms.IntegerField(label='Max Grove Harvests per Period',
min_value=1,
widget=forms.NumberInput(attrs={"v-model":"parameter_set.max_grove_harvests",
"step":"1",
Expand Down
2 changes: 1 addition & 1 deletion main/forms/session_form_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ class SessionFormAdmin(forms.ModelForm):
class Meta:
model=Session
fields = ('parameter_set', 'creator', 'collaborators', 'current_experiment_phase', 'title',
'world_state', 'world_state_avatars', 'summary_data', 'started', 'shared', 'locked',
'world_state', 'world_state_avatars', 'started', 'shared', 'locked',
'soft_delete', 'controlling_channel')
Loading

0 comments on commit 2f5bcf3

Please sign in to comment.