Changes in server/server.cpp [3b1efcc:b53c6b3] in network-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r3b1efcc rb53c6b3  
    44#include <string>
    55#include <iostream>
    6 #include <sstream>
    76#include <vector>
    87#include <algorithm>
     
    2221#include "../common/Compiler.h"
    2322#include "../common/Message.h"
    24 #include "../common/Common.h"
    2523
    2624#include "Player.h"
     
    2927using namespace std;
    3028
    31 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
     29void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg);
    3230
    3331// this should probably go somewhere in the common folder
     
    6563}
    6664
     65void set_nonblock(int sock)
     66{
     67    int flags;
     68    flags = fcntl(sock, F_GETFL,0);
     69    assert(flags != -1);
     70    fcntl(sock, F_SETFL, flags | O_NONBLOCK);
     71}
     72
    6773int main(int argc, char *argv[])
    6874{
    6975   int sock, length, n;
    7076   struct sockaddr_in server;
    71    struct sockaddr_in from; // info of client sending the message
     77   struct sockaddr_in from; // holds the info about the connected client
    7278   NETWORK_MSG clientMsg, serverMsg;
    7379   vector<Player> vctPlayers;
     80
     81   srand(time(NULL));
     82   int num = (rand() % 1000) + 1;
     83
     84   cout << "num: " << num << endl;
    7485
    7586   SSL_load_error_strings();
     
    94105   set_nonblock(sock);
    95106
    96    bool broadcastMessage;
    97107   while (true) {
    98108
     
    104114         cout << "Got a message" << endl;
    105115
    106          broadcastMessage = processMessage(clientMsg, from, vctPlayers, serverMsg);
     116         processMessage(clientMsg, from, vctPlayers, num, serverMsg);
    107117
    108118         cout << "msg: " << serverMsg.buffer << endl;
    109119
    110          if (broadcastMessage)
    111          {
    112             vector<Player>::iterator it;
    113 
    114             for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
    115             {
    116                if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
    117                   error("sendMessage");
    118             }
    119          }
    120          else
    121          {
    122             if ( sendMessage(&serverMsg, sock, &from) < 0 )
    123                error("sendMessage");
    124          }
     120         n = sendMessage(&serverMsg, sock, &from);
     121         if (n  < 0)
     122            error("sendMessage");
    125123      }
    126124
     
    130128}
    131129
    132 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
     130void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg)
    133131{
    134132   DataAccess da;
     
    138136   cout << "MSG: type: " << clientMsg.type << endl;
    139137   cout << "MSG contents: " << clientMsg.buffer << endl;
    140 
    141    bool broadcastResponse = false;
    142138
    143139   // Check that if an invalid message is sent, the client will correctly
     
    153149         cout << "password: " << password << endl;
    154150
    155          int error = da.insertPlayer(username, password);
    156 
    157          if (!error)
    158             strcpy(serverMsg.buffer, "Registration successful.");
    159          else
    160             strcpy(serverMsg.buffer, "Registration failed. Please try again.");
     151         da.insertPlayer(username, password);
     152
     153         strcpy(serverMsg.buffer, "Registration successful");
    161154
    162155         serverMsg.type = MSG_TYPE_REGISTER;
     
    186179
    187180            vctPlayers.push_back(newP);
    188             strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with outher players.");
     181            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
    189182         }
    190183
     
    229222         else
    230223         {
    231             broadcastResponse = true;
    232 
    233             stringstream ss;
    234             ss << p->name << ": " << clientMsg.buffer << endl;
    235 
    236             strcpy(serverMsg.buffer, ss.str().c_str());
     224            int guess = atoi(clientMsg.buffer);
     225
     226            cout << "guess: " << guess << endl;
     227
     228            if (guess < 1 || guess > 1000) {
     229               strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
     230            }else if(guess > num)
     231               strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
     232            else if(guess < num)
     233               strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
     234            else if(guess == num) {
     235               strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
     236               num = (rand() % 1000) + 1;
     237            }
    237238         }     
    238239
     
    249250         break;
    250251      }
    251 
    252       return broadcastResponse;
    253    }
    254 }
     252   }
     253}
Note: See TracChangeset for help on using the changeset viewer.