#!/usr/bin/perl -w use strict; use warnings; use POSIX; use Net::WebSocket::Server; use JSON; use Data::Dumper; #########################Game Logic starts here########################## 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"} = (); $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++; } return @deck; } sub addUser { my ($sessionID,$userID) = @_; } 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; } sub addPool {} sub delPool {} ########################Game Logic ends here############################# #########################Server Logic below############################## my $origin = 'ws://localhost'; sub joinSession { } my $server = Net::WebSocket::Server->new( 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()); }, 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) = @_; } } ); }, ); $server->start;