-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from csfloat/feature/improved-method-connecti…
…vity Implements Handlers for Augmented Trading
- Loading branch information
Showing
14 changed files
with
350 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
|
||
export interface CancelTradeOfferRequest { | ||
trade_offer_id: string; | ||
session_id: string; | ||
} | ||
|
||
export interface CancelTradeOfferResponse {} | ||
|
||
export const CancelTradeOffer = new SimpleHandler<CancelTradeOfferRequest, CancelTradeOfferResponse>( | ||
RequestType.CANCEL_TRADE_OFFER, | ||
async (req) => { | ||
const formData = { | ||
sessionid: req.session_id, | ||
}; | ||
|
||
const resp = await fetch(`https://steamcommunity.com/tradeoffer/${req.trade_offer_id}/cancel`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
}, | ||
body: new URLSearchParams(formData as any).toString(), | ||
}); | ||
|
||
if (!resp.ok) { | ||
throw new Error(`failed to cancel offer: ${resp.status}`); | ||
} | ||
|
||
return {} as CancelTradeOfferResponse; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
|
||
export interface CreateTradeOfferRequest { | ||
toSteamID64: string; | ||
tradeToken: string; | ||
message: string; | ||
assetIDsToGive: string[]; | ||
assetIDsToReceive: string[]; | ||
sessionID: string; | ||
forceEnglish?: boolean; | ||
} | ||
|
||
interface CreateOfferSteamResponse { | ||
email_domain?: string; | ||
strError?: string; | ||
needs_email_confirmation?: boolean; | ||
needs_mobile_confirmation?: boolean; | ||
tradeofferid?: string; | ||
} | ||
|
||
export interface CreateTradeOfferResponse { | ||
status: number; | ||
json?: CreateOfferSteamResponse; | ||
text?: string; | ||
} | ||
|
||
export const CreateTradeOffer = new SimpleHandler<CreateTradeOfferRequest, CreateTradeOfferResponse>( | ||
RequestType.CREATE_TRADE_OFFER, | ||
async (req) => { | ||
function itemMapper(assetID: string) { | ||
return { | ||
appid: 730, | ||
contextid: 2, | ||
amount: 1, | ||
assetid: assetID, | ||
}; | ||
} | ||
|
||
const offerData = { | ||
newversion: true, | ||
version: req.assetIDsToGive.length + req.assetIDsToReceive.length + 1, | ||
me: { | ||
assets: req.assetIDsToGive.map(itemMapper), | ||
currency: [], | ||
ready: false, | ||
}, | ||
them: { | ||
assets: req.assetIDsToReceive.map(itemMapper), | ||
currency: [], | ||
ready: false, | ||
}, | ||
}; | ||
|
||
const params = { | ||
trade_offer_access_token: req.tradeToken, | ||
}; | ||
|
||
const formData = { | ||
sessionid: req.sessionID, | ||
serverid: 1, | ||
partner: req.toSteamID64, | ||
tradeoffermessage: req.message || 'CSFloat Trade Offer', | ||
json_tradeoffer: JSON.stringify(offerData), | ||
captcha: '', | ||
trade_offer_create_params: JSON.stringify(params), | ||
}; | ||
|
||
const url = req.forceEnglish | ||
? 'https://steamcommunity.com/tradeoffer/new/send?l=english' | ||
: 'https://steamcommunity.com/tradeoffer/new/send'; | ||
|
||
const resp = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
// Referer is actually set in the declarative net static rules since | ||
// it is a "protected" header. Setting it here does nothing in major browsers. | ||
// Without it, Steam rejects the request as part of their anti-cross origin block. | ||
}, | ||
body: new URLSearchParams(formData as any).toString(), | ||
}); | ||
|
||
const res: CreateTradeOfferResponse = { | ||
status: resp.status, | ||
}; | ||
|
||
const text = await resp.text(); | ||
res.text = text; | ||
|
||
try { | ||
const data = JSON.parse(res.text); | ||
res.json = data; | ||
} catch (e: any) { | ||
console.error(`failed to parse json from Steam create offer: ${e.toString()}`); | ||
} | ||
|
||
return res; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
import {rgDescription, rgInventoryAsset} from '../../types/steam'; | ||
|
||
export interface FetchOwnInventoryRequest { | ||
expected_steam_id: string; | ||
app_id: number; | ||
context_id: number; | ||
} | ||
|
||
export interface InventoryResponse { | ||
more?: boolean; | ||
more_start?: boolean; | ||
Error?: string; | ||
success: boolean; | ||
rgDescriptions: Map<string, rgDescription>; | ||
rgInventory: Map<string, rgInventoryAsset>; | ||
} | ||
|
||
interface FetchOwnInventoryResponse { | ||
inventory: InventoryResponse; | ||
} | ||
|
||
export const FetchOwnInventory = new SimpleHandler<FetchOwnInventoryRequest, FetchOwnInventoryResponse>( | ||
RequestType.FETCH_OWN_INVENTORY, | ||
async (req) => { | ||
// Will error out if expected_steam_id != logged in user | ||
const resp = await fetch( | ||
`https://steamcommunity.com/profiles/${req.expected_steam_id}/inventory/json/${req.app_id}/${req.context_id}/?trading=1`, | ||
{ | ||
credentials: 'include', | ||
} | ||
); | ||
if (!resp.ok) { | ||
throw new Error(`Invalid response code: ${resp.status}`); | ||
} | ||
|
||
const inventory = (await resp.json()) as InventoryResponse; | ||
|
||
return { | ||
inventory, | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
|
||
interface FetchSteamUserRequest {} | ||
|
||
interface FetchSteamUserResponse { | ||
isLoggedIn: boolean; | ||
steamID?: string; | ||
sessionID?: string; | ||
} | ||
|
||
export const FetchSteamUser = new SimpleHandler<FetchSteamUserRequest, FetchSteamUserResponse>( | ||
RequestType.FETCH_STEAM_USER, | ||
async (req) => { | ||
const resp = await fetch('https://steamcommunity.com'); | ||
if (!resp.ok) { | ||
throw new Error('non-ok response for steamcommunity.com'); | ||
} | ||
|
||
const res: FetchSteamUserResponse = { | ||
isLoggedIn: false, | ||
}; | ||
|
||
const text = await resp.text(); | ||
const steamIDMatch = text.match(/g_steamID = "(\d+)"/); | ||
if (steamIDMatch) { | ||
res.isLoggedIn = true; | ||
res.steamID = steamIDMatch[1]; | ||
} | ||
|
||
const sessionIDMatch = text.match(/g_sessionID = "([0-9a-fA-F]+)"/); | ||
if (sessionIDMatch) { | ||
res.sessionID = sessionIDMatch[1]; | ||
} | ||
|
||
return res; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
import {gStore} from '../../storage/store'; | ||
import {StorageKey} from '../../storage/keys'; | ||
import {PING_CSFLOAT_TRADE_STATUS_ALARM_NAME} from '../../alarms/csfloat_trade_pings'; | ||
|
||
export interface PingStatusRequest {} | ||
|
||
export interface PingStatusResponse { | ||
last_ping_ms?: number; | ||
next_ping_ms?: number; | ||
} | ||
|
||
export const PingStatus = new SimpleHandler<PingStatusRequest, PingStatusResponse>( | ||
RequestType.PING_STATUS, | ||
async (req) => { | ||
const resp: PingStatusResponse = {}; | ||
|
||
const lastPing = await gStore.getWithStorage<number>(chrome.storage.local, StorageKey.LAST_TRADE_PING_ATTEMPT); | ||
if (lastPing) { | ||
resp.last_ping_ms = lastPing; | ||
} | ||
|
||
if (chrome.alarms) { | ||
const alarm = await chrome.alarms.get(PING_CSFLOAT_TRADE_STATUS_ALARM_NAME); | ||
if (alarm) { | ||
resp.next_ping_ms = alarm.scheduledTime; | ||
} | ||
} | ||
|
||
return resp; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
import {pingTradeStatus} from '../../alarms/csfloat_trade_pings'; | ||
|
||
export interface PingTradeStatusRequest { | ||
// Steam ID we're expecting to fulfill the ping for. | ||
steam_id: string; | ||
} | ||
|
||
export interface PingTradeStatusResponse {} | ||
|
||
export const PingTradeStatus = new SimpleHandler<PingTradeStatusRequest, PingTradeStatusResponse>( | ||
RequestType.PING_TRADE_STATUS, | ||
async (req) => { | ||
await pingTradeStatus(req.steam_id); | ||
|
||
return {}; | ||
} | ||
); |
Oops, something went wrong.