Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kensaa committed Jul 22, 2023
0 parents commit b2119db
Show file tree
Hide file tree
Showing 25 changed files with 4,744 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"tabWidth": 4,
"useTabs": false,
"semi": false,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"singleAttributePerLine": false
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Game Activity

a little software that records time spent on a fullscreen window (often game) and makes a pie chart

to install it you just need to run the executable at startup
(%appdata%\Microsoft\Windows\Start Menu\Programs\Startup for Windows)
5 changes: 5 additions & 0 deletions background/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
yarn-error.log
dist
node_modules
public/
build/
Binary file added background/img/clock.ico
Binary file not shown.
Binary file added background/img/clock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions background/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "game-activity",
"version": "1.0.0",
"description": "",
"main": "dist/main.js",
"bin": "dist/main.js",
"scripts": {
"start": "tsc && node .",
"build": "rm -rf public && mkdir public && cd ../ui && rm -rf dist/ && yarn && yarn build && cp -r dist/* ../background/public && cd ../background && rm -rf build && yarn && tsc && pkg ."
},
"author": "Kensa",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^20.4.2",
"pkg": "^5.8.1",
"typescript": "^5.1.6"
},
"dependencies": {
"active-win": "^8.1.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"node-hide-console-window": "^2.1.1",
"robotjs": "^0.6.0",
"systray2": "^2.1.4"
},
"pkg": {
"outputPath": "build",
"targets": [
"node-latest-win-x64"
],
"assets": [
"public/**/*",
"dist/**/*",
"img/**/*"
]
}
}
154 changes: 154 additions & 0 deletions background/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import * as activeWindow from 'active-win'
import * as robotjs from 'robotjs'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as express from 'express'
import * as cors from 'cors'
import SysTray from 'systray2'
import { showConsole, hideConsole } from 'node-hide-console-window'
hideConsole()
const PORT = 49072

const { width, height } = robotjs.getScreenSize()
const platorm = os.platform()
let folder = ''
if (platorm === 'win32') {
folder = path.join(os.homedir(), 'AppData', 'Roaming', 'game-activity')
} else if (platorm === 'linux') {
folder = path.join(os.homedir(), '.config', 'game-activity')
} else {
console.error('Unsupported platform')
process.exit(1)
}

if (!fs.existsSync(folder)) fs.mkdirSync(folder)

let timeRecord: Record<string, number> = {}

const date = new Date()
const day = date.getDate().toString().padStart(2, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const year = date.getFullYear()
const filename = `${day}-${month}-${year}.json`
const filepath = path.join(folder, filename)
if (fs.existsSync(filepath)) {
timeRecord = JSON.parse(fs.readFileSync(filepath, 'utf8'))
} else {
fs.writeFileSync(filepath, JSON.stringify({}))
}

const img = path.join(
__dirname,
'..',
'img',
`clock.${platorm === 'win32' ? 'ico' : 'png'}`
)

const urlItem = {
title: 'open webpage',
tooltip: 'open the webpage to see your game activity',
checked: false,
enabled: true,
click: () => {
var url = `http://localhost:${PORT}`
var start =
process.platform == 'darwin'
? 'open'
: process.platform == 'win32'
? 'start'
: 'xdg-open'
require('child_process').exec(start + ' ' + url)
}
}

const exitItem = {
title: 'exit',
tooltip: 'close the program',
checked: false,
enabled: true,
click: () => {
process.exit(0)
}
}

const systray = new SysTray({
menu: {
title: 'Game Activity',
icon: img,
tooltip: 'Game Activity',
items: [urlItem, exitItem]
},
copyDir: true
})

systray.onClick(action => {
//@ts-ignore
if (action.item.click != null) {
//@ts-ignore
action.item.click()
}
})
const app = express()
app.use(
cors({
origin: '*'
})
)

app.listen(PORT, () => console.log(`listening on port ${PORT}`))

app.get('/files', (req, res) => {
res.json(fs.readdirSync(folder).map(e => path.parse(e).name))
})

app.get('/file/:file', (req, res) => {
const { file } = req.params
if (!file) return res.sendStatus(404)
const filepath = path.join(
folder,
`${file}${file.endsWith('.json') ? '' : '.json'}`
)
if (!fs.existsSync(filepath)) return res.sendStatus(404)
let data = undefined
try {
data = JSON.parse(fs.readFileSync(filepath, 'utf-8'))
} catch {
return res.sendStatus(500)
}
res.json(data)
})

app.get('/all', (req, res) => {
const files = fs.readdirSync(folder)
const data: Record<string, Record<string, number>> = {}
for (const file of files) {
data[path.parse(file).name] = JSON.parse(
fs.readFileSync(path.join(folder, file), 'utf-8')
)
}
res.json(data)
})

const publicPath = path.join(__dirname, '..', 'public')
if (!fs.existsSync(publicPath)) fs.mkdirSync(publicPath)
app.use('/', express.static(publicPath))
app.get('*', (req: express.Request, res: express.Response) => {
res.sendFile(path.join(publicPath, 'index.html'))
})

setInterval(async () => {
const window = await activeWindow()
if (!window) return
const { name } = window.owner
const isFullscreen =
width == window.bounds.width && height == window.bounds.height
if (!isFullscreen) return
if (timeRecord[name] === undefined) {
timeRecord[name] = 1
} else {
timeRecord[name]++
}

fs.writeFileSync(filepath, JSON.stringify(timeRecord, null, 4))
}, 1000)
13 changes: 13 additions & 0 deletions background/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node",
"target": "ES2020",
"strict": true,
"strictFunctionTypes": true,
"sourceMap": true,
"noImplicitAny": true,
"outDir": "dist/"
},
"exclude": ["node_modules", "**/node_modules/*"]
}
Loading

0 comments on commit b2119db

Please sign in to comment.