Changeset d2b411a in network-game for server/server.cpp


Ignore:
Timestamp:
Nov 26, 2012, 7:10:40 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
171c4fe
Parents:
0cc431d
Message:

Server correctly receives and replies to login and chat messages by looking at the message type

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r0cc431d rd2b411a  
    2121#include "player.h"
    2222#include "../common/message.h"
    23 ///////////
     23
     24/*
     25 Protocol Design
     26
     27 Client sends a login message
     28 Server replies with client's position in the world and positions of
     29 oall other logged in players
     30 Merver sends player ids along with locations
     31 This means a newly logged in client will need to know which id is
     32 assigned to it
     33 So server needs to send an id message, wait for an ack, and then send
     34 the location messages
     35 When a client shuts down, it sends a message to indicate this so the
     36 server can remove it from the list of connected clients
     37 Eventually, there'll need to be a way to detect timeouts from clients
     38 (if they crashed or otherwise failed to send the logout message)
     39*/
     40
    2441using namespace std;
    2542
     
    8198      if (n < 0)
    8299         error("recieveMessage");
    83       cout << "msg: " << clientMsg.buffer << endl;
    84 
    85       // Ptoyovol Design
    86       //
    87       // Client sends a login message
    88       // Server replies with client's position in the world and positions of
    89       // oall other logged in players
    90       // Merver sends player ids along with locations
    91       // This means a newly logged in client will need to know which id is
    92       // assigned to it
    93       // So server needs to send an id message, wait for an ack, and then send
    94       // the location messages
    95 
    96       // When a client shuts down, it sends a message to indicate this so the
    97       // server can remove it from the list of connected clients
    98       // Eventually, there'll need to be a way to detect timeouts from clients
    99       // (if they crashed or otherwise failed to send the logout message)
    100 
    101       if (strcmp(clientMsg.buffer, "Hello") == 0)
    102       {
    103          player *p = findPlayerByName(vctPlayers, "Boberty");
     100      cout << "MSG: type: " << clientMsg.type << " contents: " << clientMsg.buffer << endl;
     101
     102      switch(clientMsg.type)
     103      {
     104      {
     105      case MSG_TYPE_LOGIN:
     106         string name(clientMsg.buffer);
     107         cout << "Player logging in: " << name << endl;
     108
     109         player *p = findPlayerByName(vctPlayers, name);
    104110
    105111         if (p == NULL)
    106112         {
    107             vctPlayers.push_back(player("Boberty", from));
     113            vctPlayers.push_back(player(name, from));
    108114            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
    109115         }
     
    112118            strcpy(serverMsg.buffer, "Player has already logged in.");
    113119         }
    114       }else {
     120
     121         serverMsg.type = MSG_TYPE_LOGIN;
     122
     123         break;
     124      }
     125      case MSG_TYPE_CHAT:
     126      {
    115127         int guess = atoi(clientMsg.buffer);
    116128
     
    127139            num = (rand() % 1000) + 1;
    128140         }     
    129       }
     141
     142         serverMsg.type = MSG_TYPE_CHAT;
     143
     144         break;
     145      }
     146      default:
     147      {
     148         strcpy(serverMsg.buffer, "Server error occured. Report this please.");
     149
     150         serverMsg.type = MSG_TYPE_CHAT;
     151
     152         break;
     153      }
     154      }
     155
     156      /*
     157      if (strcmp(clientMsg.buffer, "Hello") == 0)
     158      {
     159         player *p = findPlayerByName(vctPlayers, "Boberty");
     160
     161         if (p == NULL)
     162         {
     163            vctPlayers.push_back(player("Boberty", from));
     164            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
     165         }
     166         else
     167         {
     168            strcpy(serverMsg.buffer, "Player has already logged in.");
     169         }
     170      }else {
     171         int guess = atoi(clientMsg.buffer);
     172
     173         cout << "guess: " << guess << endl;
     174
     175         if (guess < 1 || guess > 1000) {
     176            strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
     177         }else if(guess > num)
     178            strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
     179         else if(guess < num)
     180            strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
     181         else if(guess == num) {
     182            strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
     183            num = (rand() % 1000) + 1;
     184         }     
     185      }
     186      */
    130187
    131188      cout << "msg: " << serverMsg.buffer << endl;
Note: See TracChangeset for help on using the changeset viewer.