Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In gameapi.py when player becomes dummy a bad request occurs #149

Open
trevor-powdrell opened this issue Dec 5, 2024 · 14 comments
Open

Comments

@trevor-powdrell
Copy link

trevor-powdrell commented Dec 5, 2024

Given this request

INFO Request: GET http://localhost:8085/play?user=Trevor&dealer=N&seat=S&vul=&ctx=--1SDb--2H------&hand=JT3.KJ9.K643.AKJ&tournament=mp&explain=true&dummy=74.AT84.Q9872.84&played=SK
INFO Response body: 400 BAD REQUEST b'{"error":"An error occurred: 'NoneType' object has no attribute 'set_hcp_constraints'"}\n'

Once that issue is resolved another bad request occurs
Request: GET http://localhost:8085/play?user=Trevor&dealer=N&seat=S&vul=&ctx=--1SDb--2H------&hand=JT3.KJ9.K643.AKJ&tournament=mp&explain=true&dummy=74.AT84.Q9872.84&played=SKS3S5
INFO Response body: 400 BAD REQUEST b'{"error":"An error occurred: Cardplay order is not correct ['SK', 'S3', 'S5'] 3 1"}\n'

I have a pull request I can post once the defect is reproduced.
I also plan to test under the following conditions
dealer is E and seat is W
dealer is S and seat is N
dealer is W and seat is E

@ThorvaldAagaard
Copy link
Collaborator

I get this message, when I call your request
image

Dummy can't play so that seems OK.

Changing seat to N (Declarer) and the API returns
image
So it would play S7

So the question is how your server gets to set_hcp_constraints, as it is trying to update a non-existing player.

BEN always expect all cards in the order they are played. Here SK was lead, and the S3 was lead out of turn :-)

BEN always expect the real dummy as parameter dummy, no matter who to lead, but Seat is defined as declarer or opponent, never dummy.

@trevor-powdrell
Copy link
Author

When I search for that message I could not find that line. Not sure what is going on there.

The 3 lines of code that I changed seems to work and does make it easier to continue after bidding without having to switch the hands around.

Here is the diff

diff --git a/src/bots.py b/src/bots.py
index c261cce..e2aea19 100644
--- a/src/bots.py
+++ b/src/bots.py
@@ -1504,19 +1504,9 @@ class CardPlayer:
 
     def find_and_update_constraints(self, players_states, quality, player_i):
         # Based on player states we should be able to find min max for suits and hcps, and add that before calling PIMC
-        # print("Updating constraints", player_i)
-        if player_i == 1:
-            idx1 = 0
-            idx2 = 2
-        if player_i == 0:
-            idx1 = 2
-            idx2 = 3
-        if player_i == 2:
-            idx1 = 0
-            idx2 = 3
-        if player_i == 3:
-            idx1 = 0
-            idx2 = 2
+        print("Updating constraints", player_i)
+        idx1 = (player_i + 1) % 4
+        idx2 = (player_i + 3) % 4
         h1 = []
         h3 = []
         s1 = []
diff --git a/src/gameapi.py b/src/gameapi.py
index 6a05fcc..5f691f6 100644
--- a/src/gameapi.py
+++ b/src/gameapi.py
@@ -68,8 +68,8 @@ def play_api(dealer_i, vuln_ns, vuln_ew, hands, models, sampler, contract, strai
 
     pimc = [None, None, None, None]
 
-    # We should only instantiate the PIMC for the position we are playing
-    if models.pimc_use_declaring and position == 3: 
+    # We should only instantiate the PIMC for the position we are playing which can be dummy
+    if models.pimc_use_declaring and (position == 3 or position == 1): 
         from pimc.PIMC import BGADLL
         declarer = BGADLL(models, dummy_hand_str, decl_hand_str, contract, is_decl_vuln, sampler, verbose)
         pimc[1] = declarer
@@ -141,7 +141,8 @@ def play_api(dealer_i, vuln_ns, vuln_ew, hands, models, sampler, contract, strai
 
             card_i += 1
             if card_i >= len(play):
-                assert (player_i == position or (player_i == 1 and position == 3)), f"Cardplay order is not correct {play} {player_i} {position}"
+                # player or partner
+                assert (player_i == position or (player_i == (position + 2) % 4)), f"Cardplay order is not correct {play} {player_i} {position}"
                 play_status = get_play_status(card_players[player_i].hand52,current_trick52)
                 if verbose:
                     print("play_status", play_status)

Here is the ask ben screenshot
askben

@ThorvaldAagaard
Copy link
Collaborator

It is line 820 in https://github.com/lorserker/ben/blob/main/src/gameapi.py
In the ask BEN screenshot you should just change the field Seledct your Seat to North, and all should be OK

You are welcome to try on my server: http://ben.aalborgdata.dk:8080/api
I have just verified I get the expected message and it works if changing seat to North

@ThorvaldAagaard
Copy link
Collaborator

ThorvaldAagaard commented Dec 5, 2024

I like you update and will apply it as it seems to do the same (So it does not fix any errors, just more compact and more correct code)

I just checked it does the same but switch the value of idx1 and idx2
So it should be

idx1 = (player_i + 3) % 4
idx2 = (player_i + 1) % 4        

@trevor-powdrell
Copy link
Author

Not sure why but I had old code. I did a full update and stashed the changes so I should be running from the main branch.
I am now getting a different error 'No samples after opening lead' when I switch the seat to North. I am running on windows if that makes any difference and just use the following command to start gameapi

start "TF2 Api internal :8085 (API)" python gameapi.py --config config\default_tf2_Short_imp_or_mp.conf --host 0.0.0.0

No samples after opening lead
File "D:\Trevor\Projects\ben\src\sample.py", line 1121, in init_rollout_states_iterative
File "D:\Trevor\Projects\ben\src\sample.py", line 931, in init_rollout_states
File "D:\Trevor\Projects\ben\src\gameapi.py", line 208, in play_api
File "D:\Trevor\Projects\ben\src\gameapi.py", line 826, in play
127.0.0.1 - - [2024-12-04 20:25:21] "GET /play?user=Trevor&dealer=S&seat=S&vul=&ctx=--1SDb--2H------&hand=JT3.KJ9.K643.AKJ&tournament=mp&explain=true&dummy=74.AT84.Q9872.84&played=SK HTTP/1.1" 400 244 1.013019

@ThorvaldAagaard
Copy link
Collaborator

Just checked on my local pc
image
Please check you have the same input

@trevor-powdrell
Copy link
Author

S7 is incorrect the card has to come out of the dummies hand not the declarer. When you switch seat you also have to swap the hand and dummy/declarer which is why I tried to create patch that would allow me to stay seated in South.

@ThorvaldAagaard
Copy link
Collaborator

You need to provide the full deal then.

Ask BEN should not be able to play a card for dummy if you select dummy seat.

Enter your seat and your hand - Bidding decides opening lead, and then the cards plyed decide who to play next. Ask BEN is primarily to test the API, and the API is used both at a playing site and I use it for about 60-100 boards a day at BBO

@trevor-powdrell
Copy link
Author

Not sure I get the logic. Once our side is declarer BEN acts for both the declarer and dummy. The position just determines which hand is dummy and it is messy switching around when starting the card play. I do not think it will cause any issues just allowing the player to remain in his seat.

For me the ask ben is the part I am using most. I might spend some time to improve the UI to make it easier to enter the cards and bids.

I am still not getting past the sample after opening lead. Not sure why it is even checking as the opponents
are on opening lead. Will try to track it down.

@ThorvaldAagaard
Copy link
Collaborator

I think what you shuold look at is changing the logic for whos on play.
Currently my seat is defining together with number of cards played, who should play next.

But swithing that around and using number of cards played together with contract could be another way of doing it.

All this should be done in gameapi, and not on the lower levels.

You should be aware that when defending dummy can be either RHO or LHO,

@ThorvaldAagaard
Copy link
Collaborator

We have a Discord server if you are interested: https://discord.gg/uy8PWTtP

I created this https://github.com/ThorvaldAagaard/bridge-with-ben some time ago, where it call NN to get the play and bid. It could be a fine template for creating a GUI, that calls the API instead.

@trevor-powdrell
Copy link
Author

Thanks I have joined the discord. I will be continue to track down the 'No samples after opening lead' issue as I an now getting the error at other times.

@ThorvaldAagaard
Copy link
Collaborator

Try to describe the entire process, and I might be able to give you a hint of what to change.

JBridge is using the API on thier own server, and you can play for free: https://play.google.com/store/apps/details?id=net.jbridge.gg&hl=da

@ThorvaldAagaard
Copy link
Collaborator

Code is updated, but still logically the same.

The code is for finding the 2 hidden hands, so it is possible to create stats for these hands. These stats are sent to PIMC as constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants