Skip to content

Commit

Permalink
update client
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 9, 2020
1 parent 5b6450d commit 6f537b4
Show file tree
Hide file tree
Showing 23 changed files with 457 additions and 258 deletions.
2 changes: 1 addition & 1 deletion last_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
last updated: Sun Oct 4 02:09:09 UTC 2020
last updated: Fri Oct 9 21:31:40 UTC 2020
10 changes: 5 additions & 5 deletions src/api/ContractsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ class ContractsAPI extends EventEmitter {

const playerIds = await aggregateBulkGetter<EthAddress>(
nPlayers,
50,
200,
async (start, end) =>
(await contract.bulkGetPlayers(start, end)).map(address)
);
Expand Down Expand Up @@ -725,7 +725,7 @@ class ContractsAPI extends EventEmitter {

const arrivalsUnflattened = await aggregateBulkGetter<QueuedArrival[]>(
nPlanets,
500,
1000,
async (start, end) => {
return (
await contract.bulkGetPlanetArrivals(start, end)
Expand All @@ -748,7 +748,7 @@ class ContractsAPI extends EventEmitter {
terminalEmitter.println('(4/6) Getting planet IDs...');
const planetIds = await aggregateBulkGetter<BigInteger>(
nPlanets,
1000,
2000,
async (start, end) => await contract.bulkGetPlanetIds(start, end),
true
);
Expand All @@ -757,15 +757,15 @@ class ContractsAPI extends EventEmitter {
RawPlanetExtendedInfo
>(
nPlanets,
500,
1000,
async (start, end) =>
await contract.bulkGetPlanetsExtendedInfo(start, end),
true
);
terminalEmitter.println('(6/6) Getting planet data...');
const rawPlanets = await aggregateBulkGetter<RawPlanetData>(
nPlanets,
500,
1000,
async (start, end) => await contract.bulkGetPlanets(start, end),
true
);
Expand Down
5 changes: 3 additions & 2 deletions src/api/EthereumAccountManager.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import * as stringify from 'json-stable-stringify';
import { Provider, TransactionReceipt } from '@ethersproject/providers';
import { JsonRpcProvider, TransactionReceipt } from '@ethersproject/providers';
import { providers, Contract, Wallet, utils } from 'ethers';
import { EthAddress } from '../_types/global/GlobalTypes';
import { address } from '../utils/CheckedTypeUtils';

class EthereumAccountManager {
static instance: EthereumAccountManager | null = null;

private readonly provider: Provider;
private readonly provider: JsonRpcProvider;
private signer: Wallet | null;
private readonly knownAddresses: EthAddress[];

private constructor() {
const isProd = process.env.NODE_ENV === 'production';
const url = isProd ? 'https://rpc.xdaichain.com/' : 'http://localhost:8545';
this.provider = new providers.JsonRpcProvider(url);
this.provider.pollingInterval = 8000;
this.signer = null;
this.knownAddresses = [];
const knownAddressesStr = localStorage.getItem('KNOWN_ADDRESSES');
Expand Down
5 changes: 4 additions & 1 deletion src/api/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,17 @@ class GameManager extends EventEmitter implements AbstractGameManager {
}

async addAccount(coords: WorldCoords): Promise<boolean> {
/*
const homePlanetId = locationIdFromBigInt(mimcHash(coords.x, coords.y));
const planet = this.getPlanetWithId(homePlanetId);
if (!planet || planet.owner !== this.account) {
return Promise.resolve(false);
}
*/
await this.localStorageManager.setHomeCoords(coords);
this.initMiningManager(coords);
this.homeCoords = coords;
this.homeHash = locationIdFromBigInt(mimcHash(coords.x, coords.y));
return true;
}

Expand All @@ -631,7 +634,7 @@ class GameManager extends EventEmitter implements AbstractGameManager {
// initialize in a lower perlin area
do {
const t = Math.random() * 2 * Math.PI;
const r = (0.5 + Math.random() * 0.5) * this.worldRadius;
const r = (0.7 + Math.random() * 0.3) * this.worldRadius;
x = Math.floor(Math.cos(t) * r);
y = Math.floor(Math.sin(t) * r);
p = perlin({ x, y }, false);
Expand Down
40 changes: 22 additions & 18 deletions src/api/PlanetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ import {
} from './ContractsAPI';
import NotificationManager from '../utils/NotificationManager';

interface MemoizedCoordHashes {
[x: number]: { [y: number]: Location };
}
type CoordsString = string;
type MemoizedCoordHashes = Map<CoordsString, Location>;

const getCoordsString = (coords: WorldCoords): CoordsString => {
return `${coords.x},${coords.y}`;
};

export class PlanetHelper {
private readonly planets: PlanetMap;
Expand Down Expand Up @@ -66,7 +69,7 @@ export class PlanetHelper {
this.address = address;
this.planets = planets;
this.contractConstants = contractConstants;
this.coordsToLocation = {};
this.coordsToLocation = new Map();
this.planetLocationMap = {};
const planetArrivalIds: PlanetVoyageIdMap = {};
const arrivals: VoyageMap = {};
Expand Down Expand Up @@ -108,12 +111,12 @@ export class PlanetHelper {
// set interval to update all planets every 120s
setInterval(() => {
for (const planetId of Object.keys(this.planets)) {
setTimeout(() => {
const planet = this.planets[planetId];
if (planet && hasOwner(planet)) {
this.updatePlanetToTime(planet, Date.now());
}
}, Math.floor(120000 * Math.random())); // evenly distribute updates
// setTimeout(() => {
const planet = this.planets[planetId];
if (planet && hasOwner(planet)) {
this.updatePlanetToTime(planet, Date.now());
}
// }, Math.floor(120000 * Math.random())); // evenly distribute updates
}
}, 120000);
}
Expand Down Expand Up @@ -188,14 +191,13 @@ export class PlanetHelper {
// returns an empty planet if planet is not in contract
// returns null if this isn't a planet, according to hash and coords
public getPlanetWithCoords(coords: WorldCoords): Planet | null {
const { x, y } = coords;
let location: Location | null = null;
if (this.coordsToLocation[x] && this.coordsToLocation[x][y]) {
location = this.coordsToLocation[x][y];
}
const str = getCoordsString(coords);

const location = this.coordsToLocation.get(str);
if (!location) {
return null;
}

return this.getPlanetWithLocation(location);
}

Expand All @@ -217,9 +219,11 @@ export class PlanetHelper {

public addPlanetLocation(planetLocation: Location): void {
this.planetLocationMap[planetLocation.hash] = planetLocation;
const { x, y } = planetLocation.coords;
if (!this.coordsToLocation[x]) this.coordsToLocation[x] = {};
this.coordsToLocation[x][y] = planetLocation;
const str = getCoordsString(planetLocation.coords);
if (!this.coordsToLocation.has(str)) {
this.coordsToLocation.set(str, planetLocation);
}

if (!this.planets[planetLocation.hash]) {
this.planets[planetLocation.hash] = this.defaultPlanetFromLocation(
planetLocation
Expand Down
4 changes: 4 additions & 0 deletions src/api/UIStateStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum UIDataKey {

notifMove = 'notifMove',
newPlayer = 'newPlayer',
highPerf = 'highPerf',
}
export type UIDataValue = boolean;

Expand All @@ -34,6 +35,7 @@ export type UIData = {

notifMove: boolean;
newPlayer: boolean;
highPerf: boolean;
};

export const defaultUserData: UIData = {
Expand All @@ -50,6 +52,8 @@ export const defaultUserData: UIData = {

notifMove: true,
newPlayer: true,

highPerf: false,
};

export function useStoredUIState<T extends UIDataValue>(
Expand Down
10 changes: 9 additions & 1 deletion src/app/GameWindow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import React, { useContext, useEffect, useState, createContext } from 'react';
import { useStoredUIState, UIDataKey } from '../api/UIStateStorageManager';
import UIEmitter, { UIEmitterEvent } from '../utils/UIEmitter';
import { copyPlanetStats, PlanetStatsInfo } from '../utils/Utils';
import { EthAddress, Planet } from '../_types/global/GlobalTypes';
Expand Down Expand Up @@ -30,6 +31,8 @@ export const AccountContext = createContext<EthAddress | null>(null);
export const SelectedContext = createContext<Planet | null>(null);
export const SelectedStatContext = createContext<PlanetStatsInfo | null>(null);

export const HiPerfContext = createContext<boolean | null>(null);

export default function GameWindow() {
const uiManager = useContext<GameUIManager | null>(GameUIManagerContext);
const [selected, setSelected] = useState<Planet | null>(null);
Expand Down Expand Up @@ -121,12 +124,17 @@ export default function GameWindow() {
setAccount(uiManager.getAccount());
}, [uiManager]);

// make this into a context (fix this later)
const hiPerfHook = useStoredUIState<boolean>(UIDataKey.highPerf, uiManager);

return (
<SelectedContext.Provider value={selected}>
<ContextMenuContext.Provider value={contextMenu}>
<AccountContext.Provider value={account}>
<SelectedStatContext.Provider value={planetStats}>
<GameWindowLayout />
<HiPerfContext.Provider value={hiPerfHook[0]}>
<GameWindowLayout hiPerfHook={hiPerfHook} />
</HiPerfContext.Provider>
</SelectedStatContext.Provider>
</AccountContext.Provider>
</ContextMenuContext.Provider>
Expand Down
18 changes: 14 additions & 4 deletions src/app/GameWindowLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ import { useStoredUIState, UIDataKey } from '../api/UIStateStorageManager';
import GameUIManager from './board/GameUIManager';
import GameUIManagerContext from './board/GameUIManagerContext';
import { PrivatePane } from './GameWindowPanes/PrivatePane';
import { Hook } from '../_types/global/GlobalTypes';

export function GameWindowLayout() {
export function GameWindowLayout({
hiPerfHook,
}: {
hiPerfHook: Hook<boolean>;
}) {
const planetDetHook = useState<boolean>(true);
const helpHook = useState<boolean>(false);
const leaderboardHook = useState<boolean>(false);
Expand All @@ -59,14 +64,15 @@ export function GameWindowLayout() {
const privateHook = useState<boolean>(false);

const uiManager = useContext<GameUIManager | null>(GameUIManagerContext);

const newPlayerHook = useStoredUIState<boolean>(
UIDataKey.newPlayer,
uiManager
);

return (
<WindowWrapper>
<Tooltip />
<Tooltip hiPerf={hiPerfHook[0]} />

{/* modals (fragment is purely semantic) */}
<>
Expand All @@ -83,7 +89,11 @@ export function GameWindowLayout() {
<TwitterVerifyPane hook={twitterVerifyHook} />
<TwitterBroadcastPane hook={twitterBroadcastHook} />
<HatPane hook={hatHook} />
<SettingsPane hook={settingsHook} privateHook={privateHook} />
<SettingsPane
hook={settingsHook}
privateHook={privateHook}
hiPerfHook={hiPerfHook}
/>
<PrivatePane hook={privateHook} />
</>

Expand Down Expand Up @@ -114,7 +124,7 @@ export function GameWindowLayout() {
<ControllableCanvas />
</CanvasWrapper>

<CoordsPane />
<CoordsPane hiPerf={hiPerfHook[0]} />

<LHSWrapper>
<ContextMenu>
Expand Down
4 changes: 3 additions & 1 deletion src/app/GameWindowPanes/CoordsPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ const StyledCoordsPane = styled.div`
width: 16em;
height: 4em;
`;
export function CoordsPane() {
export function CoordsPane({ hiPerf }: { hiPerf: boolean }) {
const [hovering, setHovering] = useState<boolean>(false);
const [hidden, setHidden] = useState<boolean>(false);

if (hiPerf) return <></>;

return (
<StyledCoordsPane
onClick={() => setHidden((b) => !b)}
Expand Down
4 changes: 2 additions & 2 deletions src/app/GameWindowPanes/HatPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState } from 'react';
import styled from 'styled-components';
import { Sub } from '../../components/Text';
import { HAT_SIZES } from '../../utils/constants';
import { getPlanetColors } from '../../utils/ProcgenUtils';
import { getPlanetCosmetic } from '../../utils/ProcgenUtils';
import UIEmitter, { UIEmitterEvent } from '../../utils/UIEmitter';
import { EthAddress, Planet } from '../../_types/global/GlobalTypes';
import GameUIManager from '../board/GameUIManager';
Expand Down Expand Up @@ -96,7 +96,7 @@ export function HatPane({ hook }: { hook: ModalHook }) {
<StyledHatPane>
<div>
<Sub>HAT</Sub>
<span>{getPlanetColors(selected).hatType}</span>
<span>{getPlanetCosmetic(selected).hatType}</span>
</div>
<div>
<Sub>HAT Level</Sub>
Expand Down
Loading

0 comments on commit 6f537b4

Please sign in to comment.