Jump to content

Private Lobby Api Examples


3 replies to this topic

#1 Congress Hating Ur Freedoms

    Member

  • Pip
  • The 1 Percent
  • The 1 Percent
  • 10 posts

Posted 30 March 2018 - 08:03 PM

Hi all. I'm pretty excited about the Match API tool that came out. Not only does it make NBT tournament extra amazing, but it can also help your teams up their game in the comp scene.

I haven't seen any talk on the forums, and even privately it doesn't seem to be too super discussed, so I figured I'd drop some ruby scripts I've been tinkering with over the past while and see if anyone had any other reports they're generating from private lobby data.

Here's a basic tech stack for working with this kinda technical stuff:

- Git (a way to share and archive code)
- A decent terminal (git-scm is bundled with git bash luckily)
- Ruby (a programming language that doesn't waterboard you with tons of boilerplate typing)

So with those tools properly installed on even a windows machine, you can right click on a project folder, choose 'git bash' and then you'll bring up a terminal that's almost like a linux terminal. In that terminal you can paste the commands to pull down my codes, and run them to check the outputs. Each bullet point is worth it's own google if you're new to this kindsa stuff and would like to figure out what I'm talking about.

# PS, pound signs are comments and are ignorned
# when you paste them into terminals
# get the codes
git clone https://github.com/TheNotary/smurfy_api.git
 
# move into the new directory with terminal
cd smurfty_api
 
# put in an example match_id into the matches.txt file
echo "312013716556150" > matches.txt
 
# Execute the API lookup and report generation code
mwo_api_key=<your API key> \
  ruby mwo_match_data.rb


Running that last line of code about mwo_match_data.rb produces a couple reports. The one I'm finding fancy right now is the `cpd.txt` one that looks like this:


PolarHighlands Component Efficacy
Team 2
WINNER
Components Opened and Destroyed by Team:  19
Component Waste: 3
Extrapolated Waste: 3
Damage Waste: <total_team_damage - total_leg_armor + total_leg_structure>
 
PolarHighlands Component Efficacy
Team 1
Loser
Components Opened and Destroyed by Team:  10
Component Waste: 2
Extrapolated Waste: 4
Damage Waste: <total_team_damage - total_leg_armor + total_leg_structure>


I like this report, not only because I was on Team 2, but also because it can help represent your whole team and how well you're working together rather than straight damage and straight kills, both of which can be padded very easily on an individual level by folks who are easily distracted from their team's success.

Pro Tips: If you're annoyed by having to put your API key in the terminal every time, just paste a line like 'export mwo_api_key=<your API key>' right into your ~/.bash_profile file.

Anyone else have reports they're using? Anyone have a favorite metric? Are you using html5 web 2.0 graphing libraries yet?

Edited by Congress Hating Ur Freedoms, 31 March 2018 - 04:53 AM.


#2 Lilferret

    Member

  • PipPipPip
  • The Territorial
  • The Territorial
  • 75 posts

Posted 09 April 2018 - 04:14 PM

That's really cool.
I wish I knew how to API

#3 Matt Redburn

    Member

  • PipPipPip
  • The Hunter
  • The Hunter
  • 93 posts

Posted 16 April 2018 - 11:14 PM

<?
class mwogame
{
  public $gameId = "";
  private $jsonContent = "";
  private $apiKey = "apikeyhere..."; 
 
  public function __construct($gameId)
  {
   $this->gameId = $gameId;
   $this->jsonContent = $this->getJson();
  }
 
  function getJson()
  {
   $matches = array();
   $jsonContent = null;
   if($this->gameId != "")
   {
    $ctx = stream_context_create(array(
	 'http' => array(
	  'timeout'  =>  1,
	  'method' => "GET",
	  'header' => "Accept: application/json\r\n"
	  )
	 )
    );
		
    @$content = file_get_contents($this->getUrl(), 0, $ctx);
    preg_match('#HTTP/\d+\.\d+ (\d+)#', $http_response_header[0], $matches);
   
    if(isset($matches[1]))
    {
	 $errorClass = new \StdClass();
	 if($matches[1] == 422)
	 {
	  $errorClass->status = $matches[1];
	  $errorClass->error = "No game found";
	  $jsonContent = json_decode(json_encode($errorClass));
	 }
	 if($matches[1] == 401)
	 {
	  $errorClass->status = $matches[1];
	  $errorClass->error = "No authentification";
	  $jsonContent = json_decode(json_encode($errorClass));
	 }
	 if($matches[1] == 500)
	 {
	  $errorClass->status = $matches[1];
	  $errorClass->error = "Server Error";
	  $jsonContent = json_decode(json_encode($errorClass));
	 }
	 if($matches[1] == 200)
	 {
	  $jsonContent = json_decode($content);
	 }
    }
   } 
   return $jsonContent;
  }
 
  function getUrl()
  {
   return "https://mwomercs.com/api/v1/matches/" . $this->gameId . "?api_token=" . $this->apiKey;
  }
  function render()
  {
   echo $this->RenderString();
  }
 
  function RenderString()
  {
   $game = $this->jsonContent;
   if(isset($game->status))
   {
    echo "Error: " . $game->error . " - StatusCode: " . $game->status;
    return;
   }
  
   $players = array("spectators" => array(), "team1" => array(), "team2" => array());
  
   foreach($game->UserDetails as $user)
   {
    if($user->IsSpectator)
    {
	 array_push($players["spectators"], $user);
	 continue;
    }   
   
    if($user->Team == "1")
	 array_push($players["team1"], $user);
   
    if($user->Team == "2")
	 array_push($players["team2"], $user);
   }
  
   $team1Players = "";
   $team2Players = "";
   $spectators = "";
   $team1Class = ($game->MatchDetails->WinningTeam == 1) ? "winner" : "loser";
   $team2Class = ($game->MatchDetails->WinningTeam == 2) ? "winner" : "loser";
   foreach($players['team1'] as $player) {
    $team1Players .= $this->renderPilot($player);
   }
   foreach($players["spectators"] as $player) {
    $spectators .= $player->Username . " ";
   }
   foreach($players["team2"] as $player) {
    $team2Players .= $this->renderPilot($player);
   }
   $date = new \DateTime($game->MatchDetails->CompleteTime);
   $dauer = floor($game->MatchDetails->MatchDuration/60).":".($game->MatchDetails->MatchDuration%60);
   $html = <<<HTML
  
   <ul class="mwomatch mwo{$this->gameId}">
    <li class="team1 {$team1Class}">
	 <ul>	
	  {$team1Players}
	 </ul>
    </li>
    <li class="vs">
	 <span>{$game->MatchDetails->Team1Score} : {$game->MatchDetails->Team2Score}</span>	
	 {$game->MatchDetails->Map} ({$game->MatchDetails->TimeOfDay }) - {$game->MatchDetails->GameMode }<br>
	 <a href="#" class="toogledetails">Details umschalten</a>
	 <div class="stats">
	  Viewmode: {$game->MatchDetails->ViewMode}<br>
	  Region: {$game->MatchDetails->Region}<br>
	  Spielzeit: {$game->MatchDetails->MatchTimeMinutes} min<br>
	  Stock: {$game->MatchDetails->UseStockLoadout}<br>
	  Quirks: {$game->MatchDetails->NoMechQuirks}<br>
	  Skills: {$game->MatchDetails->NoMechEfficiencies}<br>
	  Dauer: {$dauer} min<br>
	  Zeit: {$date->format('d.m.Y H:i')}<br>
	  Zuschauer: {$spectators}
	 </div>
    </li>
    <li class="team2 {$team2Class}">
	 <ul>
	  {$team2Players}
	 </ul>
    </li>
   </ul>
HTML;
  
  
   return $html;
  }
 
  function renderPilot($player)
  {
   $playerdeadClass = "";
   if ($player->HealthPercentage == 0)
    $playerdeadClass = "dead";
  
   $html = <<<HTML
   <li class="pilot">
    <div>
	 <span class="mech">{$player->MechName}</span>
	 <span class="name {$playerdeadClass}">[{$player->UnitTag}] {$player->Username}</span>	
	 <div class="stats">
	  Tier: {$player->SkillTier}, Health: {$player->HealthPercentage}%,<br>
	  KMDD: {$player->KillsMostDamage}, Assists: {$player->Assists}, ComponentsDestroyed: {$player->ComponentsDestroyed}, <br>
	  MatchScore: {$player->MatchScore}, Kills: {$player->Kills}, Damage: {$player->Damage}
	 </div>	 
    </div>
   </li>
HTML;
    return $html;
  }
}
?>


Here is my quickndirty php-class... if someone wants to use it, feel free to do so. No support.

#4 ShelleMech

    Rookie

  • 2 posts
  • LocationUK

Posted 20 April 2018 - 02:57 AM

We have this for MRBC: https://mwoapi.mrbcleague.com and we have included match result entry from the API this season.

(https://github.com/s...ch/mwo-comp-api)

I haven't seen much interest in the API so far yet from the community, but that might be because PGI aren't really advertising it nor do they provide a developer portal Posted Image





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users