-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
236 lines (214 loc) · 9.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import logging
import asyncio
import sys
from aiogram import types, F
from aiogram.filters.command import Command
from aiogram.methods.delete_webhook import DeleteWebhook
from aiogram.utils.markdown import hlink, hbold, hitalic
from bot import dp, bot
from keyboards import Markups
from random import randint
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
class Game(StatesGroup):
inGame = State()
@dp.message(Command('start'))
async def start(message: types.Message):
await message.answer("It's a Buckshot Roulette game in Telegram!\n"
f"Developer: {hlink('mbutsk', 'https://t.me/mbutsk_bot')}",
reply_markup=await Markups.start(),
link_preview_options=types.LinkPreviewOptions(is_disabled=True))
@dp.callback_query(F.data == 'startGame')
async def startGame(callback: types.CallbackQuery, state: FSMContext):
await callback.answer("Game has been started")
message = callback.message
blanks = randint(1, 4)
live = randint(1, 4)
await state.set_state(Game.inGame)
await state.set_data({
'blanks': blanks,
'live': live,
'hpU': 4,
'hpD': 4
})
await message.answer(f"{hbold('Dealer')}: {hbold(blanks)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(4)}, your hp: {hbold(4)}")
await asyncio.sleep(2)
await message.answer(f"{hbold('Dealer')}: {hitalic('Gives you a gun')}",
reply_markup=await Markups.pistol())
@dp.callback_query(lambda callback: callback.data.startswith('newRound'))
async def newRound(callback: types.CallbackQuery, state: FSMContext):
await callback.answer("New round has been started")
message = callback.message
blanks = randint(1, 4)
live = randint(1, 4)
data = await state.get_data()
hpU = data['hpU']
hpD = data['hpD']
await state.set_data({
'blanks': blanks,
'live': live,
'hpU': hpU,
'hpD': hpD
})
await message.answer(f"{hbold('Dealer')}: {hbold(blanks)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}")
await asyncio.sleep(2)
if callback.data[-1] == 'U':
await message.answer(f"{hbold('Dealer')}: {hitalic('Gives you a gun')}",
reply_markup=await Markups.pistol())
else:
await giveGun(callback, state)
@dp.callback_query(F.data == 'yourself', Game.inGame)
async def yourself(callback: types.CallbackQuery, state: FSMContext):
await callback.answer("You shooted yourself")
message = callback.message
data = await state.get_data()
blanks = data['blanks']
live = data['live']
hpU = data['hpU']
hpD = data['hpD']
if blanks != 0 or live != 0:
if blanks != 0:
blanks_chanches = round(100 / ((live + blanks) / live))
else:
blanks_chanches = 0
number = randint(0, 100)
if number <= blanks_chanches:
await state.update_data({
'blanks': blanks - 1
})
await message.answer(f"Blank. You survived\n{hbold(blanks - 1)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.pistol())
else:
if hpU != 1:
await state.update_data({
'hpU': hpU - 1
})
await message.answer("A live bullet. You lost\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU - 1)}",
reply_markup=await Markups.newRound('D'))
else:
await state.clear()
await message.answer("A live bullet Game over. Dealer won",
reply_markup=await Markups.again())
else:
await state.clear()
await message.answer("Tie. Out of ammo\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.newRound('U'))
@dp.callback_query(F.data == 'Dealer', Game.inGame)
async def toDealer(callback: types.CallbackQuery, state: FSMContext):
await callback.answer("You shooted the Dealer")
message = callback.message
data = await state.get_data()
blanks = data['blanks']
live = data['live']
hpD = data['hpD']
hpU = data['hpU']
if blanks != 0 or live != 0:
if blanks != 0:
blanks_chanches = round(100 / ((live + blanks) / live))
else:
blanks_chanches = 0
number = randint(0, 100)
if number <= blanks_chanches:
await state.update_data({
'blanks': blanks - 1
})
await message.answer(f"Blank. Dealer survived\n{hbold(blanks - 1)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.giveGun())
else:
if hpD != 1:
await state.update_data({
'hpD': hpD - 1
})
await message.answer("A live bullet. You won\n"
f"Dealer hp: {hbold(hpD - 1)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.newRound('D'))
else:
await state.clear()
await message.answer("A live bullet. Game over. You won",
reply_markup=await Markups.again())
else:
await state.clear()
await message.answer("Tie. Out of ammo"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.newRound('U'))
@dp.callback_query(F.data == 'giveGun', Game.inGame)
async def giveGun(callback: types.CallbackQuery, state: FSMContext):
await callback.answer("You gave the gun to Dealer")
message = callback.message
data = await state.get_data()
blanks = data['blanks']
live = data['live']
hpD = data['hpD']
hpU = data['hpU']
if blanks != 0 or live != 0:
if blanks != 0:
blanks_chanches = round(100 / ((live + blanks) / live))
else:
blanks_chanches = 0
number = randint(0, 100)
is_himself = randint(0, 1) == 0
if blanks == 0:
is_himself = False
# Диллер в себя
if is_himself:
await message.answer("Dealer shoots himself.")
await asyncio.sleep(5)
if number <= blanks_chanches:
await state.update_data({
'blanks': blanks - 1
})
await message.answer(f"Blank. Dealer survived\n{hbold(blanks - 1)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.pistol())
else:
if hpD != 1:
await state.update_data({
'hpD': hpD - 1
})
await message.answer("A live bullet. You won\n"
f"Dealer hp: {hbold(hpD - 1)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.newRound('U'))
else:
await state.clear()
await message.answer("A live bullet. Game over. You won",
reply_markup=await Markups.again())
# Диллер в игрока
else:
await message.answer("Dealer shoots you.")
await asyncio.sleep(5)
if number <= blanks_chanches:
await state.update_data({
'blanks': blanks - 1
})
await message.answer(f"Blank. You survived\n{hbold(blanks - 1)} blanks, {hbold(live)} live ammunition\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU)}",
reply_markup=await Markups.pistol())
else:
if hpU != 1:
await state.update_data({
'hpU': hpU - 1
})
await message.answer("A live bullet. You lost\n"
f"Dealer hp: {hbold(hpD)}, your hp: {hbold(hpU - 1)}",
reply_markup=await Markups.newRound('U'))
else:
await state.clear()
await message.answer("A live bullet. Game over. Dealer won",
reply_markup=await Markups.again())
else:
await state.clear()
await message.answer("Tie. Out of ammo",
reply_markup=await Markups.newRound('D'))
async def main() -> None:
# And the run events dispatching
await bot(DeleteWebhook(drop_pending_updates=True))
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())