-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_MultiAtasciiGen.php
More file actions
142 lines (120 loc) · 4.02 KB
/
class_MultiAtasciiGen.php
File metadata and controls
142 lines (120 loc) · 4.02 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
<?
require_once('./class_AtasciiGen.php'); // general AtasciGen class - required
class MAGException extends AGException {}
class MultiAtasciiGen extends AtasciiGen {
//
// path constants
const USER_CONFIG_PATH="./users_configs/";
const DEFAULT_CONFIG_PATH="./default_configs/";
const DEFAULT_CONFIG_FILE="defaults";
const CONFIG_FILE_EXTENTION=".json";
const CONFIG_LAYOUTS_DEFAULT="default";
const CONFIG_LAYOUTS="layouts";
private $gameID=null;
private $layoutID=null;
public $singleLayout;
public $subLayouts=[];
public $scoreboard=[];
public function __construct($gameID = null, $layoutID = self::CONFIG_LAYOUTS_DEFAULT) {
if ($gameID===null) throw new Exception("GameID must be defined!");
$this->gameID=$gameID;
$this->layoutID=$layoutID;
// choice configure file to load (between dedicated file and default)
$configFile=
self::USER_CONFIG_PATH.
$this->gameID.
self::CONFIG_FILE_EXTENTION;
// check game config file is exist
if ( !file_exists($configFile) ) {
// if not, check default config file is exist
$configFile=
self::DEFAULT_CONFIG_PATH.
self::DEFAULT_CONFIG_FILE.
self::CONFIG_FILE_EXTENTION;
if ( !file_exists($configFile) ) {
// if default file is not exist, throw exception
throw new Exception("Default config file not exist!");
}
// $this->layoutID=self::CONFIG_LAYOUTS_DEFAULT;
}
$this->fetchScoreboardFromDB();
$this->singleLayout=true;
try {
parent::__construct($configFile);
} catch (AGException $th) {
if ($th->getMessage()!=="No layout defined")
throw new AGException($th->getMessage());
$this->singleLayout=false;
}
if (@!$this->config[self::CONFIG_LAYOUTS]) {
if ( !$this->singleLayout ) {
throw new MAGException("Layout(s) not defined");
}
} else {
// $this->layoutData=&$this->config[self::CONFIG_LAYOUTS];
foreach ($this->config[self::CONFIG_LAYOUTS] as $subId => $layout) {
if ( isset($layout['unlisted']) ) continue;
$this->subLayouts[]=$subId;
}
}
}
function fetchScoreboardFromDB() {
throw new MAGException('Expand `MultiAtasciGen` class and inherit method `fetchScoreboardFromDB()`.');
}
function generate() {
if ( $this->layoutID!==null ) {
if ( isset($this->config[self::CONFIG_LAYOUTS][$this->layoutID]) ) {
$this->layoutData=&$this->config[self::CONFIG_LAYOUTS][$this->layoutID];
} else {
throw new MAGException("Sub layout is not present.");
}
} else {
if ( isset($this->config[SECTION_LAYOUT]) ) {
$this->layoutData=&$this->config[SECTION_LAYOUT];
} else {
throw new MAGException("Layout section is not dafined.");
}
}
return parent::generate();
}
//
//
//
public function getLayoutColorsData() {
$this->layoutData=&$this->config[self::CONFIG_LAYOUTS][$this->layoutID];
$out="";
foreach ($this->colorReg as $colId => $colVal) {
$out.=chr($colVal);
}
return $out;
}
public function getLayoutInfoData() {
$this->layoutData=&$this->config[self::CONFIG_LAYOUTS][$this->layoutID];
$out="";
$graphMode=0;
$encode=0; // 0 - antic; 1 - atasci
$out.=chr($graphMode);
$out.=chr($encode);
$out.=chr($this->layoutData['width']);
$out.=chr($this->layoutData['height']);
$out.=$this->getLayoutColorsData();
$out.=leftStr(( !isset($this->params['title'] ) )?" ":$this->params['title'],40);
$out.=leftStr(( !isset($this->params['mode'] ) )?" ":$this->params['mode'],40);
$out.=leftStr(( !isset($this->config['author'] ) )?" ":$this->config['author'],40);
return $out;
}
function getLayoutsList($includeAuthor=true) {
$list=[];
if ( isset($this->config[self::CONFIG_LAYOUTS]) ) {
$generalAuthor=(isset($this->config['author']))?$this->config['author']:'';
foreach ($this->config[self::CONFIG_LAYOUTS] as $subId => $layout) {
if ( isset($layout['unlisted']) ) continue;
$layoutName=(isset($layout['name']))?$layout['name']:$subId;
$layoutAuthor=(isset($layout['author']))?$layout['author']:$generalAuthor;
$list[$subId]=$layoutName.($includeAuthor?' ('.$layoutAuthor.')':'');
}
}
return json_encode($list);
}
}
?>