-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanetClass.php
127 lines (127 loc) · 3.62 KB
/
PlanetClass.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
class PlanetClass
{
private $id;
function __construct($id)
{
$this->id = $id;
}
function getName()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT name FROM planets WHERE id = $this->id");
$row = mysql_fetch_assoc($result);
return $row['name'];
}
function getTempLow()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT tempLow FROM planets WHERE id = $this->id");
$row = mysql_fetch_assoc($result);
return $row['tempLow'];
}
function getTempHigh()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT tempHigh FROM planets WHERE id = $this->id");
$row = mysql_fetch_assoc($result);
return $row['tempHigh'];
}
function getXLocation()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT xLocation FROM planets WHERE id = $this->id");
$row = mysql_fetch_assoc($result);
return $row['xLocation']/1000;
}
function getYLocation()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT yLocation FROM planets WHERE id = $this->id");
$row = mysql_fetch_assoc($result);
return $row['yLocation']/1000;
}
function isNull()
{
if($this->id == -1)
{
return true;
}
return false;
}
function canPlanetSupport($animalType)
{
if($animalType->getTempLow() > $this->getTempLow())
{
return false;
}
if($animalType->getTempHigh() < $this->getTempHigh())
{
return false;
}
return true;
}
function doesPlanetContain($animalType)
{
$db = new DatabaseClass();
$result = mysql_query("SELECT animalTypeID FROM planetAnimalTypes WHERE planetID = $this->id");
while($row = mysql_fetch_assoc($result))
{
if($row['animalTypeID'] == $animalType->getID())
{
return true;
}
}
return false;
}
function getAnimalTypes()
{
$db = new DatabaseClass();
$result = mysql_query("SELECT animalTypeID FROM planetAnimalTypes WHERE planetID = $this->id");
$animalTypes = array();
while($row = mysql_fetch_assoc($result))
{
$animalTypes[] = new AnimalTypeClass($row['animalTypeID']);
}
return $animalTypes;
}
private static function getRarityArr($animalTypes)
{
$rarityArr = array();
foreach ($animalTypes as $currAnimal)
{
$rarityArr[] = $currAnimal->getRarity();
}
return $rarityArr;
}
function pickAnimal()
{
$db = new DatabaseClass();
$animalTypes = $this->getAnimalTypes();
$numAnimalTypes = count($animalTypes);
$rarityArr = PlanetClass::getRarityArr($animalTypes);
$lcmRarity = MathClass::lcmArr($rarityArr);
for($i = 0; $i < count($rarityArr); $i++)
{
$rarityArr[$i] = $lcmRarity/$rarityArr[$i];
}
$rarityTotal = MathClass::sumElementsInArr($rarityArr);
$randNum = mt_rand(1,$rarityTotal);
for($i = 0; $i < count($rarityArr); $i++)
{
if($randNum <= $rarityArr[$i])
{
$chosenAnimalTypeIndex = $i;
break;
}
else
{
$randNum -= $rarityArr[$i];
}
}
$chosenAnimalType = $animalTypes[$chosenAnimalTypeIndex];
$newAnimal = $chosenAnimalType->createNewAnimal();
return $newAnimal;
}
}
?>