mirror of
https://github.com/RYGhub/nameless.git
synced 2024-11-21 21:24:18 +00:00
Added chapter two
This commit is contained in:
parent
c7decbd0f5
commit
8d937ca213
2 changed files with 84 additions and 18 deletions
19
database.py
19
database.py
|
@ -21,7 +21,6 @@ class User(Base):
|
||||||
language = Column(String)
|
language = Column(String)
|
||||||
chapter = Column(Integer)
|
chapter = Column(Integer)
|
||||||
|
|
||||||
|
|
||||||
async def message(self, bot, text):
|
async def message(self, bot, text):
|
||||||
await bot.sendMessage(self.id, text)
|
await bot.sendMessage(self.id, text)
|
||||||
|
|
||||||
|
@ -63,6 +62,24 @@ class FirstChapter(Base):
|
||||||
self.current_question = 0
|
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 the script is run as standalone, generate the database
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
Base.metadata.create_all()
|
Base.metadata.create_all()
|
83
main.py
83
main.py
|
@ -3,8 +3,8 @@ import asyncio
|
||||||
import telepot.aio
|
import telepot.aio
|
||||||
import random
|
import random
|
||||||
from telepot.aio.loop import MessageLoop
|
from telepot.aio.loop import MessageLoop
|
||||||
|
from sqlalchemy import or_
|
||||||
from database import session, User, FirstChapter
|
from database import session, User, FirstChapter, SecondChapter
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ async def on_message(message):
|
||||||
session.commit()
|
session.commit()
|
||||||
# Display when an user joins the game
|
# Display when an user joins the game
|
||||||
print(f"{user} has joined Nameless!")
|
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))
|
loop.create_task(call_every_x_seconds(advance_to_chapter_one, 10, user=user))
|
||||||
# If the user is playing the prologue, answer appropriately
|
# If the user is playing the prologue, answer appropriately
|
||||||
if user.chapter == 0:
|
if user.chapter == 0:
|
||||||
|
@ -55,6 +55,29 @@ async def on_message(message):
|
||||||
data.current_question = 2
|
data.current_question = 2
|
||||||
session.commit()
|
session.commit()
|
||||||
await user.message(nlessbot, "See you later then!")
|
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):
|
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
|
# Generate a random number from 0 to 99
|
||||||
rolled_number = random.randrange(100)
|
rolled_number = random.randrange(100)
|
||||||
# 1% chance to pass the check, it should take around 16 minutes
|
# 1% chance to pass the check, it should take around 16 minutes
|
||||||
if rolled_number == 0:
|
if rolled_number != 0:
|
||||||
# Advance the chapter number
|
return
|
||||||
user.chapter = 1
|
# Advance the chapter number
|
||||||
# Create the row for this chapter's data
|
user.chapter = 1
|
||||||
data = FirstChapter(user)
|
# Create the row for this chapter's data
|
||||||
# Add the row to the database
|
data = FirstChapter(user)
|
||||||
session.add(data)
|
# Add the row to the database
|
||||||
session.commit()
|
session.add(data)
|
||||||
# Introduce the chapter to the player
|
session.commit()
|
||||||
await user.message(nlessbot, "What do you think this game will be about?")
|
# Introduce the chapter to the player
|
||||||
# Display that an user has advanced to a new chapter
|
await user.message(nlessbot, "What do you think this game will be about?")
|
||||||
print(f"{user} has advanced to Chapter One!")
|
# Display that an user has advanced to a new chapter
|
||||||
# Stop calling the function
|
print(f"{user} has advanced to Chapter One!")
|
||||||
return ...
|
# 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
|
# Get the bot token from envvars
|
||||||
|
@ -96,6 +144,7 @@ nlessbot_token = os.environ["nameless_token"]
|
||||||
nlessbot = telepot.aio.Bot(nlessbot_token)
|
nlessbot = telepot.aio.Bot(nlessbot_token)
|
||||||
# Initialize the event loop
|
# Initialize the event loop
|
||||||
loop.create_task(MessageLoop(nlessbot, {"chat": on_message}).run_forever())
|
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
|
# Add the events that were stopped when the script was taken down
|
||||||
users = session.query(User).all()
|
users = session.query(User).all()
|
||||||
for user in users:
|
for user in users:
|
||||||
|
|
Loading…
Reference in a new issue