GameServer.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <iostream>
  2. #include "common/network/Packets.h"
  3. #include "server/GameServer.h"
  4. #include "server/commands/CommandManager.h"
  5. #include "server/commands/CommandTypes.h"
  6. #include "server/world/WorldGenerator.h"
  7. GameServer::GameServer(Server& server) : state(server), reader(0, "> ") {
  8. worlds.add(new World(blocks));
  9. WorldGenerator wg(blocks, *(worlds[0]));
  10. wg.generate();
  11. }
  12. void GameServer::tick() {
  13. tps.update();
  14. handleCommands();
  15. }
  16. void GameServer::handleCommands() {
  17. if(!reader.canRead()) {
  18. return;
  19. }
  20. const char* s = reader.readLine();
  21. if(s == nullptr) {
  22. return;
  23. }
  24. RawCommand rawCommand(s);
  25. commandManager.execute(state, rawCommand);
  26. }
  27. void GameServer::onConnect(Server::Client& client) {
  28. std::cout << "connected\n";
  29. World& w = *(worlds[0]);
  30. OutPacket out = OutPacket::reliable(
  31. w.getHeight() * w.getSize() * w.getSize() * sizeof(BlockId) + 2);
  32. out.writeU16(ServerPacket::WORLD);
  33. for(int x = 0; x < w.getSize(); x++) {
  34. for(int y = 0; y < w.getHeight(); y++) {
  35. for(int z = 0; z < w.getSize(); z++) {
  36. out.writeU16(w.getBlock(x, y, z).getId());
  37. }
  38. }
  39. }
  40. client.send(out);
  41. }
  42. void GameServer::onDisconnect(Server::Client& client) {
  43. (void)client;
  44. std::cout << "disconnected\n";
  45. }
  46. void GameServer::onPacket(Server::Client& client, InPacket& in) {
  47. (void)client;
  48. StringBuffer<256> s;
  49. int8 c;
  50. while(in.readS8(c)) {
  51. s.append(c);
  52. }
  53. std::cout << "Packet: " << s << "\n";
  54. }
  55. bool GameServer::isRunning() const {
  56. return state.running;
  57. }