Browse Source
Merge branch 'master' of gogs.informatik.hs-fulda.de:srieger/cloud-computing-msc-ai-examples
Merge branch 'master' of gogs.informatik.hs-fulda.de:srieger/cloud-computing-msc-ai-examples
# Conflicts: # aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc-designer.png # aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc.json # aws-cloudformation-demo/cloudcomp-counter-demo.json # aws-turnserver/stop.py # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc-designer.png # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc.json # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo.json # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo-with-vpc-designer.png # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo-with-vpc.json # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo.json # example-projects/tug-of-war-in-the-clouds/stop.py # example-projects/turnserver/aws-boto3/start.py # example-projects/turnserver/aws-boto3/stop.pymaster
Sebastian Rieger
5 years ago
13 changed files with 683 additions and 66 deletions
-
11.idea/cloud-computing-msc-ai-examples.iml
-
7.idea/misc.xml
-
8.idea/modules.xml
-
6.idea/vcs.xml
-
206example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py
-
57example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py
-
86example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py
-
70example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py
-
28example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py
-
24example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py
-
101example-projects/tug-of-war-in-the-clouds/web-content/cloud.php
-
3example-projects/tug-of-war-in-the-clouds/web-content/config.php
-
36example-projects/tug-of-war-in-the-clouds/web-content/index.php
@ -0,0 +1,11 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="PYTHON_MODULE" version="4"> |
||||
|
<component name="NewModuleRootManager"> |
||||
|
<content url="file://$MODULE_DIR$" /> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
</component> |
||||
|
<component name="TestRunnerService"> |
||||
|
<option name="PROJECT_TEST_RUNNER" value="Unittests" /> |
||||
|
</component> |
||||
|
</module> |
@ -0,0 +1,7 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="JavaScriptSettings"> |
||||
|
<option name="languageLevel" value="ES6" /> |
||||
|
</component> |
||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" /> |
||||
|
</project> |
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ProjectModuleManager"> |
||||
|
<modules> |
||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/cloud-computing-msc-ai-examples.iml" filepath="$PROJECT_DIR$/.idea/cloud-computing-msc-ai-examples.iml" /> |
||||
|
</modules> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,6 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="VcsDirectoryMappings"> |
||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" /> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,206 @@ |
|||||
|
import boto3 |
||||
|
from botocore.exceptions import ClientError |
||||
|
|
||||
|
region = 'eu-central-1' |
||||
|
availabilityZone = 'eu-central-1b' |
||||
|
imageId = 'ami-0cc293023f983ed53' |
||||
|
instanceType = 't3.nano' |
||||
|
keyName = 'srieger-pub' |
||||
|
#userDataDB = ('#!/bin/bash\n' |
||||
|
# '#!/bin/bash\n' |
||||
|
# '# extra repo for RedHat rpms\n' |
||||
|
# 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n' |
||||
|
# '# essential tools\n' |
||||
|
# 'yum install -y joe htop git\n' |
||||
|
# '# mysql\n' |
||||
|
# 'yum install -y mariadb mariadb-server\n' |
||||
|
# '\n' |
||||
|
# 'service mariadb start\n' |
||||
|
# '\n' |
||||
|
# 'echo "create database cloud_tug_of_war" | mysql -u root\n' |
||||
|
# '\n' |
||||
|
# 'echo "create table clouds ( cloud_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, value INT, max_value INT, PRIMARY KEY (cloud_id))" | mysql -u root cloud_tug_of_war\n' |
||||
|
# '\n' |
||||
|
# 'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloud\';" | mysql -u root\n' |
||||
|
# 'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n' |
||||
|
# 'echo "FLUSH PRIVILEGES" | mysql -u root\n' |
||||
|
# ) |
||||
|
|
||||
|
# convert with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/" |
||||
|
|
||||
|
client = boto3.setup_default_session(region_name=region) |
||||
|
ec2Client = boto3.client("ec2") |
||||
|
ec2Resource = boto3.resource('ec2') |
||||
|
|
||||
|
rdsClient = boto3.client("rds") |
||||
|
|
||||
|
response = ec2Client.describe_vpcs() |
||||
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
||||
|
subnet_id = ec2Client.describe_subnets( |
||||
|
Filters=[ |
||||
|
{ |
||||
|
'Name': 'availability-zone', 'Values': [availabilityZone] |
||||
|
} |
||||
|
])['Subnets'][0]['SubnetId'] |
||||
|
|
||||
|
print("Deleting old instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}]) |
||||
|
print(response) |
||||
|
reservations = response['Reservations'] |
||||
|
for reservation in reservations: |
||||
|
for instance in reservation['Instances']: |
||||
|
if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": |
||||
|
response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']]) |
||||
|
print(response) |
||||
|
instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) |
||||
|
instanceToTerminate.wait_until_terminated() |
||||
|
|
||||
|
print("Deleting old DB instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
try: |
||||
|
response = rdsClient.delete_db_instance( |
||||
|
DBInstanceIdentifier='tug-of-war-db1', |
||||
|
SkipFinalSnapshot=True, |
||||
|
DeleteAutomatedBackups=True |
||||
|
) |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
waiter = rdsClient.get_waiter('db_instance_deleted') |
||||
|
waiter.wait(DBInstanceIdentifier='tug-of-war-db1') |
||||
|
|
||||
|
|
||||
|
|
||||
|
print("Delete old security group...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
try: |
||||
|
response = ec2Client.delete_security_group(GroupName='tug-of-war') |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
print("Create security group...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
try: |
||||
|
response = ec2Client.create_security_group(GroupName='tug-of-war', |
||||
|
Description='tug-of-war', |
||||
|
VpcId=vpc_id) |
||||
|
security_group_id = response['GroupId'] |
||||
|
print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id)) |
||||
|
|
||||
|
data = ec2Client.authorize_security_group_ingress( |
||||
|
GroupId=security_group_id, |
||||
|
IpPermissions=[ |
||||
|
{'IpProtocol': 'tcp', |
||||
|
'FromPort': 3306, |
||||
|
'ToPort': 3306, |
||||
|
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, |
||||
|
{'IpProtocol': 'tcp', |
||||
|
'FromPort': 22, |
||||
|
'ToPort': 22, |
||||
|
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, |
||||
|
{'IpProtocol': 'tcp', |
||||
|
'FromPort': 80, |
||||
|
'ToPort': 80, |
||||
|
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, |
||||
|
{'IpProtocol': 'tcp', |
||||
|
'FromPort': 443, |
||||
|
'ToPort': 443, |
||||
|
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]} |
||||
|
]) |
||||
|
print('Ingress Successfully Set %s' % data) |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
print("Running new DB instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = rdsClient.create_db_instance(DBInstanceIdentifier="tug-of-war-db1", |
||||
|
AllocatedStorage=20, |
||||
|
DBName='tug_of_war', |
||||
|
# Engine='mariadb', |
||||
|
Engine='mysql', |
||||
|
# General purpose SSD |
||||
|
StorageType='gp2', |
||||
|
#StorageEncrypted=True, |
||||
|
AutoMinorVersionUpgrade=True, |
||||
|
# Set this to true later? |
||||
|
MultiAZ=False, |
||||
|
MasterUsername='cloud_tug_of_war', |
||||
|
MasterUserPassword='cloudcloud', |
||||
|
VpcSecurityGroupIds=[security_group_id], |
||||
|
#DBInstanceClass='db.m3.2xlarge', |
||||
|
DBInstanceClass='db.t3.micro', |
||||
|
Tags=[ |
||||
|
{'Key': 'Name', 'Value': 'tug-of-war-db1'}, |
||||
|
{'Key': 'tug-of-war', 'Value': 'db'} |
||||
|
], |
||||
|
) |
||||
|
|
||||
|
waiter = rdsClient.get_waiter('db_instance_available') |
||||
|
waiter.wait(DBInstanceIdentifier='tug-of-war-db1') |
||||
|
|
||||
|
#dbArn = response['DBInstance'][0]['DBInstanceArn'] |
||||
|
dbEndpointAddress = response['DBInstance'][0]['Endpoint']['Address'] |
||||
|
dbEndpointPort = response['DBInstance'][0]['Endpoint']['Port'] |
||||
|
|
||||
|
print(dbEndpointAddress + ":" + dbEndpointPort) |
||||
|
|
||||
|
userDataWebServer = ('#!/bin/bash\n' |
||||
|
'# extra repo for RedHat rpms\n' |
||||
|
'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n' |
||||
|
'# essential tools\n' |
||||
|
'yum install -y joe htop git\n' |
||||
|
'# mysql\n' |
||||
|
'yum install -y httpd php php-mysql\n' |
||||
|
'\n' |
||||
|
'service httpd start\n' |
||||
|
'\n' |
||||
|
'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n' |
||||
|
'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n' |
||||
|
'cd /var/www/html\n' |
||||
|
'tar zxvf tug-of-war-in-the-clouds.tar.gz\n' |
||||
|
'\n' |
||||
|
'# change hostname of db connection\n' |
||||
|
'sed -i s/localhost/' + dbEndpointAddress + '/g /var/www/html/config.php\n' |
||||
|
'sed -i s/\\\'cloud\\\'/cloudcloud/g /var/www/html/config.php\n' |
||||
|
) |
||||
|
|
||||
|
for i in range(1, 2): |
||||
|
print("Running new Web Server instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = ec2Client.run_instances( |
||||
|
ImageId=imageId, |
||||
|
InstanceType=instanceType, |
||||
|
Placement={'AvailabilityZone': availabilityZone, }, |
||||
|
KeyName=keyName, |
||||
|
MinCount=1, |
||||
|
MaxCount=1, |
||||
|
UserData=userDataWebServer, |
||||
|
SecurityGroupIds=[ |
||||
|
security_group_id, |
||||
|
], |
||||
|
|
||||
|
TagSpecifications=[ |
||||
|
{ |
||||
|
'ResourceType': 'instance', |
||||
|
'Tags': [ |
||||
|
{'Key': 'Name', 'Value': 'tug-of-war-webserver' + i}, |
||||
|
{'Key': 'tug-of-war', 'Value': 'webserver'} |
||||
|
], |
||||
|
} |
||||
|
], |
||||
|
) |
||||
|
|
||||
|
instanceIdWeb = response['Instances'][0]['InstanceId'] |
||||
|
|
||||
|
instance = ec2Resource.Instance(instanceIdWeb) |
||||
|
instance.wait_until_running() |
||||
|
instance.load() |
||||
|
|
||||
|
print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) |
@ -0,0 +1,57 @@ |
|||||
|
import time |
||||
|
import boto3 |
||||
|
from botocore.exceptions import ClientError |
||||
|
|
||||
|
region = 'eu-central-1' |
||||
|
|
||||
|
client = boto3.setup_default_session(region_name=region) |
||||
|
ec2Client = boto3.client("ec2") |
||||
|
ec2Resource = boto3.resource('ec2') |
||||
|
|
||||
|
response = ec2Client.describe_vpcs() |
||||
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
||||
|
|
||||
|
elbv2Client = boto3.client('elbv2') |
||||
|
|
||||
|
print("Deleting load balancer and deps...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
try: |
||||
|
response = elbv2Client.describe_load_balancers(Names=['tug-of-war-loadbalancer']) |
||||
|
loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '') |
||||
|
response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn) |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
time.sleep(5) |
||||
|
|
||||
|
try: |
||||
|
response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) |
||||
|
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') |
||||
|
response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn) |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
|
||||
|
|
||||
|
print("Deleting old instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}]) |
||||
|
print(response) |
||||
|
reservations = response['Reservations'] |
||||
|
for reservation in reservations: |
||||
|
for instance in reservation['Instances']: |
||||
|
if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": |
||||
|
response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']]) |
||||
|
print(response) |
||||
|
instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) |
||||
|
instanceToTerminate.wait_until_terminated() |
||||
|
|
||||
|
print("Delete old security group...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
try: |
||||
|
response = ec2Client.delete_security_group(GroupName='tug-of-war') |
||||
|
except ClientError as e: |
||||
|
print(e) |
@ -0,0 +1,86 @@ |
|||||
|
import boto3 |
||||
|
from botocore.exceptions import ClientError |
||||
|
|
||||
|
region = 'eu-central-1' |
||||
|
availabilityZone = 'eu-central-1b' |
||||
|
subnet1 = 'subnet-41422b28' |
||||
|
subnet2 = 'subnet-5c5f6d16' |
||||
|
subnet3 = 'subnet-6f2ea214' |
||||
|
|
||||
|
client = boto3.setup_default_session(region_name=region) |
||||
|
ec2Client = boto3.client("ec2") |
||||
|
ec2Resource = boto3.resource('ec2') |
||||
|
|
||||
|
response = ec2Client.describe_vpcs() |
||||
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
||||
|
|
||||
|
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}]) |
||||
|
security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '') |
||||
|
|
||||
|
elbv2Client = boto3.client('elbv2') |
||||
|
|
||||
|
response = elbv2Client.create_load_balancer( |
||||
|
Name='tug-of-war-loadbalancer', |
||||
|
Subnets=[ |
||||
|
subnet1, |
||||
|
subnet2, |
||||
|
subnet3, |
||||
|
], |
||||
|
SecurityGroups=[ |
||||
|
security_group_id |
||||
|
] |
||||
|
) |
||||
|
|
||||
|
loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '') |
||||
|
loadbalancer_dns = response.get('LoadBalancers', [{}])[0].get('DNSName', '') |
||||
|
|
||||
|
response = elbv2Client.create_target_group( |
||||
|
Name='tug-of-war-targetgroup', |
||||
|
Port=80, |
||||
|
Protocol='HTTP', |
||||
|
VpcId=vpc_id, |
||||
|
) |
||||
|
|
||||
|
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') |
||||
|
|
||||
|
response = elbv2Client.create_listener( |
||||
|
DefaultActions=[ |
||||
|
{ |
||||
|
'TargetGroupArn': targetgroup_arn, |
||||
|
'Type': 'forward', |
||||
|
}, |
||||
|
], |
||||
|
LoadBalancerArn=loadbalancer_arn, |
||||
|
Port=80, |
||||
|
Protocol='HTTP', |
||||
|
) |
||||
|
|
||||
|
response = elbv2Client.modify_target_group_attributes( |
||||
|
TargetGroupArn=targetgroup_arn, |
||||
|
Attributes=[ |
||||
|
{ |
||||
|
'Key': 'stickiness.enabled', |
||||
|
'Value': 'true' |
||||
|
}, |
||||
|
] |
||||
|
) |
||||
|
|
||||
|
print("Registering instances...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = ec2Client.describe_instances(Filters=[{'Name': 'tag:tug-of-war', 'Values': ['webserver']}]) |
||||
|
print(response) |
||||
|
reservations = response['Reservations'] |
||||
|
for reservation in reservations: |
||||
|
for instance in reservation['Instances']: |
||||
|
if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": |
||||
|
response = elbv2Client.register_targets( |
||||
|
TargetGroupArn=targetgroup_arn, |
||||
|
Targets=[ |
||||
|
{ |
||||
|
'Id': instance['InstanceId'], |
||||
|
}, |
||||
|
], |
||||
|
) |
||||
|
|
||||
|
print('Load Balancer should be reachable at: ' + loadbalancer_dns) |
@ -0,0 +1,70 @@ |
|||||
|
import boto3 |
||||
|
from botocore.exceptions import ClientError |
||||
|
|
||||
|
region = 'eu-central-1' |
||||
|
availabilityZone = 'eu-central-1b' |
||||
|
subnet1 = 'subnet-41422b28' |
||||
|
subnet2 = 'subnet-5c5f6d16' |
||||
|
subnet3 = 'subnet-6f2ea214' |
||||
|
|
||||
|
client = boto3.setup_default_session(region_name=region) |
||||
|
ec2Client = boto3.client("ec2") |
||||
|
ec2Resource = boto3.resource('ec2') |
||||
|
|
||||
|
response = ec2Client.describe_vpcs() |
||||
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
||||
|
|
||||
|
elbv2Client = boto3.client('elbv2') |
||||
|
|
||||
|
for i in range(3, 4): |
||||
|
print("Running new Web Server instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = ec2Client.run_instances( |
||||
|
ImageId=imageId, |
||||
|
InstanceType=instanceType, |
||||
|
Placement={'AvailabilityZone': availabilityZone, }, |
||||
|
KeyName=keyName, |
||||
|
MinCount=1, |
||||
|
MaxCount=1, |
||||
|
UserData=userDataWebServer, |
||||
|
SecurityGroupIds=[ |
||||
|
security_group_id, |
||||
|
], |
||||
|
|
||||
|
TagSpecifications=[ |
||||
|
{ |
||||
|
'ResourceType': 'instance', |
||||
|
'Tags': [ |
||||
|
{'Key': 'Name', 'Value': 'tug-of-war-webserver1'}, |
||||
|
{'Key': 'tug-of-war', 'Value': 'webserver'} |
||||
|
], |
||||
|
} |
||||
|
], |
||||
|
) |
||||
|
|
||||
|
instanceIdWeb = response['Instances'][0]['InstanceId'] |
||||
|
|
||||
|
instance = ec2Resource.Instance(instanceIdWeb) |
||||
|
instance.wait_until_running() |
||||
|
instance.load() |
||||
|
|
||||
|
print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) |
||||
|
|
||||
|
try: |
||||
|
response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) |
||||
|
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') |
||||
|
except ClientError as e: |
||||
|
print(e) |
||||
|
|
||||
|
print("Registering instance...") |
||||
|
print("------------------------------------") |
||||
|
|
||||
|
response = elbv2Client.register_targets( |
||||
|
TargetGroupArn=targetgroup_arn, |
||||
|
Targets=[ |
||||
|
{ |
||||
|
'Id': instanceIdWeb, |
||||
|
}, |
||||
|
], |
||||
|
) |
@ -0,0 +1,101 @@ |
|||||
|
<?php |
||||
|
session_start(); |
||||
|
include 'config.php'; |
||||
|
?>
|
||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> |
||||
|
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> |
||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> |
||||
|
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> |
||||
|
|
||||
|
<?php |
||||
|
if (!isset($_SESSION["team"])) |
||||
|
{ |
||||
|
if (isset($_POST["team_minus"])) |
||||
|
{ |
||||
|
print "You joined team minus (blue)..."; |
||||
|
$_SESSION["team"]="minus"; |
||||
|
} |
||||
|
elseif (isset($_POST["team_plus"])) |
||||
|
{ |
||||
|
print "You joined team minus (green)..."; |
||||
|
$_SESSION["team"]="plus"; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
print "<h1>Choose your team!</h1>"; |
||||
|
?>
|
||||
|
<form action="" method="post"> |
||||
|
<input type="submit" name="team_minus" value="Team Minus (Blue)"> |
||||
|
<input type="submit" name="team_plus" value="Team Plus (Green)"> |
||||
|
</form> |
||||
|
<?php |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (isset($_SESSION["team"])) |
||||
|
{ |
||||
|
?>
|
||||
|
|
||||
|
<h1>Pull!!!</h1> |
||||
|
<p> |
||||
|
<?php |
||||
|
if (isset($_GET['cloud_id'])) |
||||
|
{ |
||||
|
$sql = "SELECT * FROM clouds WHERE cloud_id = " . $_GET['cloud_id']; |
||||
|
foreach ($pdo->query($sql) as $row) |
||||
|
{ |
||||
|
if (abs(intval($row["value"])) >= intval($row["max_value"])) |
||||
|
{ |
||||
|
unset($_SESSION["team"]); |
||||
|
$_SESSION["finished"] = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (isset($_POST['pull'])) |
||||
|
{ |
||||
|
if ($_SESSION["team"] == "plus") |
||||
|
{ |
||||
|
print "You pulled for team plus (green)..."; |
||||
|
$sql = "UPDATE clouds SET value=value+1 WHERE cloud_id = " . $_GET['cloud_id']; |
||||
|
$result = $pdo->query($sql); |
||||
|
} |
||||
|
if ($_SESSION["team"] == "minus") |
||||
|
{ |
||||
|
print "You pulled for team minus (blue)..."; |
||||
|
$sql = "UPDATE clouds SET value=value-1 WHERE cloud_id = " . $_GET['cloud_id']; |
||||
|
$result = $pdo->query($sql); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
$sql = "SELECT * FROM clouds WHERE cloud_id = " . $_GET['cloud_id']; |
||||
|
foreach ($pdo->query($sql) as $row) |
||||
|
{ |
||||
|
print "<h2>Value: " . $row["value"] . " (Goal: " . $row["max_value"] . ")</h2>"; |
||||
|
} |
||||
|
|
||||
|
if (!isset($_SESSION["finished"])) |
||||
|
{ |
||||
|
?>
|
||||
|
<form action="" method="post"> |
||||
|
<input type="submit" name="pull" value="Pull"> |
||||
|
</form> |
||||
|
<?php |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
print "<h2>game over</h2>"; |
||||
|
if (intval($row["value"] == intval($row["max_value"]))) |
||||
|
{ |
||||
|
print "Team plus (green) won!"; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
print "Team minus (blue) won!"; |
||||
|
} |
||||
|
session_destroy(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
?>
|
||||
|
<br /> |
||||
|
<a href='index.php'>Leave Game</a> |
@ -0,0 +1,3 @@ |
|||||
|
<?php |
||||
|
$pdo = new PDO('mysql:host=localhost;dbname=cloud_tug_of_war', 'cloud_tug_of_war', 'cloud'); |
||||
|
?>
|
@ -0,0 +1,36 @@ |
|||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> |
||||
|
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> |
||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> |
||||
|
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> |
||||
|
<h1>Welcome to "tug of war in the clouds"!</h1> |
||||
|
<p> |
||||
|
Choose and tug a cloud: |
||||
|
</p> |
||||
|
<p> |
||||
|
<?php |
||||
|
include 'config.php'; |
||||
|
|
||||
|
if (isset($_POST['new_cloud_name'])) |
||||
|
{ |
||||
|
$sql = "INSERT INTO clouds (name, value, max_value) VALUES ('" . $_POST['new_cloud_name'] . "',0," . $_POST['new_cloud_goal'] . ")"; |
||||
|
$pdo->query($sql); |
||||
|
} |
||||
|
|
||||
|
$sql = "SELECT * FROM clouds"; |
||||
|
foreach ($pdo->query($sql) as $row) { |
||||
|
if (abs(intval($row['value'])) < intval($row['max_value'])) |
||||
|
{ |
||||
|
echo "<a href='cloud.php?cloud_id=" . $row['cloud_id'] . "'>" . $row['name'] . "</a> (score: " . $row['value'] . ", goal:" . $row['max_value'] . ")<br />"; |
||||
|
} |
||||
|
} |
||||
|
?>
|
||||
|
<!-- list --> |
||||
|
</p> |
||||
|
<p> |
||||
|
or form a new cloud: |
||||
|
<form action="" method="post"> |
||||
|
Name: <input name="new_cloud_name" value=""> |
||||
|
Goal: <input name="new_cloud_goal" value="10"> |
||||
|
<input type="submit" value="Create"> |
||||
|
</form> |
||||
|
</p> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue