Skip to content

Commit

Permalink
Modify src/services/session_management.py to use `DATABASE_SETTINGS…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
mericozkayagan committed Dec 12, 2024
1 parent 3bdcf03 commit 35f209c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/services/combat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions src/services/session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions terraform/server/main.tf
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 35f209c

Please sign in to comment.