Skip to content

Commit

Permalink
Added username syncing, added load state to replay list for large rep…
Browse files Browse the repository at this point in the history
…lay lists
  • Loading branch information
anther committed Oct 9, 2018
1 parent b01dae5 commit 3cdf763
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 236 deletions.
32 changes: 20 additions & 12 deletions app/actions/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
updateSearchRomSubdirectories
} from './dolphinSettings';
import { startReplayBrowser } from "./replayBrowse";
import {parseDolphinPlayerList} from "./dolphinStatus";

export const FETCH_BUILDS_BEGIN = 'FETCH_BUILDS_BEGIN';
export const FETCH_BUILDS_SUCCESS = 'FETCH_BUILDS_SUCCESS';
Expand Down Expand Up @@ -56,7 +57,17 @@ export const SYNC_BUILDS_FAIL = 'SYNC_BUILDS_FAIL';

export const AUTOHOTKEY_EVENT = 'AUTOHOTKEY_ACTION';

const buildLauncher = new BuildLaunchAhk();
let buildLauncher = null;
export const initializeBuildLauncher = () => (dispatch) => {
buildLauncher = new BuildLaunchAhk();
buildLauncher.on('ahkEvent', (event) =>{
if(event.action === 'player_list_info')
{
dispatch(parseDolphinPlayerList(event.value));
}
});
};


export const retrieveBuilds = () => (dispatch, getState) => {
dispatch({
Expand Down Expand Up @@ -277,17 +288,14 @@ export const startGame = () => dispatch => {
});
};

// export const authotkeyAction = event => (dispatch) =>{
// dispatch({
// type: AUTOHOTKEY_EVENT,
// payload: {
// event: event
// }
// });
// };
// buildLauncher.on('ahkEvent', event => (dispatch) => {
// authotkeyAction(event);
// });
export const authotkeyAction = event => (dispatch) =>{
dispatch({
type: AUTOHOTKEY_EVENT,
payload: {
event: event
}
});
};

export const closeDolphin = () => (dispatch, getState) => {
const authentication = getAuthenticationFromState(getState);
Expand Down
103 changes: 103 additions & 0 deletions app/actions/dolphinStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import _ from 'lodash';
import getAuthenticationFromState from "../utils/getAuthenticationFromState";
import {endpoints} from "../utils/SmashLadderAuthentication";

export const DOLPHIN_STATUS_SEND_BEGIN = 'DOLPHIN_STATUS_SEND_BEGIN';
export const DOLPHIN_STATUS_SEND_SUCCESS = 'DOLPHIN_STATUS_SEND_SUCCESS';
export const DOLPHIN_STATUS_SEND_FAIL = 'DOLPHIN_STATUS_SEND_FAIL';




export const parseDolphinPlayerList = (value = '') => (dispatch, getState) => {
const valueSplit = value.split(/\r?\n/);

const { dolphinPlayers } = getState().dolphinStatus;

const parsedUsernames = {};
valueSplit.forEach((current, index)=>{
if (!current.includes('[')) {
return;
}
const nextLine = parseInt(index, 10) + 1;
const pingLine = valueSplit[nextLine];
let ping = null;
if (pingLine) {
const pingTitle = pingLine.substring(0, pingLine.lastIndexOf(':'));
const pingSide = pingLine.substring(pingLine.lastIndexOf(':') + 1);
ping = Number.parseInt(pingSide, 10);
}

const usernameData = parseUsernameInfo(current);
usernameData.ports.forEach((port)=>{
parsedUsernames[port] = {
username: usernameData.username,
slot: port
};
});
});
if(_.isEqual(parsedUsernames, dolphinPlayers))
{
return;
}
const sendData = {
players: parsedUsernames
};
const authentication = getAuthenticationFromState(getState);
dispatch({
type: DOLPHIN_STATUS_SEND_BEGIN,
payload: parsedUsernames
});
authentication.apiPost(endpoints.DOLPHIN_PLAYER_JOINED, sendData)
.then((response)=>{
console.log(response);
dispatch({
type: DOLPHIN_STATUS_SEND_SUCCESS
});
})
.catch((error)=>{
console.error(error);
dispatch({
type: DOLPHIN_STATUS_SEND_FAIL,
payload: error
})
});
};

const parseUsernameInfo = (current) => {
const usernameSide = current.substring(0, current.lastIndexOf(':'));
const systemInfoSide = current.substring(current.lastIndexOf(':') + 1);

const ports = systemInfoSide
.substring(
systemInfoSide.indexOf('|') + 1,
systemInfoSide.lastIndexOf('|') - 1
)
.trim();
const systemInformation = systemInfoSide
.substring(0, systemInfoSide.indexOf('|'))
.trim();

const portIndexes = [];
for (
let characterIndex = 0;
characterIndex < ports.length;
characterIndex++
) {
if (ports.charAt(characterIndex) === '-') {
continue;
}
portIndexes.push(characterIndex + 1);
}
let slot = null;
if (portIndexes.length) {
slot = portIndexes[0];
}
const username = usernameSide
.substring(0, usernameSide.lastIndexOf('['))
.trim();
return {
username,
ports: portIndexes
};
};
114 changes: 91 additions & 23 deletions app/actions/replayBrowse.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const REPLAY_BROWSE_END = 'REPLAY_BROWSE_END';

export const REPLAY_BROWSE_UPDATE_START = 'REPLAY_BROWSE_UPDATE_START';
export const REPLAY_BROWSE_UPDATE_SUCCESS = 'REPLAY_BROWSE_UPDATE_SUCCESS';
export const REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS = 'REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS';

export const REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_BEGIN = 'REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_BEGIN';
export const REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_SUCCESS = 'REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_SUCCESS';
export const REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_FAIL = 'REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_FAIL';
export const REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_UPDATE = 'REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_UPDATE';

export const REPLAY_BROWSER_CHANGE_PAGE_NUMBER = 'REPLAY_BROWSER_CHANGE_PAGE_NUMBER';

Expand Down Expand Up @@ -101,36 +105,100 @@ const updateBrowsedReplayList = () => (dispatch, getState) => {
dispatch(displayReplaysBasedOnCurrentPage());
};

function yieldingLoop(count, chunksize, callback, cancelToken = {}) {
let i = 0;
let totalSkipped = 0;
return new Promise((resolve, reject)=>{
(function chunk() {
const end = Math.min(i + chunksize, count);
let skipTimeout = false;
for ( ; i < end; ++i) {
skipTimeout = callback.call(null, i);
}
if(cancelToken.cancelled)
{
reject();
return;
}
if (i < count) {
if(skipTimeout === true)
{
totalSkipped++;
chunk.call(null);
}
else
{
setTimeout(chunk, 0);
}
} else {
resolve(totalSkipped);
}
})();
})
}

let lastCancelToken = {
cancelled: false
};
const displayReplaysBasedOnCurrentPage = () => (dispatch, getState) => {
const state = getState();
const {allReplays, replayPageNumber, replaysPerPage, activeBrowseReplays } = state.replayBrowse;
const {allReplays, replayPageNumber, replaysPerPage, activeBrowseReplays, updatingReplayList, replayListHasChanged } = state.replayBrowse;

const totalReplays = allReplays.size;
const firstReplayIndex = (replayPageNumber - 1) * replaysPerPage;
const lastReplayIndex = firstReplayIndex + replaysPerPage;

let replays = Array.from(allReplays).sort((a, b) => {
return a.getFileDate() > b.getFileDate() ? -1 : 1;
});
if(firstReplayIndex > totalReplays)
{
console.log('first index is beyond total');
replays = [];
}
else
{
replays = replays.slice(firstReplayIndex, lastReplayIndex);
}
const replayList = Array.from(allReplays);

if(_.isEqual(replays, activeBrowseReplays))
{
return;
}
dispatch({
type: REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS,
payload: {
activeBrowseReplays: replays,
dispatch({
type: REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_BEGIN,
});
lastCancelToken.cancelled = true;
lastCancelToken = {
cancelled: false
};
yieldingLoop(replayList.length, 1, (index)=>{
const replay = replayList[index];
if(replay.hasFileDate())
{
return true;
}
dispatch({
type: REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_UPDATE,
payload: {
processed: index + 1
}
});
replay.getFileDate();
}, lastCancelToken).then(()=>{
let replays = replayList;
replays.sort((a, b) => {
return a.getFileDate().isAfter(b.getFileDate()) ? -1 : 1;
});
if(firstReplayIndex > totalReplays)
{
console.log('first index is beyond total');
replays = [];
}
else
{
replays = replays.slice(firstReplayIndex, lastReplayIndex);
}

dispatch({
type: REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_SUCCESS,
payload: {
activeBrowseReplays: replays,
}
});
})
.catch((error)=>{
dispatch({
type: REPLAY_BROWSE_UPDATE_DISPLAYED_REPLAYS_FAIL,
});
console.error(error);
});

};

export const viewReplayDetails = (replay) => (dispatch, getState) => {
Expand Down Expand Up @@ -176,7 +244,7 @@ export const stopReplayBrowser = () => {
};
};

export const changeReplayPageNumber = (pageNumber) => (dispatch) => {
export const changeReplayPageNumber = (pageNumber) => (dispatch, getState) => {
dispatch({
type: REPLAY_BROWSER_CHANGE_PAGE_NUMBER,
payload: pageNumber
Expand Down
2 changes: 0 additions & 2 deletions app/actions/replayWatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export const checkReplay = (filePath, watchProcessCounter) => (dispatch, getStat
dispatch({
type: VERIFY_FILE_FAIL
});
console.log(`could not get legit data from replay ${filePath}`);
if(errors)
{
throw errors[0];
Expand All @@ -183,7 +182,6 @@ export const checkReplay = (filePath, watchProcessCounter) => (dispatch, getStat
dispatch({
type: VERIFY_FILE_FAIL
});
console.error(error);
});
};

Expand Down
30 changes: 23 additions & 7 deletions app/app.global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ a {
cursor: pointer;
}

.progress .determinate{
transition: width .1s linear;
}

.joined_inputs{
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -236,6 +240,13 @@ h6 {
border: none;
overflow: visible;

.build_image{
display: inline;
max-height: 24px;
margin-left: 4px;
margin-right: 4px;
}

.build {
padding-left: 0;
padding-right: 0;
Expand Down Expand Up @@ -312,8 +323,11 @@ h6 {
display: flex;

.remove_build_path{
flex: 1;
flex: 1 0 auto;
text-align: right;
justify-content: flex-end;
display: flex;
align-items: center;
}

.build_name {
Expand All @@ -329,12 +343,6 @@ h6 {
@extend .blue-text, .lighten-2;
cursor: pointer;
}
img{
display: inline;
max-height: 24px;
margin-left: 4px;

}
}

.has_path i,
Expand All @@ -355,6 +363,14 @@ h6 {
}

.replay_browser {
position: relative;

.replay_list_updating_progress{
position: absolute;
width: 100%;
left: 0;
top: -8px;
}

.pagination{
display: flex;
Expand Down
Loading

0 comments on commit 3cdf763

Please sign in to comment.