Skip to content

Commit 6186564

Browse files
committed
Minor fixes
1 parent 3afd17e commit 6186564

6 files changed

Lines changed: 213 additions & 74 deletions

File tree

app/Flare/GameImporter/Console/Commands/ImportGameData.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,43 @@ protected function fetchFiles(): array
156156
}
157157
}
158158

159+
if (isset($result['Core Imports'])) {
160+
$result['Core Imports'] = $this->sortFilesByCustomOrder($result['Core Imports'], [
161+
'Core Imports/game_races.xlsx',
162+
'Core Imports/game_classes.xlsx',
163+
'Core Imports/class-specials.xlsx',
164+
]);
165+
}
166+
159167
$result['Kingdoms'] = array_reverse($result['Kingdoms']);
160168

161169
return $result;
162170
}
163171

172+
173+
/**
174+
* Sort files by an explicit import order.
175+
*/
176+
protected function sortFilesByCustomOrder(array $files, array $order): array
177+
{
178+
usort($files, function (string $firstFile, string $secondFile) use ($order) {
179+
$firstIndex = array_search($firstFile, $order, true);
180+
$secondIndex = array_search($secondFile, $order, true);
181+
182+
if ($firstIndex === false) {
183+
$firstIndex = PHP_INT_MAX;
184+
}
185+
186+
if ($secondIndex === false) {
187+
$secondIndex = PHP_INT_MAX;
188+
}
189+
190+
return $firstIndex <=> $secondIndex;
191+
});
192+
193+
return $files;
194+
}
195+
164196
/**
165197
* Import th excel files.
166198
*/

app/Flare/GameImporter/Values/ExcelMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class ExcelMapper
2626
'Raids' => RaidsImport::class,
2727
'Affixes' => AffixesImport::class,
2828
'Core Imports' => [
29-
ClassSpecialsImport::class,
30-
ClassImport::class,
3129
RacesImport::class,
30+
ClassImport::class,
31+
ClassSpecialsImport::class,
3232
],
3333
'Items' => ItemsImport::class,
3434
'Weapons' => ItemsImport::class,

resources/js/game/components/kingdoms/ajax/process-unit-request-ajax.ts

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,74 @@
1-
import { inject, injectable } from "tsyringe";
2-
import Ajax from "../../../lib/ajax/ajax";
3-
import AjaxInterface from "../../../lib/ajax/ajax-interface";
1+
import { injectable } from "tsyringe";
42
import { AxiosError, AxiosResponse } from "axios";
3+
import axios from "axios";
4+
import { flushSync } from "react-dom";
55
import UnitRecruitment from "../capital-city/partials/unit-management/unit-recruitment";
6+
import { runCapitalCityRequestBatches } from "./run-capital-city-request-batches";
67

78
@injectable()
89
export default class ProcessUnitRequestAjax {
9-
constructor(@inject(Ajax) private ajax: AjaxInterface) {}
10-
1110
public processRequest(
1211
component: UnitRecruitment,
1312
characterId: number,
1413
kingdomId: number,
15-
params: any,
14+
params: any[],
1615
): void {
17-
this.ajax
18-
.setRoute(
19-
"kingdom/capital-city/recruit-unit-requests/" +
20-
characterId +
21-
"/" +
22-
kingdomId,
23-
)
24-
.setParameters({
25-
request_data: params,
16+
const batches: any[][] = [];
17+
for (let i = 0; i < params.length; i += 5) {
18+
batches.push(params.slice(i, i + 5));
19+
}
20+
21+
runCapitalCityRequestBatches(
22+
batches,
23+
2,
24+
(batch) => this.sendBatch(characterId, kingdomId, batch),
25+
(batch) => {
26+
flushSync(() => {
27+
this.applyAcceptedRequestState(component, batch);
28+
});
29+
},
30+
)
31+
.then(() => {
32+
component.setState({
33+
processing_request: false,
34+
});
2635
})
27-
.doAjaxCall(
28-
"post",
29-
(result: AxiosResponse) => {
30-
this.applyAcceptedRequestState(component, params);
31-
},
32-
(error: AxiosError) => {
36+
.catch((error: AxiosError) => {
37+
component.setState({
38+
processing_request: false,
39+
});
40+
41+
if (typeof error.response !== "undefined") {
42+
const response: AxiosResponse = error.response;
43+
3344
component.setState({
34-
processing_request: false,
45+
error_message: response.data.message,
3546
});
47+
}
48+
});
49+
}
3650

37-
if (typeof error.response !== "undefined") {
38-
const response: AxiosResponse = error.response;
39-
40-
component.setState({
41-
error_message: response.data.message,
42-
});
43-
}
44-
},
45-
);
51+
private sendBatch(
52+
characterId: number,
53+
kingdomId: number,
54+
batch: any[],
55+
): Promise<void> {
56+
return axios
57+
.post(
58+
"/api/kingdom/capital-city/recruit-unit-requests/" +
59+
characterId +
60+
"/" +
61+
kingdomId,
62+
{ request_data: batch },
63+
{ headers: { "Content-Type": "application/json" } },
64+
)
65+
.then(() => undefined)
66+
.catch((error: AxiosError) => {
67+
if (error.response?.status === 401) {
68+
window.location.reload();
69+
}
70+
throw error;
71+
});
4672
}
4773

4874
private applyAcceptedRequestState(
@@ -73,7 +99,6 @@ export default class ProcessUnitRequestAjax {
7399

74100
if (fadingKingdomIds.size <= 0) {
75101
component.setState({
76-
processing_request: false,
77102
unit_recruitment_data: updatedUnitRecruitmentData,
78103
filtered_unit_recruitment_data:
79104
updatedFilteredUnitRecruitmentData,
@@ -106,7 +131,6 @@ export default class ProcessUnitRequestAjax {
106131
});
107132

108133
component.setState({
109-
processing_request: false,
110134
unit_recruitment_data: this.keepFadingKingdomsRendered(
111135
component.state.unit_recruitment_data,
112136
updatedUnitRecruitmentData,

resources/js/game/components/kingdoms/ajax/process-upgrade-buildings-ajax.ts

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,85 @@
1-
import { inject, injectable } from "tsyringe";
2-
import Ajax from "../../../lib/ajax/ajax";
3-
import AjaxInterface from "../../../lib/ajax/ajax-interface";
1+
import { injectable } from "tsyringe";
42
import { AxiosError, AxiosResponse } from "axios";
3+
import axios from "axios";
4+
import { flushSync } from "react-dom";
55
import BuildingsToUpgradeSection from "../capital-city/buildings-to-upgrade-section";
6+
import { runCapitalCityRequestBatches } from "./run-capital-city-request-batches";
67

78
@injectable()
89
export default class ProcessUpgradeBuildingsAjax {
9-
constructor(@inject(Ajax) private ajax: AjaxInterface) {}
10-
1110
public sendBuildingRequests(
1211
component: BuildingsToUpgradeSection,
1312
characterId: number,
1413
kingdomId: number,
15-
params: any,
14+
params: any[],
1615
): void {
1716
let requestType = "upgrade";
1817

1918
if (component.props.repair) {
2019
requestType = "repair";
2120
}
2221

23-
this.ajax
24-
.setRoute(
25-
"kingdom/capital-city/upgrade-building-requests/" +
26-
characterId +
27-
"/" +
28-
kingdomId,
29-
)
30-
.setParameters({
31-
request_data: params,
32-
request_type: requestType,
22+
const batches: any[][] = [];
23+
for (let i = 0; i < params.length; i += 5) {
24+
batches.push(params.slice(i, i + 5));
25+
}
26+
27+
runCapitalCityRequestBatches(
28+
batches,
29+
2,
30+
(batch) =>
31+
this.sendBatch(characterId, kingdomId, requestType, batch),
32+
(batch) => {
33+
flushSync(() => {
34+
this.applyAcceptedRequestState(component, batch);
35+
});
36+
},
37+
)
38+
.then(() => {
39+
component.setState({
40+
processing_request: false,
41+
info_message: "Building orders were accepted.",
42+
success_message: null,
43+
error_message: null,
44+
});
3345
})
34-
.doAjaxCall(
35-
"post",
36-
(result: AxiosResponse) => {
37-
this.applyAcceptedRequestState(component, params);
38-
component.setState({
39-
processing_request: false,
40-
info_message: "Building orders were accepted.",
41-
success_message: null,
42-
error_message: null,
43-
});
44-
},
45-
(error: AxiosError) => {
46+
.catch((error: AxiosError) => {
47+
component.setState({
48+
processing_request: false,
49+
});
50+
51+
if (typeof error.response !== "undefined") {
52+
const response: AxiosResponse = error.response;
53+
4654
component.setState({
47-
processing_request: false,
55+
error_message: response.data.message,
4856
});
57+
}
58+
});
59+
}
4960

50-
if (typeof error.response !== "undefined") {
51-
const response: AxiosResponse = error.response;
52-
53-
component.setState({
54-
error_message: response.data.message,
55-
});
56-
}
57-
},
58-
);
61+
private sendBatch(
62+
characterId: number,
63+
kingdomId: number,
64+
requestType: string,
65+
batch: any[],
66+
): Promise<void> {
67+
return axios
68+
.post(
69+
"/api/kingdom/capital-city/upgrade-building-requests/" +
70+
characterId +
71+
"/" +
72+
kingdomId,
73+
{ request_data: batch, request_type: requestType },
74+
{ headers: { "Content-Type": "application/json" } },
75+
)
76+
.then(() => undefined)
77+
.catch((error: AxiosError) => {
78+
if (error.response?.status === 401) {
79+
window.location.reload();
80+
}
81+
throw error;
82+
});
5983
}
6084

6185
private applyAcceptedRequestState(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export async function runCapitalCityRequestBatches<T>(
2+
batches: T[],
3+
maxConcurrent: number,
4+
processBatch: (batch: T) => Promise<void>,
5+
onBatchSuccess: (batch: T) => void,
6+
): Promise<void> {
7+
if (batches.length === 0) {
8+
return;
9+
}
10+
11+
return new Promise<void>((resolve, reject) => {
12+
let nextIndex = 0;
13+
let activeCount = 0;
14+
let failed = false;
15+
16+
function dispatch(): void {
17+
while (
18+
!failed &&
19+
activeCount < maxConcurrent &&
20+
nextIndex < batches.length
21+
) {
22+
const batch = batches[nextIndex];
23+
nextIndex++;
24+
activeCount++;
25+
26+
processBatch(batch)
27+
.then(() => {
28+
activeCount--;
29+
onBatchSuccess(batch);
30+
31+
if (activeCount === 0 && nextIndex >= batches.length) {
32+
resolve();
33+
} else {
34+
dispatch();
35+
}
36+
})
37+
.catch((error: unknown) => {
38+
activeCount--;
39+
if (!failed) {
40+
failed = true;
41+
reject(error);
42+
}
43+
});
44+
}
45+
}
46+
47+
dispatch();
48+
});
49+
}

resources/js/game/components/ui/progress-bars/timer-progress-bar.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ export default class TimerProgressBar extends React.Component<
7373
() => {
7474
if (timeRemaining > 0 && this.state.time_left > 0) {
7575
this.interval = setInterval(() => {
76-
let newTime = this.getEffectiveTimeRemaining();
76+
let newTime: number;
77+
78+
if (
79+
typeof this.props.completed_at_timestamp !==
80+
"undefined" ||
81+
typeof this.props.timer_started_at !== "undefined"
82+
) {
83+
newTime = this.getEffectiveTimeRemaining();
84+
} else {
85+
newTime = Math.max(0, this.state.time_left - 1);
86+
}
7787

7888
if (newTime <= 0) {
7989
this.setState({

0 commit comments

Comments
 (0)