-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
123 lines (104 loc) · 3.57 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
<?php
// *** PHP Configuration
// ----------------------------------------------------------------------------
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('xdebug.var_display_max_depth', '10');
ini_set('xdebug.var_display_max_children', '256');
ini_set('xdebug.var_display_max_data', '1024');
// *** Autoloads // Requires // Includes
// ----------------------------------------------------------------------------
require('vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
// *** Application
// ----------------------------------------------------------------------------
/**
* Main Application Class
*
* Class Application
*/
class Application
{
private $booksDir = "resources";
private $viewDir = "templates";
private $view = null;
/**
* Application constructor
*
* Set application specific defaults
*/
public function __construct()
{
$this->view = new Smarty();
$this->view->assign('appName', "Ancient Typist");
$this->view->assign('appVersion', "v1.20");
}
/**
* Application logic
*
* @throws SmartyException
*/
public function run()
{
// *** Set Defaults
$player = [
// Book
'bookTitle' => '?',
'bookAuthor' => '?',
'bookCurrentPage' => 0,
'bookPreviousPage' => 0,
'bookNextPage' => 0,
'bookFinishedPages' => 0,
'bookTotalPages' => 0,
'bookId' => 0,
// Player
'rewardPoints' => 0,
'rewardStars' => 0,
'totalFinishedPages' => 0,
// Lesson
'lessonText' => 'Ancient Text To Type.',
];
// Load selected book...
$bookId = (isset($_GET["book"]) ? intval($_GET["book"]) : 1);
if ($bookId > 6) $bookId = 6;
if ($bookId < 0) $bookId = 1;
// *** Load selected book's content
$bookLines = file(getcwd() . "/{$this->booksDir}/book{$bookId}.txt");
$bookTotalPages = count($bookLines) - 2;
// *** Load selected text...
$page = (isset($_GET["page"]) ? intval($_GET["page"]) : 1);
if ($page > $bookTotalPages) {
$page = $bookTotalPages;
}
if ($page < 0) $page = 1;
$bookPreviousPage = $page - 1;
if ($bookPreviousPage < 1) {
$bookPreviousPage = 1;
}
$bookNextPage = $page + 1;
if ($bookNextPage >= $bookTotalPages) {
$bookNextPage = $bookTotalPages;
}
// *** Prepare text...
$index = $page + 1;
$lessonText = $bookLines[$index];
$lessonText = trim(str_replace(["\r", "\n", "\r\n"], "", $lessonText));
$lessonText = str_split($lessonText);
// *** Fill player data...
$player['bookId'] = $bookId;
$player['bookTitle'] = $bookLines[0] ?: "Unknown";
$player['bookAuthor'] = $bookLines[1] ?: "Unnamed";
$player['bookCurrentPage'] = $page;
$player['bookPreviousPage'] = $bookPreviousPage;
$player['bookNextPage'] = $bookNextPage;
$player['bookTotalPages'] = $bookTotalPages;
$player['lessonText'] = $lessonText;
// *** Display the page...
$this->view->assign('player', $player);
$this->view->display("{$this->viewDir}/index.tpl");
}
}
// *** Run
// ----------------------------------------------------------------------------
$application = new Application();
$application->run();