-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragon_factory.php
83 lines (73 loc) · 1.76 KB
/
dragon_factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
require 'class_dragon.php';
session_start();
const COLORS = [
'red',
'blue',
'green',
'pink',
'purple',
'orange',
'grey'
];
const POWERS = [
'fire',
'thunder',
'gravity',
'glacial',
'mathematics',
'water',
'earth'
];
function receive_user_data() {
if (count($_POST) > 0) {
createDragon($_POST['name']);
}
}
function createDragon($name) {
$power = POWERS[rand(0, count(POWERS)-1)];
$color = COLORS[rand(0, count(COLORS)-1)];
if (!(isset($_SESSION['dragons']))) { $_SESSION['dragons'] = [];}
$_SESSION['dragons'][$name] = (new Dragon($name, $power, $color));
}
function echoDragons() {
if (isset($_SESSION['dragons'])) {
echo '<table>
<tr>
<th>Name</th>
<th>Power</th>
<th>Color</th>
<th>Energy</th>
</tr>';
foreach ($_SESSION['dragons'] as $dragon) {
echo '<tr>';
foreach ($dragon as $val => $k) {
echo '<td>' . $k . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
receive_user_data();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>New Dragon</title>
</head>
<body>
<h1>Create new dragon</h1>
<form action="" method='post'>
<label for="name">Name</label>
<input type="text" max='30' name='name'>
<button type="submit">Create</button>
</form>
<?= echoDragons();?>
<div>Start combat, click <a href="index.php">here</a>. </div>
</body>
</html>