-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
168 lines (145 loc) · 4.41 KB
/
index.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Created by PhpStorm.
* User: Wayson
* Date: 5/24/2015
* Time: 8:01 PM
*/
require_once 'Model/Lawn.php';
require_once 'Model/Mower.php';
require_once 'Model/Position.php';
require 'db/DB.php';
require 'lib/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->post('/lawn', function() use ($app){
$width = $app->request->params('width');
$height = $app->request->params('height');
$lawn = array('width' => $width, 'height' => $height);
$db = new DB();
$lawnId = $db->insertLawn($lawn);
$db->closeDB();
$returnResult = array(
'id' => $lawnId,
'width' => $width,
'height' => $height
);
echo json_encode($returnResult);
});
$app->get('/lawn/:id', function($id){
$db = new DB();
$lawn = $db->getLawnById($id);
$db->closeDB();
if(empty($lawn))
{
echo json_encode(array('error_code' => 404, 'message' => 'Lawn not found'));
}
else
{
echo json_encode($lawn);
}
});
$app->delete('/lawn/:id', function($id){
$db = new DB();
$result = $db->deleteLawnById($id);
$db->closeDB();
echo json_encode($result);
});
$app->post('/lawn/:id/mower', function($id) use ($app){
$mower = array(
'x' => $app->request->params('x'),
'y' => $app->request->params('y'),
'heading' => $app->request->params('heading'),
'commands' => $app->request->params('commands'),
'lawn_id' => $id
);
$db = new DB();
$mower_id = $db->insertMower($mower);
$db->closeDB();
unset($mower['lawn_id']);
$mower = array('id' => $mower_id) + $mower;
echo json_encode($mower);
});
$app->get('/lawn/:id/mower/:mid', function($id, $mid) use ($app){
$db = new DB();
$mower = $db->getMowerByIdAndLawnId($mid, $id);
$db->closeDB();
echo json_encode($mower);
});
$app->put('/lawn/:id/mower/:mid', function($id, $mid) use ($app){
$db = new DB();
$mower = $db->getMowerByIdAndLawnId($mid, $id);
if(empty($mower))
{
echo json_encode(array('error_code' => '404', 'message'=> 'Mower not found'));
}
else
{
$mower['x'] = $app->request->params('x');
$mower['y'] = $app->request->params('y');
$mower['heading'] = $app->request->params('heading');
$mower['commands'] = $app->request->params('commands');
$mower['id'] = $mid;
$mower['lawn_id'] = $id;
$result = $db->updateMower($mower);
if($result == true)
{
$mower = $db->getMowerByIdAndLawnId($mid, $id);
$db->closeDB();
unset($mower['lawn_id']);
echo json_encode($mower);
}
else
{
$db->closeDB();
echo json_encode(array('error_code' => '302', 'message' => 'Unable to update mower'));
}
}
});
$app->delete('/lawn/:id/mower/:mid', function($id, $mid){
$db = new DB();
$result = $db->deleteMowerByIdAndLawnId($mid, $id);
$db->closeDB();
echo json_encode($result);
});
$app->post('/lawn/:id/execute', function($id){
$db = new DB();
$lawnResult = $db->getLawnById($id);
if(empty($lawnResult))
{
echo json_encode(array('error_code' => '404', 'message' => 'Lawn not found'));
}
elseif(empty($lawnResult['mowers']))
{
echo json_encode(array('error_code' => '204', 'message' => 'No mower on this lawn'));
}
else
{
$Lawn = new Lawn($lawnResult['width'], $lawnResult['height']);
foreach($lawnResult['mowers'] as $mower)
{
$position = new Position($mower['x'], $mower['y'], $mower['heading']);
$mowerObj = new Mower($position, $mower['commands']);
$Lawn->addMover($mowerObj);
}
$result = $Lawn->runMovers();
if($result['process_result'] == true)
{
$mowers = $Lawn->getMowers();
for($i = 0; $i < count($lawnResult['mowers']); $i++)
{
$position = $mowers[$i]->getCurrentPosition();
$lawnResult['mowers'][$i]['x'] = $position->getCordX();
$lawnResult['mowers'][$i]['y'] = $position->getCordY();
$lawnResult['mowers'][$i]['heading'] = $position->getOrientation();
unset($lawnResult['mowers'][$i]['lawn_id']);
}
echo json_encode($lawnResult);
}
else
{
echo $result['error_message'];
}
}
});
$app->run();