akashi-esquizolandia/core/src/area_data.cpp
in1tiate 83c41c05f7 Squashed commit of the following:
commit e946bf124602f224ce0e371ba1374f0355b537eb
Merge: d6a4e64 4505909
Author: Rosemary Witchaven <32779090+in1tiate@users.noreply.github.com>
Date:   Fri Jan 28 19:43:36 2022 -0600

    Merge pull request #225 from Salanto/Dynamic-Area-Musiclist-Take2

    Allow users to add custom songs to the music list on a per-area basis

commit 45059092d2888b60912f721e43a380910d10ccd8
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 22:05:27 2022 +0100

    TIL what a typedef is

commit 02584db9640fff0a1969a7f516c4bccfae9b5388
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 21:39:17 2022 +0100

    Not all OR are equal. Explain weird command splitting

    Remove hardcoded URLs

commit d00ebd5692296cd0c29dd377113b53fe0e7b99c0
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 21:28:08 2022 +0100

    Salanto PR Language Update by in1tiate

    As usual, my English is absolutely unreadable.

    Co-authored-by: Rosemary Witchaven <32779090+in1tiate@users.noreply.github.com>

commit d3842106e06350dc02d8864bb28232fdc5643f00
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 20:35:32 2022 +0100

    Add missing config file + document commands for help

commit ac64360e1c1741023b01052977de77a7d5ea4f8c
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 19:52:13 2022 +0100

    Minor improvements to command usage and addition of clear command

commit c614578e78ce9afa0c8e22aa36bdf46a70a97169
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 22:19:54 2022 +0100

    Purge last traces of old songInformation system

commit 07618761f044a13d75587b28a9c994342a5980e2
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 22:10:54 2022 +0100

    Working version, needs some refinement and debugging in AOClient

commit 33c0358c98c0fd2de805356a9aa3ac7bbed204e1
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 21:48:05 2022 +0100

    Almost functional implementation

    Now only need to determine why I can't play the customs yet

commit b0acbace78b3f16f2fe4f3c6f65a422e3343f992
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 15:26:42 2022 +0100

    Fix build error, expand validation test slightly

commit a48c4f503998ce8e42f0bb409c5a3c7dc5e40329
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 01:03:27 2022 +0100

    Add commands

commit 88ab0b473953873166e291e5009b97df31547b3f
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 22 15:10:26 2022 +0100

    Float sucks, int has to be good enough

    + add retrival of song information

commit e924e1340be1a0909eba84072f1646fe9770bd02
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Thu Jan 20 22:28:35 2022 +0100

    Fix removing moving

    Add necessary tests

commit 3df088f8d07ce7e0d8fe08b6a97608a623e6ef97
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Wed Jan 19 19:34:16 2022 +0100

    Start work on adding this shit into commands

commit c293ecfa99d1b2bd1e0b34cb8752d69b2eca057c
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 19:07:11 2022 +0100

    Fix typo and add singal for incremental upgrades

commit 10a42322e1e23af5795278a40b2ac59f3ab952ef
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 06:17:51 2022 +0100

    Hookup packet sending to music manager

    This might sound like a bad idea on first glance, but otherwise it breaks the AreaData tests and I am NOT gonna try to fix those without even understanding why they break.

commit 319836296374162b0b847432e8a626778317b869
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 06:17:51 2022 +0100

    Enraged comments, make area send FM packet

    Revert "Enraged comments, make area send FM packet"

    This reverts commit ec7a1a25646b2c2acc8a3a748b853851cc47d205.

commit 224a0d7efe989a5f336167c3f716061813f93ee3
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 05:10:18 2022 +0100

    Change packet communication from area to client

    First steps to hookup the custom musiclist.

commit 65aa8f7855a36f2c668b1399a5ed22fefeaf186d
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 17 00:48:38 2022 +0100

    Add test for custom list sanitisation.

    + Fix intentionally broken tests

commit 7c00ab437a6ff12033742d029ce49037f5bb1ebe
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 17 00:29:51 2022 +0100

    Sanitise the custom list opposed to deleting it

    This will fail tests intentionally to test the CI.

commit 80ad401267068e75707b2517a0bf836763141f8b
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 16 17:17:12 2022 +0100

    Add custom category capabilities

commit 08d8f5f8f683816ceba532f9c47cd0d5ab34389a
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 16 03:58:18 2022 +0100

    Fix music addition and move relevant tests

commit 6ebf0d03b5da61a9c287115009d28038710ba7af
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 15 08:08:20 2022 +0100

    Hookup music_manager into server, change default musiclist source for new clients

    + More tests 🆒

commit bd50c62376f131e2508ecdd3e272209894ecaec1
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 15 03:13:42 2022 +0100

    Add central song validator for other classes

    Also added applicable test cases to ensure proper operation.
2022-01-28 19:51:30 -06:00

617 lines
14 KiB
C++

//////////////////////////////////////////////////////////////////////////////////////
// akashi - a server for Attorney Online 2 //
// Copyright (C) 2020 scatterflower //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include "include/area_data.h"
AreaData::AreaData(QString p_name, int p_index, MusicManager* p_music_manager = nullptr) :
m_index(p_index),
m_music_manager(p_music_manager),
m_playerCount(0),
m_status(IDLE),
m_locked(FREE),
m_document("No document."),
m_area_message("No area message set."),
m_defHP(10),
m_proHP(10),
m_statement(0),
m_judgelog(),
m_lastICMessage(),
m_send_area_message(false)
{
QStringList name_split = p_name.split(":");
name_split.removeFirst();
m_name = name_split.join(":");
QSettings* areas_ini = ConfigManager::areaData();
areas_ini->setIniCodec("UTF-8");
areas_ini->beginGroup(p_name);
m_background = areas_ini->value("background", "gs4").toString();
m_isProtected = areas_ini->value("protected_area", "false").toBool();
m_iniswapAllowed = areas_ini->value("iniswap_allowed", "true").toBool();
m_bgLocked = areas_ini->value("bg_locked", "false").toBool();
m_eviMod = QVariant(areas_ini->value("evidence_mod", "FFA").toString().toUpper()).value<EvidenceMod>();
m_blankpostingAllowed = areas_ini->value("blankposting_allowed","true").toBool();
m_forceImmediate = areas_ini->value("force_immediate", "false").toBool();
m_toggleMusic = areas_ini->value("toggle_music", "true").toBool();
m_shownameAllowed = areas_ini->value("shownames_allowed", "true").toBool();
m_ignoreBgList = areas_ini->value("ignore_bglist", "false").toBool();
m_jukebox = areas_ini->value("jukebox_enabled", "false").toBool();
areas_ini->endGroup();
QTimer* timer1 = new QTimer();
m_timers.append(timer1);
QTimer* timer2 = new QTimer();
m_timers.append(timer2);
QTimer* timer3 = new QTimer();
m_timers.append(timer3);
QTimer* timer4 = new QTimer();
m_timers.append(timer4);
m_jukebox_timer = new QTimer();
connect(m_jukebox_timer, &QTimer::timeout,
this, &AreaData::switchJukeboxSong);
}
const QMap<QString, AreaData::Status> AreaData::map_statuses = {
{"idle", AreaData::Status::IDLE },
{"rp", AreaData::Status::RP },
{"casing", AreaData::Status::CASING },
{"lfp", AreaData::Status::LOOKING_FOR_PLAYERS },
{"looking-for-players", AreaData::Status::LOOKING_FOR_PLAYERS },
{"recess", AreaData::Status::RECESS },
{"gaming", AreaData::Status::GAMING },
};
void AreaData::clientLeftArea(int f_charId, int f_userId)
{
--m_playerCount;
if (f_charId != -1) {
m_charactersTaken.removeAll(f_charId);
}
m_joined_ids.removeAll(f_userId);
}
void AreaData::clientJoinedArea(int f_charId, int f_userId)
{
++m_playerCount;
if (f_charId != -1) {
m_charactersTaken.append(f_charId);
}
m_joined_ids.append(f_userId);
emit userJoinedArea(m_index, f_userId);
}
QList<int> AreaData::owners() const
{
return m_owners;
}
void AreaData::addOwner(int f_clientId)
{
m_owners.append(f_clientId);
m_invited.append(f_clientId);
}
bool AreaData::removeOwner(int f_clientId)
{
m_owners.removeAll(f_clientId);
m_invited.removeAll(f_clientId);
if (m_owners.isEmpty() && m_locked != AreaData::FREE) {
m_locked = AreaData::FREE;
return true;
}
return false;
}
bool AreaData::blankpostingAllowed() const
{
return m_blankpostingAllowed;
}
void AreaData::toggleBlankposting()
{
m_blankpostingAllowed = !m_blankpostingAllowed;
}
bool AreaData::isProtected() const
{
return m_isProtected;
}
AreaData::LockStatus AreaData::lockStatus() const
{
return m_locked;
}
bool AreaData::isjukeboxEnabled() const
{
return m_jukebox;
}
void AreaData::lock()
{
m_locked = LockStatus::LOCKED;
}
void AreaData::unlock()
{
m_locked = LockStatus::FREE;
}
void AreaData::spectatable()
{
m_locked = LockStatus::SPECTATABLE;
}
bool AreaData::invite(int f_clientId)
{
if (m_invited.contains(f_clientId)) {
return false;
}
m_invited.append(f_clientId);
return true;
}
bool AreaData::uninvite(int f_clientId)
{
if (!m_invited.contains(f_clientId)) {
return false;
}
m_invited.removeAll(f_clientId);
return true;
}
int AreaData::playerCount() const
{
return m_playerCount;
}
QList<QTimer *> AreaData::timers() const
{
return m_timers;
}
QString AreaData::name() const
{
return m_name;
}
int AreaData::index() const
{
return m_index;
}
QList<int> AreaData::charactersTaken() const
{
return m_charactersTaken;
}
bool AreaData::changeCharacter(int f_from, int f_to)
{
if (m_charactersTaken.contains(f_to)) {
return false;
}
if (f_to != -1) {
if (f_from != -1) {
m_charactersTaken.removeAll(f_from);
}
m_charactersTaken.append(f_to);
return true;
}
if (f_to == -1 && f_from != -1) {
m_charactersTaken.removeAll(f_from);
}
return false;
}
QList<AreaData::Evidence> AreaData::evidence() const
{
return m_evidence;
}
void AreaData::swapEvidence(int f_eviId1, int f_eviId2)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
//swapItemsAt does not exist in Qt older than 5.13
m_evidence.swap(f_eviId1, f_eviId2);
#else
m_evidence.swapItemsAt(f_eviId1, f_eviId2);
#endif
}
void AreaData::appendEvidence(const AreaData::Evidence &f_evi_r)
{
m_evidence.append(f_evi_r);
}
void AreaData::deleteEvidence(int f_eviId)
{
m_evidence.removeAt(f_eviId);
}
void AreaData::replaceEvidence(int f_eviId, const AreaData::Evidence &f_newEvi_r)
{
m_evidence.replace(f_eviId, f_newEvi_r);
}
AreaData::Status AreaData::status() const
{
return m_status;
}
bool AreaData::changeStatus(const QString &f_newStatus_r)
{
if (AreaData::map_statuses.contains(f_newStatus_r)) {
m_status = AreaData::map_statuses[f_newStatus_r];
return true;
}
return false;
}
QList<int> AreaData::invited() const
{
return m_invited;
}
bool AreaData::isMusicAllowed() const
{
return m_toggleMusic;
}
void AreaData::toggleMusic()
{
m_toggleMusic = !m_toggleMusic;
}
void AreaData::setEviMod(const EvidenceMod &f_eviMod_r)
{
m_eviMod = f_eviMod_r;
}
void AreaData::setTestimonyRecording(const TestimonyRecording &f_testimonyRecording_r)
{
m_testimonyRecording = f_testimonyRecording_r;
}
void AreaData::restartTestimony()
{
m_testimonyRecording = TestimonyRecording::PLAYBACK;
m_statement = 0;
}
void AreaData::clearTestimony()
{
m_testimonyRecording = AreaData::TestimonyRecording::STOPPED;
m_statement = -1;
m_testimony.clear();
}
bool AreaData::forceImmediate() const
{
return m_forceImmediate;
}
void AreaData::toggleImmediate()
{
m_forceImmediate = !m_forceImmediate;
}
const QStringList& AreaData::lastICMessage() const
{
return m_lastICMessage;
}
void AreaData::updateLastICMessage(const QStringList &f_lastMessage_r)
{
m_lastICMessage = f_lastMessage_r;
}
QStringList AreaData::judgelog() const
{
return m_judgelog;
}
void AreaData::appendJudgelog(const QString &f_newLog_r)
{
if (m_judgelog.size() == 10) {
m_judgelog.removeFirst();
}
m_judgelog.append(f_newLog_r);
}
int AreaData::statement() const
{
return m_statement;
}
void AreaData::recordStatement(const QStringList &f_newStatement_r)
{
++m_statement;
m_testimony.append(f_newStatement_r);
}
void AreaData::addStatement(int f_position, const QStringList &f_newStatement_r)
{
m_testimony.insert(f_position, f_newStatement_r);
}
void AreaData::replaceStatement(int f_position, const QStringList &f_newStatement_r)
{
m_testimony.replace(f_position, f_newStatement_r);
}
void AreaData::removeStatement(int f_position)
{
m_testimony.remove(f_position);
--m_statement;
}
QPair<QStringList, AreaData::TestimonyProgress> AreaData::jumpToStatement(int f_position)
{
m_statement = f_position;
if (m_statement > m_testimony.size() - 1) {
m_statement = 1;
return {m_testimony.at(m_statement), TestimonyProgress::LOOPED};
}
if (m_statement <= 1) {
m_statement = 1;
return {m_testimony.at(m_statement), TestimonyProgress::STAYED_AT_FIRST};
}
else {
return {m_testimony.at(m_statement), TestimonyProgress::OK};
}
}
const QVector<QStringList>& AreaData::testimony() const
{
return m_testimony;
}
AreaData::TestimonyRecording AreaData::testimonyRecording() const
{
return m_testimonyRecording;
}
AreaData::EvidenceMod AreaData::eviMod() const
{
return m_eviMod;
}
bool AreaData::addNotecard(const QString &f_owner_r, const QString &f_notecard_r)
{
m_notecards[f_owner_r] = f_notecard_r;
if (f_notecard_r.isNull()) {
m_notecards.remove(f_owner_r);
return false;
}
return true;
}
QStringList AreaData::getNotecards()
{
QMapIterator<QString, QString> l_noteIter(m_notecards);
QStringList l_notecards;
while (l_noteIter.hasNext()) {
l_noteIter.next();
l_notecards << l_noteIter.key() << ": " << l_noteIter.value() << "\n";
}
m_notecards.clear();
return l_notecards;
}
QString AreaData::musicPlayerBy() const
{
return m_musicPlayedBy;
}
void AreaData::setMusicPlayedBy(const QString& f_music_player)
{
m_musicPlayedBy = f_music_player;
}
void AreaData::changeMusic(const QString &f_source_r, const QString &f_newSong_r)
{
m_currentMusic = f_newSong_r;
m_musicPlayedBy = f_source_r;
}
QString AreaData::currentMusic() const
{
return m_currentMusic;
}
void AreaData::setCurrentMusic(QString f_current_song)
{
m_currentMusic = f_current_song;
}
int AreaData::proHP() const
{
return m_proHP;
}
void AreaData::changeHP(AreaData::Side f_side, int f_newHP)
{
if (f_side == Side::DEFENCE) {
m_defHP = std::min(std::max(0, f_newHP), 10);
} else if(f_side == Side::PROSECUTOR) {
m_proHP = std::min(std::max(0, f_newHP), 10);
}
}
int AreaData::defHP() const
{
return m_defHP;
}
QString AreaData::document() const
{
return m_document;
}
void AreaData::changeDoc(const QString &f_newDoc_r)
{
m_document = f_newDoc_r;
}
QString AreaData::areaMessage() const
{
return m_area_message;
}
bool AreaData::sendAreaMessageOnJoin() const
{
return m_send_area_message;
}
void AreaData::changeAreaMessage(const QString& f_newMessage_r)
{
if(f_newMessage_r.isEmpty())
m_area_message = "No area message set.";
else
m_area_message = f_newMessage_r;
}
bool AreaData::bgLocked() const
{
return m_bgLocked;
}
void AreaData::toggleBgLock()
{
m_bgLocked = !m_bgLocked;
}
bool AreaData::iniswapAllowed() const
{
return m_iniswapAllowed;
}
void AreaData::toggleIniswap()
{
m_iniswapAllowed = !m_iniswapAllowed;
}
bool AreaData::shownameAllowed() const
{
return m_shownameAllowed;
}
QString AreaData::background() const
{
return m_background;
}
void AreaData::setBackground(const QString f_background)
{
m_background = f_background;
}
bool AreaData::ignoreBgList()
{
return m_ignoreBgList;
}
void AreaData::toggleIgnoreBgList()
{
m_ignoreBgList = !m_ignoreBgList;
}
void AreaData::toggleAreaMessageJoin()
{
m_send_area_message = !m_send_area_message;
}
void AreaData::toggleJukebox()
{
m_jukebox = !m_jukebox;
if (!m_jukebox) {
m_jukebox_queue.clear();
m_jukebox_timer->stop();
}
}
QString AreaData::addJukeboxSong(QString f_song)
{
if(!m_jukebox_queue.contains(f_song)) {
//Retrieve song information.
QPair<QString,float> l_song = m_music_manager->songInformation(f_song, index());
if (l_song.second > 0) {
if (m_jukebox_queue.size() == 0) {
emit sendAreaPacket(AOPacket("MC",{l_song.first,QString::number(-1)}), index());
m_jukebox_timer->start(l_song.second * 1000);
setCurrentMusic(f_song);
setMusicPlayedBy("Jukebox");
}
m_jukebox_queue.append(f_song);
return "Song added to Jukebox.";
}
else {
return "Unable to add song. Duration shorter than 1.";
}
}
return "Unable to add song. Song already in Jukebox.";
}
QVector<int> AreaData::joinedIDs() const
{
return m_joined_ids;
}
void AreaData::switchJukeboxSong()
{
QString l_song_name;
if(m_jukebox_queue.size() == 1) {
l_song_name = m_jukebox_queue[0];
QPair<QString,float> l_song = m_music_manager->songInformation(l_song_name, index());
emit sendAreaPacket(AOPacket("MC",{l_song.first,"-1"}), m_index);
m_jukebox_timer->start(l_song.second * 1000);
}
else {
int l_random_index = QRandomGenerator::system()->bounded(m_jukebox_queue.size() -1);
l_song_name = m_jukebox_queue[l_random_index];
QPair<QString,float> l_song = m_music_manager->songInformation(l_song_name, index());
emit sendAreaPacket(AOPacket("MC",{l_song.first,"-1"}), m_index);
m_jukebox_timer->start(l_song.second * 1000);
m_jukebox_queue.remove(l_random_index);
m_jukebox_queue.squeeze();
}
setCurrentMusic(l_song_name);
setMusicPlayedBy("Jukebox");
}