- Timestamp:
- Nov 26, 2012, 7:10:40 PM (12 years ago)
- Branches:
- master
- Children:
- 171c4fe
- Parents:
- 0cc431d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r0cc431d rd2b411a 21 21 #include "player.h" 22 22 #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 24 41 using namespace std; 25 42 … … 81 98 if (n < 0) 82 99 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); 104 110 105 111 if (p == NULL) 106 112 { 107 vctPlayers.push_back(player( "Boberty", from));113 vctPlayers.push_back(player(name, from)); 108 114 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 109 115 } … … 112 118 strcpy(serverMsg.buffer, "Player has already logged in."); 113 119 } 114 }else { 120 121 serverMsg.type = MSG_TYPE_LOGIN; 122 123 break; 124 } 125 case MSG_TYPE_CHAT: 126 { 115 127 int guess = atoi(clientMsg.buffer); 116 128 … … 127 139 num = (rand() % 1000) + 1; 128 140 } 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 */ 130 187 131 188 cout << "msg: " << serverMsg.buffer << endl;
Note:
See TracChangeset
for help on using the changeset viewer.