3c3cad9f by Barry

Basic pankration added with arena.

1 parent 8ccecd68
...@@ -337,9 +337,74 @@ def db_remove_reflector(member_id, idx): ...@@ -337,9 +337,74 @@ def db_remove_reflector(member_id, idx):
337 WHERE member_id = ?;""", (pickle.dumps(reflectors), member_id)) 337 WHERE member_id = ?;""", (pickle.dumps(reflectors), member_id))
338 conn.commit() 338 conn.commit()
339 339
340
340 def db_convert_soul_plate_to_reflector(member_id, soul_plate, idx): 341 def db_convert_soul_plate_to_reflector(member_id, soul_plate, idx):
341 db_add_reflector(member_id, soul_plate) 342 if not db_add_reflector(member_id, soul_plate):
342 db_remove_soul_plate(member_id, idx) 343 return False
344 if not db_remove_soul_plate(member_id, idx):
345 return False
346 return True
347
348
349 def db_register_battle(member_id, reflector, idx, target_member=None):
350 pan_record = db_get_pankration_record(member_id)
351 conn = sqlite3.connect('db.sqlite3')
352 c = conn.cursor()
353 db_state = c.execute("SELECT battle_id, reflector_primary member_id FROM pankration_arena WHERE battle_status = 'waiting';").fetchall()
354 if not db_state:
355 log("No battles available: {}".format(idx,))
356 c.execute("""INSERT INTO pankration_arena(primary_member_id, reflector_primary)
357 VALUES(?, ?);""", (member_id, pickle.dumps(reflector)))
358 conn.commit()
359 conn.close()
360 db_remove_reflector(member_id, idx)
361 else:
362 for row in db_state:
363 primary_reflector = pickle.loads(str(row[1]))
364 primary_level = primary_reflector.level
365 # TODO: Setup challenges based on level difference of no more than 10 or spawn a new monster.
366 # if primary_level - 10 <= reflector.level <= primary_level + 10:
367 battle_id = row[0]
368 c.execute("""UPDATE pankration_arena SET secondary_member_id = ?, reflector_secondary = ?, battle_status = 'queued', queue_start = ?
369 WHERE battle_id = ?;""", (member_id, pickle.dumps(reflector), datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), battle_id))
370 conn.commit()
371 conn.close()
372 db_remove_reflector(member_id, idx)
373 return
374
375 def db_complete_battle(battle_id, primary_member_id, primary_reflector, secondary_member_id,
376 secondary_reflector):
377 pass
378
379
380 def db_start_battle(battle_id):
381 conn = sqlite3.connect('db.sqlite3')
382 c = conn.cursor()
383 c.execute("""UPDATE pankration_arena SET battle_status = 'fighting', battle_start = ?
384 WHERE battle_id = ?;""", (datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), battle_id))
385 conn.commit()
386 conn.close()
387
388 def db_get_battle_queue():
389 conn = sqlite3.connect('db.sqlite3')
390 c = conn.cursor()
391 result = c.execute("SELECT * FROM pankration_arena WHERE battle_status = 'queued' ORDER BY queue_start;").fetchall()
392 conn.close()
393 results = []
394 for row in result:
395 results.append(dict_factory(c, row))
396
397 return results
398
399
400 def db_get_member_username(member_id):
401 # Do a lookup by ID, if it's found but the name doesn't match then add a row to aliases with the previous name and change the member name
402 member_conn = sqlite3.connect('db.sqlite3')
403
404 c = member_conn.cursor()
405 result = c.execute("SELECT member_name FROM members WHERE member_id = ?;", (member_id,)).fetchone()
406 member_conn.close()
407 return result[0]
343 408
344 409
345 def db_get_member(discord_id=None, username=None): 410 def db_get_member(discord_id=None, username=None):
......
No preview for this file type
...@@ -27,7 +27,7 @@ import wolframalpha ...@@ -27,7 +27,7 @@ import wolframalpha
27 import sqlite3 27 import sqlite3
28 from blackjack import Blackjack 28 from blackjack import Blackjack
29 import data 29 import data
30 from pankration import Pankration, HuntResponse, Jobs 30 from pankration import Pankration, HuntResponse, Jobs, Action, MissAction, AttackAction, DefeatAction, TemperamentPosture, TemperamentAttitude
31 31
32 VERSION = 2.3 32 VERSION = 2.3
33 33
...@@ -35,6 +35,8 @@ quitting = False ...@@ -35,6 +35,8 @@ quitting = False
35 35
36 grant_hour = 11 36 grant_hour = 11
37 37
38 battle_in_progress = False
39
38 conn = sqlite3.connect('db.sqlite3') 40 conn = sqlite3.connect('db.sqlite3')
39 41
40 credentials = 'creds.json' 42 credentials = 'creds.json'
...@@ -89,6 +91,8 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help', ...@@ -89,6 +91,8 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help',
89 '!listreflectors': 'do_list_reflectors', '!listreflector': 'do_list_reflectors', 91 '!listreflectors': 'do_list_reflectors', '!listreflector': 'do_list_reflectors',
90 '!listsoulplates': 'do_list_soul_plates', '!listsoulplate': 'do_list_soul_plates', 92 '!listsoulplates': 'do_list_soul_plates', '!listsoulplate': 'do_list_soul_plates',
91 '!convertplate': 'do_convert_plate', '!convertsoulplate': 'do_convert_plate', 93 '!convertplate': 'do_convert_plate', '!convertsoulplate': 'do_convert_plate',
94 '!assignskill': 'do_assign_skill',
95 '!registerbattle': 'do_register_battle',
92 '!pankration': 'do_pankration', 96 '!pankration': 'do_pankration',
93 } 97 }
94 98
...@@ -102,6 +106,7 @@ def log(message): ...@@ -102,6 +106,7 @@ def log(message):
102 except: 106 except:
103 pass 107 pass
104 108
109
105 def send_message(client, target, message): 110 def send_message(client, target, message):
106 if type(target) is not discord.channel.PrivateChannel: 111 if type(target) is not discord.channel.PrivateChannel:
107 if target.id == '121468616414724100': 112 if target.id == '121468616414724100':
...@@ -110,6 +115,7 @@ def send_message(client, target, message): ...@@ -110,6 +115,7 @@ def send_message(client, target, message):
110 return 115 return
111 client.send_message(target, message) 116 client.send_message(target, message)
112 117
118
113 def format_exception(e): 119 def format_exception(e):
114 exception_list = traceback.format_stack() 120 exception_list = traceback.format_stack()
115 exception_list = exception_list[:-2] 121 exception_list = exception_list[:-2]
...@@ -310,7 +316,7 @@ def do_gif(client, message_parts, message): ...@@ -310,7 +316,7 @@ def do_gif(client, message_parts, message):
310 316
311 def do_gameslist(client, message_parts, message): 317 def do_gameslist(client, message_parts, message):
312 limit = 20 318 limit = 20
313 log("parts: {} {}".format(message_parts,message)) 319 log("parts: {} {}".format(message_parts, message))
314 if len(message_parts) > 0 and message_parts[0].isdigit(): 320 if len(message_parts) > 0 and message_parts[0].isdigit():
315 limit = int(message_parts[0]) 321 limit = int(message_parts[0])
316 games_list = data.db_get_games_list(limit) 322 games_list = data.db_get_games_list(limit)
...@@ -507,7 +513,7 @@ def do_startraffle(client, message_parts, message): ...@@ -507,7 +513,7 @@ def do_startraffle(client, message_parts, message):
507 for member in client.get_all_members(): 513 for member in client.get_all_members():
508 log(member.id) 514 log(member.id)
509 if member.id == '78767557628133376': 515 if member.id == '78767557628133376':
510 priv_message = "1st: {} key: {}\n2nd: {} keys: {}\n3rd: {} keys: {}".format(byteify(winner), game_key, byteify(second), ' '.join(random_keys[0:2]), byteify(third), random_keys[3]) 516 priv_message = "1st: {} key: {}\n2nd: {} keys: {}\n3rd: {} keys: {}".format(byteify(winner), game_key, byteify(second), ' '.join(random_keys[0:2]), byteify(third), random_keys[2])
511 log(priv_message) 517 log(priv_message)
512 send_message(client, member, priv_message) 518 send_message(client, member, priv_message)
513 break 519 break
...@@ -1047,6 +1053,41 @@ def do_pankration(client, message_parts, message): ...@@ -1047,6 +1053,41 @@ def do_pankration(client, message_parts, message):
1047 1053
1048 """.format(message.author.mention())) 1054 """.format(message.author.mention()))
1049 1055
1056
1057 def do_register_battle(client, message_parts, message):
1058 member = data.db_get_member(message.author.id)
1059
1060 if len(message_parts) < 1:
1061 send_message(client, message.channel, 'You must provide at least 1 arguments to register for a battle. The reflector number and optionally a user name you wish to battle. Please see !pankration for an example.')
1062 return
1063
1064 reflector_num = message_parts[0]
1065 pankration_data = data.db_get_pankration_record(member['member_id'])
1066 if pankration_data and pankration_data['reflectors']:
1067 reflectors = pickle.loads(str(pankration_data['reflectors']))
1068
1069 if not reflector_num.isdigit() or int(reflector_num) < 1 or int(reflector_num) > len(reflectors):
1070 send_message(client, message.channel, 'The requested reflector is invalid. Please provide the number from !listreflectors')
1071 return
1072 reflector_num = int(reflector_num) - 1
1073
1074 if len(message_parts) > 1:
1075 user = ' '.join(message_parts[1:])
1076
1077 data.db_register_battle(member['member_id'], reflectors[reflector_num], reflector_num, user)
1078 send_message(client, message.channel, "Your *{} level {}* has been registered for battle with {}. The battle will begin when the arena is availble with the next challenger".format(reflectors[reflector_num].monster_type, reflectors[reflector_num].level, user))
1079 else:
1080 data.db_register_battle(member['member_id'], reflectors[reflector_num], reflector_num)
1081 send_message(client, message.channel, "Your *{} level {}* has been registered for battle. The battle will begin when the arena is availble with the next challenger".format(reflectors[reflector_num].monster_type, reflectors[reflector_num].level))
1082 else:
1083 send_message(client, message.channel, 'You have no available reflectors. You can get reflectors by converting soul plates with !convertplate')
1084 return
1085
1086
1087 def do_assign_skill(client, message_parts, message):
1088 send_message(client, message.channel, '**Feral Skills are not yet supported**')
1089
1090
1050 def do_convert_plate(client, message_parts, message): 1091 def do_convert_plate(client, message_parts, message):
1051 member = data.db_get_member(message.author.id) 1092 member = data.db_get_member(message.author.id)
1052 1093
...@@ -1064,12 +1105,15 @@ def do_convert_plate(client, message_parts, message): ...@@ -1064,12 +1105,15 @@ def do_convert_plate(client, message_parts, message):
1064 if not plate_num.isdigit() or int(plate_num) < 1 or int(plate_num) > len(soul_plates): 1105 if not plate_num.isdigit() or int(plate_num) < 1 or int(plate_num) > len(soul_plates):
1065 send_message(client, message.channel, 'The requested plate is invalid. Please provide the number from !listsoulplates') 1106 send_message(client, message.channel, 'The requested plate is invalid. Please provide the number from !listsoulplates')
1066 return 1107 return
1067 plate_num = int(plate_num) 1108 plate_num = int(plate_num) - 1
1068 if convert_to == "reflector": 1109 if convert_to == "reflector":
1069 # do reflector stuff 1110 # do reflector stuff
1070 data.db_convert_soul_plate_to_reflector(member['member_id'], soul_plates[plate_num], plate_num) 1111 if data.db_convert_soul_plate_to_reflector(member['member_id'], soul_plates[plate_num], plate_num):
1071 pass 1112 send_message(client, message.channel, 'The soul plate was successfully converted into an official reflector. To view your reflectors: !listreflectors')
1113 else:
1114 send_message(client, message.channel, 'There was an issue converting your soul plate...Conversion failed.')
1072 elif convert_to == "skill": 1115 elif convert_to == "skill":
1116 send_message(client, message.channel, '**Feral Skills are not yet supported**')
1073 # do skill stuff 1117 # do skill stuff
1074 pass 1118 pass
1075 else: 1119 else:
...@@ -1080,6 +1124,7 @@ def do_convert_plate(client, message_parts, message): ...@@ -1080,6 +1124,7 @@ def do_convert_plate(client, message_parts, message):
1080 else: 1124 else:
1081 send_message(client, message.channel, 'You have no soul plates to convert.') 1125 send_message(client, message.channel, 'You have no soul plates to convert.')
1082 1126
1127
1083 def do_list_soul_plates(client, message_parts, message): 1128 def do_list_soul_plates(client, message_parts, message):
1084 member = data.db_get_member(message.author.id) 1129 member = data.db_get_member(message.author.id)
1085 1130
...@@ -1090,6 +1135,7 @@ def do_list_soul_plates(client, message_parts, message): ...@@ -1090,6 +1135,7 @@ def do_list_soul_plates(client, message_parts, message):
1090 else: 1135 else:
1091 send_message(client, message.channel, 'You have no soul plates.') 1136 send_message(client, message.channel, 'You have no soul plates.')
1092 1137
1138
1093 def do_list_reflectors(client, message_parts, message): 1139 def do_list_reflectors(client, message_parts, message):
1094 member = data.db_get_member(message.author.id) 1140 member = data.db_get_member(message.author.id)
1095 1141
...@@ -1111,6 +1157,7 @@ def do_list_feral_skills(client, message_parts, message): ...@@ -1111,6 +1157,7 @@ def do_list_feral_skills(client, message_parts, message):
1111 else: 1157 else:
1112 send_message(client, message.channel, 'You have no feral skills.') 1158 send_message(client, message.channel, 'You have no feral skills.')
1113 1159
1160
1114 def do_hunt_monster(client, message_parts, message): 1161 def do_hunt_monster(client, message_parts, message):
1115 p = Pankration() 1162 p = Pankration()
1116 hunt_response = p.hunt_monster(' '.join(message_parts)) 1163 hunt_response = p.hunt_monster(' '.join(message_parts))
...@@ -1122,16 +1169,82 @@ def do_hunt_monster(client, message_parts, message): ...@@ -1122,16 +1169,82 @@ def do_hunt_monster(client, message_parts, message):
1122 str_out += str(soul_plate.get_soul_plate_description()) 1169 str_out += str(soul_plate.get_soul_plate_description())
1123 send_message(client, message.channel, str_out) 1170 send_message(client, message.channel, str_out)
1124 1171
1172
1125 def do_list_zones(client, message_parts, message): 1173 def do_list_zones(client, message_parts, message):
1126 p = Pankration() 1174 p = Pankration()
1127 send_message(client, message.channel, ', '.join(p.list_zones())) 1175 send_message(client, message.channel, ', '.join(p.list_zones()))
1128 return 1176 return
1129 1177
1130 1178
1179 def check_arena():
1180 global battle_in_progress
1181 if battle_in_progress:
1182 return
1183 try:
1184 battle_in_progress = True
1185 battle = data.db_get_battle_queue()
1186 if len(battle) == 0:
1187 return
1188 battle = battle[0]
1189
1190 for channel in client.get_all_channels():
1191 if channel.id == '151942626130657280':
1192 arena_channel = channel
1193 break
1194 # pop first battle off queue
1195 log("Starting Battle: {}".format(battle['battle_id']))
1196 monster = pickle.loads(str(battle['reflector_primary']))
1197 monster2 = pickle.loads(str(battle['reflector_secondary']))
1198
1199 send_message(client, arena_channel, "Ladies and gentlemen!\nFor our next match...")
1200 time.sleep(5)
1201 send_message(client, arena_channel, "In the red corner we have...\n **{}**!".format(monster.monster_type))
1202 time.sleep(5)
1203 send_message(client, arena_channel, "Hmmm... This monster seems {} and {}.".format(monster.get_current_posture()["name"], monster.get_current_attitude()["name"]))
1204 time.sleep(5)
1205 send_message(client, arena_channel, "And in the blue corner is...\n **{}**!".format(monster2.monster_type))
1206 time.sleep(5)
1207 send_message(client, arena_channel, "Hmmm... This monster seems {} and {}.".format(monster2.get_current_posture()["name"], monster2.get_current_attitude()["name"]))
1208 time.sleep(5)
1209 send_message(client, arena_channel, "Alright, Pankration fans, the match is about to begin!\n *Chaaaaaarge!*".format(monster2.monster_type))
1210 time.sleep(5)
1211 data.db_start_battle(battle['battle_id'])
1212
1213 p = Pankration()
1214
1215 battle_arena = p.start_battle(monster, monster2, "not used")
1216 fighting = True
1217 while fighting:
1218 actions = battle_arena.step()
1219 time.sleep(4)
1220 out_str = ""
1221 for action in actions:
1222 if isinstance(action, MissAction):
1223 out_str += "{} {} {}.\n".format(action.attacker.monster_type, action.message, action.target.monster_type)
1224 if isinstance(action, AttackAction):
1225 out_str += "{} {} {} for {}.\n".format(action.attacker.monster_type, action.message, action.target.monster_type, int(action.damage))
1226 if isinstance(action, DefeatAction):
1227 out_str += "\n\n**{}** {}. {} gains {} xp.\n\n".format(action.target.monster_type, action.message, action.attacker.monster_type, action.xp)
1228 fighting = False
1229 break
1230 out_str += "\n{} {}% - {} {}%\n".format(monster.monster_type, monster.get_hp_percent(), monster2.monster_type, monster2.get_hp_percent())
1231 log(out_str)
1232 send_message(client, arena_channel, byteify(out_str))
1233 #wait after each match before starting a new fight.
1234 time.sleep(30)
1235
1236 except Exception as e:
1237 log("{} - {}".format(format_exception(e), e.message))
1238 finally:
1239 battle_in_progress = False
1240
1131 ### END PANKRATION 1241 ### END PANKRATION
1132 def start_timer(client): 1242 def start_timer(client):
1133 needs_loot = True 1243 needs_loot = True
1134 while not quitting: 1244 while not quitting:
1245 if not battle_in_progress:
1246 thread.start_new_thread(check_arena, ())
1247
1135 if datetime.datetime.now().hour == grant_hour: 1248 if datetime.datetime.now().hour == grant_hour:
1136 needs_loot = True 1249 needs_loot = True
1137 # This should fire anytime they need loot and it isn't 9.. this will change to 11 after testing 1250 # This should fire anytime they need loot and it isn't 9.. this will change to 11 after testing
...@@ -1140,8 +1253,9 @@ def start_timer(client): ...@@ -1140,8 +1253,9 @@ def start_timer(client):
1140 if channel.id == '47934985176354816': 1253 if channel.id == '47934985176354816':
1141 do_grantcredits(client, None, None, channel) 1254 do_grantcredits(client, None, None, channel)
1142 needs_loot = False 1255 needs_loot = False
1143 time.sleep(10000) 1256 time.sleep(10)
1144 #log('Sleeping') 1257 # log('Sleeping... Time: {}'.format(datetime.datetime.now()))
1258
1145 1259
1146 def thread_exception_handler(method, client, message_parts, message): 1260 def thread_exception_handler(method, client, message_parts, message):
1147 try: 1261 try:
...@@ -1152,6 +1266,7 @@ def thread_exception_handler(method, client, message_parts, message): ...@@ -1152,6 +1266,7 @@ def thread_exception_handler(method, client, message_parts, message):
1152 except Exception as e: 1266 except Exception as e:
1153 log("{} - {}".format(format_exception(e), e.message)) 1267 log("{} - {}".format(format_exception(e), e.message))
1154 1268
1269
1155 ################# 1270 #################
1156 # Client Events 1271 # Client Events
1157 ################# 1272 #################
...@@ -1177,11 +1292,12 @@ def on_status(member): ...@@ -1177,11 +1292,12 @@ def on_status(member):
1177 log("Exception: {}".format(format_exception(e))) 1292 log("Exception: {}".format(format_exception(e)))
1178 pass 1293 pass
1179 1294
1295
1180 @client.event 1296 @client.event
1181 def on_message(message): 1297 def on_message(message):
1182 #print message.content 1298 # print message.content
1183 #print message.author 1299 # print message.author
1184 #print client.user 1300 # print client.user
1185 # we do not want the bot to reply to itself 1301 # we do not want the bot to reply to itself
1186 if message.author == client.user: 1302 if message.author == client.user:
1187 return 1303 return
......
...@@ -365,10 +365,10 @@ class Monster: ...@@ -365,10 +365,10 @@ class Monster:
365 break 365 break
366 366
367 def get_current_posture(self): 367 def get_current_posture(self):
368 return TemperamentPosture(self.temperament_posture) 368 return TemperamentPosture[self.temperament_posture]
369 369
370 def get_current_attitude(self): 370 def get_current_attitude(self):
371 return TemperamentAttitude(self.temperament_attitude) 371 return TemperamentAttitude[self.temperament_attitude]
372 372
373 def get_dicipline_level(self): 373 def get_dicipline_level(self):
374 if self.dicipline_level < 2: 374 if self.dicipline_level < 2:
......