Logo
~Sockets~
~Examples~
~Contact~


PlayerFactory.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 //#include <stdio.h>
00024 
00025 #include "SmallSocket.h"
00026 #include "PlayerFactory.h"
00027 
00028 
00029 
00030 PlayerFactory::PlayerFactory()
00031 :IPersist()
00032 {
00033 }
00034 
00035 
00036 PlayerFactory::~PlayerFactory()
00037 {
00038 }
00039 
00040 
00041 void PlayerFactory::Load()
00042 {
00043         FILE *fil = fopen("players.txt","rt");
00044         if (fil)
00045         {
00046                 char slask[1000];
00047                 fgets(slask,1000,fil);
00048                 while (!feof(fil))
00049                 {
00050                         while (strlen(slask) && (slask[strlen(slask) - 1] == 13 || slask[strlen(slask) - 1] == 10))
00051                         {
00052                                 slask[strlen(slask) - 1] = 0;
00053                         }
00054                         long nr = atol(slask);
00055                         fgets(slask,1000,fil);
00056                         while (strlen(slask) && (slask[strlen(slask) - 1] == 13 || slask[strlen(slask) - 1] == 10))
00057                         {
00058                                 slask[strlen(slask) - 1] = 0;
00059                         }
00060                         std::string name = slask;
00061                         fgets(slask,1000,fil);
00062                         while (strlen(slask) && (slask[strlen(slask) - 1] == 13 || slask[strlen(slask) - 1] == 10))
00063                         {
00064                                 slask[strlen(slask) - 1] = 0;
00065                         }
00066                         std::string passwd = slask;
00067                         PLAYER *p = new PLAYER(nr,name,passwd);
00068                         m_players.push_back(p);
00069                         //
00070                         fgets(slask,1000,fil);
00071                 }
00072         }
00073 }
00074 
00075 
00076 void PlayerFactory::Save()
00077 {
00078         FILE *fil = fopen("players.0","wt");
00079         if (fil)
00080         {
00081                 for (player_v::iterator it = m_players.begin(); it != m_players.end(); it++)
00082                 {
00083                         PLAYER *p = *it;
00084                         fprintf(fil,"%ld\n",p -> nr);
00085                         fprintf(fil,"%s\n",p -> m_name.c_str());
00086                         fprintf(fil,"%s\n",p -> m_passwd.c_str());
00087                 }
00088                 fclose(fil);
00089                 unlink("players.old");
00090                 rename("players.txt","players.old");
00091                 rename("players.0","players.txt");
00092         }
00093 }
00094 
00095 
00096 PlayerFactory::PLAYER *PlayerFactory::FindPlayer(const std::string& n,const std::string& p)
00097 {
00098         for (player_v::iterator it = m_players.begin(); it != m_players.end(); it++)
00099         {
00100                 PLAYER *p = *it;
00101                 if (p -> m_name == n) // && p -> passwd == p)
00102                 {
00103                         return p;
00104                 }
00105         }
00106         return NULL;
00107 }
00108 
00109 
00110 PlayerFactory::PLAYER *PlayerFactory::AddPlayer(const std::string& n,const std::string& pw)
00111 {
00112         PLAYER *p = new PLAYER(0,n,pw);
00113         m_players.push_back(p);
00114         p -> nr = m_players.size(); // never delete any player
00115         return p;
00116 }
00117 
00118 
00119 void PlayerFactory::PLAYER::DisplayInventory(SmallSocket *p) 
00120 {
00121         for (item_v::iterator it = m_inventory.begin(); it != m_inventory.end(); it++)
00122         {
00123                 ITEM *i = *it;
00124                 std::string str;
00125                 p -> Send(" " + i -> GetDescription() + "\n");
00126         }
00127 }
00128 
00129 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4