~ubuntu-branches/ubuntu/raring/lordsawar/raring

« back to all changes in this revision

Viewing changes to src/game-client.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese, Gonéri Le Bouder
  • Date: 2008-06-17 11:15:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617111526-yjyvu9df50zmpdo0
Tags: 0.0.9-1
[ Barry deFreese ]
* New upstream release.
  + Fixes gcc-4.3 builds so drop ftbfs_gcc-4.3_fix.diff.
  + Add new build-dependency for libgnet-dev.
* Add simple man page for new lordsawar-tile-editor.
* Add desktop file for lordsawar-tile-editor.
* Remove French translation on install.

[ Gonéri Le Bouder ]
* bump Debian Policy to 3.8.0. No change needed.
* fix wording in the 0.0.8-3 entry of the Debian changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008 Ole Laursen
 
2
//
 
3
//  This program is free software; you can redistribute it and/or modify
 
4
//  it under the terms of the GNU General Public License as published by
 
5
//  the Free Software Foundation; either version 2 of the License, or
 
6
//  (at your option) any later version.
 
7
//
 
8
//  This program is distributed in the hope that it will be useful,
 
9
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
//  GNU Library General Public License for more details.
 
12
//
 
13
//  You should have received a copy of the GNU General Public License
 
14
//  along with this program; if not, write to the Free Software
 
15
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
16
//  02110-1301, USA.
 
17
 
 
18
#include <iostream>
 
19
#include <fstream>
 
20
 
 
21
#include "game-client.h"
 
22
 
 
23
#include "network-connection.h"
 
24
#include "File.h"
 
25
#include "action.h"
 
26
#include "playerlist.h"
 
27
#include "network_player.h"
 
28
#include "xmlhelper.h"
 
29
 
 
30
 
 
31
 
 
32
GameClient::GameClient()
 
33
{
 
34
}
 
35
 
 
36
GameClient::~GameClient()
 
37
{
 
38
}
 
39
 
 
40
void GameClient::start(std::string host, int port)
 
41
{
 
42
  network_connection.reset(new NetworkConnection());
 
43
  network_connection->connected.connect(
 
44
    sigc::mem_fun(this, &GameClient::onConnected));
 
45
  network_connection->connection_lost.connect(
 
46
    sigc::mem_fun(this, &GameClient::onConnectionLost));
 
47
  network_connection->got_message.connect(
 
48
    sigc::mem_fun(this, &GameClient::onGotMessage));
 
49
 
 
50
  network_connection->connectToHost(host, port);
 
51
}
 
52
 
 
53
void GameClient::onConnected() 
 
54
{
 
55
  std::cerr << "connected" << std::endl;
 
56
 
 
57
  network_connection->send(MESSAGE_TYPE_PING, "");
 
58
}
 
59
 
 
60
void GameClient::onConnectionLost()
 
61
{
 
62
  std::cerr << "connection lost" << std::endl;
 
63
}
 
64
 
 
65
void GameClient::onGotMessage(MessageType type, std::string payload)
 
66
{
 
67
  std::cerr << "got message of type " << type << std::endl;
 
68
  switch (type) {
 
69
  case MESSAGE_TYPE_PING:
 
70
    network_connection->send(MESSAGE_TYPE_PONG, "PONGOGOGO");
 
71
    break;
 
72
 
 
73
  case MESSAGE_TYPE_PONG:
 
74
    std::cerr << "sending join" << std::endl;
 
75
    network_connection->send(MESSAGE_TYPE_JOIN, "I wanna join");
 
76
    break;
 
77
 
 
78
  case MESSAGE_TYPE_SENDING_ACTIONS:
 
79
    gotActions(payload);
 
80
    break;
 
81
    
 
82
  case MESSAGE_TYPE_SENDING_MAP:
 
83
    gotScenario(payload);
 
84
    break;
 
85
 
 
86
  case MESSAGE_TYPE_JOIN:
 
87
    // FIXME: faulty server
 
88
    break;
 
89
  }
 
90
}
 
91
 
 
92
void GameClient::gotScenario(const std::string &payload)
 
93
{
 
94
  std::string file = "network.sav";
 
95
  std::string path = File::getSavePath();
 
96
  path += file;
 
97
  
 
98
  std::ofstream f(path.c_str());
 
99
  f << payload;
 
100
  f.close();
 
101
  
 
102
  game_scenario_received.emit(path);
 
103
}
 
104
 
 
105
class ActionLoader 
 
106
{
 
107
public:
 
108
  bool loadAction(std::string tag, XML_Helper* helper)
 
109
  {
 
110
    if (tag == "action") {
 
111
      Action* action = Action::handle_load(helper);
 
112
      actions.push_back(action);
 
113
    }
 
114
    return true;
 
115
  }
 
116
 
 
117
  std::list<Action *> actions;
 
118
};
 
119
  
 
120
 
 
121
void GameClient::gotActions(const std::string &payload)
 
122
{
 
123
  std::istringstream is(payload);
 
124
 
 
125
  ActionLoader loader;
 
126
  
 
127
  XML_Helper helper(&is);
 
128
  helper.registerTag("action", sigc::mem_fun(loader, &ActionLoader::loadAction));
 
129
  helper.parse();
 
130
 
 
131
  for (std::list<Action *>::iterator i = loader.actions.begin(),
 
132
       end = loader.actions.end(); i != end; ++i)
 
133
  {
 
134
    Action *action = *i;
 
135
    std::cerr << "decoding action " << action->getType() << std::endl;
 
136
    
 
137
    Player *p = Playerlist::getInstance()->getPlayer(action->getPlayer());
 
138
    NetworkPlayer *np = dynamic_cast<NetworkPlayer *>(p);
 
139
 
 
140
    if (!np) {
 
141
      std::cerr << "warning: ignoring action for player " << p << std::endl;
 
142
      continue;
 
143
    }
 
144
 
 
145
    np->decodeAction(action);
 
146
  }
 
147
 
 
148
  for (std::list<Action *>::iterator i = loader.actions.begin(),
 
149
       end = loader.actions.end(); i != end; ++i)
 
150
    delete *i;
 
151
}