1
Fork 0
mirror of https://github.com/RYGhub/nameless.git synced 2024-10-16 05:17:26 +00:00

Added chapter two

This commit is contained in:
Steffo 2017-07-28 15:37:10 +02:00
parent c7decbd0f5
commit 8d937ca213
2 changed files with 84 additions and 18 deletions

View file

@ -21,7 +21,6 @@ class User(Base):
language = Column(String)
chapter = Column(Integer)
async def message(self, bot, text):
await bot.sendMessage(self.id, text)
@ -63,6 +62,24 @@ class FirstChapter(Base):
self.current_question = 0
class SecondChapter(Base):
"""The save data of the second chapter"""
__tablename__ = "secondchapter"
mic_user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
mic_user = relationship("User", foreign_keys=[mic_user_id])
button_user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
button_user = relationship("User", foreign_keys=[button_user_id])
target_button = Column(Integer, nullable=False)
button_pressed = Column(Integer)
def __init__(self, micuser, buttonuser, target):
self.mic_user_id = micuser.id
self.button_user_id = buttonuser.id
self.target_button = target
# If the script is run as standalone, generate the database
if __name__ == "__main__":
Base.metadata.create_all()

83
main.py
View file

@ -3,8 +3,8 @@ import asyncio
import telepot.aio
import random
from telepot.aio.loop import MessageLoop
from database import session, User, FirstChapter
from sqlalchemy import or_
from database import session, User, FirstChapter, SecondChapter
loop = asyncio.get_event_loop()
@ -22,7 +22,7 @@ async def on_message(message):
session.commit()
# Display when an user joins the game
print(f"{user} has joined Nameless!")
await user.message(nlessbot, "Welcome to Nameless!\nThere is no game here yet.\nOr maybe there is.")
await user.message(nlessbot, "Welcome to Nameless!\nThere is no game here yet.\nOr maybe there is.\nAnyways, please do not delete this chat.")
loop.create_task(call_every_x_seconds(advance_to_chapter_one, 10, user=user))
# If the user is playing the prologue, answer appropriately
if user.chapter == 0:
@ -55,6 +55,29 @@ async def on_message(message):
data.current_question = 2
session.commit()
await user.message(nlessbot, "See you later then!")
elif user.chapter == 2:
data = session.query(SecondChapter).filter(or_(SecondChapter.mic_user == user, SecondChapter.button_user == user)).first()
# Check that the button hasn't been pressed yet
if data.button_pressed is not None:
return
# Message sent from the keyboard user
if data.mic_user == user:
# Forwarc the message to the other player
await data.button_user.message(nlessbot, "The display updated, and now shows:\n" + message["text"].replace("\n", " "))
# Message sent from the button user
elif data.button_user == user:
try:
number = int(message["text"])
except ValueError:
return
if number < 1 or number > 50:
return
await user.message(nlessbot, f"You pressed button #{number}.")
data.button_pressed = number
session.commit()
# Notify the other player that the keyboard stopped working.
await data.mic_user.message(nlessbot, f"The keyboard suddenly vanished.")
async def call_every_x_seconds(coroutine: asyncio.coroutine, timeout: int, *args, **kwargs):
@ -74,20 +97,45 @@ async def advance_to_chapter_one(user):
# Generate a random number from 0 to 99
rolled_number = random.randrange(100)
# 1% chance to pass the check, it should take around 16 minutes
if rolled_number == 0:
# Advance the chapter number
user.chapter = 1
# Create the row for this chapter's data
data = FirstChapter(user)
# Add the row to the database
session.add(data)
session.commit()
# Introduce the chapter to the player
await user.message(nlessbot, "What do you think this game will be about?")
# Display that an user has advanced to a new chapter
print(f"{user} has advanced to Chapter One!")
# Stop calling the function
return ...
if rolled_number != 0:
return
# Advance the chapter number
user.chapter = 1
# Create the row for this chapter's data
data = FirstChapter(user)
# Add the row to the database
session.add(data)
session.commit()
# Introduce the chapter to the player
await user.message(nlessbot, "What do you think this game will be about?")
# Display that an user has advanced to a new chapter
print(f"{user} has advanced to Chapter One!")
# Stop calling the function
return ...
async def advance_to_chapter_two():
"""Try to match two players and advance them to chapter two."""
# Check if at least two players are available
available = session.query(User).filter_by(chapter=1).join(FirstChapter).filter_by(current_question=2).all()
if len(available) < 2:
return
# Match the two players together
firstplayer = available[0]
secondplayer = available[1]
# Advance the players to the second chapter
firstplayer.chapter = 2
secondplayer.chapter = 2
# Create second chapter data
target_number = random.randrange(0, 50) + 1
data = SecondChapter(firstplayer, secondplayer, target_number)
session.add(data)
session.commit()
# Introduce the chapter to the players
await firstplayer.message(nlessbot, f"Look! A floating keyboard appeared!\nA small note on it reads: \"You must get them to press button #{target_number} at all costs.\"\nYou start typing on the keyboard, hoping something will happen...\n(All the messages you'll send from now on will be typed through the keyboard.)")
await secondplayer.message(nlessbot, f"Hey! An array of fifty buttons appeared in front of you.\nThe buttons are numbered from 1 to 50.\nA small display is connected to the button array, and it currently displays a single word: \"STOP.\"\n(Type a number from 1 to 50 to press the corresponding button. You can press only a single button, so be careful! If you don't know what to press, be patient and wait for hints!)")
# Display that two users have been matched and entered a new chapter
print(f"{firstplayer} and {secondplayer} have advanced to Chapter Two!")
# Get the bot token from envvars
@ -96,6 +144,7 @@ nlessbot_token = os.environ["nameless_token"]
nlessbot = telepot.aio.Bot(nlessbot_token)
# Initialize the event loop
loop.create_task(MessageLoop(nlessbot, {"chat": on_message}).run_forever())
loop.create_task(call_every_x_seconds(advance_to_chapter_two, 600))
# Add the events that were stopped when the script was taken down
users = session.query(User).all()
for user in users: