Changeset 171c4fe in network-game


Ignore:
Timestamp:
Nov 26, 2012, 8:02:56 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
07028b9, ec48e7d
Parents:
d2b411a
Message:

Added a logout message and the ability for the client to save it's username once it logs in

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    rd2b411a r171c4fe  
    140140   struct sockaddr_in server, from;
    141141   struct hostent *hp;
    142    //char buffer[256];
    143142   NETWORK_MSG msgTo, msgFrom;
     143   string username;
    144144
    145145   if (argc != 3) {
     
    162162   server.sin_port = htons(atoi(argv[2]));
    163163
    164    strcpy(msgTo.buffer, "Hello");
    165    n=sendMessage(&msgTo, sock, &server);
    166    if (n < 0)
    167       error("sendMessage");
    168 
    169    n = receiveMessage(&msgFrom, sock, &from);
    170    if (n < 0)
    171       error("receiveMessage");
    172        
    173    chatConsole.addLine(msgFrom.buffer);
    174 
    175164   al_start_timer(timer);
    176165 
     
    215204               case STATE_START:
    216205                  msgTo.type = MSG_TYPE_LOGIN;
     206                  username = input;
    217207                  break;
    218208               case STATE_LOGIN:
    219                   msgTo.type = MSG_TYPE_CHAT;
     209                  if (input.compare("quit") == 0 ||
     210                      input.compare("exit") == 0 ||
     211                      input.compare("logout") == 0)
     212                  {
     213                     strcpy(msgTo.buffer, username.c_str());
     214                     msgTo.type = MSG_TYPE_LOGOUT;
     215                  }
     216                  else
     217                     msgTo.type = MSG_TYPE_CHAT;
    220218                  break;
    221219               default:
     
    234232               switch(state)
    235233               {
    236                case STATE_START:
    237                   state = STATE_LOGIN;
    238                   break;
    239                case STATE_LOGIN:
    240                   chatConsole.addLine(string(msgFrom.buffer));
    241                   cout << "Added new line" << endl;
    242                   break;
    243                default:
    244                   cout << "The state has an invalid value: " << state << endl;
    245                   break;
     234                  case STATE_START:
     235                  {
     236                     string loginResponse = string(msgFrom.buffer);
     237                     chatConsole.addLine(string(msgFrom.buffer));
     238
     239                     if (loginResponse.compare("Player has already logged in.") == 0)
     240                     {
     241                        cout << "User login failed" << endl;
     242                        username.clear();
     243                     }
     244                     else
     245                     {
     246                        cout << "User login successful" << endl;
     247                        state = STATE_LOGIN;
     248                     }
     249                     break;
     250                  }
     251                  case STATE_LOGIN:
     252                  {
     253                     chatConsole.addLine(string(msgFrom.buffer));
     254                     cout << "Added new line" << endl;
     255                     break;
     256                  }
     257                  default:
     258                  {
     259                     cout << "The state has an invalid value: " << state << endl;
     260                     break;
     261                  }
    246262               }
    247263            }
  • common/message.h

    rd2b411a r171c4fe  
    33
    44#define MSG_TYPE_LOGIN  1
    5 #define MSG_TYPE_CHAT   2
     5#define MSG_TYPE_LOGOUT 2
     6#define MSG_TYPE_CHAT   3
    67
    78typedef struct
Note: See TracChangeset for help on using the changeset viewer.