mirror of
https://github.com/solero/houdini.git
synced 2024-11-08 20:28:20 +00:00
commit
438d083602
@ -1,3 +1,5 @@
|
||||
from random import uniform
|
||||
|
||||
from houdini.data import db, AbstractDataCollection
|
||||
|
||||
|
||||
@ -79,7 +81,6 @@ class MancalaLogic:
|
||||
self.current_player = 2 if self.current_player == 1 else 1
|
||||
return 'f'
|
||||
|
||||
|
||||
def is_position_win(self):
|
||||
if sum(self.board[0:6]) == 0 or sum(self.board[7:-1]) == 0:
|
||||
if sum(self.board[0:6]) > sum(self.board[7:-1]):
|
||||
@ -104,6 +105,138 @@ class MancalaLogic:
|
||||
return ','.join(map(str, self.board))
|
||||
|
||||
|
||||
class TreasureHuntLogic:
|
||||
|
||||
def __init__(self):
|
||||
self.map_width = 10
|
||||
self.map_height = 10
|
||||
self.coins_hidden = 0
|
||||
self.gems_hidden = 0
|
||||
self.turns = 12
|
||||
self.gem_value = 25
|
||||
self.coin_value = 1
|
||||
self.gem_locations = []
|
||||
self.treasure_map = []
|
||||
self.coins_found = 0
|
||||
self.gems_found = 0
|
||||
self.emerald_found = 0
|
||||
self.dig_record_names = []
|
||||
self.dig_record_directions = []
|
||||
self.dig_record_numbers = []
|
||||
self.emerald = 0
|
||||
self.current_player = 1
|
||||
self.generate_map()
|
||||
|
||||
def generate_map(self):
|
||||
for row in range(self.map_height):
|
||||
self.treasure_map.append([])
|
||||
for column in range(self.map_width):
|
||||
self.treasure_map[row].append([self.generate_treasure(row, column), 0])
|
||||
|
||||
def generate_treasure(self, row, column):
|
||||
treasure_type = [
|
||||
('None', 0, 60), ('Coin', 1, 40), ('Gem', 2, 1), ('Emerald', 4, 0.5)
|
||||
]
|
||||
if self.get_gem_by_piece(row, column):
|
||||
return 3
|
||||
if row + 1 == self.map_height or column + 1 == self.map_width:
|
||||
treasure_type = treasure_type[:2]
|
||||
total = sum(weight for name, value, weight in treasure_type)
|
||||
r, i = uniform(0, total), 0
|
||||
for name, value, weight in treasure_type:
|
||||
if i + weight >= r:
|
||||
self.coins_hidden += 1 if value == 1 else self.coins_hidden
|
||||
if value > 1:
|
||||
self.gems_hidden += 1
|
||||
self.gem_locations.append(str(row) + ',' + str(column))
|
||||
if self.emerald:
|
||||
return 2
|
||||
if value == 4 and not self.emerald:
|
||||
self.emerald = 1
|
||||
return value
|
||||
i += weight
|
||||
|
||||
def get_gem_by_piece(self, row, column):
|
||||
for delta_row, delta_col in [(0, -1), (-1, -1), (-1, 0)]:
|
||||
if row > 0 and column > 0:
|
||||
treasure, digs = self.treasure_map[row + delta_row][column + delta_col]
|
||||
if treasure == 2 or treasure == 4:
|
||||
return row + delta_row, column + delta_col
|
||||
return False
|
||||
|
||||
def is_gem_uncovered(self, row, column):
|
||||
for delta_row, delta_col in [(0, 1), (1, 1), (1, 0)]:
|
||||
treasure, digs = self.treasure_map[row + delta_row][column + delta_col]
|
||||
if digs != 2:
|
||||
return False
|
||||
return True
|
||||
|
||||
def make_move(self, movie, direction, spade):
|
||||
if direction == 'right':
|
||||
row = self.treasure_map[spade]
|
||||
for column, tiles in enumerate(row):
|
||||
self.dig(spade, column)
|
||||
elif direction == 'down':
|
||||
for row, columns in enumerate(self.treasure_map):
|
||||
self.dig(row, spade)
|
||||
self.turns -= 1
|
||||
self.dig_record_names.append(movie)
|
||||
self.dig_record_directions.append(direction)
|
||||
self.dig_record_numbers.append(spade)
|
||||
|
||||
def dig(self, row, column):
|
||||
self.treasure_map[row][column][1] += 1
|
||||
treasure, digs = self.treasure_map[row][column]
|
||||
if digs == 2:
|
||||
if treasure == 1:
|
||||
self.coins_found += 1
|
||||
elif treasure == 2 or treasure == 4:
|
||||
if not self.is_gem_uncovered(row, column):
|
||||
return
|
||||
self.gems_found += 1
|
||||
elif treasure == 3:
|
||||
treasure_row, treasure_col = self.get_gem_by_piece(row, column)
|
||||
if not self.is_gem_uncovered(treasure_row, treasure_col):
|
||||
return
|
||||
self.gems_found += 1
|
||||
if treasure == 4:
|
||||
self.emerald_found = 1
|
||||
|
||||
def determine_winnings(self):
|
||||
total = self.coins_found * self.coin_value
|
||||
total += self.gems_found * self.gem_value
|
||||
total += self.emerald_found * self.gem_value * 3
|
||||
return total
|
||||
|
||||
def is_valid_move(self, movie, direction, spade):
|
||||
test_movie = direction + 'button' + str(spade) + '_mc'
|
||||
if test_movie == movie and direction in ['down', 'right'] and 0 <= spade <= 9:
|
||||
if direction == 'right':
|
||||
row = self.treasure_map[spade]
|
||||
for column, tiles in enumerate(row):
|
||||
treasure, digs = self.treasure_map[spade][column]
|
||||
if digs == 2:
|
||||
return False
|
||||
elif direction == 'down':
|
||||
for row, columns in enumerate(self.treasure_map):
|
||||
treasure, digs = self.treasure_map[row][spade]
|
||||
if digs == 2:
|
||||
return False
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_string(self):
|
||||
treasure_map = ','.join(str(item) for row in self.treasure_map for item, digs in row)
|
||||
gem_locations = ','.join(self.gem_locations)
|
||||
game_array = [self.map_width, self.map_height, self.coins_hidden, self.gems_hidden, self.turns,
|
||||
self.gem_value, self.coin_value, gem_locations, treasure_map]
|
||||
if self.dig_record_numbers:
|
||||
game_array += [self.coins_found, self.gems_found, self.emerald_found]
|
||||
game_array += [','.join(self.dig_record_names), ','.join(self.dig_record_directions),
|
||||
','.join(map(str, self.dig_record_numbers))]
|
||||
return '%'.join(map(str, game_array))
|
||||
|
||||
|
||||
def stealth_mod_filter(stealth_mod_id):
|
||||
def f(p):
|
||||
return not p.stealth_moderator or p.id == stealth_mod_id
|
||||
@ -277,7 +410,7 @@ class RoomTable(db.Model):
|
||||
GameClassMapping = {
|
||||
'four': ConnectFourLogic,
|
||||
'mancala': MancalaLogic,
|
||||
'treasure': str
|
||||
'treasure': TreasureHuntLogic
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -20,10 +20,36 @@ async def handle_join_game(p):
|
||||
await p.table.send_xt('uz', seat_id, p.safe_name)
|
||||
|
||||
if len(p.table.penguins) == 2:
|
||||
await p.table.send_xt('sz', 0) # Todo: Is this 0 needed?
|
||||
await p.table.send_xt('sz', 0)
|
||||
|
||||
|
||||
@handlers.handler(XTPacket('zm', ext='z'))
|
||||
@table_handler(MancalaLogic)
|
||||
async def handle_send_move(p, hollow: int):
|
||||
pass # Todo
|
||||
async def handle_send_move(p, move: int):
|
||||
seat_id = p.table.get_seat_id(p)
|
||||
is_player = seat_id < 2
|
||||
game_ready = len(p.table.penguins) > 1
|
||||
|
||||
if is_player and game_ready:
|
||||
hollow, = map(int, move)
|
||||
current_player = p.table.penguins[p.table.logic.current_player - 1]
|
||||
|
||||
if current_player != p: return
|
||||
if not p.table.logic.is_valid_move(hollow): return
|
||||
|
||||
move_result = p.table.logic.place_stone(hollow)
|
||||
await p.table.send_xt('zm', seat_id, hollow, move_result)
|
||||
opponent = p.table.penguins[1 if p.table.logic.current_player == 1 else 0]
|
||||
|
||||
if p.table.logic.is_position_win():
|
||||
await p.add_coins(10)
|
||||
await opponent.add_coins(5)
|
||||
await p.table.reset()
|
||||
return
|
||||
elif p.table.logic.is_position_tie():
|
||||
await p.add_coins(5)
|
||||
await opponent.add_coins(5)
|
||||
await p.table.reset()
|
||||
return
|
||||
|
||||
p.table.logic.current_player = 2 if p.table.logic.current_player == 1 else 1
|
||||
|
Loading…
Reference in New Issue
Block a user