diff --git a/ConfigPacket.hpp b/ConfigPacket.hpp new file mode 100644 index 0000000000000000000000000000000000000000..973c9e2a50975d53432df9286ded293bb71aef7d --- /dev/null +++ b/ConfigPacket.hpp @@ -0,0 +1,62 @@ + +// Created by hoelshare on 27.05.17. +// + +#ifndef GPN_BADGELASERTAG_CONFIGPAKET_HPP +#define GPN_BADGELASERTAG_CONFIGPAKET_HPP + +#include "url-encode.h" + +struct ConfigPacket { + int magazineSize[3]; + int damage[3]; + int magazineReloadTime[3]; + int shotTime[3]; + int resistance; + int health; +#define TYP "ConfigPacket" + + String serial() { + String ret = ""; + ret += "typ=" + String(TYP); + ret += "&mag0=" + String(magazineSize[0]); + ret += "&mag1=" + String(magazineSize[1]); + ret += "&mag2=" + String(magazineSize[2]); + ret += "&dmg0=" + String(damage[0]); + ret += "&dmg1=" + String(damage[1]); + ret += "&dmg2=" + String(damage[2]); + ret += "&rlt0=" + String(magazineReloadTime[0]); + ret += "&rlt1=" + String(magazineReloadTime[1]); + ret += "&rlt2=" + String(magazineReloadTime[2]); + ret += "&shotT0=" + String(shotTime[0]); + ret += "&shotT1=" + String(shotTime[1]); + ret += "&shotT2=" + String(shotTime[2]); + ret += "&resistance=" + String(resistance); + ret += "&health=" + String(health); + return ret; + } + + static ConfigPacket *read(String value) { + ConfigPacket *config = new ConfigPacket(); + UrlDecode decode(value.c_str()); + config->magazineSize[0] = int(decode.getKey("mag0")); + config->magazineSize[1] = int(decode.getKey("mag1")); + config->magazineSize[2] = int(decode.getKey("mag2")); + config->damage[0] = int(decode.getKey("dmg0")); + config->damage[1] = int(decode.getKey("dmg1")); + config->damage[2] = int(decode.getKey("dmg2")); + config->magazineReloadTime[0] = int(decode.getKey("rlt0")); + config->magazineReloadTime[1] = int(decode.getKey("rlt1")); + config->magazineReloadTime[2] = int(decode.getKey("rlt2")); + config->shotTime[0] = int(decode.getKey("shotT0")); + config->shotTime[1] = int(decode.getKey("shotT1")); + config->shotTime[2] = int(decode.getKey("shotT2")); + config->resistance = int(decode.getKey("resistance")); + config->health = int(decode.getKey("health")); + + return config; + } +}; + + +#endif //GPN_BADGELASERTAG_CONFIGPAKET_HPP diff --git a/GPN_BadgeLasertag.ino b/GPN_BadgeLasertag.ino index 574fb515925895f83903c81940c05e5f66fe513f..55787ea149d024d45bec50a6329d1bae7c8b8575 100644 --- a/GPN_BadgeLasertag.ino +++ b/GPN_BadgeLasertag.ino @@ -14,7 +14,7 @@ #define USEIR #ifdef DEBUG -#define BADGE_PULL_INTERVAL 6000 +#define BADGE_PULL_INTERVAL 10000 #else #define BADGE_PULL_INTERVAL 5*60*1000 #endif diff --git a/GameServer.cpp b/GameServer.cpp index 45f3dd4f12ded874c0c975f6b6c9844fe5d1676c..49c69f1aa20e97d583bdc4345c7053f8112d8029 100644 --- a/GameServer.cpp +++ b/GameServer.cpp @@ -4,6 +4,11 @@ #define AUSGABE #include "GameServer.hpp" +#include "ConfigPacket.hpp" +#include "Player.hpp" + +#define RESISTANCE 100 +#define HEALTH 100 void GameServer::setTeamPlay(char isTeam) { #ifdef AUSGABE @@ -41,7 +46,18 @@ void GameServer::update() { handleWelcome(&(serverClients[numConnections]), numConnections); } } else { - // todo Normal game functions + for (short i = 0; i <= numConnections; i++) { //iterate throu the slots + if (serverClients[i].connected() && serverClients[i].available()) { //skip unconnected slots + String message = serverClients[i].readStringUntil('\n'); + UrlDecode decode(message.c_str()); + String strTyp = String(decode.getKey("typ")); + if (strTyp == "hit") { + HitPacket* packet = HitPacket::read(message); + sendHit(packet); + delete packet; + } + } + } } } @@ -94,8 +110,29 @@ void GameServer::sendIP() { } void GameServer::startGame() { - this->gameStarted = true; - // todo something more + if (!gameStarted) { + Serial.println("Game started"); + ConfigPacket config; + for (int i = 0; i < 3; i++) { + config.magazineSize[i] = Player::magazineSize[i]; + config.magazineReloadTime[i] = Player::magazineReloadTime[i]; + config.shotTime[i] = Player::shotTime[i]; + config.damage[i] = Player::damage[i]; + } + config.resistance = RESISTANCE; + config.health = HEALTH; + String value = config.serial() + "\n"; + sendAll(value); + this->gameStarted = true; + ui->closeCurrent(); + } +} + +void GameServer::sendHit(HitPacket* hitPacket) { + int pid = hitPacket->playerId; + serverClients[pid].print(hitPacket->serial()); + serverClients[pid].flush(); + Serial.printf(String("Send hit: " + hitPacket->serial()).c_str()); } String GameServer::buildPlayerList() { diff --git a/GameServer.hpp b/GameServer.hpp index 20699333658338e6bd451c5656a13cb37cc8ebe4..f0a90f3c37787ed5a492c9dba0f6f7e46254f9db 100644 --- a/GameServer.hpp +++ b/GameServer.hpp @@ -7,6 +7,7 @@ #include <GPNBadge.hpp> #include "BadgeUI.h" +#include "HitPacket.hpp" #include "LaserMenuItem.hpp" #include "PlayerListMenu.hpp" #include "RemotePlayer.hpp" @@ -57,12 +58,12 @@ public: Serial.printf("Opened menu\n"); #endif playerList = new PlayerListMenu(9); - LaserMenuItem* item = new LaserMenuItem([]() {}, nullptr); + LaserMenuItem *item = new LaserMenuItem([]() {}, nullptr); item->setText("Playerlist"); playerList->addMenuItem(item); playerList->setLeftAction([=]() { this->kick(); }); playerList->setRightAction([=]() { this->secureQuestion(); }); - playerList->setEnterAction([=]() { this->sendIP(); }); + playerList->setEnterAction([=]() { this->sendIP(); }); ui->open(playerList); ui->dispatchInput(badge->getJoystickState()); ui->draw(); @@ -92,6 +93,8 @@ protected: void sendAll(String value); + void sendHit(HitPacket *hitPacket); + private: WindowSystem *ui; Badge *badge; @@ -102,8 +105,11 @@ private: WiFiClient *serverClients; // the clients connected to the server short numConnections = 0; bool gameStarted = false; + void sendIP(); + RemotePlayer rPlayers[MAX_SRV_CLIENTS]; + String buildPlayerList(); }; diff --git a/HitPacket.hpp b/HitPacket.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d6e3582aefa260bdbc2e6a962d936d260db465ac --- /dev/null +++ b/HitPacket.hpp @@ -0,0 +1,34 @@ +// +// Created by hoelshare on 27.05.17. +// + +#ifndef GPN_BADGELASERTAG_HITPACKET_HPP +#define GPN_BADGELASERTAG_HITPACKET_HPP + +#include "url-encode.h" + +struct HitPacket { + int playerId; + String nickname; + int lastHp; +#define TYP "hit" + String serial(){ + String ret = ""; + ret += "typ=" + String(TYP); + ret += "&pid=" + String(playerId); + ret += "&nick=" + String(nickname); + ret += "&lastHP=" + String(lastHp); + return ret; + } + + static HitPacket* read(String value) { + UrlDecode decode(value.c_str()); + HitPacket* ret = new HitPacket(); + ret->lastHp = int(decode.getKey("lastHP")); + ret->nickname = String(decode.getKey("nick")); + ret->playerId = int(decode.getKey("pid")); + return ret; + } +}; + +#endif //GPN_BADGELASERTAG_HITPACKET_HPP diff --git a/Player.cpp b/Player.cpp index cd1548054b1098642fde395ad411a3d05592b176..ebf7d59f32a2b2b8f5f05135ee39783dd9f45fbd 100644 --- a/Player.cpp +++ b/Player.cpp @@ -6,6 +6,13 @@ #define min(a, b) ((a)<(b)?(a):(b)) #define max(a, b) ((a)>(b)?(a):(b)) +// TODO set constants +const int Player::magazineSize[] = {10, 25, 1}; +const int Player::shotTime[] = {100, 20, 300}; +const int Player::magazineReloadTime[] = {800, 400, 2000}; +const int Player::damage[] = {25, 10, 100}; + + void Player::heal(unsigned short healthPoints) { unsigned short calcHP = this->currentHP + healthPoints; this->currentHP = min(this->maxHP, calcHP); diff --git a/Player.hpp b/Player.hpp index 9ba56b427b62a362233141d8d1b3badd1921d61c..f0199bb18466701168ce95ab462a4abe1cf27224 100644 --- a/Player.hpp +++ b/Player.hpp @@ -62,28 +62,28 @@ public: delete healthMenuItem, armorMenuItem, ammoMenuItem; } + static const int magazineSize[3]; + static const int shotTime[3]; + static const int magazineReloadTime[3]; + static const int damage[3]; + private: - unsigned short pID; - unsigned short tID; + unsigned int pID; + unsigned int tID; unsigned char teamColor[3]; // 3 Colors RGB char *nickname; - unsigned short maxHP; - unsigned short currentHP; - unsigned short shotMagazine[3]; // 3 Weapon categories [pistol, machine gun, explosive] - unsigned short currentShot[3]; - unsigned char weaponIndex; - unsigned short currentArmor; - unsigned short maxArmor; + unsigned int maxHP; + unsigned int currentHP; + unsigned int shotMagazine[3]; // 3 Weapon categories [pistol, machine gun, explosive] + unsigned int currentShot[3]; + unsigned int weaponIndex; + unsigned int currentArmor; + unsigned int maxArmor; float maxArmorPercent; // max protection between 0 and 1 void die(); - - // TODO set constants - const char magazineSize[3] = {10, 25, 1}; - const short shotTime[3] = {100, 20, 300}; - const short magazineReloadTime[3] = {800, 400, 2000}; - const short damage[3] = {25, 10, 100}; Badge *badge; + protected: void updateHealthMenuItem(); @@ -95,5 +95,4 @@ protected: LaserMenuItem *armorMenuItem; LaserMenuItem *ammoMenuItem; }; - #endif //GPN_LASERTAG_PLAYER_HPP diff --git a/README.md b/README.md index d94935dadeafd8e1d78e36456ebf0bc5ae1f213a..ba13b21826c70fb10999de264cbe765a72bc2a5a 100644 --- a/README.md +++ b/README.md @@ -53,3 +53,9 @@ ### UID von Server - char(4) - char(UID) + +### Hit von Client +- Typ: Hit +- From: {playerId} +- who: {nickname} +- lastHP: {verbliebene HP} \ No newline at end of file diff --git a/RemotePlayer.hpp b/RemotePlayer.hpp index 555e1fbf551a81e2b9a1cc0fa7806e67f812da9e..486f1d57fdcbb687c3a4ccc60ad40d696397a43b 100644 --- a/RemotePlayer.hpp +++ b/RemotePlayer.hpp @@ -11,13 +11,14 @@ struct RemotePlayer { String nickname; unsigned short pid; unsigned char tid; - +#define TYP "RemotePlayer" String serial() { String ret = ""; - ret += "len=" + String(String(nickname).length()) + ";"; - ret += "&nick=" + String(nickname) + ";"; - ret += "&pid=" + String(pid) + ";"; - ret += "&tid=" + String(tid) + ";"; + ret += "typ=" + String(TYP); + ret += "&len=" + String(String(nickname).length()); + ret += "&nick=" + String(nickname); + ret += "&pid=" + String(pid); + ret += "&tid=" + String(tid); return ret; }