Skip to content

Commit

Permalink
First pass at getting Playlists working again
Browse files Browse the repository at this point in the history
  • Loading branch information
opsnlops committed Jul 14, 2024
1 parent cf70aa9 commit c132406
Show file tree
Hide file tree
Showing 20 changed files with 977 additions and 641 deletions.
21 changes: 14 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25)

project(creature-server
VERSION "2.1.8"
VERSION "2.2.0"
DESCRIPTION "Server for April's Creatures"
HOMEPAGE_URL https://github.com/opsnlops/creature-server
LANGUAGES C CXX)
Expand Down Expand Up @@ -186,6 +186,10 @@ add_executable(creature-server
src/model/LogItem.cpp
src/model/Notice.h
src/model/Notice.cpp
src/model/Playlist.h
src/model/Playlist.cpp
src/model/PlaylistItem.h
src/model/PlaylistItem.cpp
src/model/StreamFrame.cpp
src/model/StreamFrame.h
src/model/Sound.h
Expand Down Expand Up @@ -244,22 +248,25 @@ add_executable(creature-server
src/server/ws/messaging/StreamFrameHandler.h
src/server/ws/messaging/StreamFrameHandler.cpp

src/server/ws/service/AnimationService.cpp
src/server/ws/service/AnimationService.h
src/server/ws/service/CreatureService.cpp
src/server/ws/service/AnimationService.cpp
src/server/ws/service/CreatureService.h
src/server/ws/service/CreatureService.cpp
src/server/ws/service/MetricsService.h
src/server/ws/service/MetricsService.cpp
src/server/ws/service/MetricsService.cpp
src/server/ws/service/SoundService.cpp
src/server/ws/service/PlaylistService.h
src/server/ws/service/PlaylistService.cpp
src/server/ws/service/SoundService.h
src/server/ws/service/SoundService.cpp
src/server/ws/service/VoiceService.h
src/server/ws/service/VoiceService.cpp

src/server/ws/websocket/ClientCafe.h
src/server/ws/websocket/ClientConnection.h
src/server/ws/websocket/ClientCafe.cpp
src/server/ws/websocket/ClientConnection.cpp
src/server/ws/messaging/NoticeMessageCommandDTO.h
src/server/ws/service/VoiceService.h
src/server/ws/service/VoiceService.cpp

src/server/ws/messaging/SensorReportHandler.h
src/server/ws/messaging/SensorReportHandler.cpp

Expand Down
4 changes: 1 addition & 3 deletions src/model/Animation.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


#include <vector>
#include <string>
#include <vector>


#include "model/Animation.h"
Expand Down Expand Up @@ -32,7 +31,6 @@ namespace creatures {
animationDto->metadata = convertToDto(animation.metadata);
animationDto->tracks = oatpp::Vector<oatpp::Object<TrackDto>>::createShared();


for (const auto &frame : animation.tracks) {
animationDto->tracks->emplace_back(convertToDto(frame));
}
Expand Down
52 changes: 52 additions & 0 deletions src/model/Playlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#include <vector>
#include <string>

#include <oatpp/core/Types.hpp>

#include "model/Playlist.h"
#include "model/PlaylistItem.h"

namespace creatures {


std::vector<std::string> playlist_required_fields = {
"id", "number_of_items", "items"
};

std::vector<std::string> playlistitems_required_fields = {
"animation_id", "weight"
};


oatpp::Object<PlaylistDto> convertToDto(const Playlist &playlist) {
auto playlistDto = PlaylistDto::createShared();
playlistDto->id = playlist.id;
playlistDto->name = playlist.name;
playlistDto->number_of_items = playlist.number_of_items;
playlistDto->items = oatpp::List<oatpp::Object<PlaylistItemDto>>::createShared();

// Now do the items
for (const auto &item: playlist.items) {
playlistDto->items->emplace_back(convertToDto(item));
}

return playlistDto;
}

Playlist convertFromDto(const std::shared_ptr<PlaylistDto> &playlistDto) {
Playlist playlist;
playlist.id = playlistDto->id;
playlist.name = playlistDto->name;
playlist.number_of_items = playlistDto->number_of_items;

// Ensure the list is initialized before iterating
playlist.items = std::vector<PlaylistItem>();
for (const auto &listItem: *playlistDto->items.getPtr()) {
playlist.items.push_back(convertFromDto(listItem.getPtr()));
}

return playlist;
}

}
64 changes: 64 additions & 0 deletions src/model/Playlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

#pragma once

#include <vector>
#include <string>

#include <oatpp/core/Types.hpp>
#include <oatpp/core/macro/codegen.hpp>

#include "server/namespace-stuffs.h"

#include "model/PlaylistItem.h"


namespace creatures {

struct Playlist {
playlistId_t id;
std::string name;
std::vector<PlaylistItem> items;
uint32_t number_of_items;
};


#include OATPP_CODEGEN_BEGIN(DTO)

class PlaylistDto : public oatpp::DTO {

DTO_INIT(PlaylistDto, DTO /* extends */)

DTO_FIELD_INFO(id) {
info->description = "The ID of this playlist in the form of an UUID";
}

DTO_FIELD(String, id);

DTO_FIELD_INFO(name) {
info->description = "The name of this playlist in the UI";
}

DTO_FIELD(String, name);

DTO_FIELD_INFO(items) {
info->description = "The items in the playlist";
}

DTO_FIELD(List < Object < PlaylistItemDto >>, items);

DTO_FIELD_INFO(number_of_items) {
info->description = "The number of items in this playlist";
}

DTO_FIELD(UInt32, number_of_items);


};

#include OATPP_CODEGEN_END(DTO)

oatpp::Object<PlaylistDto> convertToDto(const Playlist &playlist);

Playlist convertFromDto(const std::shared_ptr<PlaylistDto> &playlistDto);

}
37 changes: 37 additions & 0 deletions src/model/PlaylistItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


#include <string>

#include <spdlog/spdlog.h>
#include <oatpp/core/Types.hpp>

#include "model/PlaylistItem.h"

namespace creatures {

std::vector<std::string> playlistitem_required_fields = {
"animation_id", "weight"
};


// Convert a DTO to the real thing
PlaylistItem convertFromDto(const std::shared_ptr<PlaylistItemDto> &playlistItemDto) {

trace("Converting PlaylistItemDto to PlaylistItem");

PlaylistItem playlistItem;
playlistItem.animation_id = playlistItemDto->animation_id;
playlistItem.weight = playlistItemDto->weight;
trace("animation_id: {}, weight: {}", playlistItem.animation_id, playlistItem.weight);

return playlistItem;
}

oatpp::Object<PlaylistItemDto> convertToDto(const PlaylistItem &playlistItem) {
auto playlistItemDto = PlaylistItemDto::createShared();
playlistItemDto->animation_id = playlistItem.animation_id;
playlistItemDto->weight = playlistItem.weight;
return playlistItemDto;
}

}
58 changes: 58 additions & 0 deletions src/model/PlaylistItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#pragma once

#include <string>
#include <vector>

#include <oatpp/core/Types.hpp>
#include <oatpp/core/macro/codegen.hpp>

#include "server/namespace-stuffs.h"


namespace creatures {

/*
* This is one item in a playlist
*/


struct PlaylistItem {

/**
* The ID of the playlist
*/
animationId_t animation_id;

/**
* The weight of the item
*/
uint32_t weight;

};


#include OATPP_CODEGEN_BEGIN(DTO)

class PlaylistItemDto : public oatpp::DTO {

DTO_INIT(PlaylistItemDto, DTO /* extends */)

DTO_FIELD_INFO(animation_id) {
info->description = "The ID of the animation for this entry";
}
DTO_FIELD(String, animation_id);

DTO_FIELD_INFO(weight) {
info->description = "This item's weight";
}
DTO_FIELD(UInt32 , weight);

};

#include OATPP_CODEGEN_END(DTO)

oatpp::Object<PlaylistItemDto> convertToDto(const PlaylistItem &playlistItem);
creatures::PlaylistItem convertFromDto(const std::shared_ptr<PlaylistItemDto> &playlistItemDto);

}
22 changes: 22 additions & 0 deletions src/server/creature/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace creatures {
extern std::vector<std::string> creature_required_top_level_fields;
extern std::vector<std::string> creature_required_input_fields;

extern std::vector<std::string> playlist_required_fields;
extern std::vector<std::string> playlistitems_required_fields;


Result<creatures::Creature> Database::creatureFromJson(json creatureJson) {

debug("attempting to create a creature from JSON via creatureFromJson()");
Expand Down Expand Up @@ -189,4 +193,22 @@ namespace creatures {

return Result<bool>{true};
}

Result<bool> Database::validatePlaylistJson(const nlohmann::json &json) {

auto topLevelOkay = has_required_fields(json, creatures::playlist_required_fields);
if(!topLevelOkay.isSuccess()) {
return topLevelOkay;
}

// Confirm that the items are valid
for( const auto& item : json["items"]) {
auto itemOkay = has_required_fields(item, playlistitems_required_fields);
if(!itemOkay.isSuccess()) {
return itemOkay;
}
}

return Result<bool>{true};
}
}
25 changes: 15 additions & 10 deletions src/server/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using json = nlohmann::json;
#include "model/Animation.h"
#include "model/AnimationMetadata.h"
#include "model/Creature.h"
#include "model/Playlist.h"
#include "model/Track.h"
#include "model/SortBy.h"
#include "server/namespace-stuffs.h"
Expand Down Expand Up @@ -80,6 +81,14 @@ namespace creatures {
*/
static Result<bool> validateAnimationJson(const nlohmann::json& json);

/**
* Validates that the JSON for an Playlist contains the fields we expect.
*
* @param json the JSON to validate
* @return true if good, or ServerError if not
*/
static Result<bool> validatePlaylistJson(const nlohmann::json& json);


/**
* Helper function that checks if a JSON object has all of the required fields. Used
Expand Down Expand Up @@ -108,11 +117,10 @@ namespace creatures {


// Playlist stuff
// grpc::Status createPlaylist(const Playlist *playlist, DatabaseInfo *reply);
// grpc::Status listPlaylists(const PlaylistFilter *filter, ListPlaylistsResponse *animationList);
// void getPlaylist(const PlaylistIdentifier *playlistIdentifier, Playlist *playlist);
// void updatePlaylist(const Playlist *playlist);

Result<json> getPlaylistJson(playlistId_t playlistId);
Result<std::vector<creatures::Playlist>> getAllPlaylists();
Result<creatures::Playlist> getPlaylist(const playlistId_t& playlistId);
Result<creatures::Playlist> upsertPlaylist(const std::string& playlistJson);

/**
* Request that the database perform a health check
Expand Down Expand Up @@ -155,15 +163,12 @@ namespace creatures {
/*
* Playlists
*/
// static bsoncxx::document::value playlistToBson(const Playlist *playlist, bsoncxx::oid playlistId);
// static int32_t playlistItemsToBson(bsoncxx::builder::stream::document &doc, const server::Playlist *playlist);
// static void bsonToPlaylist(const bsoncxx::document::view &doc, Playlist *playlist);
// static void bsonToPlaylistItems(const bsoncxx::document::view &doc, server::Playlist *playlist);
static Result<creatures::Playlist> playlistFromJson(json playlistJson);
static Result<creatures::PlaylistItem> playlistItemFromJson(json playlistItemJson);

// Start out thinking that the server is pingable
std::atomic<bool> serverPingable{true};


};


Expand Down
3 changes: 2 additions & 1 deletion src/server/namespace-stuffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ using universe_t = uint32_t;
using framenum_t = uint64_t;

using creatureId_t = std::string;
using animationId_t = std::string;
using animationId_t = std::string;
using playlistId_t = std::string;
Loading

0 comments on commit c132406

Please sign in to comment.