-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
313 lines (233 loc) · 11.3 KB
/
game.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import telepot
import time
import random
import os
import requests
import io
import json
import redis
import threading
import functools
import costant
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineQueryResultArticle, InputTextMessageContent,InlineQueryResultCachedGif,InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, KeyboardButton
from pprint import pprint
from threading import Timer
from threading import Thread
import time
class Game:
def __init__(self,bot,gamer):
self.bot=bot
self.gamer=gamer
#TODO scrivere le regole
#TODO aggiungere ad update,la list di turner deve essere aggiornata ogni votla che un giocatore viene eliminato e
# bisogna controllare se è 0
#TODO rendere idempotente l'attacco
#TODO scrivere cosa succede se esce evocazione e preghiera
self.attackTurn=2
self.listTurner=[]
self.fillListTurner()
self.currentTurn='Attack'
self.effectUsed=0
self.effectOn=0
self.currentEffect=0
self.defenseWin=0
self.currentIndexGifAttack=0
self.currentDefensor=0
def getIndexFromId(self,id):
for i in range(2, len(self.gamer) ):
if self.gamer[i].id==id:
return i
return -1
def update(self,msg):
i=self.gamer.getIndexFromId(msg['from']['id'])
self.gamer.pop(i)
if self.attackTurn==i:
self.addTurn()
def chatIdIsInGame(self,chat_id):
if self.gamer[1]==chat_id:
return 1
return 0
def addTurn(self):
self.attackTurn+=1
if self.attackTurn > len(self.gamer)-1:
self.attackTurn=2
def fillListTurner(self):
if len(self.gamer)==0:
pprint("ERRORE,LISTA VUOTA DI GAMER QUANDO RIEMPIO LA LISTA DI TURNI")
else:
for i in range(2,len(self.gamer)):
self.listTurner.append(i)
def checkRefillListTurner(self):
if len(self.listTurner)==0:
self.fillListTurner()
def setRandomAttackTurn(self):
#la random mi restituisce l elemento random nella lista di turni che salva le posizioni del vettore dei giocatori
# ad esempio restituisce la posizione dell utente 2 nel vettore di gamer
self.checkRefillListTurner()
self.attackTurn=random.choice(self.listTurner)
pprint("lista di turni"+str(self.listTurner))
self.listTurner.remove(self.attackTurn)
def start(self):
self.setRandomAttackTurn()
for x in range(len(self.gamer)-2):
keyboard=ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text=costant.kboardAttack),KeyboardButton(text=costant.kboardEvocation)],
[KeyboardButton(text=costant.kboardDefense),KeyboardButton(text=costant.kboardPrayer)],
[KeyboardButton(text='Punteggio: '+str(self.gamer[x+2].punteggio)),KeyboardButton(text=costant.kboardEffect)],
[KeyboardButton(text=costant.kboardEscape)]
],one_time_keyboard=True,selective=True
)
self.bot.sendMessage(self.gamer[1],self.gamer[x+2].name+" è entrato nel gioco",reply_to_message_id=self.gamer[x+2].messageId ,reply_markup=keyboard)
self.currentTurn='Attack'
self.bot.sendMessage(self.gamer[1],self.gamer[self.attackTurn].name+" deve attaccare/evocare! ⚔️⚔️")
def newRound(self):
self.setRandomAttackTurn()
time.sleep(5)
for x in range(len(self.gamer)-2):
keyboard=ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text=costant.kboardAttack),KeyboardButton(text=costant.kboardEvocation)],
[KeyboardButton(text=costant.kboardDefense),KeyboardButton(text=costant.kboardPrayer)],
[KeyboardButton(text='Punteggio: '+str(self.gamer[x+2].punteggio)),KeyboardButton(text=costant.kboardEffect)],
[KeyboardButton(text=costant.kboardEscape)]
],one_time_keyboard=True,selective=True
)
self.bot.sendMessage(self.gamer[1],self.gamer[x+2].name+" ha "+ str(self.gamer[x+2].punteggio) + " punti.",reply_to_message_id=self.gamer[x+2].messageId ,reply_markup=keyboard)
self.currentTurn='Attack'
self.bot.sendMessage(self.gamer[1],self.gamer[self.attackTurn].name+" deve attaccare/evocare! ")
def getPlayer(self,idGamer):
index=self.getIndexFromId(idGamer)
return self.gamer[index]
def trollEffect(self,idGamer):
index=self.getIndexFromId(idGamer)
for i in range(len(self.gamer[index].trollEv)):
if self.gamer[index].trollEv[i]>0:
if costant.evocationTroll[i]=='perditurno':
self.gamer[index].trollEv[i]-=1
return 1
else :
if costant.evocationTroll[i]=='sceglichifarperdereturno' :
self.gamer[index].trollEv[i]-=1
return 0
def setTrollEvocation(self,effect,idGamer):
index=self.getIndexFromId(idGamer)
self.gamer[index].trollEv[effect]+=1
if self.defenseWin==1:
self.currentDefensor=0
self.defenseWin=0
def setEvocation(self,effect,idGamer):
index=self.getIndexFromId(idGamer)
self.gamer[index].effectEv[effect]+=1
if self.defenseWin==1:
self.currentDefensor=0
self.defenseWin=0
def rightTurnEvocation(self,gamer):
if self.rightTurnAttack(gamer)==1 and self.effectOn==0:
self.effectOn=1
return 1
if self.currentDefensor.id== gamer and self.currentTurn=='Defense' and self.defenseWin==1 and self.effectOn==0:
self.effectOn=1
return 1
return -1
def rightTurnEffect(self,gamer):
if self.rightTurnAttack(gamer)==1 or self.rightTurnDefense(gamer)==1:
return 1
return -1
def rightTurnAttack(self,gamer):
if self.gamer[self.attackTurn].id== gamer and self.currentTurn=='Attack' :
return 1
return -1
def updatePoint(self,gamer,point):
self.getPlayer(gamer)
gamer.punteggio+=point
def back(self,player,index):
keyboard=ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text=costant.kboardAttack),KeyboardButton(text=costant.kboardEvocation)],
[KeyboardButton(text=costant.kboardDefense),KeyboardButton(text=costant.kboardPrayer)],
[KeyboardButton(text='Punteggio: '+str(player.punteggio)),KeyboardButton(text=costant.kboardEffect)],
[KeyboardButton(text=costant.kboardEscape)]
],one_time_keyboard=True,selective=True
)
self.bot.sendMessage(self.gamer[1],"L'effetto è stato utilizzato:"+ costant.kboardEffect[index] +"puoi attaccare!",reply_to_message_id=player.messageId ,reply_markup=keyboard)
def useEffect(self,index):
#attacco 2x
if index==0:
self.currentEffect=1
#attacco due volte
elif index==4:
self.currentEffect=2
elif index==1:
#def 2x
self.currentEffect=3
elif index==3:
#non ti attaccano
self.currentEffect=4
def useAttEffect(self,msg,idGamer):
pprint("use effect")
self.effectUsed=1
index=costant.keyboardEvocation.index(msg)
indexUser= self.getIndexFromId(idGamer)
self.gamer[indexUser].effectEv[index]-=1
pprint("use effect"+ str( self.gamer[index].effectEv))
self.useEffect(index)
self.back(self.gamer[indexUser],index)
def showEffects(self,gamer):
index=self.getIndexFromId(gamer)
effects=[]
for i in range(len(self.gamer[index].effectEv)):
if self.gamer[index].effectEv[i]>0:
effects.append(costant.keyboardEvocation[i])
if len(effects)==0:
return -1
else:
key=[]
i=0
while (i<len(effects)):
if len(effects)==1:
key.append([KeyboardButton(text=effects[i])])
else:
key.append([KeyboardButton(text=effects[i]),KeyboardButton(text=effects[i+1])])
i+=2
keyboard=ReplyKeyboardMarkup(keyboard=key,one_time_keyboard=True,selective=True)
self.bot.sendMessage(self.gamer[1],self.gamer[index].name+" scegli cosa evocare!",reply_to_message_id=self.gamer[index].messageId ,reply_markup=keyboard)
self.currentTurn='Attack'
self.bot.sendMessage(self.gamer[1],self.gamer[self.attackTurn].name+" deve attaccare/evocare! ")
return 1
#controlla se l'avversario scelto dall'attaccante è valido e se l'attaccante aveva il permesso di sferrare l'attacco
def checkResultTargetAttack(self,id,name):
pprint("sono in attack 2")
pprint(self.attackTurn)
if id==self.gamer[self.attackTurn].id and self.currentTurn=='Attack':
pprint("sono in attack 3")
for i in range(2,len(self.gamer)):
if name==self.gamer[i].name:
self.currentDefensor=self.gamer[i]
return 1
else:
return -1
def computePoint(self,defensePoint):
pprint("defense: "+str(defensePoint))
pprint("attack: "+str(self.currentIndexGifAttack))
total_point=0
total_point=self.currentIndexGifAttack-defensePoint
#se il numero è positivo significa che ha vinto l'attacco e quindi tolgo i punti alla difesa, se è negativo il contrario
if total_point>0:
#vince attacco
total_point-=2*total_point
self.updatePoint(self.currentDefensor,total_point)
self.bot.sendMessage(self.gamer[1],"VINCEEE....................L'ATTACCOOOOO! 🎊🎉🎊🎉🎊🎉 ")
self.newRound()
self.currentDefensor=0
elif total_point==0:
self.bot.sendMessage(self.gamer[1],"Signori e signori ...vinceeeeeee....PAREGGIO")
self.newRound()
self.currentDefensor=0
else:
#vince difesa
self.updatePoint(self.gamer[self.attackTurn],total_point)
self.bot.sendMessage(self.gamer[1],"VINCEEEE...................LA DIFESAAAAAA! 🎊🎉🎊🎉🎊🎉")
self.bot.sendMessage(self.gamer[1],"PUOI LANCIARE UN'EVOCAZIONE! 🎊🎉🎊🎉🎊🎉")
self.defenseWin=1
def rightTurnDefense(self,gamer):
if self.currentDefensor.id== gamer and self.currentTurn=='Defense' :
return 1
return -1
def prayer(self):
self.bot.sendSticker(self.gamer[1],sticker=costant.oak)