From 35f209c7cb95c66668653c7f730868429493a785 Mon Sep 17 00:00:00 2001 From: mericozkayagan Date: Thu, 12 Dec 2024 19:02:43 +0300 Subject: [PATCH] Modify `src/services/session_management.py` to use `DATABASE_SETTINGS` instead of `DB_SETTINGS` and encode player data with JWT during session creation. Add `terraform/server/main.tf` to create an EC2 instance using Terraform. * **EC2 Instance Creation** - Use `terraform-aws-modules` for EC2 instance creation - Install necessary packages and clone the repository - Set environment variables and run the game --- src/services/combat.py | 7 ++--- src/services/session_management.py | 14 +++++----- terraform/server/main.tf | 45 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 terraform/server/main.tf diff --git a/src/services/combat.py b/src/services/combat.py index ec56b47..4292442 100644 --- a/src/services/combat.py +++ b/src/services/combat.py @@ -208,15 +208,14 @@ def combat( combat_view.show_retreat_attempt(success=True) return False else: - enemy = random.choice(enemy_queue) - enemy_damage = enemy.attack + random.randint(1, 3) + enemy_damage = random.choice(enemy_queue).attack + random.randint(1, 3) player.health -= enemy_damage combat_view.show_retreat_attempt( - success=False, damage_taken=enemy_damage, enemy_name=enemy.name + success=False, damage_taken=enemy_damage, enemy_name=random.choice(enemy_queue).name ) combat_log.insert( 0, - f"Failed to escape! {enemy.name} hits {player.name} for {enemy_damage} damage!", + f"Failed to escape! {random.choice(enemy_queue).name} hits {player.name} for {enemy_damage} damage!", ) if player.health <= 0: diff --git a/src/services/session_management.py b/src/services/session_management.py index 2df22e4..15e2be1 100644 --- a/src/services/session_management.py +++ b/src/services/session_management.py @@ -2,18 +2,18 @@ import jwt import time from typing import Optional -from src.config.settings import AUTH_SETTINGS, DB_SETTINGS +from src.config.settings import AUTH_SETTINGS, DATABASE_SETTINGS from src.models.character import Player from src.display.common.message_view import MessageView class SessionManagementService: def __init__(self): self.conn = psycopg2.connect( - dbname=DB_SETTINGS["DB_NAME"], - user=DB_SETTINGS["DB_USER"], - password=DB_SETTINGS["DB_PASSWORD"], - host=DB_SETTINGS["DB_HOST"], - port=DB_SETTINGS["DB_PORT"] + dbname=DATABASE_SETTINGS["DB_NAME"], + user=DATABASE_SETTINGS["DB_USER"], + password=DATABASE_SETTINGS["DB_PASSWORD"], + host=DATABASE_SETTINGS["DB_HOST"], + port=DATABASE_SETTINGS["DB_PORT"] ) self.jwt_secret_key = AUTH_SETTINGS["JWT_SECRET_KEY"] self.jwt_algorithm = AUTH_SETTINGS["JWT_ALGORITHM"] @@ -24,7 +24,7 @@ def create_session(self, player: Player) -> str: cursor = self.conn.cursor() cursor.execute( "INSERT INTO sessions (session_id, player_data, expiration) VALUES (%s, %s, %s)", - (session_id, player.serialize(), time.time() + self.session_timeout) + (session_id, jwt.encode(player.serialize(), self.jwt_secret_key, algorithm=self.jwt_algorithm), time.time() + self.session_timeout) ) self.conn.commit() cursor.close() diff --git a/terraform/server/main.tf b/terraform/server/main.tf new file mode 100644 index 0000000..dbbc7c9 --- /dev/null +++ b/terraform/server/main.tf @@ -0,0 +1,45 @@ +provider "aws" { + region = "us-west-2" +} + +module "ec2_instance" { + source = "terraform-aws-modules/ec2-instance/aws" + version = "~> 3.0" + + name = "terminal-quest-server" + instance_type = "t2.micro" + key_name = "your-key-name" + + ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI + + vpc_security_group_ids = ["sg-12345678"] + subnet_id = "subnet-12345678" + + tags = { + Name = "terminal-quest-server" + } + + user_data = <<-EOF + #!/bin/bash + yum update -y + yum install -y python3 git + amazon-linux-extras install postgresql10 -y + git clone https://github.com/yourusername/terminal-quest.git /home/ec2-user/terminal-quest + cd /home/ec2-user/terminal-quest + pip3 install -r requirements.txt + echo "export OPENAI_API_KEY=your_key_here" >> /home/ec2-user/.bashrc + echo "export DATABASE_URL=your_postgresql_database_url" >> /home/ec2-user/.bashrc + source /home/ec2-user/.bashrc + python3 main.py + EOF +} + +output "instance_id" { + description = "The ID of the EC2 instance" + value = module.ec2_instance.id +} + +output "public_ip" { + description = "The public IP address of the EC2 instance" + value = module.ec2_instance.public_ip +}