Skip to content

Commit 95d9975

Browse files
Implement host page
1 parent 7246f3e commit 95d9975

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed

page/admin.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

33
if($_host_session->is_login){
4-
$_experiment_id = $request->get_uri(0, 1);
4+
$_experiment_id = $_request->get_uri(0, 1);
55
if($_experiment_id !== false){
66
$_experiment = $_experiment_model->get($_experiment_id);
77
if($_host_session->user_id !== $_experiment['host_id']){
88
redirect_uri(_URL);
99
}
1010
$_modui = new ModUI('test', new NormalContainer());
11-
require DIR_ROOT . 'game/' . $_game_id . '/admin.php';
11+
$_game = $_game_model->get($_experiment['game_id']);
12+
require DIR_ROOT . 'game/' . $_game['directory'] . '/admin.php';
1213
if($_request->request_method === Request::GET){
1314
$_modui->enable_auto_reload(5000, <<<JS
1415
function(){
@@ -22,15 +23,15 @@ function(){
2223
}
2324
JS
2425
);
25-
$result = $modui->display();
26+
$_result = $_modui->display();
2627

27-
$tmpl = new Template();
28-
foreach($result['templates'] as $key => $template){
29-
$tmpl->lwte_add($key, $template);
28+
$_tmpl = new Template();
29+
foreach($_result['templates'] as $key => $template){
30+
$_tmpl->lwte_add($key, $template);
3031
}
31-
$tmpl->lwte_use('#container', 'test', $result['values']);
32-
$tmpl->add_script($result['script']);
33-
$tmpl->add_script(<<<JS
32+
$_tmpl->lwte_use('#container', 'test', $_result['values']);
33+
$_tmpl->add_script($_result['script']);
34+
$_tmpl->add_script(<<<JS
3435
function update_modui(name, value){
3536
$.ajax({
3637
url: window.location.pathname,
@@ -45,9 +46,9 @@ function update_modui(name, value){
4546
}
4647
JS
4748
);
48-
echo $tmpl->display();
49+
echo $_tmpl->display();
4950
}else{
50-
$modui->input($_POST);
51+
$_modui->input($_POST);
5152
}
5253
}
5354
}else{

page/host.php

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
11
<?php
2-
32
if($_host_session->is_login){
4-
$_experiment_model = new ExperimentModel($_pdo);
53
$_host = $_host_model->get($_host_session->user_id);
6-
$_experiments = $_experiment_model->get_all_by_host($_host['id']);
4+
if($_request->request_method === Request::GET){
5+
$_games = $_game_model->get_all();
6+
$_experiments = $_experiment_model->get_all_by_host($_host['id']);
7+
$_data = ['token_name' => _TOKEN, 'token' => get_token('host')];
8+
$_data['games'] = $_games;
9+
$_data['experiments'] = $_experiments;
10+
$_template = '';
11+
$_template .= <<<TMPL
12+
[games]<br/>
13+
<form action="./host/games" method="post">
14+
{each games}
15+
ID: {id}, name: {name};
16+
<input type="submit" name="game_id" value="{id}"></input><br/>
17+
{/each}
18+
<input type="hidden" name="{token_name}" value="{token}"></input>
19+
</form>
20+
[experiments]<br/>
21+
<form action="./host/experiments" method="post">
22+
{each experiments}
23+
ID: {id} , gameID: {game_id};
24+
<a href="./admin/{id}">admin</a>
25+
<input type="submit" name="experiment_id" value="{id}"></input><br/>
26+
{/each}
27+
<input type="hidden" name="{token_name}" value="{token}"></input>
28+
</form><br/>
29+
TMPL;
30+
$_tmpl = new Template();
31+
$_tmpl->lwte_add('host', $_template);
32+
$_tmpl->lwte_use('#container', 'host', $_data);
33+
echo $_tmpl->display();
34+
}else{
35+
if(!check_token('host', $_request->get_string(_TOKEN))){
36+
switch($_request->get_uri()){
37+
case 'games':
38+
if((($_game_id = $_request->get_string('game_id')) !== null) &&
39+
($_game_model->exist_id($_game_id))){
40+
$_experiment_model->insert($_host['id'], $_game_id);
41+
}
42+
break;
43+
case 'experiments':
44+
if((($_experiment_id = $_request->get_string('experiment_id')) !== null) &&
45+
($_experiment_model->exist_id($_experiment_id))){
46+
$_experiment_model->set_status(ExperimentModel::S_RUNNING);
47+
redirect_uri(_URL . 'admin/' . $_exiperient_id);
48+
}
49+
break;
50+
}
51+
redirect_uri(_URL . 'host');
52+
}
53+
}
754
}else{
855
//game list
56+
$_games = $_game_model->get_all();
57+
$_data = [];
58+
foreach($_games as $_game){
59+
$_data['games'][] = ['id' => $_game['id'], 'name' => $_game['name']];
60+
}
61+
$_tmpl = new Template();
62+
$_tmpl->lwte_add('host', <<<TMPL
63+
[games]<br/>
64+
{each games}
65+
ID: {id}, name: {name};<br/>
66+
{/each}
67+
TMPL
68+
);
69+
$_tmpl->lwte_use('#container', 'host', $_data);
70+
echo $_tmpl->display();
971
}

require/model/ExperimentModel.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class ExperimentModel extends Model{
1010

1111
function insert($host_id, $game_id){
1212
$password = $this->create_password();
13-
return ['id' => $this->con->insert('experiment', ['host_id' => $host_id, 'game_id' => $game_id, 'password' => $this->hash_password($password)], true), 'password' => $password];
13+
return $this->con->insert('experiment', ['host_id' => $host_id, 'game_id' => $game_id, 'password' => $password], true);
1414
}
1515

1616
function get_all(){
1717
return $this->con->fetchAll('SELECT `id`, `host_id`, `game_id` FROM `experiment`');
1818
}
1919

2020
function get($id){
21-
return $this->con->fetch('SELECT `id`, `host_id`, `game_id`, `status` FROM `experiment` WHERE `id` = ?', $id);
21+
return $this->con->fetch('SELECT `id`, `host_id`, `game_id`, `password`, `status` FROM `experiment` WHERE `id` = ?', $id);
2222
}
2323

2424
function get_all_by_host($host_id){
@@ -30,11 +30,11 @@ function get_experiment($password){
3030
}
3131

3232
function hash_password($password){
33-
return sha256($password + self::$salt);
33+
return sha256($password . self::$salt);
3434
}
3535

3636
function check_password($password){
37-
if($this->con->get_count('experiment', ['password' => $this->hash_password($password)]) === 0){
37+
if($this->con->get_count('experiment', ['password' => $password]) === 0){
3838
return false;
3939
}
4040
return true;
@@ -54,4 +54,7 @@ function exist_id($id){
5454
return false;
5555
}
5656

57+
function set_status($id, $status){
58+
return $this->con->update('experiment', ['status' => $status], '`id` = ?', [$id]);
59+
}
5760
}

require/model/GameModel.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ function get($id){
1010
return $this->con->fetch('SELECT `id`, `name`, `directory` FROM `game` WHERE `id` = ?', $id);
1111
}
1212

13-
13+
function exist_id($id){
14+
if($this->con->get_count('game', ['id' => $id]) === 1){
15+
return true;
16+
}
17+
return false;
18+
}
1419
}

tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'id' => 'INT PRIMARY KEY AUTO_INCREMENT',
1616
'host_id' => 'INT',
1717
'game_id' => 'INT',
18-
'password' => 'VARCHAR(64)', //hashed by sha256
18+
'password' => 'VARCHAR(6)',
1919
'status' => 'TINYINT'
2020
],
2121
'participant' => [

0 commit comments

Comments
 (0)