123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include "PlayerRegister.h"
- #include <Datei.h>
- Framework::Text randomSecret()
- {
- int len = 1024 + (int)(((double)rand() / RAND_MAX - 0.5) * 512);
- char* key = new char[len + 1];
- for (int i = 0; i < len; i++)
- key[i] = (char)((((double)rand() / RAND_MAX) * 254) + 1);
- key[len] = 0;
- Framework::Text secret = key;
- delete[] key;
- return secret;
- }
- RegisteredPlayer::RegisteredPlayer(int id, Framework::Text name)
- : Framework::ReferenceCounter(),
- id(id),
- name(name),
- secret(randomSecret())
- {}
- RegisteredPlayer::RegisteredPlayer(Framework::StreamReader* zReader)
- : Framework::ReferenceCounter()
- {
- zReader->lese((char*)&id, 4);
- char len;
- zReader->lese(&len, 1);
- char* buffer = new char[len + 1];
- zReader->lese(buffer, len);
- buffer[(int)len] = 0;
- name = buffer;
- delete[] buffer;
- short sLen;
- zReader->lese((char*)&sLen, 2);
- buffer = new char[sLen + 1];
- zReader->lese(buffer, sLen);
- buffer[sLen] = 0;
- secret = buffer;
- delete[] buffer;
- }
- int RegisteredPlayer::getId() const {
- return id;
- }
- Framework::Text RegisteredPlayer::getName() const {
- return name;
- }
- Framework::Text RegisteredPlayer::getSecret() const {
- return secret;
- }
- void RegisteredPlayer::save(Framework::StreamWriter* zWriter) const
- {
- zWriter->schreibe((char*)&id, 4);
- char len = (char)name.getLength();
- zWriter->schreibe(&len, 1);
- zWriter->schreibe(name.getText(), len);
- short sLen = (short)secret.getLength();
- zWriter->schreibe((char*)&sLen, 2);
- zWriter->schreibe(secret.getText(), sLen);
- }
- PlayerRegister::PlayerRegister(Framework::Text worldPath)
- : Framework::ReferenceCounter(),
- path(worldPath + "/player/players.reg")
- {
- Framework::Datei regDat;
- regDat.setDatei(path);
- if (!regDat.existiert())
- {
- regDat.erstellen();
- }
- regDat.open(Framework::Datei::Style::lesen);
- while (!regDat.istEnde())
- {
- RegisteredPlayer* p = new RegisteredPlayer(®Dat);
- players.add(p);
- }
- regDat.close();
- }
- Framework::Text PlayerRegister::addPlayer(Framework::Text name) {
- cs.lock();
- int nextId = 0;
- for (RegisteredPlayer* player : players)
- {
- nextId = MAX(nextId, player->getId() + 1);
- if (player->getName().istGleich(name))
- {
- cs.unlock();
- return "";
- }
- }
- RegisteredPlayer* p = new RegisteredPlayer(nextId, name);
- players.add(p);
- cs.unlock();
- return p->getSecret();
- }
- bool PlayerRegister::hasPlayer(Framework::Text name)
- {
- cs.lock();
- for (RegisteredPlayer* player : players)
- {
- if (player->getName().istGleich(name))
- {
- cs.unlock();
- return 1;
- }
- }
- cs.unlock();
- return 0;
- }
- int PlayerRegister::getPlayerId(Framework::Text name) {
- cs.lock();
- for (RegisteredPlayer* player : players)
- {
- if (player->getName().istGleich(name))
- {
- cs.unlock();
- return player->getId();
- }
- }
- cs.unlock();
- return -1;
- }
- bool PlayerRegister::checkSecret(Framework::Text name, Framework::Text secret)
- {
- cs.lock();
- for (RegisteredPlayer* player : players)
- {
- if (player->getName().istGleich(name))
- {
- cs.unlock();
- return player->getSecret().istGleich(secret);
- }
- }
- cs.unlock();
- return secret.getLength() == 0; // new player
- }
- void PlayerRegister::save()
- {
- cs.lock();
- Framework::Datei regDat;
- regDat.setDatei(path);
- if (!regDat.existiert())
- {
- regDat.erstellen();
- }
- regDat.open(Framework::Datei::Style::schreiben);
- for (RegisteredPlayer* player : players)
- {
- player->save(®Dat);
- }
- regDat.close();
- cs.unlock();
- }
|