Skip to content

Commit

Permalink
Exporting works
Browse files Browse the repository at this point in the history
  • Loading branch information
chesrowe committed May 31, 2023
1 parent 6abaedf commit 0ae6c40
Show file tree
Hide file tree
Showing 132 changed files with 8,302 additions and 19 deletions.
75 changes: 75 additions & 0 deletions MTG-Generator/MTG-Generator.yyp

Large diffs are not rendered by default.

51 changes: 44 additions & 7 deletions MTG-Generator/objects/obj_controller/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ magicBot.gatewayEventCallbacks[$ "INTERACTION_CREATE"] = function(){
switch(_eventData.type){
case DISCORD_INTERACTION_TYPE.applicationCommand:
switch(_eventData.data.name){
// /generate command for creating cards
case "generate":
var _interactionToken = _eventData.token;
var _userId = _eventData.member.user.id;
Expand All @@ -40,25 +41,60 @@ magicBot.gatewayEventCallbacks[$ "INTERACTION_CREATE"] = function(){
if (array_length(_eventData.data.options) > 3){
var _customTemperature = _eventData.data.options[3].value;
}else{
var _customTemperature = 1.0;
var _customTemperature = CHATGPT_TEMPERATURE_DEFAULT;
}

obj_controller.magicBot.interactionResponseSend(_eventData.id, _eventData.token, DISCORD_INTERACTION_CALLBACK_TYPE.channelMessageWithSource, "Card(s) generating (0 of " + string(_cardNumber) + ")");
var _newJob = new job(_cardTheme, _cardNumber, _interactionToken, _userId, _excludeThemeInImageGen);
var _newJob = new job(_cardTheme, _cardNumber, _interactionToken, UUID_generate(), _userId, _excludeThemeInImageGen, _customTemperature);
array_push(jobsInProgressArray, _newJob);
var _firstRequest = chatgpt_request_send(card_prompt(_cardTheme), _customTemperature);
array_push(_newJob.cardTextRequestIdArray, _firstRequest);
break;
}
break;

//Handling "export cards" and "generate more" buttons
case DISCORD_INTERACTION_TYPE.messageComponent:
switch(_eventData.data.custom_id){
//Export the cards as an .xml file compatible with Cockatrice
case "exportButton":
var _UUID = export_button_parse_UUID(_eventData.message.content);
var _theme = export_button_parse_theme(_eventData.message.content);
var _userId = _eventData.message.mentions[0].id;
obj_controller.magicBot.interactionResponseSend(_eventData.id, _eventData.token, DISCORD_INTERACTION_CALLBACK_TYPE.deferredUpdateMessage);
var _xmlBuffer = buffer_load("Card Exports/" + _UUID + ".xml");
var _xmlString = buffer_read(_xmlBuffer, buffer_string);
buffer_delete(_xmlBuffer);
var _xmlStruct = SnapFromXML(_xmlString);

//Find the names of all the cards in the set
var _xmlCardArray = _xmlStruct.children[0].children[1].children;
var _exportZipFile = zip_create();
var _i = 0;

repeat(array_length(_xmlCardArray)){
var _currentCardName = _xmlCardArray[_i].children[0].text;
zip_add_file(_exportZipFile, _currentCardName + ".png", "Completed Cards/" + _currentCardName + ".png", -1);
_i++;
}

zip_add_file(_exportZipFile, _theme + ".xml", "Card Exports/" + _UUID + ".xml", -1);

zip_save(_exportZipFile, "Card Exports/Zips/" + _UUID + ".zip");

var _cardArchive = new discordFileAttachment("Card Exports/Zips/" + _UUID + ".zip", _theme + ".zip", "Exported card data for Cockatrice");

magicBot.interactionResponseFollowUp(_eventData.token, "<@" + string(_userId) + ">\nExported data data.", -1, -1, -1, -1, [_cardArchive]);
break;
}
break;
}
}

//magicBot.gatewayEventCallbacks[$ "MESSAGE_CREATE"] = function(){
// var _event = discord_gateWay_event_parse();
// var _eventData = _event.d;
// //show_debug_message("New message: " + string(_eventData.content));
//}
magicBot.gatewayEventCallbacks[$ "MESSAGE_CREATE"] = function(){
var _event = discord_gateWay_event_parse();
var _testing = 0;
}

//Add card generation command to bot
var _optionTheme = new discordCommandOption(DISCORD_COMMAND_OPTION_TYPE.string, "theme", "The theme that the cards will be generated based on.", true);
Expand All @@ -74,5 +110,6 @@ magicBot.guildCommandCreate(global.config.serverId, _createCardCommand, function
//Card gen jobs
jobsInProgressArray = [];
jobsWaitingToBeDrawnAndSentArray = [];
jobsFinished = [];


7 changes: 6 additions & 1 deletion MTG-Generator/objects/obj_controller/Draw_64.gml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ repeat(array_length(jobsWaitingToBeDrawnAndSentArray)){
//In case the user inputs a stupidly long theme
var _trimmedTheme = string_copy(_currentJob.theme, 0, 1500);

magicBot.interactionResponseFollowUp(_currentJob.interactionToken, "<@" + string(_currentJob.userId) + ">\nCards completed!\nTheme: " + string(_trimmedTheme), _completedCardsEditCallback, [_actionRow], -1, -1, _completedCardImages);
magicBot.interactionResponseFollowUp(_currentJob.interactionToken, "<@" + string(_currentJob.userId) + ">\nCards completed!\nTheme: " + string(_trimmedTheme) + "\nSet ID: " + _currentJob.UUID, _completedCardsEditCallback, [_actionRow], -1, -1, _completedCardImages);

//Add this job to the finished jobs array and export the card data
array_push(jobsFinished, _currentJob);
export_to_cockatrice(_currentJob.cardTextArray, "Card Exports/" + _currentJob.UUID + ".xml");

_i++;
}

Expand Down
5 changes: 3 additions & 2 deletions MTG-Generator/objects/obj_controller/Other_62.gml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var _status = async_load[? "status"];
var _i = 0;
var _markedForDeletion = [];

// For each active job
repeat(array_length(jobsInProgressArray)){
var _currentJob = jobsInProgressArray[_i];

Expand Down Expand Up @@ -41,12 +42,12 @@ repeat(array_length(jobsInProgressArray)){
array_push(_currentJob.imageRequestArray, _imageRequestStruct);
}else{
show_debug_message("Text returned was not vaild.");
_currentJob.cardTextRequestIdArray[_j] = chatgpt_request_send(card_prompt(_currentJob.theme, _currentJob.cardTextArray));
_currentJob.cardTextRequestIdArray[_j] = chatgpt_request_send(card_prompt(_currentJob.theme, _currentJob.cardTextArray), _currentJob.temperature);
}
}catch(_error){
show_debug_message("Failed to generate text");
discord_error(_error);
_currentJob.cardTextRequestIdArray[_j] = chatgpt_request_send(card_prompt(_currentJob.theme, _currentJob.cardTextArray));
_currentJob.cardTextRequestIdArray[_j] = chatgpt_request_send(card_prompt(_currentJob.theme, _currentJob.cardTextArray), _currentJob.temperature);
}

break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// Config struct should be in this format:
///
/// {
/// columnTitle: {
/// ignore: <true> or <false>,
/// numeric: <true> or <false>,
/// },
/// ...
/// }
///
/// @param inputArray 2D array to convert. Array should be row-major
/// @param [configStruct] See above
///
/// @jujuadams 2022-10-30

function Snap2DArrayToStructArray(_inputArray, _configStruct = {})
{
var _outputArray = array_create(array_length(_inputArray)-1);
var _headerArray = _inputArray[0];

var _ignore = false;
var _numeric = false;

var _i = 1;
repeat(array_length(_inputArray)-1)
{
var _struct = {};
_outputArray[@ _i-1] = _struct;

var _subArray = _inputArray[_i];
var _j = 0;
repeat(array_length(_subArray))
{
var _value = _subArray[_j];
var _variableName = _headerArray[_j];

var _config = _configStruct[$ _variableName];
if (is_struct(_config))
{
_ignore = _config[$ "ignore" ] ?? false;
_numeric = _config[$ "numeric"] ?? false;
}

if (_numeric)
{
_numeric = false;
try { _value = real(_value); } catch(_error) {}
}

if (_ignore)
{
_ignore = false;
}
else
{
_struct[$ _variableName] = _value;
}

++_j;
}

++_i;
}

return _outputArray;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// @param buffer
/// @param offset
///
/// @jujuadams 2022-10-30

function SnapBufferRead2DArray(_buffer, _inOffset)
{
if (_inOffset != undefined)
{
var _oldOffset = buffer_tell(_buffer);
buffer_seek(_buffer, buffer_seek_start, _inOffset);
}

var _datatype = buffer_read(_buffer, buffer_u8);
var _width = buffer_read(_buffer, buffer_u32);
var _height = buffer_read(_buffer, buffer_u32);

var _array = array_create(_width);

var _x = 0;
repeat(_width)
{
var _sub_array = array_create(_height);
_array[@ _x] = _sub_array;

var _y = 0;
repeat(_height)
{
_sub_array[@ _y] = buffer_read(_buffer, _datatype);
++_y;
}

++_x;
}

if (_inOffset != undefined) buffer_seek(_buffer, buffer_seek_start, _oldOffset);

return _array;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions MTG-Generator/scripts/SnapBufferRead2DArray/snap_to_grid.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions MTG-Generator/scripts/SnapBufferReadBOM/SnapBufferReadBOM.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// Attempts to read a UTF-8 byte order mark from a buffer, and returns <true> if a BOM is found
///
/// @param buffer Buffer to try to read the byte order mark from
///
/// @jujuadams 2022-10-30

function SnapBufferReadBOM(_buffer)
{
var _tell = buffer_tell(_buffer);
if ((buffer_get_size(_buffer) >= 3)
&& (buffer_peek(_buffer, _tell, buffer_u8) == 0xEF)
&& (buffer_peek(_buffer, _tell+1, buffer_u8) == 0xBB)
&& (buffer_peek(_buffer, _tell+2, buffer_u8) == 0xBF))
{
buffer_seek(_buffer, buffer_seek_relative, 3);
return true;
}

return false;
}
11 changes: 11 additions & 0 deletions MTG-Generator/scripts/SnapBufferReadBOM/SnapBufferReadBOM.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0ae6c40

Please sign in to comment.