Bombsquad-Ballistica-Modded.../dist/ba_root/mods/features/votingmachine.py

132 lines
5.1 KiB
Python
Raw Normal View History

2023-05-28 14:07:54 +05:30
# Electronic Voting Machine (EVM) by -mr.smoothy
2022-05-11 00:29:10 +05:30
2023-02-19 13:24:59 +05:30
import _ba
import ba
2022-09-25 16:35:32 +05:30
import ba.internal
2022-05-08 15:22:27 +05:30
import time
2022-05-08 22:06:53 +05:30
2022-05-08 15:22:27 +05:30
game_started_on = 0
2023-05-28 14:07:54 +05:30
vote_machine = {"end": {"last_vote_start_time": 0, "vote_duration": 50,
"min_game_duration_to_start_vote": 30, "voters": []},
"sm": {"last_vote_start_time": 0, "vote_duration": 50,
"min_game_duration_to_start_vote": 1, "voters": []},
"nv": {"last_vote_start_time": 0, "vote_duration": 50,
"min_game_duration_to_start_vote": 1, "voters": []},
"dv": {"last_vote_start_time": 0, "vote_duration": 50,
"min_game_duration_to_start_vote": 1, "voters": []}}
def vote(pb_id, client_id, vote_type):
global vote_machine
voters = vote_machine[vote_type]["voters"]
last_vote_start_time = vote_machine[vote_type]["last_vote_start_time"]
vote_duration = vote_machine[vote_type]["vote_duration"]
min_game_duration_to_start_vote = vote_machine[vote_type]["min_game_duration_to_start_vote"]
2022-05-08 15:22:27 +05:30
now = time.time()
2023-05-28 14:07:54 +05:30
if now > last_vote_start_time + vote_duration:
2022-05-08 15:22:27 +05:30
voters = []
2023-05-28 22:11:31 +05:30
vote_machine[vote_type]["last_vote_start_time"] = now
2023-05-28 14:07:54 +05:30
if now < game_started_on + min_game_duration_to_start_vote:
2022-05-08 15:22:27 +05:30
_ba.screenmessage("Seems game just started, Try again after some time", transient=True,
clients=[client_id])
2022-05-08 22:06:53 +05:30
return
2022-05-08 15:22:27 +05:30
if len(voters) == 0:
2023-05-28 14:07:54 +05:30
ba.internal.chatmessage(f"{vote_type} vote started")
2022-05-08 15:22:27 +05:30
# clean up voters list
active_players = []
2022-09-25 16:35:32 +05:30
for player in ba.internal.get_game_roster():
2022-05-08 15:22:27 +05:30
active_players.append(player['account_id'])
for voter in voters:
if voter not in active_players:
voters.remove(voter)
if pb_id not in voters:
voters.append(pb_id)
2023-05-28 14:07:54 +05:30
_ba.screenmessage(f'Thanks for vote , encourage other players to type {vote_type} too.', transient=True,
2022-05-08 15:22:27 +05:30
clients=[client_id])
2023-05-28 14:07:54 +05:30
if vote_type == 'end':
2023-05-28 22:11:31 +05:30
update_vote_text(max_votes_required(
len(active_players)) - len(voters))
else:
_ba.screenmessage(f"{max_votes_required(len(active_players)) - len(voters)} votes required for {vote_type}",
image={"texture": ba.gettexture("achievementSharingIsCaring"),
"tint_texture": ba.gettexture("achievementSharingIsCaring"),
"tint_color": (0.5, 0.1, 0.2), "tint2_color": (0.1, 0.2, 0.9)},
top=True)
vote_machine[vote_type]["voters"] = voters
2022-05-11 22:20:06 +05:30
2023-05-28 22:11:31 +05:30
if len(voters) >= max_votes_required(len(active_players)):
2023-05-28 14:07:54 +05:30
ba.internal.chatmessage(f"{vote_type} vote succeed")
if vote_type == "end":
try:
with _ba.Context(_ba.get_foreground_host_activity()):
_ba.get_foreground_host_activity().end_game()
except:
pass
elif vote_type == "nv":
_ba.chatmessage("/nv")
elif vote_type == "dv":
_ba.chatmessage("/dv")
elif vote_type == "sm":
_ba.chatmessage("/sm")
2022-05-08 15:22:27 +05:30
2023-05-28 22:11:31 +05:30
2023-05-28 14:07:54 +05:30
def reset_votes():
global vote_machine
for value in vote_machine.values():
value["voters"] = []
2022-05-08 15:22:27 +05:30
2023-05-28 22:11:31 +05:30
def max_votes_required(players):
2022-05-08 15:22:27 +05:30
if players == 2:
2023-05-28 22:11:31 +05:30
return 1
2022-05-08 15:22:27 +05:30
elif players == 3:
2022-05-11 00:29:10 +05:30
return 2
2022-05-08 15:22:27 +05:30
elif players == 4:
return 2
elif players == 5:
return 3
elif players == 6:
return 3
elif players == 7:
return 4
elif players == 8:
return 4
elif players == 10:
return 5
2022-05-08 15:22:27 +05:30
else:
2023-02-19 13:24:59 +05:30
return players - 5
2022-05-11 22:20:06 +05:30
2022-05-11 00:29:10 +05:30
def update_vote_text(votes_needed):
2022-05-11 22:20:06 +05:30
activity = _ba.get_foreground_host_activity()
try:
2023-02-19 13:24:59 +05:30
activity.end_vote_text.node.text = "{} more votes to end this map\ntype 'end' to vote".format(
votes_needed)
2022-05-11 22:20:06 +05:30
except:
2022-05-11 00:29:10 +05:30
with _ba.Context(_ba.get_foreground_host_activity()):
2022-05-11 22:20:06 +05:30
node = ba.NodeActor(ba.newnode('text',
attrs={
'v_attach': 'top',
'h_attach': 'center',
'h_align': 'center',
'color': (1, 1, 0.5, 1),
'flatness': 0.5,
'shadow': 0.5,
'position': (-200, -30),
'scale': 0.7,
'text': '{} more votes to end this map \n type \'end\' to vote'.format(
votes_needed)
})).autoretain()
activity.end_vote_text = node
ba.timer(20, remove_vote_text)
2022-05-11 00:29:10 +05:30
def remove_vote_text():
activity = _ba.get_foreground_host_activity()
2022-05-11 22:20:06 +05:30
if hasattr(activity, "end_vote_text") and activity.end_vote_text.node.exists():
2022-05-11 00:29:10 +05:30
activity.end_vote_text.node.delete()