Skip to content

Commit

Permalink
add agent control
Browse files Browse the repository at this point in the history
  • Loading branch information
tess3ract committed Mar 7, 2014
1 parent 4f7134b commit e9fcbc3
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cockpit/application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
| MUST set an encryption key. See the user guide for info.
|
*/
$config['encryption_key'] = '';
$config['encryption_key'] = 'THERE ARE ONLY 11 CYLON MODELS';

/*
|--------------------------------------------------------------------------
Expand All @@ -247,7 +247,7 @@
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
Expand Down
48 changes: 47 additions & 1 deletion cockpit/application/controllers/agent.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ticket extends CI_Controller {
class Agent extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->library('Session');
$this->load->helper('user');
}

public function index() {
$user = get_user();
if (!$user) {
$this->load->view('agent_logging');
} else {
$this->load->vars('user', $user);
$this->load->view('agent_control');
}
}

private function check_login($name, $password) {
Expand Down Expand Up @@ -32,6 +43,41 @@ public function login() {
}
}

public function changepwd() {
$current_password = $this->input->get_post('current_password');
$new_password = $this->input->get_post('new_password');
$user = get_user();
if (!$user) {
echo json_encode(array('success' => 0));
return;
}
if ($user['passhash'] != md5($current_password)) {
echo json_encode(array('success' => 0));
return;
}
$this->db->update('agent', array('passhash' => md5($new_password)), array('name' => $user['name']));
echo json_encode(array('success' => 1));
}

public function status() {
$user = get_user();
if (!$user) {
echo json_encode(array('success' => 0));
return;
}
echo json_encode(array('name' => $user['name'], 'success' => 1, 'code' => $user['code'], 'level' => $user['level'], 'balance' => $user['credit_balance']));
}

public function log($page=0) {
$user = get_user();
if (!$user) {
echo json_encode(array('success' => 0));
return;
}
$logs = $this->db->where('agent', $user['id'])->order_by('time', 'DESC')->limit(50, $page*50)->get('creditlog')->result_array();
echo json_encode(array('success' => 1, 'logs' => $logs));
}

}

/* End of file */
8 changes: 8 additions & 0 deletions cockpit/application/controllers/apifeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public function area() {
}
}

public function makeprice($level, $discount) {
$products = $this->db->get('product')->result_array();
foreach ($products as $product) {
$price = $product['stock_price'] * $discount;
$this->db->replace('price', array('product_id' => $product['id'], 'agent_level' => $level, 'stock_price' => $price));
}
}

}

/* End of file */
69 changes: 69 additions & 0 deletions cockpit/application/controllers/make.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Make extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->helper('ticket');
$this->load->helper('misc');
$this->load->helper('user');
$this->load->helper('product');
$this->user = get_user();
if (!$this->user) {
die;
}
}

public function index() {
// list products and prices
$cates = make_dict($this->db->get('cate')->result_array());
$subcates = make_dict($this->db->get('subcate')->result_array());
$products = $this->db->get('product')->result_array();
$price = make_dict($this->db->get_where('price', array('agent_level' => $this->user['level']))->result_array(), 'product_id');
$tree = array();
foreach ($products as $product) {
$cate_id = $product['cate'];
$subcate_id = $product['subcate'];
if (!isset($tree[$cate_id])) {
$tree[$cate_id] = $cates[$cate_id];
$tree[$cate_id]['subcates'] = array();
}
if (!isset($tree[$cate_id]['subcates'][$subcate_id])) {
$tree[$cate_id]['subcates'][$subcate_id] = $subcates[$subcate_id];
$tree[$cate_id]['subcates'][$subcate_id]['products'] = array();
}
$product['stock_price'] = $price[$product['id']]['stock_price'];
$tree[$cate_id]['subcates'][$subcate_id]['products'][$product['id']] = $product;
}
$this->load->vars('tree', $tree);
$this->load->view('make');
}

public function create($product, $product_count, $ticket_count) {
$product = $this->db->get_where('product', array('id' => $product))->row_array();
if ($product_count > $product['count_max'] || $product_count < $product['count_min']) {
echo json_encode(array('success' => 0, 'max' => $product['count_max'], 'min' => $product['count_min']));
return;
}
$tickets = array();
for ($i = 0; $i < $ticket_count; $i ++) {
$number = new_ticket_number($product['id']);
$price = get_price($product['id'], $this->user['level']);
$this->db->insert('ticket', array(
"number" => $number,
"create_time" => time(),
"credit" => $price * $product_count,
"product_id" => $product['id'],
"count" => $product_count,
"state" => 0,
"fulfill_time" => 0,
"agent" => $this->user['id']
));
$tickets[] = $number;
}
echo json_encode(array('success' => 1, 'tickets' => $tickets));
}

}

/* End of file */
3 changes: 1 addition & 2 deletions cockpit/application/controllers/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public function fulfill() {
$reason = $ret;
}
}
if ($this->db->trans_status() === FALSE) {
if (!$success || $this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
$success = FALSE;
$reason = 'TRANSACTION_FAILED';
} else {
$this->db->trans_commit();
}
Expand Down
11 changes: 11 additions & 0 deletions cockpit/application/helpers/misc_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function make_dict($rows, $field = 'id') {
$dict = array();
foreach ($rows as $row) {
$dict[$row[$field]] = $row;
}
return $dict;
}

/* End of file */
29 changes: 27 additions & 2 deletions cockpit/application/helpers/product_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ function get_subcate($subcate) {
return $subcate;
}

function get_price($product, $agent_level) {
$CI =& get_instance();
$price = $CI->db->get_where('price', array('product_id' => $product, 'agent_level' => $agent_level))->row_array();
return $price['stock_price'];
}

function get_product_fields($product) {
$fields = array();
$areas = get_areas($product['id']);
Expand All @@ -32,10 +38,26 @@ function get_product_fields($product) {
return $fields;
}

function change_credit($user_id, $diff, $ticket_number) {
$CI =& get_instance();
$agent = get_user_by_id($user_id);
if ($agent['credit_balance'] + $diff < 0) {
return FALSE;
}
$new_balance = $agent['credit_balance'] + $diff;
$this->db->insert('creditlog', array('agent' => $agent['id'], 'diff' => $diff, 'balance' => $new_balance, 'ticket' => $ticket_number, 'time' => time()));
$this->db->update('agent', array('credit_balance' => $new_balance), array('id' => $agent['id']));
}

function create_job($ticket, $values) {
$CI =& get_instance();
$product = $ticket['product'];
$to = $values['to'];
$agent = get_user_by_id($ticket['agent']);
$price = get_price($product['id'], $agent['level']);
if (!change_credit($agent['id'], -$price, $ticket['number'])) {
return 'INSUFFICIENT_BALANCE_FROM_AGENT';
}
if ($product['cate'] == 4) {
$this->load->helper('user');
$user = get_user();
Expand All @@ -45,7 +67,7 @@ function create_job($ticket, $values) {
// hack self business here
if ($product['subcate'] == 900) {
// increase balance
$this->db->set("credit_balance", "credit_balance + " . $product['norm_value'])->where('name', $username)->update('agent');
change_credit($user['id'], $product['norm_value'], $ticket['number']);
}
if ($product['subcate'] == 910) {
// upgrade agent
Expand All @@ -54,7 +76,10 @@ function create_job($ticket, $values) {
if ($user['level'] != $from) {
return 'INVALID_CURRENT_LEVEL';
}
$this->db->update('agent', array('level' => $to), array('name' => $username));
if ($user['parent'] && $user['parent'] != $ticket['agent']) {
return 'PARENT_CONFLICTED';
}
$this->db->update('agent', array('level' => $to, 'parent' => $ticket['agent']), array('name' => $username));
}
}
else {
Expand Down
13 changes: 11 additions & 2 deletions cockpit/application/helpers/user_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function get_username() {
$CI =& get_instance();
$CI->load->library('Session');
$username = $CI->Session->userdata('username');
$username = $CI->session->userdata('username');
if (!$username) {
return FALSE;
}
Expand All @@ -16,7 +16,16 @@ function get_user() {
if (!$username) {
return FALSE;
}
return $CI->db->get_where('agent', array('username' => $username))->row_array();
return $CI->db->get_where('agent', array('name' => $username))->row_array();
}

function get_user_by_id($id, $lock = FALSE) {
$CI =& get_instance();
if ($lock) {
$this->db->lock(LOCK_EXCLUSIVE);
}
$user = $CI->db->get_where('agent', array('id' => $id))->row_array();
return $user;
}


Expand Down
77 changes: 77 additions & 0 deletions cockpit/application/views/agent_control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php $this->load->view('header'); ?>

<p><?php echo $user['name'];?>様、お帰りなさい。</p>

<p>お前のアカウントには <?php echo $user['credit_balance'];?>ポイント がありますよ〜</p>

<div id="stage">

</div>

<script type="text/template" id="tpl-password">
<form action="<?php echo site_url('/agent/changepwd');?>" method="post" id="form-pwd">
<p>CURRENT PWD: <input type="password" name="current_password" /></p>
<p>NEW PASSWOD: <input type="password" name="new_password" /></p>
<p><button type="submit" class="btn btn-danger">DO IT</button></p>
<p id="p-err"></p>
</form>
</script>

<p><button href="<?php echo site_url('/agent/status');?>" class="btn btn-primary btn-status">STATUS</button></p>

<p><button href="<?php echo site_url('/make');?>" class="btn btn-danger btn-lnk">MAKE TICKET</button></p>

<p><button class="btn btn-primary btn-password">CHANGE PASSWORD</button></p>

<p><button href="<?php echo site_url('/agent/log');?>" class="btn btn-info btn-log">CREDIT LOG</button></p>

<p><button href="<?php echo site_url('/agent/logout');?>" class="btn btn-danger btn-lnk">LOGOUT</button></p>

<script type="text/javascript">
function make_request(url, callback) {
$.get(url, function(data) {
if (!data.success) {
$('#stage').html('FAILED TO MAKE REQUEST!');
} else {
callback(data);
}
}, "json");
}
$('.btn-lnk').click(function(){
window.location.href = $(this).attr('href');
return false;
});
$('.btn-status').click(function(){
make_request($(this).attr('href'), function(data) {
$('#stage').html('');
for (var k in data) {
$('<p>' + k + ': ' + data[k] + '</p>').appendTo('#stage');
}
});
});
$('.btn-log').click(function(){
make_request($(this).attr('href'), function(data) {
$('#stage').html('これはログです:');
for (var i in data.logs) {
var row = data.logs[i];
$('<p>TICKET ' + row.ticket + ': DIFF ' + row.diff + ' BALANCE ' + row.balance + '</p>').appendTo('#stage');
}
$('<p>ーーー終わるーーー</p>').appendTo('#stage');
});
});
$('.btn-password').click(function(){
$('#stage').html($('#tpl-password').html());
$('#form-pwd').submit(function(){
$.get($(this).attr('action'), $(this).serialize(), function(data) {
if (data.success) {
$('#stage').html('NEW PASSWORD SET!');
} else {
$('#stage').html('FAILED TO CHANGE PASSWORD!');
}
}, "json");
return false;
});
});
$('#tab-control').addClass('active');
</script>
<?php $this->load->view('footer'); ?>
23 changes: 23 additions & 0 deletions cockpit/application/views/agent_logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php $this->load->view('header'); ?>

<form action="<?php echo site_url('/agent/login');?>" method="post" id="form-logging">
<p>USERNAME: <input type="text" name="name" /></p>
<p>PASSWORD: <input type="password" name="password" /></p>
<p><button type="submit" class="btn btn-primary">LOGIN</button></p>
<p id="p-err"></p>
</form>

<script type="text/javascript">
$('#form-logging').submit(function(){
$.get($(this).attr('action'), $(this).serialize(), function(data) {
if (data.success) {
window.location.reload();
} else {
$('#p-err').html('Incorrect name or password.');
}
}, "json");
return false;
});
$('#tab-logging').addClass('active');
</script>
<?php $this->load->view('footer'); ?>
1 change: 1 addition & 0 deletions cockpit/application/views/fulfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function resetpage() {
}, "json");
});
$('#number').keyup(check_number);
$('#tab-ticket').addClass('active');
</script>

<?php $this->load->view('footer'); ?>
Loading

0 comments on commit e9fcbc3

Please sign in to comment.