120 lines
2.5 KiB
Perl
Raw Normal View History

2020-08-25 13:21:57 -06:00
#!/usr/bin/perl -w
use strict;
use warnings;
2020-09-02 10:19:17 -06:00
use POSIX;
2020-08-25 13:21:57 -06:00
use Net::WebSocket::Server;
use JSON;
use Data::Dumper;
#########################Game Logic starts here##########################
2020-08-25 13:21:57 -06:00
my %sessions = ();
sub generateSession {
my $newSession = shift;
unless (defined $newSession) { $newSession = time; }
if ( grep( /$newSession/, keys %sessions ) ) {
generateSession();
} else {
print "Adding session ".$newSession." to database\n";
$sessions{$newSession} = ();
$sessions{$newSession}{"id"} = $newSession;
$sessions{$newSession}{"users"} = ();
2020-08-25 13:21:57 -06:00
$sessions{$newSession}{"decks"} = ();
$sessions{$newSession}{"pools"} = ();
}
return $newSession;
}
sub generateDeck {
$DDF = from_json(shift);
# Add proper handling for reading of DDFs
}
sub shuffleDeck {
my @deck = @_;
my $index = 0;
for (@deck) {
my $swapCardIndex = floor(rand() * @deck);
my $swapCard = @deck[$swapCardIndex];
@deck[$swapCardIndex] = $_;
$deck[$index] = $swapCard;
$index++;
2020-08-25 13:21:57 -06:00
}
return @deck;
2020-08-25 13:21:57 -06:00
}
sub addUser {
my ($sessionID,$userID) = @_;
}
2020-09-02 10:19:17 -06:00
sub delUser {
my ($sessionID,$userID) = @_;
}
sub addCards {
my ($sessionID,$deckID, $DDF) = @_;
}
sub addDeck {
my ($sessionID,$deckID) = @_;
my %newDeck = {
cards => []
}
$sessions{$sessionID}{"decks"}{$deckID} = %newDeck;
}
sub delDeck {
my ($sessionID,$deckID) = @_;
$sessions{$sessionID}{"decks"}{$deckID} = undef;
}
2020-09-02 10:19:17 -06:00
sub addPool {}
2020-09-02 10:19:17 -06:00
sub delPool {}
########################Game Logic ends here#############################
#########################Server Logic below##############################
my $origin = 'ws://localhost';
sub joinSession {
}
my $server = Net::WebSocket::Server->new(
2020-08-25 13:21:57 -06:00
listen => 8080,
on_connect => sub {
my ($serv, $conn) = @_;
$conn->on(
handshake => sub {
my ($conn,$handshake) = @_;
$conn->{"initHand"} = $handshake;
},
ready => sub {
my ($conn) = @_;
#strip the / off of the resource request and then assign just the request to $session
#Check for game ID, if none generate one and send it then disconnect.
#Check for user ID, if none $conn->disconnect("noid",generateUser());
2020-08-25 13:21:57 -06:00
},
utf8 => sub {
my ($conn, $msg) = @_;
my $sessionID = $conn->{"currentSession"}{"id"};
my $messageData = "";
eval { $messageData = from_json($msg) };
if ($@) { $conn->send_utf8('{"error":1, "message":"ERROR: Invalid json"}'); return 0;}
},
disconnect => sub {
my ($conn, $code, $reason) = @_;
}
}
2020-08-25 13:21:57 -06:00
);
},
);
$server->start;