@@ -1105,66 +1105,6 @@ API.functions["use_consumable"] = function(args)
11051105 return
11061106 end
11071107
1108- -- If cards parameter is provided, handle card selection
1109- if args .cards then
1110- -- Validate current game state is SELECTING_HAND when cards are provided
1111- if G .STATE ~= G .STATES .SELECTING_HAND then
1112- API .send_error_response (
1113- " Cannot use consumable with cards when there are no cards to select. Expects SELECTING_HAND state." ,
1114- ERROR_CODES .INVALID_GAME_STATE ,
1115- { current_state = G .STATE , required_state = G .STATES .SELECTING_HAND }
1116- )
1117- return
1118- end
1119-
1120- -- Validate cards is an array
1121- if type (args .cards ) ~= " table" then
1122- API .send_error_response (
1123- " Invalid parameter type for cards. Expected array, got " .. tostring (type (args .cards )),
1124- ERROR_CODES .INVALID_PARAMETER ,
1125- { parameter = " cards" , expected_type = " array" }
1126- )
1127- return
1128- end
1129-
1130- -- Validate number of cards is between 1 and 5 (inclusive) for consistency with play_hand_or_discard
1131- if # args .cards < 1 or # args .cards > 3 then
1132- API .send_error_response (
1133- " Invalid number of cards. Expected 1-3, got " .. tostring (# args .cards ),
1134- ERROR_CODES .PARAMETER_OUT_OF_RANGE ,
1135- { cards_count = # args .cards , valid_range = " 1-3" }
1136- )
1137- return
1138- end
1139-
1140- -- Convert from 0-based to 1-based indexing
1141- for i , card_index in ipairs (args .cards ) do
1142- args .cards [i ] = card_index + 1
1143- end
1144-
1145- -- Check that all cards exist and are selectable
1146- for _ , card_index in ipairs (args .cards ) do
1147- if not G .hand or not G .hand .cards or not G .hand .cards [card_index ] then
1148- API .send_error_response (
1149- " Invalid card index" ,
1150- ERROR_CODES .INVALID_CARD_INDEX ,
1151- { card_index = card_index - 1 , hand_size = G .hand and G .hand .cards and # G .hand .cards or 0 }
1152- )
1153- return
1154- end
1155- end
1156-
1157- -- Clear any existing highlights before selecting new cards
1158- if G .hand then
1159- G .hand :unhighlight_all ()
1160- end
1161-
1162- -- Select cards for the consumable to target
1163- for _ , card_index in ipairs (args .cards ) do
1164- G .hand .cards [card_index ]:click ()
1165- end
1166- end
1167-
11681108 -- Validate that consumables exist
11691109 if not G .consumeables or not G .consumeables .cards or # G .consumeables .cards == 0 then
11701110 API .send_error_response (
@@ -1215,13 +1155,107 @@ API.functions["use_consumable"] = function(args)
12151155 return
12161156 end
12171157
1158+ -- Get consumable's card requirements
1159+ local max_cards = consumable_card .ability .consumeable .max_highlighted
1160+ local min_cards = consumable_card .ability .consumeable .min_highlighted or 1
1161+ local consumable_name = consumable_card .ability .name or " Unknown"
1162+ local required_cards = max_cards ~= nil
1163+
1164+ -- Validate cards parameter type if provided
1165+ if args .cards ~= nil then
1166+ if type (args .cards ) ~= " table" then
1167+ API .send_error_response (
1168+ " Invalid parameter type for cards. Expected array, got " .. tostring (type (args .cards )),
1169+ ERROR_CODES .INVALID_PARAMETER ,
1170+ { parameter = " cards" , expected_type = " array" }
1171+ )
1172+ return
1173+ end
1174+
1175+ -- Validate all elements are numbers
1176+ for i , card_index in ipairs (args .cards ) do
1177+ if type (card_index ) ~= " number" then
1178+ API .send_error_response (
1179+ " Invalid card index type. Expected number, got " .. tostring (type (card_index )),
1180+ ERROR_CODES .INVALID_PARAMETER ,
1181+ { index = i - 1 , value_type = type (card_index ) }
1182+ )
1183+ return
1184+ end
1185+ end
1186+ end
1187+
1188+ -- The consumable does not require any card selection
1189+ if not required_cards and args .cards then
1190+ if # args .cards > 0 then
1191+ API .send_error_response (
1192+ " The selected consumable does not require card selection. Cards array must be empty or no cards array at all." ,
1193+ ERROR_CODES .INVALID_PARAMETER ,
1194+ { consumable_name = consumable_name }
1195+ )
1196+ return
1197+ end
1198+ -- If cards=[] (empty), that's fine, just skip the card selection logic
1199+ end
1200+
1201+ if required_cards then
1202+ if G .STATE ~= G .STATES .SELECTING_HAND then
1203+ API .send_error_response (
1204+ " Cannot use consumable with cards when there are no cards to select. Expects SELECTING_HAND state." ,
1205+ ERROR_CODES .INVALID_GAME_STATE ,
1206+ { current_state = G .STATE , required_state = G .STATES .SELECTING_HAND }
1207+ )
1208+ return
1209+ end
1210+
1211+ local num_cards = args .cards == nil and 0 or # args .cards
1212+ if num_cards < min_cards or num_cards > max_cards then
1213+ local range_msg = min_cards == max_cards and (" exactly " .. min_cards ) or (min_cards .. " -" .. max_cards )
1214+ API .send_error_response (
1215+ " Invalid number of cards for "
1216+ .. consumable_name
1217+ .. " . Expected "
1218+ .. range_msg
1219+ .. " , got "
1220+ .. tostring (num_cards ),
1221+ ERROR_CODES .PARAMETER_OUT_OF_RANGE ,
1222+ { cards_count = num_cards , min_cards = min_cards , max_cards = max_cards , consumable_name = consumable_name }
1223+ )
1224+ return
1225+ end
1226+
1227+ -- Convert from 0-based to 1-based indexing
1228+ for i , card_index in ipairs (args .cards ) do
1229+ args .cards [i ] = card_index + 1
1230+ end
1231+
1232+ -- Check that all cards exist and are selectable
1233+ for _ , card_index in ipairs (args .cards ) do
1234+ if not G .hand or not G .hand .cards or not G .hand .cards [card_index ] then
1235+ API .send_error_response (
1236+ " Invalid card index" ,
1237+ ERROR_CODES .INVALID_CARD_INDEX ,
1238+ { card_index = card_index - 1 , hand_size = G .hand and G .hand .cards and # G .hand .cards or 0 }
1239+ )
1240+ return
1241+ end
1242+ end
1243+
1244+ -- Clear any existing highlights before selecting new cards
1245+ if G .hand then
1246+ G .hand :unhighlight_all ()
1247+ end
1248+
1249+ -- Select cards for the consumable to target
1250+ for _ , card_index in ipairs (args .cards ) do
1251+ G .hand .cards [card_index ]:click ()
1252+ end
1253+ end
1254+
12181255 -- Check if the consumable can be used
12191256 if not consumable_card :can_use_consumeable () then
1220- API .send_error_response (
1221- " Consumable cannot be used at this time. Some consumables require cards to be selected first." ,
1222- ERROR_CODES .INVALID_ACTION ,
1223- { index = args .index }
1224- )
1257+ local error_msg = " Consumable cannot be used for unknown reason."
1258+ API .send_error_response (error_msg , ERROR_CODES .INVALID_ACTION , {})
12251259 return
12261260 end
12271261
0 commit comments