Skip to content

Commit

Permalink
update store
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagwep committed Aug 7, 2024
1 parent fba3810 commit 69932c0
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 13 deletions.
30 changes: 30 additions & 0 deletions oware-dapp/dapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,28 @@ def create_challenge(payload,sender):
else:
add_report(f"Failed to create challenge. Error: {result['error']}")
return "reject"

def create_tournament(payload,sender):

result = store.create_tournament(sender, payload)

if result["success"]:
add_notice(f"Tournament with id {result['tournament_id']} was was created by {sender} ")
return "accept"
else:
add_report(f"Failed to create challenge. Error: {result['error']}")
return "reject"

def join_tournament(payload,sender):

result = store.join_tournament(sender, payload)

if result["success"]:
add_notice(f"tournement with id {result['tournament_id']} was was created by {sender} ")
return "accept"
else:
add_report(f"Failed to create tournament. Error: {result['error']}")
return "reject"

def accept_challenge(payload,sender):

Expand Down Expand Up @@ -119,6 +139,16 @@ def make_move(payload,sender):
else:
add_report(f"Failed to make move. Error: {result['error']}")
return 'reject'

def make_move_tournament(payload,sender):
result = store.make_move_tournament(sender, payload)

if result["success"]:
add_notice(f"Move made successfully. Result: {str(result['result'])}")
return 'accept'
else:
add_report(f"Failed to make move. Error: {result['error']}")
return 'reject'


def get_all_challenges():
Expand Down
3 changes: 1 addition & 2 deletions oware-dapp/game_play/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ def __init__(self,creator, number_of_players, rounds_per_challenge):
raise ValueError(f"Invalid number of players. Must be an even number between 4 and 16 inclusive.")


def join_tournament(self,name,address,model_name= None):
def join_tournament(self,player):
if len(self.players) >= self.max_players:
raise ValueError("The tournament is already full")
player = OPlayer(name, address, model_name)
self.players.append(player)
if len(self.players) == self.max_players:
self.start()
Expand Down
163 changes: 152 additions & 11 deletions oware-dapp/store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from game_play.challenge import Challenge
from game_play.tournaments import Tournament
from game_play.tournaments import OPlayer, Tournament
from utils.utils import HexConverter
from game_play.game.coordinate_house_map import coordinates_houses_map
import json
Expand All @@ -11,6 +11,7 @@ class Store:
def __init__(self):
self.challenges = {}
self.player_challenges = {}
self.player_tournaments = {}
self.challenge_next_id = 0
self.tournament_next_id = 0
self.tournaments = {}
Expand Down Expand Up @@ -55,10 +56,40 @@ def create_challenge(self, creator_address, challenge_data):


def create_tournament(self, creator_address, tournament_data):
tournament = Tournament(creator_address, **tournament_data)
tournament_id = self.add_tournament(tournament)
self.add_player_tournament(creator_address, tournament_id)
return tournament_id

if self.player_tournaments.get(creator_address) is not None:
return {
"success": False,
"error": "Player already has an active tournament"
}

try:
tournament_id = self.get_next_tournament_id()

number_of_players = tournament_data.get("number_of_players")
rounds_per_challenge = tournament_data.get("rounds_per_challenge")

# Validate required fields
if not all([rounds_per_challenge]):
return {
"success": False,
"error": "Missing required fields: rounds per challenge"
}

tournament = Tournament(creator_address,number_of_players, rounds_per_challenge)
self.tournaments[tournament_id] = tournament
self.player_tournaments[creator_address] = tournament_id

return {
"success": True,
"tournament_id": tournament_id
}

except Exception as e:
return {
"success": False,
"error": str(e)
}

def join_challenge(self, player_address, challenge_data):

Expand Down Expand Up @@ -154,13 +185,56 @@ def start_challenge(self,player_address,challenge_data):
"error": f"Failed to start challenge: {str(e)}"
}

def join_tournament(self, player_address, tournament_id):
def join_tournament(self, player_address, tournament_data):

tournament_id = tournament.get("tournament_id")

if not tournament_id:
return {
"success": False,
"error": "Tournament ID is required"
}

tournament = self.get_tournament(tournament_id)
if tournament and tournament.can_join(player_address):
tournament.add_player(player_address)
self.add_player_tournament(player_address, tournament_id)
return True
return False

if not tournament:
return {
"success": False,
"error": "Tournament not found"
}

player_name = tournament_data.get("creator_name")
model_name = tournament_data.get("model")


# Validate required fields
if not all([player_name]):
return {
"success": False,
"error": "Missing required fields: player_name"
}


player = OPlayer(player_name, player_address, model_name)

if player in tournament.players:
return {
"success": False,
"error": "already in tournament"
}

try:
tournament.join_tournament(player)
return {
"success": True,
"message": "Joined tournament successfully",
"tournament_id": tournament_id
}
except Exception as e:
return {
"success": False,
"error": f"Failed to start challenge: {str(e)}"
}

def make_move(self,sender, challenge_data):

Expand Down Expand Up @@ -229,7 +303,74 @@ def make_move(self,sender, challenge_data):
"error": f"Failed to make move: {str(e)}"
}

def make_move_tournament(self,sender, tournament_data):

house = tournament_data.get("house")

tournament_id = tournament_data.get("tournament_id")

if not tournament_id:
return {
"success": False,
"error": "Tournament ID is required"
}

tournament = self.tournaments.get(tournament_id)


if not tournament:
return {
"success": False,
"error": "Tournament not found"
}

if sender != challenge.turn or not challenge.in_progress:
return {
"success": False,
"error": "It's not your turn or the challenge is not in progress"
}

seeds = challenge.game.board.get_seeds()
moves, moves_state = challenge.game.get_valid_moves(challenge.turn,seeds)

coordinate = next((coord for coord, name in coordinates_houses_map.items() if name == house), None)

if coordinate is None:
return {
"success": False,
"error": "Invalid house name"
}

if coordinate not in moves:
return {
"success": False,
"error": "Invalid MOve"
}


try:

result = challenge.move(house)

if result['challenge_ended']:
self.delete_player_from_active_challenge(challenge)
self.add_or_update_player(challenge.winner.name, challenge.winner.address, 100)
opponent = challenge.opponent if challenge.winner == challenge.creator else challenge.creator
self.add_or_update_player(opponent.name, opponent.address, 2)

return {
"success": True,
"message": "Move made successfully",
"result": result
}

except Exception as e:
return {
"success": False,
"error": f"Failed to make move: {str(e)}"
}


def get_next_challenge_id(self):
self.challenge_next_id += 1
return self.challenge_next_id
Expand Down

0 comments on commit 69932c0

Please sign in to comment.