~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to game_server/Protocol/ProtocolPacket_GetUserInfo.pm

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###################################
 
2
# Packet base class               #
 
3
###################################
 
4
 
 
5
 
 
6
package ProtocolPacket_GetUserInfo;
 
7
 
 
8
use strict;
 
9
 
 
10
use Protocol::ProtocolPacket;
 
11
use Carp;
 
12
 
 
13
 
 
14
our @ISA = "ProtocolPacket";
 
15
 
 
16
# Reply codes
 
17
 
 
18
my $UI_ACK = 1;
 
19
my $UI_UNKNOWN = 2;
 
20
 
 
21
 
 
22
###############################################
 
23
# Generate this packet                        #
 
24
###############################################
 
25
sub init {
 
26
   my $self = shift;
 
27
 
 
28
   $self->{USER} = undef;
 
29
   $self->{ROOM} = undef;
 
30
   $self->{GAME} = [ ];
 
31
   $self->{REP_CODE} = undef;
 
32
}
 
33
 
 
34
################################################
 
35
# Read this packet, checks for correct syntax  #
 
36
################################################
 
37
sub read {
 
38
   my ( $self, $server, $client ) = @_;
 
39
 
 
40
   # First read features
 
41
   $self->{USER} = $client->read_string();
 
42
}
 
43
 
 
44
#################################################
 
45
# Execute a read() packet and collect the data  #
 
46
# the client wants                              #
 
47
#################################################
 
48
sub execute {
 
49
   my ( $self, $server, $client ) = @_;
 
50
   
 
51
   my $sclient  = $server->get_client_by_name( $self->{USER} );
 
52
   
 
53
   if( ! $sclient ) {
 
54
      $self->{REP_CODE} = $UI_UNKNOWN;
 
55
      return;
 
56
   }
 
57
 
 
58
   $self->{REP_CODE} = $UI_ACK;
 
59
   $self->{ROOM} = $sclient->room();
 
60
   $self->{GAME} = $sclient->game();
 
61
};
 
62
 
 
63
############################################################
 
64
# Send a reply package, this assumes read() and execute    #
 
65
# have been called or a error has occured in one of them   #
 
66
############################################################
 
67
sub write_reply {
 
68
   my ( $self, $server, $client, $flags ) = @_;
 
69
 
 
70
   $flags = $self->set_is_reply( $flags );
 
71
 
 
72
   my $size = 8 + 1;
 
73
   
 
74
   if($self->{REP_CODE} != $UI_ACK) {
 
75
      $client->write_16( $size );
 
76
      $client->write_16( $self->{ID} );
 
77
      $client->write_32( $self->{INDEX} );
 
78
      $client->write_16( $flags );
 
79
      $client->write_8( $self->{REP_CODE} );
 
80
      return;
 
81
   }
 
82
  
 
83
   $size += (length($self->{ROOM})+1)*2 
 
84
          +(length($self->{GAME})+1)*2;
 
85
          
 
86
 
 
87
   $client->write_16( $size );
 
88
   $client->write_16( $self->{ID} );
 
89
   $client->write_32( $self->{INDEX} );
 
90
   $client->write_16( $flags );
 
91
   $client->write_8( $self->{REP_CODE} );
 
92
   $client->write_string ( $self->{GAME} );
 
93
   $client->write_string ( $self->{ROOM} );
 
94
   
 
95
   # that's all
 
96
};
 
97
 
 
98
1;