Skip to content

Commit

Permalink
Merge pull request #46 from milselarch/feat/prompt_dm_votes
Browse files Browse the repository at this point in the history
feat: halve stars cost for voters increase, add current poll limit info to prompt
  • Loading branch information
milselarch authored Nov 25, 2024
2 parents c621c3f + 31cff1c commit 38df98a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
39 changes: 24 additions & 15 deletions handlers/chat_context_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,46 +273,54 @@ async def complete_chat_context(
self, chat_context: CallbackContextState,
update: ModifiedTeleUpdate, context: ContextTypes.DEFAULT_TYPE
):
message: Message = update.message
msg: Message = update.message
extract_context_res = extract_chat_context(update)

if extract_context_res.is_err():
error = extract_context_res.unwrap_err()
return await message.reply_text(error.to_message())
return await msg.reply_text(error.to_message())

extracted_context: ExtractedChatContext = extract_context_res.unwrap()
chat_context = extracted_context.chat_context
inc_voters_context_res = IncMaxVotersChatContext.load(chat_context)
if inc_voters_context_res.is_err():
chat_context.delete()
return await message.reply_text(
return await msg.reply_text(
"Unexpected error loading increase max voter context"
)

inc_voters_context = inc_voters_context_res.unwrap()
poll_id = inc_voters_context.get_poll_id()

if poll_id == BLANK_POLL_ID:
return await message.reply_text(strings.ENTER_POLL_ID_PROMPT)
return await msg.reply_text(strings.ENTER_POLL_ID_PROMPT)
else:
return await message.reply_text(
strings.generate_max_voters_prompt(poll_id)
)
user_id = update.user.get_user_id()
poll_res = Polls.get_as_creator(poll_id, user_id)
if poll_res.is_err():
return await msg.reply_text(
strings.MAX_VOTERS_NOT_EDITABLE
)

poll = poll_res.unwrap()
return await msg.reply_text(strings.generate_max_voters_prompt(
poll_id, current_max=poll.max_voters
))


async def handle_messages(
self, extracted_context: ExtractedChatContext,
update: ModifiedTeleUpdate, context: ContextTypes.DEFAULT_TYPE
):
message: Message = update.message
msg: Message = update.message
chat_context = extracted_context.chat_context
message_text = extracted_context.message_text
user = update.user

inc_voters_context_res = IncMaxVotersChatContext.load(chat_context)
if inc_voters_context_res.is_err():
chat_context.delete()
return await message.reply_text(
return await msg.reply_text(
"Unexpected error loading increase max voter context"
)

Expand All @@ -321,25 +329,26 @@ async def handle_messages(
try:
poll_id = int(message_text)
except ValueError:
return await message.reply_text("Invalid poll ID")
return await msg.reply_text("Invalid poll ID")

poll_res = Polls.get_as_creator(poll_id, user.get_user_id())
if poll_res.is_err():
return await message.reply_text(
return await msg.reply_text(
strings.MAX_VOTERS_NOT_EDITABLE
)

poll = poll_res.unwrap()
inc_voters_context.poll_id = poll_id
inc_voters_context.save_state()
return await message.reply_text(
strings.generate_max_voters_prompt(poll_id)
)
return await msg.reply_text(strings.generate_max_voters_prompt(
poll_id, current_max=poll.max_voters
))
else:
poll_id = inc_voters_context.poll_id
try:
new_max_voters = int(message_text)
except ValueError:
return await message.reply_text(
return await msg.reply_text(
"New maximum number of voters must be an integer"
)

Expand Down
24 changes: 14 additions & 10 deletions handlers/payment_handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
import logging
import math
import re
import textwrap
import pydantic
Expand Down Expand Up @@ -313,39 +314,41 @@ async def fail(err_message: str):
async def set_max_voters(
cls, update: ModifiedTeleUpdate, context: ContextTypes.DEFAULT_TYPE
):
message = update.message
msg = update.message
raw_args = TelegramHelpers.read_raw_command_args(update)
user = update.user

if raw_args == '':
# TODO: implement callback context behavior
IncMaxVotersChatContext(
user_id=user.get_user_id(), chat_id=message.chat_id
user_id=user.get_user_id(), chat_id=msg.chat_id
).save_state()
return await message.reply_text(
return await msg.reply_text(
strings.ENTER_POLL_ID_PROMPT
)
elif constants.ID_PATTERN.match(raw_args) is not None:
poll_id = int(raw_args)
poll_res = Polls.get_as_creator(poll_id, user.get_user_id())
if poll_res.is_err():
return await message.reply_text(
return await msg.reply_text(
strings.MAX_VOTERS_NOT_EDITABLE
)

IncMaxVotersChatContext(
user_id=user.get_user_id(), chat_id=message.chat_id,
user_id=user.get_user_id(), chat_id=msg.chat_id,
poll_id=poll_id
).save_state()
return await message.reply_text(
strings.generate_max_voters_prompt(poll_id)
)

poll = poll_res.unwrap()
return await msg.reply_text(strings.generate_max_voters_prompt(
poll_id, current_max=poll.max_voters
))

# matches two numbers seperated by a space
pattern = re.compile(r'^([1-9]\d*)\s+([1-9]\d*)$')
match_result = pattern.match(raw_args)
if match_result is None:
return await message.reply_text(textwrap.dedent(f"""
return await msg.reply_text(textwrap.dedent(f"""
Invalid arguments
Command format is:
/{Command.SET_MAX_VOTERS} {{poll_id}} {{new_voter_limit}}
Expand Down Expand Up @@ -385,7 +388,8 @@ async def set_max_voters_with_params(

voters_increase = new_max_voters - poll.max_voters
assert voters_increase > 0
payment_amount = voters_increase
payment_amount = math.ceil(voters_increase / 2)
assert isinstance(payment_amount, int)
invoice = IncreaseVoterLimitParams(
poll_id=poll_id, voters_increase=voters_increase
)
Expand Down
6 changes: 3 additions & 3 deletions helpers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def generate_vote_option_prompt(rank: int) -> str:
)


def generate_max_voters_prompt(poll_id: int):
def generate_max_voters_prompt(poll_id: int, current_max: int):
return (
"Enter the new maximum number of voters "
f"that can vote for poll #{poll_id}:"
f"Enter the new maximum number of voters that can vote for "
f"poll #{poll_id} (current limit is {current_max}):"
)


Expand Down

0 comments on commit 38df98a

Please sign in to comment.