Skip to content

Commit

Permalink
feat(ui): check is it on replays viewer site
Browse files Browse the repository at this point in the history
  • Loading branch information
Molmin committed Jun 6, 2024
1 parent 169976e commit cd92c83
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 15 deletions.
3 changes: 3 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ writeFileSync('dist/game_replay.html', indexFile)
writeFileSync('dist/ui.css', styleFile)
writeFileSync('dist/ui.js', scriptFile)

const jqueryFile = readFileSync('frontend/node_modules/jquery/dist/jquery.min.js').toString()
writeFileSync('dist/jquery.min.js', jqueryFile)

ensureDir('dist/public')
for (const file of publicFiles) copyFileSync(`frontend/assets/${file}`, `dist/public/${file}`)
2 changes: 1 addition & 1 deletion frontend/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" type="image/png" href="{{ var.sitePrefix }}/favicon.png">
<link rel="icon" type="image/png" href="{{ var.sitePrefix }}/public/favicon.png">
<link rel="stylesheet" href="{{ var.sitePrefix }}/ui.css">
<script>
window.site_prefix = '{{ var.sitePrefix }}'
Expand Down
6 changes: 3 additions & 3 deletions frontend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as pageGameReplay from './pages/game_replay'
import { gotoPage, registerPage } from './lib/page'
import { UserService } from './lib/user'
import { registerAlertEvent } from './lib/alert'
import { redirectTo } from './lib/redirect'
import { getPathName, redirectTo } from './lib/path'

registerPage('home', pageHome.init)
registerPage('login', pageLogin.init)
Expand All @@ -15,12 +15,12 @@ registerPage('game_replay', pageGameReplay.init)
$(async () => {
registerAlertEvent()
await UserService.init()
const pathname = window.location.pathname.replace(window['site_prefix'], '')
const pathname = getPathName()
if (/^\/login$/.test(pathname)) gotoPage('login')
else {
if (/^\/$/.test(pathname)) gotoPage('home')
else if (/^\/game\/[1-9][0-9]*?\/play$/.test(pathname)) gotoPage('game_play')
else if (/^\/game\/[1-9][0-9]*?\/replay$/.test(pathname)) gotoPage('game_replay')
else if (/^\/game\/[1-9][0-9]*?\/replay$/.test(pathname) || window['site_prefix'] !== '') gotoPage('game_replay')
else redirectTo('/')
}
})
4 changes: 4 additions & 0 deletions frontend/lib/redirect.ts → frontend/lib/path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function redirectTo(path: string) {
window.location.pathname = `${window['site_prefix']}${path}`
}

export function getPathName() {
return window.location.pathname.replace(window['site_prefix'], '')
}
6 changes: 3 additions & 3 deletions frontend/lib/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import superagent from 'superagent'
import { } from './jquery'
import { redirectTo } from './redirect'
import { getPathName, redirectTo } from './path'

let name = ''
let uid = 0
Expand All @@ -26,13 +26,13 @@ export class UserService {
uid = body.uid
isAdmin = body.isAdmin
if (isAdmin) $('body').addClass('isadmin')
if (window.location.pathname === '/login') {
if (getPathName() === '/login') {
redirectTo('/')
}
}
else {
UserService.token = ''
if (window.location.pathname !== '/login') {
if (getPathName() !== '/login') {
redirectTo('/login')
}
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/pages/game_play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { } from '../lib/jquery'
import { UserService } from '../lib/user'
import { GeneralsGame, PLAYER_STATUS, PlayerInfo } from '../lib/game'
import { Alert } from '../lib/alert'
import { redirectTo } from '../lib/redirect'
import { getPathName, redirectTo } from '../lib/path'

async function getInfo() {
const id = +window.location.pathname.split('/')[2]
const id = +getPathName().split('/')[2]
const { body } = await superagent.post('/game/info')
.send({ token: UserService.token, id })
if (body.error || ![body.player1, body.player2].includes(UserService.uid) || body.done)
Expand Down
9 changes: 7 additions & 2 deletions frontend/pages/game_replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import superagent from 'superagent'
import { } from '../lib/jquery'
import { UserService } from '../lib/user'
import { GeneralsGameReplay, GeneralsReplay } from '../lib/replay'
import { redirectTo } from '../lib/redirect'
import { getPathName, redirectTo } from '../lib/path'

async function getReplay() {
const id = +window.location.pathname.split('/')[2]
if (window['site_prefix'] !== '') {
const id = +(window.location.search.split('?id=')[1] || '').split('&')[0]
const { body } = await superagent.get(`/generals-replays/replays/${id}.json`)
return body
}
const id = +getPathName().split('/')[2]
const { body } = await superagent.post('/game/replay')
.send({ token: UserService.token, id })
if (body.error) redirectTo('/')
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/home.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import superagent from 'superagent'
import { } from '../lib/jquery'
import { UserService } from '../lib/user'
import { redirectTo } from '../lib/redirect'
import { redirectTo } from '../lib/path'

function registerCreateGame() {
const target = $('.page--home > .create-game > p')
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import superagent from 'superagent'
import { } from '../lib/jquery'
import { UserService } from '../lib/user'
import { redirectTo } from '../lib/redirect'
import { redirectTo } from '../lib/path'

export async function init() {
const response = await superagent.post('/user/newToken').send({ token: '' })
Expand Down
1 change: 1 addition & 0 deletions lib/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const publicFiles = [
'general.png',
'mountain.png',
'obstacle.png',
'favicon.png',
]
2 changes: 0 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ function getIndexFile() {
.replace(/{{ var.sitePrefix }}/g, '')
}
const indexFile = getIndexFile()
const favicon = readFileSync('frontend/assets/favicon.png')

app.get(`/ui-${scriptVersion}.js`, (req, res) => res.send(scriptFile))
app.get(`/ui.js`, (req, res) => res.send(readFileSync('frontend/dist/ui.js').toString()))
app.get(`/ui-${styleVersion}.css`, (req, res) => res.type('text/css').send(styleFile))
app.get(`/ui.css`, (req, res) => res.type('text/css').send(getStyleFile()))
app.get(`/jquery.min.js`, (req, res) => res.send(jqueryFile))
app.get('/favicon.png', (req, res) => res.send(favicon))

const pages = ['/', '/game/:id/play', '/game/:id/replay', '/login']
pages.forEach((url) => app.get(url, (req, res) => res.send(getIndexFile())))
Expand Down

0 comments on commit cd92c83

Please sign in to comment.