Westmarches Game Blog

Official blog for the Westmarches Game Software

Turning Server Actions into Real Game Controls

Building JavaScript-Driven Game Buttons in West Marches Game

Modern web apps — especially games — live or die by responsiveness.

Click → wait → page reload is fine for CRUD dashboards.
It’s terrible for gameplay.

In my latest commit to West Marches Game, I moved several core game interactions away from traditional full-page redirects and toward JavaScript-driven actions.

👉 Commit: “javascript game buttons” View Commit on GitHub

This post walks through what changed, why it matters, and how small controller tweaks unlock much richer UX.


The Problem: Every Action Reloaded the Page

Originally, game interactions behaved like classic MVC applications:

  • Delete a poll slot → redirect
  • Change player status → redirect
  • Modify game state → redirect

Every action ended with:

return $this->redirect([$url]);

Technically correct.

But functionally painful.

From a player’s perspective:

  1. Click button
  2. Entire page reloads
  3. Scroll position lost
  4. Context broken
  5. Game feels slow

For something meant to feel interactive, this was a mismatch.


The Goal: Buttons That Feel Like a Game

The objective was simple:

Game buttons should behave like game controls — not admin forms.

That means:

  • Instant feedback
  • No page reloads
  • Background server updates
  • Progressive enhancement (still works without JS)

The Key Idea: Dual-Mode Controller Actions

Instead of creating new endpoints, I upgraded existing ones.

New Pattern

Controller actions now accept a $js flag:

public function actionPlayerstatus($id, $campaignId, $gamePlayerId, $js = false)

and

public function actionPollslotdelete($id, $campaignId, $slotId, $js = false)

This tiny change unlocked two execution modes:

ModeBehavior
Normal requestRedirect as before
JavaScript requestReturn simple success response

The Core Change

Previously:

return $this->redirect([$url]);

Now:

return ($js) ? "true" : $this->redirect([$url]);

That’s it.

But architecturally, it’s huge.

The same endpoint now supports:

  • traditional navigation
  • AJAX interaction

No duplicated logic.
No parallel APIs.
No rewrite.


Why This Works So Well

1. Progressive Enhancement

If JavaScript fails:

✅ App still works
✅ Controllers still redirect
✅ No broken flows

JavaScript becomes an enhancement — not a requirement.


2. Zero Business Logic Duplication

A common mistake:

  • /delete → redirect controller
  • /api/delete → AJAX controller

Now we have one source of truth.

All validation stays centralized:

if (empty($player)) {
    return ($js) ? "true" : $this->redirect([$url]);
}

3. Cleaner Frontend Integration

JavaScript buttons can now do:

$.post('/game/playerstatus?...&js=1')

Then simply:

  • update UI
  • toggle icons
  • remove rows
  • refresh partial sections

No navigation required.


Where This Was Applied

This commit touched multiple gameplay surfaces:

  • Poll slot deletion
  • Player status toggling
  • Event-driven game interactions
  • Partial view updates

Six files changed overall, introducing JavaScript-aware workflows across controllers and views. (GitHub)


The UX Difference

Before:

Click → reload → wait → find your place again.

After:

Click → state updates instantly.

The application finally behaves like a live session manager, not a static web form.


An Unexpected Benefit: Backend Simplicity

Ironically, adding JavaScript made the backend simpler.

Instead of designing a full REST API layer, we:

  • reused MVC routes
  • added one optional parameter
  • returned minimal responses

Sometimes the cleanest solution is not adding architecture — but making existing architecture flexible.


Design Principle: Controllers Should Speak Two Languages

A powerful takeaway from this change:

Controllers should handle both navigation and interaction.

Think of them as bilingual:

  • HTML for humans
  • lightweight responses for JavaScript

You don’t always need a separate API to build interactive systems.


What Comes Next

This commit lays the groundwork for bigger improvements:

  • inline updates
  • live campaign management
  • reactive UI components
  • eventually real-time gameplay features

The game now has controls instead of links — and that changes everything.


Author

  • I’ve been passionate about computer programming since I finished playing my very first video game. You could say I was obsessed with computers after that. It wasn’t long until my parents got their first home computer. I learned how to connect to a dial-up modem to check email. Then I memorized the file structure of the Operating System. Then I took the computer apart to try and make sense what was going on. I was nine years old.

    I spent my most of my teenage years learning how to make video games on the family computer. I was already taking college level programming classes before I graduated high school.

    I spent my twenties working for many different types of companies, getting mentorship where I could and learning the business side of software.

    Westmarches game is a passion project. I enjoy playing tabletop games with my friends and this software makes our games better. I plan on contributing to it and improving it for a long time. Learn More.