forked from KamalChaya/WebDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_class.js
More file actions
148 lines (128 loc) · 4.68 KB
/
selection_class.js
File metadata and controls
148 lines (128 loc) · 4.68 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
143
144
145
146
147
148
console.log("Included: selection_class.js");
/*****
//Selection Definition: A card is considered selected by the appearance
// of a border around it (blue for the selector and red for other players)
// and a locked value of the owner's player.player_id in the db.
//
//Grabbed Definition: A card is considered grabbed when it is both selected
// and draggable. This card is currently being clicked on by the user and
// should follow the mouse.
*****/
function mk_selection()
{
this.grabbed_card;
this.selected_cards = new Array();
//Function: grab_card()
//Description: If not already selected, gets lock for the card,
// changes the border, adds to grabbed and selected_cards
// makes draggable, and changes setInterval to send changes
// to the server.
//Params: card_idx - Again, the string index of the card in the card array
//Preconditions: None, the game handles another user with the locked card.
//Return: None
this.grab_card = function(card_idx)
{
network.stop_send("");
var got_lock = this.secure_lock(card_idx);
//console.log("Got lock returned: " + got_lock + " in grab_card().");
if (got_lock == 1){
//The user is permitted to move it
card_array[card_idx].set_selected(1);
//Ignore this card's updates
this.grabbed_card = card_idx;
this.selected_cards[card_idx] = card_idx;
card_array[card_idx].set_drag(1);
//Set the timer interval
if (!card_array[card_idx].card_div.in_hand){
network.send_update_timer = setInterval("network._send_pos(" + '"' + card_idx + '"' +");", network.send_update_interval);
}
} else if (got_lock == 2) {
card_array[card_idx].set_drag(1);
this.grabbed_card = card_idx;
if (!card_array[card_idx].card_div.in_hand){
network.send_update_timer = setInterval("network._send_pos(" + '"' + card_idx + '"' +");", network.send_update_interval);
}
console.log("Already selected");
} else {
//Ensure the user cannot move it
card_array[card_idx].set_drag(0);
}
}
//Function: ungrab_card()
//Description: Changes a card's state from grabbed to
// only selected. i.e. removes the dragging and db
// updating
//Parameters: card_idx - the string index of the card in the
// global card array.
//Postconditions: The card is no longer considered grabbed
// and is not transmitting its position.
//Return: None
this.ungrab_card = function(card_idx)
{
network.stop_send(this.id);
this.grabbed_card = "";
}
//Function: secure_lock()
//Description: Requests the server to place a lock.
// informs the caller whether the lock was successful.
//Parameters: card_idx - the card to be locked.
// Global values also need to be set including network's
// game_id and the player's player_id.
//Return: whether the lock was obtained.
// A 1 indicates success. 2 indicates a player already
// had the lock. 0 indicates failure to obtain lock.
this.secure_lock = function (card_idx)
{
var var_string = "op=4&player_id=" + player.player_id + "&card_id=" + card_idx + "&game_id=" + network.game_id;
var ajax_obj = network.ajax("cards_mgmt.php", var_string, err_funct, false);
try {
var board_state = JSON.parse(ajax_obj.responseText);
} catch (e) {
console.log("Parsing error:", '"', e, '"');
}
return (board_state.result);
}
//Function: remove_lock()
//Description: Will remove the user's lock on the
// currently selected card.
//Preconditions: This user must have a lock on this card.
//Return: None.
this.remove_lock = function(card_idx)
{
var var_string = "op=5&player_id=" + player.player_id + "&card_id=" + card_idx + "&game_id=" + network.game_id;
var ajax_obj = network.ajax("cards_mgmt.php", var_string, err_funct, false);
try {
var board_state = JSON.parse(ajax_obj.responseText);
} catch (e) {
console.log("Parsing error:", e);
}
if ((board_state.result) == 0){
console.log("We have lock release failures!");
} else {
this.grabbed_card = "";
delete this.selected_cards[card_idx];
}
}
//Function: release_locks()
//Description: Will free all locks on selected cards
// this implies that the cards are no longer selected.
//Parameters: None
//Preconditions: Called from global onclick event in init.js
//Postconditions: All locks this player owns have been
// released.
this.release_locks = function()
{
var var_string = 'op=7&game_id=' + network.game_id + "&player_id=" + player.player_id;
var ajax_obj = network.ajax('cards_mgmt.php', var_string, err_funct, false);
try {
var cards = JSON.parse(ajax_obj.responseText);
} catch (e) {
console.log("Parsing error:", e);
}
for(card_idx in cards.result){
cur_card = cards.result[card_idx];
card_array[cur_card.cid].set_selected(0);
delete this.selected_cards[cur_card.cid];
}
}
}