Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion client/src/Backend/GameLogic/ContractsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,10 @@ export class ContractsAPI extends EventEmitter {
public async getPlayerById(playerId: EthAddress): Promise<Player | undefined> {
const rawPlayer = await this.makeCall(this.contract.players, [playerId]);
if (!rawPlayer.isInitialized) return undefined;
const player = decodePlayer(rawPlayer);
// This is horrible and unsustainable. We need some way to get Player + Silver token balance for
// bulk players as well.
const balance = await this.makeCall(this.contract.getSilverBalance, [playerId]);
const player = decodePlayer(rawPlayer, balance.toNumber());

return player;
}
Expand Down
80 changes: 69 additions & 11 deletions client/src/Backend/GameLogic/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
Transaction,
TxIntent,
UnconfirmedActivateArtifact,
UnconfirmedBulkWithdrawSilver,
UnconfirmedBuyHat,
UnconfirmedCapturePlanet,
UnconfirmedClaimReward,
Expand Down Expand Up @@ -2429,7 +2430,6 @@ class GameManager extends EventEmitter {

public async withdrawSilver(
locationId: LocationId,
amount: number,
bypassChecks = false
): Promise<Transaction<UnconfirmedWithdrawSilver>> {
try {
Expand All @@ -2439,24 +2439,19 @@ class GameManager extends EventEmitter {
throw new Error('game has ended');
}
const planet = this.entityStore.getPlanetWithId(locationId);
console.log(`basic withdraw planet`, planet);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(`basic withdraw planet`, planet);

if (!planet) {
throw new Error('tried to withdraw silver from an unknown planet');
}
if (planet.planetType !== PlanetType.TRADING_POST) {
throw new Error('can only withdraw silver from spacetime rips');
if (planet.planetType !== PlanetType.SILVER_MINE) {
throw new Error('can only withdraw silver from asteroid');
}
if (planet.owner !== this.account) {
throw new Error('can only withdraw silver from a planet you own');
}
if (planet.transactions?.hasTransaction(isUnconfirmedWithdrawSilverTx)) {
throw new Error('a withdraw silver action is already in progress for this planet');
}
if (amount > planet.silver) {
throw new Error('not enough silver to withdraw!');
}
if (amount === 0) {
throw new Error('must withdraw more than 0 silver!');
}
if (planet.destroyed) {
throw new Error("can't withdraw silver from a destroyed planet");
}
Expand All @@ -2467,9 +2462,8 @@ class GameManager extends EventEmitter {
const txIntent: UnconfirmedWithdrawSilver = {
methodName: 'withdrawSilver',
contract: this.contractsAPI.contract,
args: Promise.resolve([locationIdToDecStr(locationId), amount * CONTRACT_PRECISION]),
args: Promise.resolve([locationIdToDecStr(locationId)]),
locationId,
amount,
};

// Always await the submitTransaction so we can catch rejections
Expand All @@ -2482,6 +2476,70 @@ class GameManager extends EventEmitter {
}
}

public async bulkWithdrawSilver(
locationIds: LocationId[],
bypassChecks = false
): Promise<Transaction<UnconfirmedBulkWithdrawSilver>> {
const planets: Planet[] = [];
// Clean Ids
for (const locationId of locationIds) {
if (!bypassChecks) {
const planet = this.entityStore.getPlanetWithId(locationId);
if (!planet) {
continue;
}
if (planet.planetType !== PlanetType.SILVER_MINE) {
continue;
}
if (planet.owner !== this.account) {
continue;
}
if (planet.destroyed) {
continue;
}
planets.push(planet);
}
}
try {
if (!bypassChecks) {
if (!this.account) throw new Error('no account');
if (this.checkGameHasEnded()) {
throw new Error('game has ended');
}
}

if (locationIds.length === 0) throw new Error('no asteriods found to withdraw');

const ids = planets.map((p) => `0x${p.locationId}`);
const txIntent: UnconfirmedBulkWithdrawSilver = {
methodName: 'bulkWithdrawSilver',
contract: this.contractsAPI.contract,
args: Promise.resolve([[...ids]]),
locationIds: planets.map((p) => p.locationId),
};

console.log(`clean args`, Promise.resolve([[...ids]]));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(`clean args`, Promise.resolve([[...ids]]));


// Always await the submitTransaction so we can catch rejections
const tx = await this.contractsAPI.submitTransaction(txIntent, { gasLimit: 15000000 });

return tx;
} catch (e) {
this.getNotificationsManager().txInitError('withdrawSilver', e.message);
throw e;
}
}

public async withdrawFrom20Largest() {
const locationIds = this.getMyPlanets()
.filter((p) => p.planetType === PlanetType.SILVER_MINE)
.sort((a, b) => b.planetLevel - a.planetLevel)
.slice(0, 20)
.map((p) => p.locationId);
console.log(`withdrawing from`, locationIds);
return this.bulkWithdrawSilver(locationIds);
}

/**
* We have two locations which planet state can live: on the server, and on the blockchain. We use
* the blockchain for the 'physics' of the universe, and the webserver for optional 'add-on'
Expand Down
14 changes: 2 additions & 12 deletions client/src/Backend/GameLogic/GameUIManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,8 @@ class GameUIManager extends EventEmitter {
this.gameManager.deactivateArtifact(locationId, artifactId);
}

public withdrawSilver(locationId: LocationId, amount: number) {
const dontShowWarningStorageKey = `${this.getAccount()?.toLowerCase()}-withdrawnWarningAcked`;

if (localStorage.getItem(dontShowWarningStorageKey) !== 'true') {
localStorage.setItem(dontShowWarningStorageKey, 'true');
const confirmationText =
`Are you sure you want withdraw this silver? Once you withdraw it, you ` +
`cannot deposit it again. Your withdrawn silver amount will be added to your score. You'll only see this warning once!`;
if (!confirm(confirmationText)) return;
}

this.gameManager.withdrawSilver(locationId, amount);
public withdrawSilver(locationId: LocationId) {
this.gameManager.withdrawSilver(locationId);
}

public startWormholeFrom(planet: LocatablePlanet): Promise<LocatablePlanet | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion client/src/Frontend/Components/OpenPaneButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function OpenPlanetInfoButton({
return (
<OpenPaneButton
modal={modal}
title='Info'
title='More Info'
shortcut={TOGGLE_PLANET_INFO_PANE}
element={() => <PlanetInfoPane initialPlanetId={planetId} />}
helpContent={PlanetInfoHelpContent()}
Expand Down
11 changes: 1 addition & 10 deletions client/src/Frontend/Panes/PlanetContextPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { CapturePlanetButton } from '../Components/CapturePlanetButton';
import { VerticalSplit } from '../Components/CoreUI';
import { MineArtifactButton } from '../Components/MineArtifactButton';
import {
OpenBroadcastPaneButton,
OpenHatPaneButton,
OpenManagePlanetInventoryButton,
OpenPlanetInfoButton,
OpenUpgradeDetailsPaneButton,
Expand Down Expand Up @@ -53,15 +51,10 @@ function PlanetContextPaneContent({
}

let upgradeRow = null;
if (!p?.destroyed && owned) {
if (!p?.destroyed && owned && p?.planetType == PlanetType.PLANET) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!p?.destroyed && owned && p?.planetType == PlanetType.PLANET) {
if (!p?.destroyed && owned && p?.planetType === PlanetType.PLANET) {

upgradeRow = <OpenUpgradeDetailsPaneButton modal={modal} planetId={p?.locationId} />;
}

let hatRow = null;
if (!p?.destroyed && owned) {
hatRow = <OpenHatPaneButton modal={modal} planetId={p?.locationId} />;
}

let withdrawRow = null;
if (!p?.destroyed && owned && p?.planetType === PlanetType.TRADING_POST) {
withdrawRow = <WithdrawSilver wrapper={planet} />;
Expand All @@ -86,12 +79,10 @@ function PlanetContextPaneContent({
<VerticalSplit>
<>
{upgradeRow}
<OpenBroadcastPaneButton modal={modal} planetId={p?.locationId} />
<OpenPlanetInfoButton modal={modal} planetId={p?.locationId} />
</>
<>
<OpenManagePlanetInventoryButton modal={modal} planetId={p?.locationId} />
{hatRow}
</>
</VerticalSplit>
{withdrawRow}
Expand Down
1 change: 1 addition & 0 deletions client/src/Frontend/Utils/ShortcutConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const TOGGLE_TRANSACTIONS_PANE = "'";
export const TOGGLE_PLANET_INVENTORY_PANE = 's';
export const TOGGLE_HAT_PANE = 'x';
export const TOGGLE_ABANDON = 'r';
export const TOGGLE_WITHDRAW = ']';
export const INVADE = 'y';
export const MINE_ARTIFACT = 'f';
export const TOGGLE_BROADCAST_PANE = 'z';
Expand Down
33 changes: 20 additions & 13 deletions client/src/Frontend/Views/ArtifactRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,25 @@ export function SelectArtifactRow({
artifacts: Artifact[];
}) {
return (
<RowWrapper>
{artifacts.length > 0 &&
artifacts.map((a) => (
<span key={a.id}>
<ArtifactThumb
artifact={a}
selectedArtifact={selectedArtifact}
onArtifactChange={onArtifactChange}
/>
<Spacer width={4} />
</span>
))}
</RowWrapper>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
}}
>
<RowWrapper>
{artifacts.length > 0 &&
artifacts.map((a) => (
<span key={a.id}>
<ArtifactThumb
artifact={a}
selectedArtifact={selectedArtifact}
onArtifactChange={onArtifactChange}
/>
<Spacer width={4} />
</span>
))}
</RowWrapper>
</div>
);
}
Loading