forked from AustinWise/fleet_links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlliance.php
More file actions
152 lines (131 loc) · 3.59 KB
/
Copy pathAlliance.php
File metadata and controls
152 lines (131 loc) · 3.59 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once('DataManager.php');
require_once('Utilities.php');
class Alliance {
function __construct() {
}
public $Id;
public $Name;
private $inDatabase = FALSE;
// fills a Alliance object with data from mysql
private static function fill(&$a, $id, $name) {
$a->Id = (int)$id;
$a->Name = $name;
$a->inDatabase = TRUE;
}
// returns the alliance with the given id.
// throws an exception if it's not found.
public static function Get($id) {
if (!is_int($id)) {
throw new Exception("id was not an int.");
}
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('SELECT id, name FROM alliance WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($id, $name);
if (!$stmt->fetch()) {
throw new Exception("Alliance not found.");
}
$a = new Alliance();
Alliance::fill($a, $id, $name);
$stmt->close();
return $a;
}
// Returns array contain all the Alliances.
public static function GetAll() {
$query = DataManager::GetInstance()->Query("SELECT id, name FROM alliance");
$items = array();
while ($assoc = $query->fetch_assoc()) {
$a = new Alliance();
Alliance::fill($a, $assoc['id'], $assoc['name']);
$items[] = $a;
}
$query->close();
return $items;
}
// Make sure an alliance is already in the database.
public static function EnsureAlliance($id, $name) {
$a;
try {
$a = Alliance::Get($id);
}
catch (Exception $ex) {
$a = new Alliance();
$a->Id = $id;
$a->Name = $name;
$a->Save();
}
return $a;
}
public static function GetAlliancesOtherThanMineWithFleets($myAllianceId) {
if (!is_int($myAllianceId)) {
throw new Exception("myAllianceId was not an int.");
}
$conn = DataManager::GetInstance()->GetConnection();
$stmt = $conn->prepare('SELECT id, name FROM alliance WHERE id in (SELECT allianceId FROM fleet WHERE added > ?) AND id != ?');
$stmt->bind_param('si', DataManager::GetInstance()->FormatTimestampForSql(LastDowntimeMidpoint()), $myAllianceId);
$stmt->execute();
$stmt->bind_result($id, $name);
$alliances = array();
while ($stmt->fetch()) {
$a = new Alliance();
Alliance::fill($a, $id, $name);
$alliances[] = $a;
}
$stmt->close();
return $alliances;
}
public function ActiveFleets() {
return Fleet::GetFleetsForAlliance($this->Id);
}
// Returns TRUE if the Aliiance record was added or updated in
// the database, FALSE otherwise.
// Update not implemented yet.
public function Save() {
if (!$this->Validate())
throw new Exception('Alliance not valid; unable to save.');
$conn = DataManager::GetInstance()->GetConnection();
if (!$this->inDatabase) {
$stmt = $conn->prepare('INSERT INTO alliance (id, name) VALUES (?, ?)');
$stmt->bind_param('is', $this->Id, $this->Name);
$stmt->execute();
$rows = $stmt->affected_rows;
$stmt->close();
if ($rows === 1) {
$this->inDatabase = TRUE;
return TRUE;
}
else
return FALSE;
}
else {
$stmt = $conn->prepare('UPDATE alliance SET name=? WHERE id=?');
$stmt->bind_param('si', $this->Name, $this->Id);
$stmt->execute();
$rows = $stmt->affected_rows;
$stmt->close();
if ($rows === 1)
return TRUE;
else
return FALSE;
}
return FALSE;
}
// Returns TRUE if the Alliance is valid and ready to be
// saved to the database.
public function Validate() {
if (!isset($this->Id) || !is_int($this->Id))
return FALSE;
if (isset($this->Name) && is_string($this->Name)) {
$strlen = strlen($this->Name);
if ($strlen < 1 || $strlen > 50)
return FALSE;
}
else {
return FALSE;
}
return TRUE;
}
}
?>