Changeset 59


Ignore:
Timestamp:
Apr 21, 2007, 3:25:33 AM (14 years ago)
Author:
zerodeux
Message:

client+server important fixes

Location:
insia/perl/bomberman
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • insia/perl/bomberman/client.pl

    r58 r59  
    131131  exit(1) if SDL::GetKeyState(SDLK_ESCAPE);
    132132
    133   if    (SDL::GetKeyState(SDLK_UP)    && $player_row > 0)           { $player_row--; }
    134   elsif (SDL::GetKeyState(SDLK_DOWN)  && $player_row < $levelh - 1) { $player_row++; }
    135   elsif (SDL::GetKeyState(SDLK_LEFT)  && $player_col > 0)           { $player_col--; }
    136   elsif (SDL::GetKeyState(SDLK_RIGHT) && $player_col < $levelw - 1) { $player_col++; }
     133  if    (SDL::GetKeyState(SDLK_UP)   ) { send_to_server('MOVE UP'); }
     134  elsif (SDL::GetKeyState(SDLK_DOWN) ) { send_to_server('MOVE DOWN'); }
     135  elsif (SDL::GetKeyState(SDLK_LEFT) ) { send_to_server('MOVE LEFT'); }
     136  elsif (SDL::GetKeyState(SDLK_RIGHT)) { send_to_server('MOVE RIGHT'); }
     137  elsif (SDL::GetKeyState(SDLK_SPACE)) { send_to_server('BOMB-DROP'); }
    137138
    138139  win_update();
  • insia/perl/bomberman/server.pl

    r58 r59  
    3434    }
    3535
     36    $val =~ s/ /./g;
    3637    my @row = split(//, $val);
    3738    push(@level, \@row);
     
    4142  for (my $col = 0; $col < $linfo{'width'}; $col++) {
    4243    for (my $row = 0; $row < $linfo{'height'}; $row++) {
    43       push(@lstart, [ $col, $row ]) if $level[$row][$col] =~ s/(\d)/./;
     44      push(@lstart, [ $col, $row ]) while ($level[$row][$col] =~ s/(\d)/./g);
    4445    }
    4546  }
     
    5960  my ($cli, $msg) = @_;
    6061
    61   my $fd = $cli->{'fd'};
     62  my $handle = $cli->{'handle'};
    6263  print "$cli->{'name'}> $msg\n";
    63   print $fd "$msg\n";
     64  print $handle "$msg\n";
    6465}
    6566
     
    6768  my ($cli) = @_;
    6869
    69   my $fd = $cli->{'fd'};
    70   my $msg = <$fd>;
     70  my $handle = $cli->{'handle'};
     71  my $msg    = <$handle>;
    7172  return undef if !defined $msg;
    7273
     
    9394  my $conn = $server->accept();
    9495  my $cli = {
    95     'fd'  => $conn,
    96     'name' => '(new)',
     96    'handle' => $conn,
     97    'name'   => '(new)',
    9798  };
    98 
    99   sleep(1); # FIXME
    100   my ($join, $name) = receive_from_client($cli);
    101   send_to_client($cli, "JOIN-STATUS ERROR Unknown command (JOIN expected)"), close($conn), return if $join ne 'JOIN';
    102   send_to_client($cli, "JOIN-STATUS ERROR Invalid player name"), close($conn), return if ! $name =~ /^\w+$$/;
    103   send_to_client($cli, "JOIN-STATUS ERROR Too many players"), close($conn), return if @clients >= @lstart;
     99  push @clients, $cli;
     100}
     101
     102sub del_client {
     103  my ($cli) = @_;
     104
     105  print "$cli->{'name'} disconnected\n";
     106  close($cli->{'handle'});
     107
     108  @clients = grep { $_ != $cli } @clients;
     109
     110  return if !defined $cli->{'dir'} || $cli->{'dir'} eq 'DEAD';
     111  send_to_client($_, "PLAYER-KILL $cli->{'name'}") foreach @clients;
     112}
     113
     114sub op_client {
     115  my ($cli) = @_;
     116
     117  my ($op, @param) = receive_from_client($cli);
     118
     119  if    (!defined $op)       { del_client($cli); }
     120  elsif ($op eq 'JOIN')      { op_join($cli, @param); }
     121  elsif ($op eq 'MOVE')      { op_move($cli, @param); }
     122  elsif ($op eq 'BOMB-DROP') { op_bomb_drop($cli, @param); }
     123  else {
     124    send_to_client($cli, "ERROR Unknown command '$op'");
     125  }
     126}
     127
     128sub op_join {
     129  my ($cli, $name) = @_;
     130
     131  send_to_client($cli, "JOIN-STATUS ERROR Too many players"), return if @clients >= @lstart;
     132  send_to_client($cli, "JOIN-STATUS ERROR Invalid player name"), return if ! $name =~ /^\w+$$/;
     133  send_to_client($cli, "JOIN-STATUS ERROR Player already connected"), return if grep { $_->{'name'} eq $name; } @clients;
    104134
    105135  $cli->{'name'} = $name;
     
    109139  $cli->{'dir'} = 'DOWN';
    110140  ( $cli->{'col'}, $cli->{'row'} )  = @{$lstart[@clients]};
    111   push @clients, $cli;
    112141  send_player_status($cli, $_) foreach @clients;
    113142  send_player_status($_, $cli) foreach grep { $_ != $cli } @clients;
    114   return;
    115 }
    116 
    117 sub del_client {
    118   my ($cli) = @_;
    119 
    120   print "$cli->{'name'} disconnected\n";
    121   close($cli->{'fd'});
    122   @clients = grep { $_ != $cli } @clients;
    123 }
    124 
    125 sub op_client {
    126   my ($cli) = @_;
    127 
    128   #receive_from_client($cli);
    129   print "FIXME: op_client()\n";
    130 }
    131 
    132 
    133 #
    134 # Programme principal
     143}
     144
     145sub op_move {
     146  my ($cli, $dir) = @_;
     147
     148  my ($col, $row) = ($cli->{'col'}, $cli->{'row'});
     149  if    ($dir eq 'UP')    { $row--; }
     150  elsif ($dir eq 'DOWN')  { $row++ }
     151  elsif ($dir eq 'LEFT')  { $col--; }
     152  elsif ($dir eq 'RIGHT') { $col++ }
     153  else {
     154    send_to_client($cli, "ERROR Unkown direction '$dir'");
     155  }
     156
     157  return if (
     158    $col < 0 || $col >= $linfo{'width'} ||
     159    $row < 0 || $row >= $linfo{'height'} ||
     160    $level[$row][$col] ne '.' ||
     161    is_other_player_at($cli, $col, $row));
     162
     163  ($cli->{'col'}, $cli->{'row'}) = ($col, $row);
     164  $cli->{'dir'} = $dir;
     165  send_player_status($cli, $_) foreach @clients;
     166}
     167
     168sub op_bomb_drop {
     169  my ($cli) = @_;
     170  send_to_client($cli, "ERROR bomb drop not implemented");
     171}
     172
     173sub is_other_player_at {
     174  my ($cli, $col, $row) = @_;
     175
     176  foreach (grep { $_ != $cli } @clients) {
     177    return 1 if ($row, $col) == ($_->{'row'}, $_->{'col'});
     178  }
     179  return 0;
     180}
     181
     182
     183#
     184# Main loop
    135185#
    136186
     
    152202  my ($rset, $wset, $eset) = ('', '', '');
    153203
    154   vec($rset, $server->fileno(), 1) = 1;
     204  my $s = $server->fileno();
     205  my @reader = (
     206    $s,
     207    map { $_->{'handle'}->fileno() } @clients,
     208  );
     209  vec($rset, $_, 1) = 1 foreach @reader;
     210
     211  next if select($rset, $wset, $eset, undef) < 1;
     212
    155213  foreach (@clients) {
    156     vec($rset, $_->{'fd'}, 1) = 1;
    157     vec($eset, $_->{'fd'}, 1) = 1;
    158   }
    159 
    160   next if !select($rset, $wset, $eset, undef);
    161 
    162   foreach (@clients) {
    163     op_client($_)  if vec($rset, $_->{'fd'}, 1);
    164     del_client($_) if vec($eset, $_->{'fd'}, 1);
    165   }
    166   new_client() if vec($rset, $server->fileno(), 1);
    167 }
     214    my $c = $_->{'handle'}->fileno();
     215    op_client($_) if vec($rset, $c, 1);
     216  }
     217  new_client() if vec($rset, $s, 1);
     218}
Note: See TracChangeset for help on using the changeset viewer.