diff --git a/Server/server.pl b/Server/server.pl index f03ee7f..1e7d54a 100755 --- a/Server/server.pl +++ b/Server/server.pl @@ -109,6 +109,7 @@ sub addCards { sub addDeck { my ($sessionID,$deckID) = @_; + if (defined($sessions{$sessionID}{"decks"}{$deckID})) { print "ERROR: Deck already exsists!\n"; return 0; } $sessions{$sessionID}{"decks"}{$deckID} = { name => "$deckID", cards => [] }; } @@ -124,6 +125,7 @@ sub uploadDDF { sub addPool { my ($sessionID,$poolID) = @_; + if (defined($sessions{$sessionID}{"pools"}{$poolID})) { print "ERROR: Pool already exsists!\n"; return 0; } $sessions{$sessionID}{"pools"}{$poolID} = { name => "$poolID", cards => [] }; } @@ -184,6 +186,7 @@ sub renamePool { sub addHand { my ($sessionID, $userID, $handID) = @_; + if (defined($sessions{$sessionID}{"users"}{$userID}{hands}{$handID})) { print "ERROR: Hand Already Exsists\n"; return 0; } $sessions{$sessionID}{"users"}{$userID}{hands}{$handID} = { cards => [] }; } @@ -247,6 +250,16 @@ sub getPoolTop { return @{$sessions{$sessionID}{"pools"}{$poolID}->{cards}}[0]; } +sub normalizeSession { + my $sessionID = shift; + unless ($sessionID) { return print "ERROR: Session not provided\n"; } + unless (scalar getDecks($sessionID)) { addDeck($sessionID,"default"); } + unless (scalar getPools($sessionID)) { addPool($sessionID,"default"); } + for (getUsers($sessionID)) { + unless(scalar getHands($sessionID, $_)) { addHand($sessionID,$_,"default"); } + } +} + ########################Game Logic ends here############################# #########################Server Logic below############################## @@ -304,7 +317,7 @@ my $server = Net::WebSocket::Server->new( } if (defined($messageData->{action})) { if ($messageData->{action} =~ /join/ ) { if (joinSession($conn->{session},$conn->{user})) { $conn->send_utf8('{"info":"join success", "request":"update"}'); } } - if ($messageData->{action} =~ /update/ ) { my $sessionID = $conn->{session}; for ($conn->server->connections) { if ($_->{session} == $conn->{session}) { + if ($messageData->{action} =~ /update/ ) { my $sessionID = $conn->{session}; normalizeSession($conn->{session}); for ($conn->server->connections) { if ($_->{session} == $conn->{session}) { #This might be a lot to do at once, might need to break it up in the future. # Update all connections joined to this session. # User List @@ -370,9 +383,9 @@ my $server = Net::WebSocket::Server->new( $conn->send_utf8('{"info":"pool cleared", "request":"update"}'); } if ($messageData->{action} =~ /muligan/) { - my @cards = delHand($conn->{session},$messageData->{hand}); + my @cards = delHand($conn->{session},$conn->{user},$messageData->{hand}); addCards($conn->{session},$messageData->{deck},\@cards); - addHand($conn->{session},$messageData->{hand}); + addHand($conn->{session},$conn->{user},$messageData->{hand}); $conn->send_utf8('{"info":"hand returned to deck","request":"update"}'); } if ($messageData->{action} =~ /shuffle/) { diff --git a/UI/index.html b/UI/index.html index ab98f26..84d4bc7 100644 --- a/UI/index.html +++ b/UI/index.html @@ -78,15 +78,21 @@ function updateEvent() { myHands.forEach(function (handID) { var hand = window.gameState.hands[handID]; window.UI.display[handID] = elementPlace("#hands","Hand_"+hand.name,"hand"+(hand.name == window.gameState.active.hand ? " selected" : ""),"li"); - window.UI.display[handID].controls = elementPlace("#Hand_"+hand.name,null,"controls","div"); + window.UI.display[handID].controls = elementPlace("#Hand_"+hand.name,"Hand_controls_"+hand.name,"controls","div"); window.UI.display[handID].controls.innerHTML = "
"+hand.name+"
"; -// Muligan Hand -// Delete Hand + buttonAdd("#Hand_controls_"+hand.name,null,"Muligan",function () { + window.gameSession.send('{"action":"muligan","hand":"'+handID+'","deck":"'+window.gameState.active.deck+'"}'); + }, "hand_button muligan", "div"); + buttonAdd("#Hand_controls_"+hand.name,null,"X",function () { + window.gameSession.send('{"action":"del","type":"hand","id":"'+handID+'"}'); + }, "hand_button delete", "div"); window.UI.display[handID].cards = elementPlace("#Hand_"+hand.name,hand.name+"_cards",null,"ol"); hand.cards.forEach(function (card,index) { window.UI.display[handID].cards[index] = elementPlace("#"+hand.name+"_cards",null,"card","li"); - window.UI.display[handID].cards[index].innerHTML = card; -// Play Card + window.UI.display[handID].cards[index].innerHTML = card+'
'; + buttonAdd("#card"+handID+index+"_controls",null,"Play Card", function () { + window.gameSession.send('{"action":"play","hand":"'+handID+'","cardID":"'+index+'","pool":"'+window.gameState.active.pool+'"}'); + }, "card_button play", "div"); }); window.UI.display[handID].onclick = function () { window.gameState.active.hand = hand.name; @@ -106,7 +112,7 @@ function updateEvent() { myPools.forEach(function (poolID) { var pool = window.gameState.pools[poolID]; window.UI.display[poolID] = elementPlace("#pools","Pool_"+pool.name,"pool"+(pool.name == window.gameState.active.pool ? " selected" : ""),"li"); - window.UI.display[poolID].innerText = pool.name+":"+pool.top; + window.UI.display[poolID].innerHTML = pool.name+":"+pool.top; // Delete Pool window.UI.display[poolID].onclick = function () { window.gameState.active.pool = pool.name;