forked from Cryz/battle_royale_sim
show map with players on telegram bot
This commit is contained in:
4
bot.py
4
bot.py
@@ -24,7 +24,7 @@ async def bot_start(update, context):
|
|||||||
['Init/Restart'],
|
['Init/Restart'],
|
||||||
['Add Player', 'Add random Players', 'Add random color Players'],
|
['Add Player', 'Add random Players', 'Add random color Players'],
|
||||||
['Get Players', 'Get Alive Players', 'Get Death Players', 'Get Ranking Players',],
|
['Get Players', 'Get Alive Players', 'Get Death Players', 'Get Ranking Players',],
|
||||||
['Simulate Day', 'Run Periodically']
|
['Simulate Day', 'Run Periodically', 'Show Map']
|
||||||
]
|
]
|
||||||
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
|
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
|
||||||
|
|
||||||
@@ -64,6 +64,8 @@ async def bot_commands(update, context):
|
|||||||
return await _cmd.cmd_add_random_players(context, update, chat_id)
|
return await _cmd.cmd_add_random_players(context, update, chat_id)
|
||||||
if text == 'Add random color Players':
|
if text == 'Add random color Players':
|
||||||
return await _cmd.cmd_add_random_color_players(context, update, chat_id)
|
return await _cmd.cmd_add_random_color_players(context, update, chat_id)
|
||||||
|
if text == 'Show Map':
|
||||||
|
return await _cmd.cmd_show_game_map(context, update, chat_id)
|
||||||
|
|
||||||
# special commands
|
# special commands
|
||||||
if text == 'upstart': return await _scmd.update_bot(update, context)
|
if text == 'upstart': return await _scmd.update_bot(update, context)
|
||||||
|
|||||||
@@ -61,6 +61,12 @@ async def cmd_get_ranking_players(context, update, chat_id):
|
|||||||
async def cmd_simulate_day(context, update, chat_id):
|
async def cmd_simulate_day(context, update, chat_id):
|
||||||
return await _bot_simulation.simulate_day(context, chat_id)
|
return await _bot_simulation.simulate_day(context, chat_id)
|
||||||
|
|
||||||
|
async def cmd_show_game_map(context, update, chat_id):
|
||||||
|
Arena= context.application.bot_data['arena']
|
||||||
|
Gamemap= Arena.get_map()
|
||||||
|
rendered_map= Gamemap.get_renderized_map()
|
||||||
|
return await update.message.reply_text(rendered_map)
|
||||||
|
|
||||||
async def cmd_simulate_day_cron(context, update, chat_id):
|
async def cmd_simulate_day_cron(context, update, chat_id):
|
||||||
context.application.bot_data['ask_seconds'] = 1
|
context.application.bot_data['ask_seconds'] = 1
|
||||||
if 'ask_name' in context.application.bot_data:
|
if 'ask_name' in context.application.bot_data:
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ from bot_libs import syms as _bot_syms
|
|||||||
async def add_player(update, context, chat_id, name):
|
async def add_player(update, context, chat_id, name):
|
||||||
_log.log_info(f'add_player: {chat_id} - {name}')
|
_log.log_info(f'add_player: {chat_id} - {name}')
|
||||||
Arena= context.application.bot_data['arena']
|
Arena= context.application.bot_data['arena']
|
||||||
if len(Arena.get_players()) >= 70: return # prevent message too long error
|
#if len(Arena.get_players()) >= 70: return # prevent message too long error
|
||||||
|
if len(Arena.get_players()) >= 20: return # maybe this should depend on the map dimension
|
||||||
Arena.add_player(name)
|
Arena.add_player(name)
|
||||||
|
|
||||||
async def add_random_players(update, context, chat_id, colors_names= False):
|
async def add_random_players(update, context, chat_id, colors_names= False):
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from entities import player as _player
|
from entities import player as _player
|
||||||
from entities import event_picker as _events
|
from entities import event_picker as _events
|
||||||
|
from entities import gamemap as _map
|
||||||
from entities.items import weapons as _weapons
|
from entities.items import weapons as _weapons
|
||||||
|
|
||||||
class BrSimArena():
|
class BrSimArena():
|
||||||
@@ -14,6 +15,7 @@ class BrSimArena():
|
|||||||
self.eventClass = _events.ArenaEventPicker(self.players)
|
self.eventClass = _events.ArenaEventPicker(self.players)
|
||||||
self.init_players(players)
|
self.init_players(players)
|
||||||
self.init_weapons(weapons)
|
self.init_weapons(weapons)
|
||||||
|
self.Map= _map.BrSimMap(self.players, self.weapons)
|
||||||
|
|
||||||
def init_players(self, players):
|
def init_players(self, players):
|
||||||
if not players: return
|
if not players: return
|
||||||
@@ -110,10 +112,12 @@ class BrSimArena():
|
|||||||
def add_player(self, name, inventory= None):
|
def add_player(self, name, inventory= None):
|
||||||
player= _player.BrSimPlayer(name, inventory)
|
player= _player.BrSimPlayer(name, inventory)
|
||||||
self.players.append(player)
|
self.players.append(player)
|
||||||
|
self.Map.add_player_to_map(player)
|
||||||
|
|
||||||
def add_weapon(self, weapon_type):
|
def add_weapon(self, weapon_type):
|
||||||
weapon= _weapons.BrSimWeapon(weapon_type)
|
weapon= _weapons.BrSimWeapon(weapon_type)
|
||||||
self.weapons.append(weapon)
|
self.weapons.append(weapon)
|
||||||
|
self.Map.add_item_to_map(item)
|
||||||
|
|
||||||
def get_players(self):
|
def get_players(self):
|
||||||
return self.players
|
return self.players
|
||||||
@@ -124,3 +128,6 @@ class BrSimArena():
|
|||||||
#XXX implement me
|
#XXX implement me
|
||||||
res.append(w)
|
res.append(w)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def get_map(self):
|
||||||
|
return self.Map
|
||||||
|
|||||||
@@ -54,38 +54,44 @@ class BrSimMap():
|
|||||||
i_coord_x, i_coord_y= item.get_item_coordinates()
|
i_coord_x, i_coord_y= item.get_item_coordinates()
|
||||||
self.game_map[i_coord_y][i_coord_x]= self.item_sym
|
self.game_map[i_coord_y][i_coord_x]= self.item_sym
|
||||||
|
|
||||||
def get_map_matrix(self):
|
def _set_coordinates(self, target):
|
||||||
return self.game_map
|
|
||||||
|
|
||||||
def get_renderized_map(self):
|
|
||||||
res= ''
|
|
||||||
game_map= self.get_map_matrix()
|
|
||||||
for y in game_map:
|
|
||||||
for x in y:
|
|
||||||
res+= x
|
|
||||||
res+= '\n'
|
|
||||||
return res
|
|
||||||
|
|
||||||
def init_players_coordinates(self):
|
|
||||||
# XXX init random player.coord_x and player.coord_y (of course not already used coords)
|
|
||||||
# parse all self.players and define random coordinates (player.coord_x, and player.coord_y)
|
|
||||||
for player in self.players:
|
|
||||||
x= _random.randint(1, self.world_width -2) # -2 because 1 cell is occupied by the mountain
|
x= _random.randint(1, self.world_width -2) # -2 because 1 cell is occupied by the mountain
|
||||||
y= _random.randint(1, self.world_height -2)
|
y= _random.randint(1, self.world_height -2)
|
||||||
while self.get_map_matrix()[y][x] != self.field_sym:
|
while self.get_map_matrix()[y][x] != self.field_sym:
|
||||||
print('init_players_coordinates: collision, regenerate coordinates')
|
print('init_players_coordinates: collision, regenerate coordinates')
|
||||||
x= _random.randint(1, self.world_width -2)
|
x= _random.randint(1, self.world_width -2)
|
||||||
y= _random.randint(1, self.world_height -2)
|
y= _random.randint(1, self.world_height -2)
|
||||||
player.set_player_coordinates(x, y)
|
target.set_player_coordinates(x, y)
|
||||||
|
|
||||||
|
def init_players_coordinates(self):
|
||||||
|
# XXX init random player.coord_x and player.coord_y (of course not already used coords)
|
||||||
|
# parse all self.players and define random coordinates (player.coord_x, and player.coord_y)
|
||||||
|
for player in self.players:
|
||||||
|
self._set_coordinates(target)
|
||||||
|
|
||||||
def init_items_coordinates(self):
|
def init_items_coordinates(self):
|
||||||
# XXX init random item.coord_x and item.coord_y (of course not already used coords)
|
# XXX init random item.coord_x and item.coord_y (of course not already used coords)
|
||||||
# parse all self.items and define random coordinates (item.coord_x, and item.coord_y)
|
# parse all self.items and define random coordinates (item.coord_x, and item.coord_y)
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
x= _random.randint(1, self.world_width -2) # -2 because 1 cell is occupied by the mountain
|
self._set_coordinates(item)
|
||||||
y= _random.randint(1, self.world_height -2)
|
|
||||||
while self.get_map_matrix()[y][x] != self.field_sym:
|
def add_player_to_map(self, player):
|
||||||
print('init_items_coordinates: collision, regenerate coordinates')
|
self.players.append(player)
|
||||||
x= _random.randint(1, self.world_width -2)
|
self._set_coordinates(player)
|
||||||
y= _random.randint(1, self.world_height -2)
|
|
||||||
item.set_item_coordinates(x, y)
|
def add_item_to_map(self, item):
|
||||||
|
self.items.append(item)
|
||||||
|
self._set_coordinates(item)
|
||||||
|
|
||||||
|
def get_map_matrix(self):
|
||||||
|
return self.game_map
|
||||||
|
|
||||||
|
def get_renderized_map(self):
|
||||||
|
res= ''
|
||||||
|
self.populate_map()
|
||||||
|
game_map= self.get_map_matrix()
|
||||||
|
for y in game_map:
|
||||||
|
for x in y:
|
||||||
|
res+= x
|
||||||
|
res+= '\n'
|
||||||
|
return res
|
||||||
Reference in New Issue
Block a user