Changes in / [ca44f82:3a79253] in network-game


Ignore:
Files:
4 added
3 deleted
15 edited

Legend:

Unmodified
Added
Removed
  • client/Client/Client.vcxproj

    rca44f82 r3a79253  
    4747      <GenerateDebugInformation>true</GenerateDebugInformation>
    4848      <AdditionalLibraryDirectories>c:\allegro\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    49       <AdditionalDependencies>allegro-5.0.8-monolith-md-debug.lib;%(AdditionalDependencies)</AdditionalDependencies>
     49      <AdditionalDependencies>allegro-5.0.7-monolith-md-debug.lib;%(AdditionalDependencies)</AdditionalDependencies>
    5050    </Link>
    5151  </ItemDefinitionGroup>
     
    6565  <ItemGroup>
    6666    <ClCompile Include="..\..\common\Common.cpp" />
    67     <ClCompile Include="..\..\common\WorldMap.cpp" />
    6867    <ClCompile Include="..\..\common\Message.cpp" />
    6968    <ClCompile Include="..\..\common\Player.cpp" />
     
    7877    <ClInclude Include="..\..\common\Common.h" />
    7978    <ClInclude Include="..\..\common\Compiler.h" />
    80     <ClInclude Include="..\..\common\WorldMap.h" />
    8179    <ClInclude Include="..\..\common\Message.h" />
    8280    <ClInclude Include="..\..\common\Player.h" />
  • client/Client/Client.vcxproj.filters

    rca44f82 r3a79253  
    5555      <Filter>Source Files\common</Filter>
    5656    </ClCompile>
    57     <ClCompile Include="..\..\common\WorldMap.cpp">
    58       <Filter>Source Files\common</Filter>
    59     </ClCompile>
    6057  </ItemGroup>
    6158  <ItemGroup>
     
    8784      <Filter>Header Files\common</Filter>
    8885    </ClInclude>
    89     <ClInclude Include="..\..\common\WorldMap.h">
    90       <Filter>Header Files\common</Filter>
    91     </ClInclude>
    9286  </ItemGroup>
    9387  <ItemGroup>
  • client/Client/main.cpp

    rca44f82 r3a79253  
    1414
    1515#include <sys/types.h>
    16 #include <cstdio>
    17 #include <cstdlib>
     16#include <stdio.h>
     17#include <stdlib.h>
    1818#include <string>
    1919#include <iostream>
    20 #include <sstream>
    21 
    22 #include <map>
    2320
    2421#include <map>
     
    2724#include <allegro5/allegro_font.h>
    2825#include <allegro5/allegro_ttf.h>
    29 #include <allegro5/allegro_primitives.h>
    3026
    3127#include "../../common/Message.h"
    3228#include "../../common/Common.h"
    33 #include "../../common/WorldMap.h"
    3429#include "../../common/Player.h"
    3530
     
    4742void initWinSock();
    4843void shutdownWinSock();
    49 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
    50 void drawMap(WorldMap* gameMap);
    51 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
    52 POSITION screenToMap(POSITION pos);
    53 POSITION mapToScreen(POSITION pos);
     44void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
    5445
    5546// callbacks
     
    10899   bool redraw = true;
    109100   doexit = false;
    110    map<unsigned int, Player> mapPlayers;
    111    unsigned int curPlayerId = -1;
    112101
    113102   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    121110   }
    122111
    123    if (al_init_primitives_addon())
    124       cout << "Primitives initialized" << endl;
    125    else
    126       cout << "Primitives not initialized" << endl;
    127 
     112   al_init_primitives_addon();
    128113   al_init_font_addon();
    129114   al_init_ttf_addon();
    130115
    131    #if defined WINDOWS
    132       ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
    133    #elif defined LINUX
    134       ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
    135    #endif
    136 
     116   ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
     117 
    137118   if (!font) {
    138119      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    163144      return -1;
    164145   }
    165 
    166    WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
    167    //delete gameMap;
    168    //gameMap = WorldMap::createDefaultMap();
    169146
    170147   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    314291         }
    315292      }
    316       else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
    317          if(wndCurrent == wndMain) {
    318             msgTo.type = MSG_TYPE_PLAYER_MOVE;
    319 
    320             POSITION pos;
    321             pos.x = ev.mouse.x;
    322             pos.y = ev.mouse.y;
    323             pos = screenToMap(pos);
    324 
    325             if (pos.x != -1)
    326             {
    327                memcpy(msgTo.buffer, &curPlayerId, 4);
    328                memcpy(msgTo.buffer+4, &pos.x, 4);
    329                memcpy(msgTo.buffer+8, &pos.y, 4);
    330 
    331                sendMessage(&msgTo, sock, &server);
    332             }
    333             else
    334                cout << "Invalid point: User did not click on the map" << endl;
    335          }
    336       }
    337293
    338294      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    339295      {
    340          processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
     296         processMessage(msgFrom, state, chatConsole);
    341297         cout << "state: " << state << endl;
    342298      }
     
    345301      {
    346302         redraw = false;
    347 
     303 
    348304         wndCurrent->draw(display);
     305 
     306         al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    349307
    350308         chatConsole.draw(font, al_map_rgb(255,255,255));
     
    356314         else if(wndCurrent == wndMain) {
    357315            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
    358 
    359             drawMap(gameMap);
    360             drawPlayers(mapPlayers, curPlayerId);
    361316         }
    362317
     
    375330   delete wndLogin;
    376331   delete wndMain;
    377 
    378    delete gameMap;
    379332
    380333   al_destroy_event_queue(event_queue);
     
    419372}
    420373
    421 POSITION screenToMap(POSITION pos)
    422 {
    423    pos.x = pos.x-300;
    424    pos.y = pos.y-100;
    425 
    426    if (pos.x < 0 || pos.y < 0)
    427    {
    428       pos.x = -1;
    429       pos.y = -1;
    430    }
    431 
    432    return pos;
    433 }
    434 
    435 POSITION mapToScreen(POSITION pos)
    436 {
    437    pos.x = pos.x+300;
    438    pos.y = pos.y+100;
    439 
    440    return pos;
    441 }
    442 
    443 POSITION mapToScreen(FLOAT_POSITION pos)
    444 {
    445    POSITION p;
    446    p.x = pos.x+300;
    447    p.y = pos.y+100;
    448 
    449    return p;
    450 }
    451 
    452 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
     374void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole)
    453375{
    454376   string response = string(msg.buffer);
     
    461383      {
    462384         cout << "In STATE_START" << endl;
     385
     386         chatConsole.addLine(response);
    463387
    464388         switch(msg.type)
     
    484408                  state = STATE_LOGIN;
    485409                  wndCurrent = wndMain;
    486                  
    487                   Player p("", "");
    488                   p.deserialize(msg.buffer);
    489                   mapPlayers[p.id] = p;
    490                   curPlayerId = p.id;
    491 
    492                   cout << "Got a valid login response with the player" << endl;
    493                   cout << "Player id: " << curPlayerId << endl;
     410                  cout << "User login successful" << endl;
    494411               }
    495 
    496412               break;
    497413            }
     
    502418      case STATE_LOGIN:
    503419      {
    504          switch(msg.type)
     420         chatConsole.addLine(response);
     421
     422          switch(msg.type)
    505423         {
    506424            case MSG_TYPE_REGISTER:
     
    510428            case MSG_TYPE_LOGIN:
    511429            {
    512                chatConsole.addLine(response);
    513 
    514430               if (response.compare("You have successfully logged out.") == 0)
    515431               {
     
    532448
    533449               cout << "p.id: " << p.id << endl;
     450               cout << "p.name: " << p.name << endl;
     451               cout << "p.pos.x: " << p.pos.x << endl;
     452               cout << "p.pos.y: " << p.pos.y << endl;
    534453
    535454               break;
    536455            }
    537             case MSG_TYPE_CHAT:
    538             {
    539                chatConsole.addLine(response);
    540 
    541                break;
    542             }
    543          }
    544 
     456         }
     457                     
    545458         break;
    546459      }
     
    554467}
    555468
    556 void drawMap(WorldMap* gameMap)
    557 {
    558    POSITION mapPos;
    559    mapPos.x = 0;
    560    mapPos.y = 0;
    561    mapPos = mapToScreen(mapPos);
    562    for (int x=0; x<12; x++)
    563    {
    564       for (int y=0; y<12; y++)
    565       {
    566          WorldMap::TerrainType el = gameMap->getElement(x, y);
    567 
    568          if (el == WorldMap::TERRAIN_GRASS)
    569             al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
    570          else if (el == WorldMap::TERRAIN_OCEAN)
    571             al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
    572          else if (el == WorldMap::TERRAIN_ROCK)
    573             al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
    574       }
    575    }
    576 }
    577 
    578 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
    579 {
    580    map<unsigned int, Player>::iterator it;
    581 
    582    Player* p;
    583    POSITION pos;
    584 
    585    for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    586    {
    587       p = &it->second;
    588       pos = mapToScreen(p->pos);
    589 
    590       if (p->id == curPlayerId)
    591          al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0));
    592       else
    593          al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
    594    }
    595 }
    596 
    597469void registerAccount()
    598470{
  • client/makefile

    rca44f82 r3a79253  
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Common.o Message.o Player.o chat.o GuiComponent.o Window.o Textbox.o Button.o
     5DEPENDENCIES = Message.o Player.o chat.o GuiComponent.o Window.o Textbox.o Button.o
    66
    77gameClient : Client/main.cpp $(DEPENDENCIES)
    88        $(CC) -o $@ $+ $(FLAGS)
    9 
    10 Common.o : $(COMMON_PATH)/Common.cpp
    11         $(CC) -c -o $@ $? $(FLAGS)
    129
    1310Message.o : $(COMMON_PATH)/Message.cpp
  • common/Common.cpp

    rca44f82 r3a79253  
    11#include "Common.h"
    2 
    3 #include <iostream>
    4 using namespace std;
    5 
    6 #if defined WINDOWS
    7    #include <Windows.h>
    8 #elif defined LINUX
    9    #include <time.h>
    10 #endif
    112
    123void set_nonblock(int sock)
     
    2213   #endif
    2314}
    24 
    25 unsigned long long getCurrentMillis()
    26 {
    27    unsigned long long numMilliseconds;
    28 
    29    #if defined WINDOWS
    30       numMilliseconds = GetTickCount();
    31    #elif defined LINUX
    32       timespec curTime;
    33       clock_gettime(CLOCK_REALTIME, &curTime);
    34 
    35       numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
    36    #endif
    37 
    38    return numMilliseconds;
    39 }
  • common/Common.h

    rca44f82 r3a79253  
    1313
    1414void set_nonblock(int sock);
    15 unsigned long long getCurrentMillis();
    1615
    1716typedef struct
     
    1918   int x;
    2019   int y;
    21 } POSITION;
    22 
    23 typedef struct
    24 {
    25    float x;
    26    float y;
    27 } FLOAT_POSITION;
     20} PLAYER_POS;
    2821
    2922#endif
  • common/Message.cpp

    rca44f82 r3a79253  
    1111#endif
    1212
    13 #include <iostream>
    14 
    15 using namespace std;
    16 
    1713int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest)
    1814{
    19    int ret =  sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
    20 
    21    cout << "Sent message of type " << msg->type << endl;
    22 
    23    return ret;
     15   return sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
    2416}
    2517
     
    2921
    3022   // assume we don't care about the value of socklen
    31    int ret =  recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, &socklen);
    32 
    33    if (ret > -1)
    34       cout << "Received message of type " << msg->type << endl;
    35 
    36    return ret;
     23   return recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, &socklen);
    3724}
  • common/Message.h

    rca44f82 r3a79253  
    66#define MSG_TYPE_LOGOUT       3
    77#define MSG_TYPE_CHAT         4
    8 #define MSG_TYPE_PLAYER       5  // server sends this to update player positions
    9 #define MSG_TYPE_PLAYER_MOVE  6  // client sends this when a player wants to move
     8#define MSG_TYPE_PLAYER       5
    109
    1110typedef struct
  • common/Player.cpp

    rca44f82 r3a79253  
    44#include <sstream>
    55#include <cstring>
    6 #include <cmath>
    76
    87using namespace std;
     
    1312   this->name = "";
    1413   this->password = "";
    15    this->pos.x = this->target.x = 0;
    16    this->pos.y = this->target.y = 0;
    17    this->timeLastUpdated = 0;
     14   this->pos.x = 0;
     15   this->pos.y = 0;
    1816}
    1917
     
    2523   this->pos.x = p.pos.x;
    2624   this->pos.y = p.pos.y;
    27    this->target.x = p.target.x;
    28    this->target.y = p.target.y;
    2925   this->addr = p.addr;
    3026}
     
    3531   this->name = name;
    3632   this->password = password;
    37    this->pos.x = this->target.x = 200;
    38    this->pos.y = this->target.y = 200;
     33   this->pos.x = 200;
     34   this->pos.y = 200;
    3935}
    4036
     
    4440   this->name = name;
    4541   this->password = "";
    46    this->pos.x = this->target.x = 200;
    47    this->pos.y = this->target.y = 200;
     42   this->pos.x = 200;
     43   this->pos.y = 200;
    4844   this->addr = addr;
    4945}
     
    5551void Player::serialize(char* buffer)
    5652{
    57    memcpy(buffer, &this->id, 4);
    58    memcpy(buffer+4, &this->pos.x, 4);
    59    memcpy(buffer+8, &this->pos.y, 4);
    60    memcpy(buffer+12, &this->target.x, 4);
    61    memcpy(buffer+16, &this->target.y, 4);
    62    strcpy(buffer+20, this->name.c_str());
     53   ostringstream oss;
     54
     55   oss.write((char*)&(this->id), sizeof(int));
     56   oss << this->name;
     57   oss.write("\0", 1);
     58   oss.write((char*)&(this->pos.x), sizeof(int));
     59   oss.write((char*)&(this->pos.y), sizeof(int));
     60
     61   memcpy(buffer, oss.str().c_str(), this->name.length()+1+2*sizeof(int));
    6362}
    6463
    6564void Player::deserialize(char* buffer)
    6665{
    67    memcpy(&this->id, buffer, 4);
    68    memcpy(&this->pos.x, buffer+4, 4);
    69    memcpy(&this->pos.y, buffer+8, 4);
    70    memcpy(&this->target.x, buffer+12, 4);
    71    memcpy(&this->target.y, buffer+16, 4);
    72    this->name.assign(buffer+20);
     66   istringstream iss;
     67   iss.str(buffer);
    7368
    74    cout << "id: " << this->id << endl;
    75    cout << "pos x: " << this->pos.x << endl;
    76    cout << "pos y: " << this->pos.y << endl;
    77    cout << "target x: " << this->target.x << endl;
    78    cout << "target y: " << this->target.y << endl;
    79    cout << "name: " << this->name << endl;
     69   iss.read((char*)&(this->id), sizeof(int));
     70   iss >> this->name;
     71   iss.read((char*)&(this->pos.x), sizeof(int));
     72   iss.read((char*)&(this->pos.y), sizeof(int));
    8073}
    8174
     
    8982   this->addr = addr;
    9083}
    91 
    92 void Player::move(void) {
    93    int speed = 100; // pixels per second
    94    unsigned long long curTime = getCurrentMillis();
    95 
    96    // if we're at our target, don't move
    97    if (pos.x == target.x && pos.y == target.y)
    98       cout << "We're already at our target" << endl;
    99    else {
    100       float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
    101       cout << "We need to move " << pixels << " pixels" << endl;
    102 
    103       double angle = atan2(target.y-pos.y, target.x-pos.x);
    104 
    105       float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
    106       if (dist <= pixels) {
    107          pos.x = target.x;
    108          pos.y = target.y;
    109       }else {
    110          pos.x += cos(angle)*pixels;
    111          pos.y += sin(angle)*pixels;
    112       }
    113    }
    114 
    115    timeLastUpdated = curTime;
    116 }
  • common/Player.h

    rca44f82 r3a79253  
    3232   void setAddr(sockaddr_in addr);
    3333
    34    void move();
    35 
    3634   int id;
    3735   string name;
    3836   string password;
    3937   sockaddr_in addr;
    40    FLOAT_POSITION pos;
    41    POSITION target;
    42    unsigned long long timeLastUpdated;
     38   PLAYER_POS pos;
    4339};
    4440
  • readme.txt

    rca44f82 r3a79253  
    1 This info is outdated. The latest info is on the github wiki.
    2 
    31BoostPro Installer options
    42
  • server/DataAccess.cpp

    rca44f82 r3a79253  
    33#include <iostream>
    44#include <sstream>
    5 #include <cstdlib>
    65
    76using namespace std;
     
    2827   ostringstream oss;
    2928
    30    string salt = "$1$";
    31    int random;
    32    char chr;
    33    for(int i=0; i<8; i++)
    34    {
    35       random = rand() % 62;
    36       if (random < 26)
    37          chr = (char)('a'+random);
    38       else if (random < 52)
    39          chr = (char)('A'+random-26);
    40       else
    41          chr = (char)('0'+random-52);
    42       salt += chr;
    43    }
    44    salt += '$';
    45 
    46    string encrypted(crypt(password.c_str(), salt.c_str()));
    47 
    48    oss << "'" << username << "', '" << encrypted << "'";
     29   oss << "'" << username << "', '" << password << "'";
    4930
    5031   return insert("users", "name, password", oss.str());
    51 }
    52 
    53 int DataAccess::updatePlayer(string username, string password)
    54 {
    55    ostringstream values, where;
    56 
    57    values << "password='" << password << "'";
    58    
    59    where << "name='" << username << "'";
    60 
    61    return update("users", values.str(), where.str());
    6232}
    6333
     
    8151   }
    8252
    83    if ( ( row = mysql_fetch_row(result)) != NULL ) {
    84       cout << "Creating a new player" << endl;
     53   if ( ( row = mysql_fetch_row(result)) != NULL )
    8554      p = new Player(string(row[1]), string(row[2]));
    86    }else {
     55   else {
    8756      cout << "Returned no results for some reason" << endl;
    8857      p = NULL;
     
    9463}
    9564
    96 // need to make sure this list is freed
    97 // since we need to create a DataAccess class
    98 // when calling these functions,
    99 // we could free this list in the destructor
    100 list<Player*>* DataAccess::getPlayers()
     65int DataAccess::printPlayers()
    10166{
    10267   MYSQL_RES *result;
     
    10873   if (result == NULL) {
    10974      cout << mysql_error(connection) << endl;
    110       return NULL;
     75      return 1;
    11176   }
    11277
    113    list<Player*>* lstPlayers = new list<Player*>();
    11478   while ( ( row = mysql_fetch_row(result)) != NULL ) {
    11579      cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
    116       lstPlayers->push_back(new Player(row[1], row[2]));
    11780   }
    11881
    11982   mysql_free_result(result);
    12083
    121    return lstPlayers;
     84   return 0;
    12285}
    12386
    124 bool DataAccess::verifyPassword(string password, string encrypted)
    125 {
    126    string test(crypt(password.c_str(), encrypted.c_str()));
    127 
    128    return encrypted.compare(test) == 0;
    129 }
    130 
    131 int DataAccess::insert(string table, string columns, string values)
     87int DataAccess::insert(string table, string rows, string values)
    13288{
    13389   int query_state;
    13490   ostringstream oss;
    13591
    136    oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
    137    cout << "query: " << oss.str() << endl;
    138 
    139    query_state = mysql_query(connection, oss.str().c_str());
    140 
    141    if (query_state != 0) {
    142       cout << mysql_error(connection) << endl;
    143       return 1;
    144    }
    145 
    146    return 0;
    147 }
    148 
    149 int DataAccess::update(string table, string values, string where)
    150 {
    151    int query_state;
    152    ostringstream oss;
    153 
    154    oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
     92   oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")";
    15593   cout << "query: " << oss.str() << endl;
    15694
  • server/DataAccess.h

    rca44f82 r3a79253  
    33
    44#include <string>
    5 #include <list>
    65
    76#include <mysql/mysql.h>
     
    1716
    1817   int insertPlayer(string username, string password);
    19    int updatePlayer(string username, string password);
    2018
    21    Player* getPlayer(string username);
    22    list<Player*>* getPlayers();
    23    bool verifyPassword(string encrypted, string password);
     19   Player *getPlayer(string username);
     20   int printPlayers();
    2421
    2522   int insert(string table, string rows, string values);
    26    int update(string table, string values, string where);
    2723   MYSQL_RES *select(string table, string filter);
    2824
  • server/makefile

    rca44f82 r3a79253  
    11CC = g++
    2 LIB_FLAGS = -lssl -lmysqlclient -lcrypt -lrt
     2LIB_FLAGS = -lssl -lmysqlclient
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o
     5DEPENDENCIES = Common.o Message.o Player.o DataAccess.o
    66
    77server : server.cpp $(DEPENDENCIES)
     
    1717        $(CC) -c -o $@ $?
    1818
    19 WorldMap.o : $(COMMON_PATH)/WorldMap.cpp
    20         $(CC) -c -o $@ $?
    21 
    2219%.o : %.cpp
    2320        $(CC) -c -o $@ $?
  • server/server.cpp

    rca44f82 r3a79253  
    66#include <sstream>
    77#include <cstring>
    8 #include <cmath>
    98
    109#include <vector>
     
    1514#include <netinet/in.h>
    1615#include <arpa/inet.h>
    17 
    18 #include <crypt.h>
    1916
    2017/*
     
    2724#include "../common/Common.h"
    2825#include "../common/Message.h"
    29 #include "../common/WorldMap.h"
    3026#include "../common/Player.h"
    3127
     
    3430using namespace std;
    3531
    36 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
    37 
    38 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     32bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     33
     34void updateUnusedId(unsigned int& id);
    3935
    4036// this should probably go somewhere in the common folder
     
    9894   NETWORK_MSG clientMsg, serverMsg;
    9995   map<unsigned int, Player> mapPlayers;
    100    unsigned int unusedId = 1;
     96   unsigned int unusedId = 0;
    10197
    10298   //SSL_load_error_strings();
     
    108104      exit(1);
    109105   }
    110 
    111    WorldMap* gameMap = NULL; //WorldMap::createDefaultMap();
    112106 
    113107   sock = socket(AF_INET, SOCK_DGRAM, 0);
     
    133127         cout << "Got a message" << endl;
    134128
    135          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
    136 
    137          // probably replace this with a function that prints based on the
    138          // message type
     129         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
     130
    139131         cout << "msg: " << serverMsg.buffer << endl;
    140          cout << "broadcastResponse: " << broadcastResponse << endl;
     132
    141133         if (broadcastResponse)
    142134         {
     
    144136
    145137            map<unsigned int, Player>::iterator it;
     138
    146139            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    147140            {
     
    158151         }
    159152
    160          // update player positions
    161          map<unsigned int, Player>::iterator it;
    162          for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    163          {
    164             it->second.move();
    165          }
    166 
    167153         broadcastPlayerPositions(mapPlayers, sock);
    168154      }
     
    172158}
    173159
    174 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
     160bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
    175161{
    176162   DataAccess da;
     
    209195      case MSG_TYPE_LOGIN:
    210196      {
    211          cout << "Got login message" << endl;
    212 
    213197         string username(clientMsg.buffer);
    214198         string password(strchr(clientMsg.buffer, '\0')+1);
     199         cout << "Player logging in: " << username << endl;
    215200
    216201         Player* p = da.getPlayer(username);
    217202
    218          if (p == NULL || !da.verifyPassword(password, p->password))
     203         if (p == NULL || p->password != password)
    219204         {
    220205            strcpy(serverMsg.buffer, "Incorrect username or password");
     
    227212         {
    228213            p->setAddr(from);
    229             updateUnusedId(unusedId, mapPlayers);
    230214            p->id = unusedId;
    231215            mapPlayers[unusedId] = *p;
    232 
    233             // sendd back the new player info to the user
    234             p->serialize(serverMsg.buffer);
     216            updateUnusedId(unusedId);
     217
     218            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
    235219         }
    236220
     
    251235         {
    252236            strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
    253             cout << "Player not logged in" << endl;
    254237         }
    255238         else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
     
    257240         {
    258241            strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
    259             cout << "Player logged in using a different connection" << endl;
    260242         }
    261243         else
     
    265247            mapPlayers.erase(p->id);
    266248            strcpy(serverMsg.buffer, "You have successfully logged out.");
    267             cout << "Player logged out successfuly" << endl;
    268          }
    269 
    270          // should really be serverMsg.type = MSG_TYPE_LOGOUT;
    271          serverMsg.type = MSG_TYPE_LOGIN;
     249         }
    272250
    273251         break;
     
    287265            broadcastResponse = true;
    288266
    289             ostringstream oss;
    290             oss << p->name << ": " << clientMsg.buffer;
    291 
    292             strcpy(serverMsg.buffer, oss.str().c_str());
     267            stringstream ss;
     268            ss << p->name << ": " << clientMsg.buffer;
     269
     270            strcpy(serverMsg.buffer, ss.str().c_str());
    293271         }     
    294272
     
    297275         break;
    298276      }
    299       case MSG_TYPE_PLAYER_MOVE:
    300       {
    301          cout << "Got a move message" << endl;
    302 
    303          istringstream iss;
    304          iss.str(clientMsg.buffer);
    305 
    306          cout << "PLAYER_MOVE" << endl;
    307 
    308          int id, x, y;
    309 
    310          memcpy(&id, clientMsg.buffer, 4);
    311          memcpy(&x, clientMsg.buffer+4, 4);
    312          memcpy(&y, clientMsg.buffer+8, 4);
    313          
    314          cout << "x: " << x << endl;
    315          cout << "y: " << y << endl;
    316          cout << "id: " << id << endl;
    317 
    318          if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
    319               mapPlayers[id].addr.sin_port == from.sin_port )
    320          {
    321             // we need to make sure the player can move here
    322             if (0 <= x && x < 300 && 0 <= y && y < 300 &&
    323                gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
    324             {
    325                // first we get the correct vector
    326                mapPlayers[id].target.x = x;
    327                mapPlayers[id].target.y = y;
    328                int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
    329                int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
    330                cout << "xDiff: " << xDiff << endl;               
    331                cout << "yDiff: " << yDiff << endl;               
    332 
    333                // then we get the correct angle
    334                double angle = atan2(yDiff, xDiff);
    335                cout << "angle: " << angle << endl;               
    336 
    337                // finally we use the angle to determine
    338                // how much the player moves
    339                // the player will move 50 pixels in the correct direction
    340                mapPlayers[id].pos.x += cos(angle)*50;
    341                mapPlayers[id].pos.y += sin(angle)*50;
    342                cout << "new x: " << mapPlayers[id].pos.x << endl;               
    343                cout << "new y: " << mapPlayers[id].pos.y << endl;               
    344 
    345                serverMsg.type = MSG_TYPE_PLAYER_MOVE;
    346                
    347                memcpy(serverMsg.buffer, &id, 4);
    348                memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
    349                memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
    350                //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
    351 
    352                broadcastResponse = true;
    353             }
    354             else
    355                cout << "Bad terrain detected" << endl;
    356          }
    357          else  // nned to send back a message indicating failure
    358             cout << "Player id (" << id << ") doesn't match sender" << endl;
    359 
    360          break;
    361       }
    362277      default:
    363278      {
     
    370285   }
    371286
    372    cout << "Got to the end of the switch" << endl;
    373 
    374287   return broadcastResponse;
    375288}
    376289
    377 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
    378 {
    379    while (mapPlayers.find(id) != mapPlayers.end())
    380       id++;
    381 }
     290void updateUnusedId(unsigned int& id)
     291{
     292   id = 5;
     293}
Note: See TracChangeset for help on using the changeset viewer.