Trust Trials is a game based on the prisoner's dilemma, or more specifically the iterated prisoner's dilemma. In this game, you provide a strategy that the game server places in matchups against other strategies. The strategy you provide is an API and can be written in any language as long as it responds per the specification. It can be hosted anywhere as long as it responds within the defined SLA.
The prisoner's dilemma is a game theory scenario where two individuals must choose to cooperate or betray each other without knowing the other's choice. The outcomes are:
Despite better collective outcomes from cooperation, rational self-interest often leads both to betray.
The iterated prisoner's dilemma involves repeated rounds of the prisoner's dilemma. Players remember previous interactions and adjust their strategies. Cooperation can emerge over time as players reward cooperation and punish betrayal, leading to long-term stable strategies.
I'm alive!
.Cooperate
, Defect
, or Bye!
. Bye!
is only valid when the game server indicates the game is over.Strategies must be able to respond to 5 types of messages.
Health Check:
Message: It's a-me, Mario!
Response Time: Within 1000ms
Valid Response: I'm alive!
First Round of Matchup:
Message: Let's-a go, little guys!
Response Time: Within 1000ms
Valid Responses: Cooperate
or Defect
Subsequent Rounds of Matchup:
Message: Mama Mia!
Response Time: Within 500ms
Valid Responses: Cooperate
or Defect
Last Round:
Message: Mario time!
Response Time: Within 500ms
Valid Responses: Cooperate
or Defect
End of Matchup:
Message: Thanks for playing! Way to go!
Response Time: Within 500ms
Valid Response: Bye!
Every request made by the game server is an HTTP POST request. The payload always has the following structure:
{
"secret_key": "secret_key",
"message": "message",
"opponent": {
"id" : "opponent_id",
"last_decision": "opponent_last_decision"
}
}
last_decision
will be null on the first round of the matchup.Every response to the game server should be a valid JSON string. Note that a JSON string is encapsulated in quotes!
"your_response"
This is an full request and response as conducted through Postman.
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.39.0
Accept: */*
Postman-Token: f95f9489-f6df-4679-83c2-7ce726bb0a85
Host: XXXXXXX.execute-api.us-west-2.amazonaws.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 165
Request Body
{
"secret_key": "XXXXXXX",
"message": "It's a-me, Mario!",
"opponent": {
"id" : null,
"last_decision": null
}
}
Response Headers
Date: Sat, 01 Jun 2024 01:36:46 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Connection: keep-alive
Apigw-Requestid: YqgvTjYFPHcEMPA=
Response Body
"I'm alive!"
In this section, we will guide you through creating a basic strategy for the game using AWS services. We will use AWS Lambda to run the strategy code and API Gateway to handle the HTTP requests. The example strategy we'll create is called "TitForTat," which is one of the strategies preloaded into the game. This setup should fit within the AWS Free Tier, meaning there should be no cost, but you should always verify costs on your own.
Before you begin, ensure you have the following:
Follow these steps to set up your strategy:
Navigate to AWS Lambda: Go to the AWS Management Console and open the Lambda service. You can follow the detailed instructions here.
Create a New Lambda Function:
a. Choose "Create function".
b. Select "Author from scratch".
c. Name your function (e.g., TitForTatStrategy
).
d. Choose Python as the runtime.
e. Choose "Create Function".
Example Code:
import json
secret_key = "XXXXXXX"
def validate_request(body):
required_fields = ["secret_key", "message", "opponent"]
opponent_fields = ["id", "last_decision"]
if any(field not in body for field in required_fields):
return 400, "Malformed request."
if any(field not in body["opponent"] for field in opponent_fields):
return 400, "Malformed request."
if body["secret_key"] != secret_key:
return 401, "Unauthorized"
return None, None
def process_message(body):
match body["message"]:
case "It's a-me, Mario!":
return 200, "I'm alive!"
case "Let's-a go, little guys!":
return 200, "Cooperate"
case "Mama Mia!":
match body["opponent"]["last_decision"]:
case "Cooperate":
return 200, "Cooperate"
case "Defect":
return 200, "Defect"
case _:
return 400, "Invalid last_decision"
case "Mario time!":
match body["opponent"]["last_decision"]:
case "Cooperate":
return 200, "Cooperate"
case "Defect":
return 200, "Defect"
case _:
return 400, "Invalid last_decision"
case "Thanks for playing! Way to go!":
return 200, "Bye!"
case _:
return 400, "Invalid message."
def lambda_handler(event, context):
body = json.loads(event["body"])
status_code, response = validate_request(body)
if status_code:
return {
'statusCode': status_code,
'body': json.dumps(response)
}
status_code, response = process_message(body)
return {
'statusCode': status_code,
'body': json.dumps(response)
}
Based on the instructions for "Create an HTTP API by using the AWS Management Console" here.
myStrategies
).Now we will get the API URL that you can use to register your strategy for Trust Trials.
You can test your API using tools like Postman.
Once you register on the site, you will be provided with a secret_key
. Update your Lambda function with this key as soon as possible to avoid any failed calls from the game server.