Skip to content

Commit

Permalink
Integrated testing for fourth milestone.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Carter committed Mar 3, 2014
1 parent 992b9b5 commit 3de0581
Show file tree
Hide file tree
Showing 18 changed files with 457 additions and 104 deletions.
23 changes: 23 additions & 0 deletions card_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ function card(fname, id)
top_z = top_z + 1;
console.log("top_z" + top_z);
}

//Since bring_to_top relies on a click to set 'this'
// we need a programmably-called z-index setter.
this.set_z_idx = function(new_z)
{
this.card_div.style.zIndex = new_z;
}

this.set_position = function (x_pos, y_pos)
{
Expand Down Expand Up @@ -135,6 +142,22 @@ function card(fname, id)
card.border = "2px solid red";
}
}


//Remove the card from the table if it exists
this.remove_card = function()
{
//check if it's on the table and remove it.
var cardDiv = document.getElementById(this.card_id);
if (cardDiv && cardDiv.parentNode && cardDiv.parentNode.removeChild){
cardDiv.parentNode.removeChild(cardDiv);
};
}

this.reinst_card = function()
{
cont_div.appendChild(this.card_div);
}

//Constructor Revised!
card_array[id] = this; //This links the id of the card to its position in the card array
Expand Down
15 changes: 10 additions & 5 deletions card_interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<script type = "text/javascript" src="network_class.js"></script>
<script type = "text/javascript" src = "tests/network_tests.js"> </script>
<script type = "text/javascript" src = "tests/testcard.js"> </script>
<script type = "text/javascript" src = "test_selection_class.js"> </script>
<script type = "text/javascript" src = "tests/test_selection_class.js"> </script>
<script type = "text/javascript" src = "tests/chat_tests.js"> </script>
<script type = "text/javascript" src = "tests/hand_tests.js"> </script>
<script type = "text/javascript" src = "selection_class.js"> </script>
<script type = "text/javascript" src = "player_class.js"> </script>
<script src="init.js"></script>
<script type = 'text/javascript'>
var last_update = (8 * 3600) + 1; //The EPOCH was 16:00 our time........... The server has a locale!
var shift_down = 0;
var test_cnt = 7;
//

window.onclick = function(e)
{
//alert("You clicked on the page!");
if(e.button == 0){
if(e.button == 0 && e.target.name != "test"){
//We got a left click
//release all locks
select.release_locks();
Expand Down Expand Up @@ -71,7 +72,11 @@
<div id="container">
<div id="hand">HAND</div>
<div id="context_menu">CONTEXT MENU
<button type = 'button' onclick = 'test_grab_card();'>Tests</button>
<button type = 'button' name = 'test' onclick = 'test_hand_harness();'>Test Hand</button>
<button type = 'button' name = 'test' onclick = 'test_all_card();'> Test Card/Network </button>
<button type = 'button' name = 'test' onclick = 'chat_test_harness();'> Test Chat </button>
<button type = 'button' name = 'test' onclick = 'test_select_harness();'>Test Select</button>
<button type = 'button' name = 'test' onclick = 'window.location = "deck_test.html";'> Test Deck </button>
</div>
</div>
<div id="chat_bar">
Expand Down
70 changes: 64 additions & 6 deletions cards_mgmt.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
release_locks();
break;

case 8: //Set id of player based on username
case 8: //Set id of player based on username
get_player_id();
break;

case 9:
//RESERVED
case 9: //Adding card to hand
add_to_hand();
break;

case 10:
Expand All @@ -52,7 +52,11 @@
case 11:
add_new_player();
break;


case 12: //Removing cards from hand
remove_hand();
break;

default:
echo 'unrecognized operation' . $op;
}
Expand All @@ -66,11 +70,12 @@ function move_card()
$player_id = $_POST['pid'];
$x_pos = $_POST['x_pos'];
$y_pos = $_POST['y_pos'];
$z_pos = $_POST['z_pos'];

$time = date('Y-m-d H:i:s', time());

$sql = "UPDATE Cards C
SET last_update = NOW(), x_pos = $x_pos, y_pos = $y_pos
SET last_update = NOW(), x_pos = $x_pos, y_pos = $y_pos, z_pos = $z_pos
WHERE C.cid = '$card_id' AND C.game_id = $game_id AND C.locked = $player_id";

$result = execute_query($sql);
Expand Down Expand Up @@ -110,7 +115,7 @@ function get_positions()

$time = date('Y-m-d H:i:s', strtotime($time) - 30);

$sql = "SELECT cid, x_pos, y_pos, flipped, locked
$sql = "SELECT cid, x_pos, y_pos, z_pos, flipped, locked, in_hand
FROM Cards
WHERE game_id = $game_id AND last_update > '$time';";

Expand Down Expand Up @@ -235,6 +240,59 @@ function release_locks()
$result = execute_query($sql2);
}

/* Function add_to_hand()
* Return: If the card's in_hand value was changed, this returns a
* 0. Failure is a 1.
*/
function add_to_hand()
{
$card_id = $_POST['card_id'];
$game_id = $_POST['game_id'];
$player_id = $_POST['player_id'];

$sql = " UPDATE Cards C
SET C.in_hand = $player_id
WHERE C.cid = '$card_id' AND C.game_id = $game_id AND C.locked = $player_id";

$result = execute_query($sql);
if($mysqli->affected_rows != 0){
$result = '"1"'; //Failure, did not add card to hand

} else {
$result = '"0"';

}

$result = make_json($sql, $game_id, "", $result);
echo $result;
}

//Empties selected cards from your hand to table at a
// hardcoded location. TODO: make this value not hardcoded.
// Will deselect cards too.
function remove_hand()
{
$player_id = $_POST['player_id'];
$game_id = $_POST['game_id'];

$sql = " UPDATE Cards C
SET C.locked = -1, C.in_hand = 0, C.x_pos = 200, C.y_pos = 200
WHERE C.in_hand = $player_id AND C.game_id = $game_id;";

$result = execute_query($sql);
if($mysqli->affected_rows != 0){
$result = '"1"'; //Failure, did not add card to hand

} else {
$result = '"0"';

}

$result = make_json($sql, $game_id, "", $result);
echo $result;
}


function get_player_id()
{
$username = $_POST['username'];
Expand Down
Empty file modified change_perms.sh
100755 → 100644
Empty file.
Empty file modified chat_init.js
100755 → 100644
Empty file.
Empty file modified example_files/connect_mysql.php
100755 → 100644
Empty file.
Empty file modified example_files/create_new_account.php
100755 → 100644
Empty file.
Empty file modified example_files/login.php
100755 → 100644
Empty file.
Empty file modified example_files/validate_login.php
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions interface.css
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ body{
height:50%;
left:5%;
overflow:visible;
overflow-y:scroll;
word-wrap:break-word;
}

Expand Down
70 changes: 57 additions & 13 deletions network_class.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function mk_network()
var locations = document.URL.split("/");
this.web_root = document.URL.replace(locations[locations.length - 1], "");
this.game_id = 4;
this.board_update_interval = 500; //milliseconds between updates
this.board_update_interval = 1500; //milliseconds between updates
this.board_update_timer; //A reference to the update timer (incase we need to remove it)
//this.last_update; //Seconds since the EPOCH that we last updated =)
this.send_update_interval = 300; //milliseconds between updating a card.
this.send_update_interval = 1300; //milliseconds between updating a card.
this.send_update_timer; //A reference to the send timer (we will need to remove it)
//Member functions
//Responsible for getting the 52 cards from the server, creating them
Expand Down Expand Up @@ -127,20 +127,33 @@ function mk_network()
}

//Set positions, flipped and locks.
//Handle cards in other people's hands
for (card_idx in board_state.result) {
var cur_card = board_state.result[card_idx];

if (select.grabbed_card != cur_card.cid) {
card_array[cur_card.cid].set_position(cur_card.x_pos, cur_card.y_pos);

card_array[cur_card.cid].db_flip_card((+cur_card.flipped));
if ((+cur_card.locked) == -1) {
card_array[cur_card.cid].set_selected(0);
} else if ((+cur_card.locked) != player.player_id) {
card_array[cur_card.cid].set_selected(-1);

if (cur_card.in_hand == player.player_id) {
console.log("Found ", cur_card.cid, " in hand.");

} else if(cur_card.in_hand == 0){

if (select.grabbed_card != cur_card.cid) {
card_array[cur_card.cid].set_position(cur_card.x_pos, cur_card.y_pos);
card_array[cur_card.cid].set_z_idx(cur_card.z_pos);
if (cur_card.z_pos > top_z){
top_z = (+cur_card.z_pos) + 1;
}

card_array[cur_card.cid].db_flip_card((+cur_card.flipped));
if ((+cur_card.locked) == -1) {
card_array[cur_card.cid].set_selected(0);
} else if ((+cur_card.locked) != player.player_id) {
card_array[cur_card.cid].set_selected(-1);
}
} else {
//alert("skipping card update: " + grabbed_card);
}
} else {
//alert("skipping card update: " + grabbed_card);
card_array[cur_card.cid].remove_card();
}
}
}
Expand All @@ -161,9 +174,10 @@ function mk_network()
var card = document.getElementById(card_array[card_idx].card_id);
var x_pos = card.offsetLeft;
var y_pos = card.offsetTop;
var z_pos = card.style.zIndex;

//AJAX
var var_string = "op=1&game_id=" + this.game_id + "&cid=" + card_idx + "&pid=" + player.player_id + "&x_pos=" + x_pos + "&y_pos=" + y_pos;
var var_string = "op=1&game_id=" + this.game_id + "&cid=" + card_idx + "&pid=" + player.player_id + "&x_pos=" + x_pos + "&y_pos=" + y_pos + "&z_pos=" + z_pos;
var ajax_obj = this.ajax("cards_mgmt.php", var_string, empty_funct, true);
//console.log(ajax_obj.responseText);
}
Expand All @@ -190,6 +204,36 @@ function mk_network()
return board_state;
} //Sets "flipped" to the new flip_val


this.st_add_hand_db = function(card_idx)
{
var var_string = 'op=9&game_id=' + this.game_id + '&card_id=' + card_idx + "&player_id=" + player.player_id;
console.log(var_string);
var ajax_obj = network.ajax('cards_mgmt.php', var_string, this.fin_add_hand_db, false);

try {
var ret_val = JSON.parse(ajax_obj.responseText);
} catch (e) {
console.log("Parsing error:", e);
}

console.log("fin_add_hand_db result: ", ret_val.result);
return ret_val.result;
}

/*this.fin_add_hand_db = function(ajax_obj)
{
try {
var ret_val = JSON.parse(ajax_obj.responseText);
} catch (e) {
console.log("Parsing error:", e);
}
console.log("fin_add_hand_db result: ", ret_val.result);
return ret_val.result;
}*/


//Responsible for carrying out AJAX requests
//async: true or false for asynchronous requests. non-async requests return an AJAX obj
this.ajax = function(file, var_string, funct, async){
Expand Down
34 changes: 27 additions & 7 deletions player_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function mk_player()//
this.hand = new Array();
this.next_hand_pos_x = 50;

this.add_all = function()
/*this.add_all = function()
{
for(card_idx in select.selected_cards){
//add to hand array
Expand All @@ -18,18 +18,38 @@ function mk_player()//
//Actually move
this.addhand(card_idx);
}
}*/

this.add_sel_hand = function()
{
console.log("Adding selected cards to hand.");
for(card_idx in select.selected_cards){
//add to hand array
hand[card_idx] = card_idx;

//Actually move
this.add_hand(card_idx);
}
}

this.addhand = function(card_idx)
this.add_hand = function(card_idx)
{
//Remove the div from the table and place in hand
//Update card's in_hand in database
//Deselect cards
//Remove the div from the table and place in hand, if possible.
if (network.st_add_hand_db(card_idx) == "0"){
console.log("Adding ", card_idx, " to hand");
this.hand[card_idx] = card_idx;
//Update card's in_hand in database
//Deselect cards
select.selected_cards.splice(card_idx, 1);
select.remove_lock(card_idx);
}
}
//places card in hand.
this.rmvhand = function(){}
this.rmv_hand = function()
{}
//puts card from hand on table.

this.arrangehand = function(){}
this.arrange_hand = function()
{}
//sorts the cards by suit and by number
}
Loading

0 comments on commit 3de0581

Please sign in to comment.