Skip to content

Commit

Permalink
Fix #46 - Replace stderr with game log
Browse files Browse the repository at this point in the history
  • Loading branch information
danBhentschel committed Feb 24, 2017
1 parent 41d87c1 commit 552e4f3
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 58 deletions.
4 changes: 2 additions & 2 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
}
}
},
"btnStderr": {
"message": "STDERR",
"btnGameLog": {
"message": "Game log",
"description": "Link title for the Batch Run results table"
},
"replayBtnTip": {
Expand Down
43 changes: 43 additions & 0 deletions app/dialogs/matchGameLog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!doctype html>

<html>
<head>
<title>CG Spunk match game log display</title>
<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="../scripts/matchGameLogDialog.js"></script>
</head>
<body>

<!-- Haven't implemented translation -->
<div class="container">
<h1>Game log for this match</h1>

<div class="row">
<div class="btn-group" data-toggle="buttons" id="selections">
<label class="btn btn-primary active">
<input type="checkbox" id="labels" autocomplete="off" checked> Labels
</label>
<label class="btn btn-success active" data-toggle="tooltip" title="To get stdin, log the inputs to stderr between two lines: 'IN' and '/IN'">
<input type="checkbox" id="stdin" autocomplete="off" checked> stdin
</label>
<label class="btn btn-danger active">
<input type="checkbox" id="stderr" autocomplete="off" checked> stderr
</label>
<label class="btn btn-warning active">
<input type="checkbox" id="stdout" autocomplete="off" checked> stdout
</label>
<label class="btn btn-info active">
<input type="checkbox" id="summary" autocomplete="off" checked> Summary
</label>
</div>
</div>
<div class="row">
<pre id="gameLog" style="background-color:white;"></pre>
</div>
</div>


</body>
</html>
22 changes: 0 additions & 22 deletions app/dialogs/matchStderr.html

This file was deleted.

47 changes: 35 additions & 12 deletions app/scripts/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ var __CGSpunk_Injected =
return myAgents[0];
}

function getGameHistoryFromGameManager(gameManager) {
let agentNames = gameManager.agents.map(_ => getNameOfAgent(_));
let frames = gameManager.frames;
let history = {
agents: agentNames,
data: []
};
let frameNo = 1;
while (frameNo < frames.length) {
let turn = {
stdout: [],
stderr: []
};
for (let agent = 0; agent < agentNames.length; agent++) {
if (frameNo >= frames.length) {
break;
}
let frame = frames[frameNo];
turn.stdout.push(frame.stdout);
turn.stderr.push(frame.stderr);
if (!!frame.summary) {
turn.summary = frame.summary;
}
frameNo++;
}
history.data.push(turn);
}

return history;
}

function stopPlayback() {
waitForGameManager()
.then(gameManager => { gameManager.pause(); return gameManager; })
Expand Down Expand Up @@ -344,6 +375,7 @@ var __CGSpunk_Injected =
function getNameOfAgent(agent) {
if (agent.codingamer) return agent.codingamer.pseudo;
if (agent.arenaboss) return agent.arenaboss.nickname;
if (!!agent.name) return agent.name;
return 'Default';
}

Expand Down Expand Up @@ -486,7 +518,7 @@ var __CGSpunk_Injected =
return {
rankings: rankingsForAgents(gameManager.agents),
options: getMatchOptions(),
stderr: getMatchStderr(),
history: getGameHistoryFromGameManager(gameManager),
crash: getCrashInfo(),
replay: getReplayUrl()
};
Expand All @@ -506,16 +538,6 @@ var __CGSpunk_Injected =
return $('.options-text').val();
}

function getMatchStderr() {
let stderr = [];

$('.stderr > .outputLine').each(function() {
stderr.push($(this).text());
});

return stderr;
}

function getCrashInfo() {
let info = $('.error > .consoleError').text();
if (!info) return '';
Expand All @@ -535,6 +557,7 @@ var __CGSpunk_Injected =

return {
__FOR_TEST_getMyAgentFromGameManager: getMyAgentFromGameManager,
__FOR_TEST_getMyStateFromGameManager: getMyStateFromGameManager
__FOR_TEST_getMyStateFromGameManager: getMyStateFromGameManager,
__FOR_TEST_getGameHistoryFromGameManager: getGameHistoryFromGameManager
};
})();
16 changes: 8 additions & 8 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
g_optionsResponseFunc(request.options);
} else if (request.action === 'showResultsWindow') {
showResultsWindow(request.instanceNum, sender.tab.id);
} else if (request.action === 'showMatchStderr') {
showStderrWindow(request.stderr);
} else if (request.action === 'showMatchGameLog') {
showGameLogWindow(request.gameLog);
} else if (request.action === 'showMatchCrashInfo') {
showCrashInfoWindow(request.crashInfo);
} else if (request.action === 'showBatchData') {
showBatchDataWindow(request.data);
} else if (request.action === 'getLastStderr') {
sendResponse(g_lastStderr);
} else if (request.action === 'getLastGameLog') {
sendResponse(g_lastGameLog);
} else if (request.action === 'getLastCrashInfo') {
sendResponse(g_lastCrashInfo);
} else if (request.action === 'getLastBatchData') {
Expand All @@ -45,12 +45,12 @@ function showResultsWindow(instanceNum, tabId) {
});
}

var g_lastStderr;
var g_lastGameLog;

function showStderrWindow(stderr) {
g_lastStderr = stderr;
function showGameLogWindow(gameLog) {
g_lastGameLog = gameLog;
chrome.windows.create({
url: 'dialogs/matchStderr.html',
url: 'dialogs/matchGameLog.html',
type: 'popup',
width: 600,
height: 600
Expand Down
14 changes: 7 additions & 7 deletions app/scripts/batchRunResultsDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var __CGSpunk_batchRunResultsDialog =
row += playersLabelCell(match, results.userName);
row += scoresCell(match);
row += winnerLabelCell(match, results.userName);
row += stderrLinkCell();
row += gameLogLinkCell();
row += '</tr>';

let tbody = $('#resultsTable tbody');
Expand Down Expand Up @@ -215,9 +215,9 @@ var __CGSpunk_batchRunResultsDialog =
return rankings.filter(_ => _.rank === 1);
}

function stderrLinkCell() {
return '<td><button type="button" class="btn btn-link" id="stderrBtn' + g_matchNum + '">' +
chrome.i18n.getMessage('btnStderr') + '</button></td>';
function gameLogLinkCell() {
return '<td><button type="button" class="btn btn-link" id="gameLogBtn' + g_matchNum + '">' +
chrome.i18n.getMessage('btnGameLog') + '</button></td>';
}

function addButtonEventHandlers(tbody, match) {
Expand All @@ -238,10 +238,10 @@ var __CGSpunk_batchRunResultsDialog =
});
});

tbody.on('click', '#stderrBtn' + g_matchNum, () => {
tbody.on('click', '#gameLogBtn' + g_matchNum, () => {
chrome.runtime.sendMessage({
action: 'showMatchStderr',
stderr: match.results.stderr
action: 'showMatchGameLog',
gameLog: match.results.history
});
});

Expand Down
72 changes: 72 additions & 0 deletions app/scripts/matchGameLogDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

var g_gameLog;

$(document).ready(() => {
chrome.runtime.sendMessage({ action: 'getLastGameLog' }, gameLog => {
g_gameLog = gameLog;
showLogData();
$('#stdin').tooltip({ trigger: 'hover' });
});

$('#selections').on('change', ':checkbox', function() {
showLogData();
});

function showLogData() {
let data = [];
let showLabels = $('#labels').is(':checked');
let showStdin = $('#stdin').is(':checked');
let showStderr = $('#stderr').is(':checked');
let showStdout = $('#stdout').is(':checked');
let showSummary = $('#summary').is(':checked');
let agents = g_gameLog.agents;

for (let t = 0; t < g_gameLog.data.length; t++) {
let turn = g_gameLog.data[t];
let stderr = turn.stderr.find(_ => !!_);
let stdin = getStdinFromStderr(stderr);
if (showLabels) {
data.push(`<span class="text-primary"> ** TURN ${t+1}</span>`);
}
if (showStdin && !!stdin) {
data.push(`<span class="text-success">${stdin}</span>`);
}
if (showStderr && !!stderr) {
stderr = stderr.replace(/IN\n[\s\S]*?\/IN\n/g, '').trim();
data.push(`<span class="text-danger">${stderr}</span>`);
}
if (showStdout) {
for (let a = 0; a < agents.length; a++) {
let stdout = turn.stdout[a];
if (!stdout) {
continue;
}
if (showLabels) {
data.push(`<span class="text-primary"> ** ${agents[a]}:</span>`);
}
data.push(`<span class="text-warning">${stdout.trim()}</span>`);
}
}
if (showSummary && !!turn.summary) {
let summary = turn.summary.replace(/\$(\d)/g, (grp, idx) => {
return agents[idx];
}).trim();
data.push(`<span class="text-info">${summary}</span>`);
}
}

$('#gameLog').html(data.join('\n'));
}

function getStdinFromStderr(stderr) {
if (!stderr) {
return null;
}
let stdin = stderr.match(/IN\n[\s\S]*?\/IN\n/g);
if (!!stdin) {
stdin = stdin.map(_ => _.match(/IN\n([\s\S]*)\/IN\n/)[1]).join('');
}
return stdin.trim();
}
});
7 changes: 0 additions & 7 deletions app/scripts/matchStderrDialog.js

This file was deleted.

2 changes: 2 additions & 0 deletions test/data/CodeBustersFramesData.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/spec/InjectedSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,36 @@

});

describe('getGameHistoryFromGameManager()', function () {
var gameManager;

beforeEach(function () {
gameManager = {
agents: __CodeBustersAgents_1,
frames: __CodeBustersFrames_1
};
});

it('should have 199 turns of data', function () {
var history = __CGSpunk_Injected.__FOR_TEST_getGameHistoryFromGameManager(gameManager);
expect(history.data.length).toBe(199);
});

it('should have summary info for each frame', function () {
var history = __CGSpunk_Injected.__FOR_TEST_getGameHistoryFromGameManager(gameManager);
expect(history.data.filter(_ => !!_.summary).length).toBe(199);
});

it('should have 2 stdout entries for each frame', function () {
var history = __CGSpunk_Injected.__FOR_TEST_getGameHistoryFromGameManager(gameManager);
expect([].concat.apply([], history.data.map(_ => _.stdout)).filter(_ => !!_).length).toBe(398);
});

it('should have 1 stderr entry for each frame', function () {
var history = __CGSpunk_Injected.__FOR_TEST_getGameHistoryFromGameManager(gameManager);
expect([].concat.apply([], history.data.map(_ => _.stderr)).filter(_ => !!_).length).toBe(199);
});

});

})();

0 comments on commit 552e4f3

Please sign in to comment.