Finished update function on server

This commit is contained in:
bluesaxman 2020-09-10 11:07:07 -06:00
parent a4758c5ea2
commit af89d886bf

View File

@ -93,7 +93,7 @@ sub shuffleHand {
sub addUser {
my ($sessionID,$userID) = @_;
if (grep( /$userID/, keys $sessions{$sessionID}{"users"})) { print "ERROR: Duplicate user\n"; return 0; }
if (grep( /$userID/, keys %{$sessions{$sessionID}{"users"}})) { print "ERROR: Duplicate user\n"; return 0; }
$sessions{$sessionID}{"users"}{$userID} = {
name => "$userID",
hands => {}
@ -255,6 +255,11 @@ sub getDeckSize {
return length $sessions{$sessionID}{"decks"}{$deckID}->{cards};
}
sub getPoolTop {
my ($sessionID, $poolID) = @_;
return @{$sessions{$sessionID}{"pools"}{$poolID}->{cards}}[0];
}
########################Game Logic ends here#############################
#########################Server Logic below##############################
@ -311,35 +316,47 @@ my $server = Net::WebSocket::Server->new(
$conn->send_utf8('{"info":"session success", "request":"join"}');
}
if (defined($messageData->{action})) {
if ("join" == $messageData->{action}) { if (joinSession($conn->{session},$conn->{user})) { $conn->send_utf8('{"info":"join success", "request":"update"}'); } }
my $sessionID = $conn->{session};
if ("update" == $messageData->{action}) { for ($conn->server->connections) { if ($_->{session} == $conn->{session}) {
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}) {
#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
my @users = getUsers($sessionID);
my @userNames = [];
my @userNames;
my $current = $_;
for (@users) {
push(@userNames,getUserName($sessionID,$_);
push(@userNames,getUserName($sessionID,$_));
}
$_->send_utf8('{"users":['.join(",",getUsers(@userNames)).']}');
$current->send_utf8('{"users":["'.join('","',@userNames).'"]}');
# Deck List
my @decks = getDecks($sessionID);
my %deckStats = {};
my %deckStats = ();
for (@decks) {
$deckStats{$_} = getDeckSize($sessionID,$_);
$deckStats{$_}{name} = $sessions{$sessionID}{"decks"}{$_}{name};
$deckStats{$_}{size} = getDeckSize($sessionID,$_);
}
$_->send_utf8('{"decks":'.to_json().'}');
# Each decks count
$_->send_utf8('{}');
$current->send_utf8('{"decks":'.to_json(\%deckStats).'}');
# Pool List
$_->send_utf8('{}');
# Top card in each pool
$_->send_utf8('{}');
my @pools = getPools($sessionID);
my %poolStats = ();
for (@pools) {
$poolStats{$_}{name} = $sessions{$sessionID}{"pools"}{$_}{name};
$poolStats{$_}{top} = getPoolTop($sessionID,$_);
}
$current->send_utf8('{"pools":'.to_json(\%poolStats).'}');
# Users Hands
$_->send_utf8('{}');
# for each hand
# each card (so we scale better)
$_->send_utf8('{}');
my @hands = getHands($sessionID,$conn->{user});
$current->send_utf8('{"hands":'.to_json(\@hands).'}');
# for each hand
for (@hands) {
my $handID = $_;
my @hand = @{$sessions{$sessionID}{"users"}{$conn->{user}}{hands}{$handID}{cards}};
# each card (so we scale better)
if (@hand) { for (@hand) {
my $card = $_;
$current->send_utf8('{"action":"addCard","hand":"'.$handID.'", "card":"'.$card.'"}');
}}
}
}}}
}
},
@ -350,4 +367,14 @@ my $server = Net::WebSocket::Server->new(
}
);
my $ses = generateSession(50);
addDeck($ses,"default");
my @tCards = generateDeck('[{"data":["Male ","Female "]},{"data":["Ardvark ","Platipus ","Cat "]},{"data":["Jumping","Falling","Flying"]}]');
addCards($ses,"default",\@tCards);
addPool($ses,"default");
addUser($ses,"Sergiy");
drawCard($ses,"default","Sergiy","default");
drawCard($ses,"default","Sergiy","default");
playCard($ses,"Sergiy","default",0,"default");
$server->start;