Skip to content

Instantly share code, notes, and snippets.

@harakka
Created November 25, 2020 11:01
Show Gist options
  • Save harakka/6a00455fd22e0e42a1729a400f600dda to your computer and use it in GitHub Desktop.
Save harakka/6a00455fd22e0e42a1729a400f600dda to your computer and use it in GitHub Desktop.
<?php
/**
* @package ARPSMissions
*/
/*
Plugin Name: ARPSMissions
Description: Creates and manages ArmA2-mission related posts, based on files with the Folk naming scheme.
Version: 1.0.0
Author: Antti 'harakka' Riikonen
License: Modified BSD license
*/
/*
Copyright (c) 2011-2012 Antti 'harakka' Riikonen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
register_activation_hook( __FILE__, array('ARPSMissions', 'plugin_activate'));
add_action('admin_menu', array('ARPSMissions', 'plugin_menu'));
add_action('init', array('ARPSMissions', 'register_mission_type'));
define('MISSION_LIST_FILE', plugin_dir_path(__FILE__) . 'missionlist.txt');
class ARPSMissions {
static function plugin_menu() {
add_management_page('ARPSMissions', 'ARPSMissions', 'manage_options', 'arpsmissions', 'ARPSMissions::plugin_options');
}
static function callback_add_meta() {
/*
add_meta_box('myplugin_sectionid', 'My Post Section TitleFooobarbaz', 'myplugin_inner_custom_box', 'post');
add_meta_box('myplugin_sectionid', 'My Post Section TitleFooobarbaz', 'myplugin_inner_custom_box', 'page');
*/
}
// TODO: implement this when you implement callback_add_meta
function myplugin_inner_custom_box ($post) {}
static function register_mission_type() {
register_post_type('arps_mission',
array(
'label' => 'Missions',
'labels' => array(
'name' => 'Missions',
'singular_name' => 'Mission'
),
'description' => 'ARMA mission',
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
//'rewrite' => array('slug' => 'missions'),
'supports' => array(
'title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields'
),
// 'register_meta_box_cb' => array('ARPSMissions', 'callback_add_meta')
'taxonomies' => array('category', 'post_tag')
)
);
}
static function plugin_options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
?>
<div class="wrap">
<div id="icon-tools" class="icon32"></div><h2>ARPS Mission Manager</h2>
<p>You can do all sorts of neat things here in the future, maybe. Right now you can only press the button to generate mission posts. What more do you want? Do I look like some kind of wizard to you?</p>
<?php
// The 'generate' button was pressed
if (isset($_POST['generate_posts_submit_hidden']) && $_POST['generate_posts_submit_hidden'] == 'Y') {
ARPSMissions::plugin_generate_posts();
echo '<h3>Mission import done!</h3>';
echo '</div>';
} else {
?>
<form name="generate_posts" method="post" action="">
<input type="hidden" name="generate_posts_submit_hidden" value="Y">
<p class="submit">
<label for="Submit">Generate mission posts from external source</label>
<input type="submit" name="Submit" class="button-primary" value="Generate" />
</p>
</form>
</div>
<?php
}
}
static function plugin_activate() {
// TODO: create an arps_mission post type with relevant info.
/*if (!post_type_exists('arps_mission')) {
register_post_type('arps_mission'), ...
}*/
}
static function plugin_generate_posts() {
$results = array();
$misses = array();
// Matches form [typeplayernumber]name_using_underscores_vversionnumber_requiredgame.island
// Actually matches a lot of other stuff too, because I mede it to match anything in my
// mission cache, and there was a lot of really bad stuff in there...
$arps_preg = "#^(zz_)?\[?(?P<gamemode>co|otea|adv|a&d|tvt|rf|rc)(-|_|\h)?(?P<playercount>[0-9]*)\]?(-|_|\h)?(?P<name>(?P<firstword>[[:alnum:]]*)(_(?P<lastword>[[:alnum:]]*))*(?=(_v)[\d]+))_v(?P<version>[\d]+[[:alnum:]]*([-|_][\d]+[[:alnum:]]*)?)?(_(?P<reqgame>a2|oa|co))?\.(?P<map>[[:alnum:]_]*)#i";
$arps_noversion_preg = "#^(zz_)?\[?(?P<gamemode>co|otea|adv|a&d|tvt|rf|rc)(-|_|\h)?(?P<playercount>[0-9]*)\]?(-|_|\h)?(?P<name>[[:alnum:]_]*)\.(?P<map>[[:alnum:]_]*)#i";
// Matches form fkoa_co24_victory_rose_v3.island
$folk_preg = "#^(?P<folk>fk)(?P<reqgame>(co|oa|a2))_(?P<gamemode>co|adv|misc)(?P<playercount>\d+)_(?P<name>(?P<firstword>[[:alnum:]]*)(_(?P<lastword>[[:alnum:]]*))*(?=_v[\d]+))(_(?P<version>(v[\d]+)?(-[\d]+)?))?\.(?P<map>[[:alnum:]_]*)#i";
$folk_noversion_preg = "#^(?P<folk>fk)(?P<reqgame>(co|oa|a2))_(?P<gamemode>co|adv|misc)(?P<playercount>\d+)_(?P<name>[[:alnum:]_]*)\.(?P<map>[[:alnum:]_]*)#i";
foreach (file(MISSION_LIST_FILE) as $line) {
$line = trim(str_ireplace('.pbo', '', $line));
if (preg_match($arps_preg, $line, $matches) != 0) {
$results[$line] = $matches;
}
elseif (preg_match($arps_noversion_preg, $line, $matches) != 0) {
$results[$line] = $matches;
}
elseif (preg_match($folk_preg, $line, $matches) != 0) {
$results[$line] = $matches;
}
elseif (preg_match($folk_noversion_preg, $line, $matches) != 0) {
$results[$line] = $matches;
}
else {
$misses[] = $line;
}
}
$all_missions = array_merge(get_posts(array('numberposts' => 500, 'post_type' => 'arps_mission', 'post_status' => 'publish')), get_posts(array('numberposts' => 500, 'post_type' => 'arps_mission', 'post_status' => 'pending')));
$mission_filenames = array();
// This bit hits the database harder than needed, could be replaced by a custom SQL query, or maybe get_posts
// OTOH it's only used when the generate button is pressed...
foreach ($all_missions as $mission) {
$mission_filenames[] = get_post_meta($mission->ID, 'filename', true);
}
$new_count = 0;
$old_count = 0;
echo '<pre>';
foreach ($results as $name => $mission) {
//print_r($mission);
if (!in_array($name, $mission_filenames)) {
$post = array(
'ping_status' => 'closed',
'post_name' => $name,
'post_content' => 'To do',
'post_status' => 'pending',
'post_title' => '',
'post_type' => 'arps_mission',
'tags_input' => ucfirst(strtolower($mission['map']))
);
if ($mission['playercount'] >= 16)
$post['tags_input'] = $post['tags_input'] . ", 4-16 Players";
if ($mission['playercount'] >= 24)
$post['tags_input'] = $post['tags_input'] . ", 17-24 Players";
if ($mission['playercount'] >= 32)
$post['tags_input'] = $post['tags_input'] . ", 25-32 Players";
if ($mission['playercount'] >= 48)
$post['tags_input'] = $post['tags_input'] . ", 33-48 Players";
if ($mission['playercount'] >= 49)
$post['tags_input'] = $post['tags_input'] . ", 49 Or more players";
switch ($mission['gamemode']) {
case 'co':
case 'coop':
$post['tags_input'] = $post['tags_input'] . ", Coop";
$title_gametype = 'CO';
break;
case 'adv':
case 'tvt':
$post['tags_input'] = $post['tags_input'] . ", Adversarial";
$title_gametype = 'ADV';
break;
}
if ($mission['folk']) {
$post['tags_input'] = $post['tags_input'] . ", Folk";
}
switch ($mission['reqgame']) {
case 'co':
$post['tags_input'] = $post['tags_input'] . ", ReqCO";
break;
case 'a2':
$post['tags_input'] = $post['tags_input'] . ", ReqA2";
break;
case 'oa':
$post['tags_input'] = $post['tags_input'] . ", ReqOA";
break;
}
$post['post_title'] = $title_gametype . $mission['playercount'] . ' - ' . ucwords(strtolower(str_ireplace('_', ' ', $mission['name']))) . ' (' . ucfirst(strtolower($mission['map'])) . ')';
print_r($post);
//$new_mission_id = wp_insert_post($post);
if ($new_mission_id != 0) {
add_post_meta($new_mission_id, 'filename', $name, false);
$new_count++;
}
} else {
$old_count++;
}
}
echo '</pre>';
echo "<h3>New count: {$new_count}, old count: {$old_count}</h3>";
echo '<p>';
foreach ($misses as $miss) { echo "Unparseable: <b>{$miss}</b></br>";}
echo '<h3>For debugging purposes, all parseable mission names:</h3>';
echo '<table>';
echo '<tr><th>Full name</th><th>Parsed name</th><th>The good name</th><th>Game mode</th><th>Playercount</th><th>Version</th><th>Map</th><th>Required game</th></tr>';
foreach ($results as $name => $mission) {
echo "<tr><td>{$name}</td><td>{$mission[0]}</td><td>{$mission['name']}</td><td>{$mission['gamemode']}</td><td>{$mission['playercount']}</td><td>{$mission['version']}</td><td>{$mission['map']}</td><td>{$mission['reqgame']}</td></tr>";
}
echo '</table>';
echo '</p>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment