SlideShare ist ein Scribd-Unternehmen logo
1 von 32
go-man API
Learning go-lang & writing a game
Why?
 By day: I am architect/developer hacking
at Java based api’s
 By night: Still harboring a passion for
video games and regularly write small
games to try out new tech on my daily
commute (rarely finish them…)
Fusion
Why not combine both pursuits to retain a
level of interest?
Goals:-
 Better understanding of go-lang
 Maybe and actual finished skunkworks
project?
Dissecting the game
 Broke the idea of “pac-man” down to a
series of API calls.
 Create game
 Add player(s)
 Move players
 Repeat to fade
Concurrency
 Lightbulb moment!
 Multiple players in same game =
concurrency
Parallelism
 Multiple games in single server =
parallelism
Caveats
 Probably not the best way to do this
 First golang code I’ve developed
 Highly inefficient
 Websockets would’ve been MUCH better
 BUT, it’s fun to try it
Starting the game
HTTP POST request to create a new game with
following parms:-
 Game name
 Max number of GoMen
 Max number of GoGhosts
 Time to wait for players
What happens?
 Creates new game on server
 Creates golang process waiting for timeout to start
game.
 Creates a single channel to listen for player
updates
Add players to game
HTTP PUT request to add a player to a
game with parms:-
 Game id
 Player name
 Player type (GoMan/GoGhost)
If total players reached game will begin
otherwise game waits for players
Time up
If wait time expires, game server adds
remaining players as CPU controlled players.
Creates new golang process for each missing
player
Dumb AI:
Wait for ¼ second.
Pick random number between 0-3
Send move request to game channel
repeat…
Game States
“new” – just created
“waiting” – waiting for players to join
“playing” – game active
“over” – all GoMan players have died
“won” – all pills eaten
Power Pills
If a player eats a power pill a separate
golang process is submitted that waits for
duration of pill. At end of timer game reverts
to normal.
Game Client
Originally I started developing a separate
client in go-lang.
Stopped development of this because I
wanted people to try the game easily on the
web.
Switched to JavaScript based client as this
was the most likely client to consume the
API.
Game Client - technology
 HTML5 + JavaScript
 Twitter bootstrap
 HTML5 canvas
 Ajax calls to RESTful API
Game Client - features
 List games
 Create new game
 Join existing game
 Play game
Game Server
Server implements MVC pattern:
Models = game board + players
View = JSON representation of game state
Controller = RESTful API calls to perform
CRUD on a game instance.
Game Server - technology
 Standalone golang executable or
 Google app engine app
Uses:
 Gorillamux (for http routing)
 Game state stored in memory
Game Server – lessons learned
Things I learned during development:-
Started using filesystem as simple datastore for
games. Got tired of serialisation / deserialisation and
realised it was unnecessary for my simple demo.
Originally modeled board cells as a 2d byte array.
But these force client to have to deal with Base64
encoded binary data (nasty). Changed definition to a
2d array of “runes” for an easier life.
BoardCells [][]rune
Game Server – logic
 All game logic resides on server. Game
state / player state etc.
 Game clients are completely dumb.
 Weakness in API is that players are not
authenticated so they could easily fake
each others moves by using the other
player’s GUID.
Game Server – concurrency
 Game controller writes PlayerMove
requests to a single channel (one per
game)
 This prevents concurrent updates to same
board (two players eaten same pill etc.)
 PlayerMove request includes a unique
response channel, to return response
back to original requester
Game Server – concurrent requests
move me
Game
request
channel
Players can be either remote web players
or local CPU controlled players on server
Game Server – concurrent responses
Players receive a new instance of the
Game board after their move
Bad move
Game Server – stats
golang is FAST!
 Normal game 1 goman & 4 goghosts runs
in browser @ 60fps
 5 x 60 (300) requests per second.
Average 3k per response. 1MB per sec
traffic.
 Running locally it is handled with ease
80+ players in a single game
 DO NOT PLAY ON MOBILE PHONE
WITH DATA CONTRACT!!!
Game Models - GameBoard
type GameBoard struct {
Id string
Name string
PillsRemaining int
Players map[string]*Player
MaxGoMenAllowed int
MaxGoGhostsAllowed int
WaitForPlayersSeconds int
State GameState
PowerPillsActive int
CreatedTime time.Time
LastUpdatedTime time.Time
GameStartTime time.Time
BoardCells [][]rune
}
Game Models - Player
type Player struct {
Location Point
Id string
Type PlayerType
State PlayerState
Name string
cpuControlled bool
Score int
Lives int
}
Game Models - GameState
const (
NewGame GameState = "new"
WaitingForPlayers = "waiting"
PlayingGame = "playing"
GameWon = "won"
GameOver = "over"
)
Game Models - PlayerState
const (
Alive PlayerState = "alive"
Dying = "dying"
Dead = "dead"
Spawning = "spawning"
)
Game Models - PlayerMove
Request interface
type PlayerMove struct {
GameId string
PlayerToMove Player
ResponseChannel chan (PlayerMoveResponse)
}
Response interface
type PlayerMoveResponse struct {
Board GameBoard
Error error
}
Response written back to channel passed in request
ASCII Client
HTML5 Canvas Client
Demo
Client: http://go-man-client.herokuapp.com/
(Hosted as simple static site)
Source: https://github.com/telecoda/go-man-javascript-client
API: http://go-man-app.appspot.com
(Hosted on single GAE instance)
Source: https://github.com/telecoda/go-man-app
Wiki: https://github.com/telecoda/go-man-app/wiki
About me
robbaines@gmail.com
@telecoda

Weitere ähnliche Inhalte

Was ist angesagt?

Final presentation
Final presentationFinal presentation
Final presentationrabman
 
Local development environment
Local development environmentLocal development environment
Local development environmentJohn Dorner
 
GNU Backgammon vs eXtremeGammon - my favourite features
GNU Backgammon vs eXtremeGammon - my favourite featuresGNU Backgammon vs eXtremeGammon - my favourite features
GNU Backgammon vs eXtremeGammon - my favourite featuresWayne Joseph
 
Remastering of ubuntu
Remastering of ubuntuRemastering of ubuntu
Remastering of ubuntuAmit Karpe
 
Emulation: Machines Within Machines
Emulation: Machines Within MachinesEmulation: Machines Within Machines
Emulation: Machines Within MachinesSchool
 
warp engine - an open source realtime push engine
warp engine - an open source realtime push enginewarp engine - an open source realtime push engine
warp engine - an open source realtime push engineDavid Pichsenmeister
 
Installing Japanese environment(mozc) on Debian 8 + Mate
Installing Japanese environment(mozc) on Debian 8 + Mate Installing Japanese environment(mozc) on Debian 8 + Mate
Installing Japanese environment(mozc) on Debian 8 + Mate Ikuru Kanuma
 
Developing Multiplayer Games in Unity3D
Developing Multiplayer Games in Unity3DDeveloping Multiplayer Games in Unity3D
Developing Multiplayer Games in Unity3DAdrian Popovici
 

Was ist angesagt? (11)

Final presentation
Final presentationFinal presentation
Final presentation
 
Desktop Application In Linux
Desktop Application In LinuxDesktop Application In Linux
Desktop Application In Linux
 
Local development environment
Local development environmentLocal development environment
Local development environment
 
GNU Backgammon vs eXtremeGammon - my favourite features
GNU Backgammon vs eXtremeGammon - my favourite featuresGNU Backgammon vs eXtremeGammon - my favourite features
GNU Backgammon vs eXtremeGammon - my favourite features
 
Remastering of ubuntu
Remastering of ubuntuRemastering of ubuntu
Remastering of ubuntu
 
Emulation: Machines Within Machines
Emulation: Machines Within MachinesEmulation: Machines Within Machines
Emulation: Machines Within Machines
 
warp engine - an open source realtime push engine
warp engine - an open source realtime push enginewarp engine - an open source realtime push engine
warp engine - an open source realtime push engine
 
Installing Japanese environment(mozc) on Debian 8 + Mate
Installing Japanese environment(mozc) on Debian 8 + Mate Installing Japanese environment(mozc) on Debian 8 + Mate
Installing Japanese environment(mozc) on Debian 8 + Mate
 
Squash that Bug!
Squash that Bug!Squash that Bug!
Squash that Bug!
 
Amazon Ec2
Amazon Ec2Amazon Ec2
Amazon Ec2
 
Developing Multiplayer Games in Unity3D
Developing Multiplayer Games in Unity3DDeveloping Multiplayer Games in Unity3D
Developing Multiplayer Games in Unity3D
 

Ähnlich wie go-man API

Behind the Scenes: Deploying a Low-Latency Multiplayer Game Globally
Behind the Scenes: Deploying a Low-Latency Multiplayer Game GloballyBehind the Scenes: Deploying a Low-Latency Multiplayer Game Globally
Behind the Scenes: Deploying a Low-Latency Multiplayer Game GloballyJames Gwertzman
 
Building fast,scalable game server in node.js
Building fast,scalable game server in node.jsBuilding fast,scalable game server in node.js
Building fast,scalable game server in node.jsXie ChengChao
 
Making a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancingMaking a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancingJulio Gorgé
 
Full stack development in Go
Full stack development in GoFull stack development in Go
Full stack development in GoYves Junqueira
 
GDC Next 2013 - Synching Game States Across Multiple Devices
GDC Next 2013 - Synching Game States Across Multiple DevicesGDC Next 2013 - Synching Game States Across Multiple Devices
GDC Next 2013 - Synching Game States Across Multiple DevicesDavid Geurts
 
Html5 Game Development with Canvas
Html5 Game Development with CanvasHtml5 Game Development with Canvas
Html5 Game Development with CanvasPham Huy Tung
 
Proving correctness of a multiplayer game server
Proving correctness of a multiplayer game serverProving correctness of a multiplayer game server
Proving correctness of a multiplayer game serverIndicThreads
 
SuperStreamer: Enabling Progressive Content Streaming in a Game Engine
SuperStreamer: Enabling Progressive Content Streaming in a Game EngineSuperStreamer: Enabling Progressive Content Streaming in a Game Engine
SuperStreamer: Enabling Progressive Content Streaming in a Game EngineAnand Bhojan
 
Dedicated Game Servers
Dedicated Game ServersDedicated Game Servers
Dedicated Game Serverswebhostingguy
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerTikal Knowledge
 
Game server development in node.js in jsconf eu
Game server development in node.js in jsconf euGame server development in node.js in jsconf eu
Game server development in node.js in jsconf euXie ChengChao
 
Supersize your production pipe enjmin 2013 v1.1 hd
Supersize your production pipe    enjmin 2013 v1.1 hdSupersize your production pipe    enjmin 2013 v1.1 hd
Supersize your production pipe enjmin 2013 v1.1 hdslantsixgames
 
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...Amazon Web Services
 
Programming Language Final PPT
Programming Language Final PPTProgramming Language Final PPT
Programming Language Final PPTMatthew Chang
 
GDC 2015 - Low-latency Multiplayer Gaming with AWS
GDC 2015 - Low-latency Multiplayer Gaming with AWS GDC 2015 - Low-latency Multiplayer Gaming with AWS
GDC 2015 - Low-latency Multiplayer Gaming with AWS Nate Wiger
 
Building Multiplayer Games (w/ Unity)
Building Multiplayer Games (w/ Unity)Building Multiplayer Games (w/ Unity)
Building Multiplayer Games (w/ Unity)Noam Gat
 
Lessons Learned with Unity and WebGL
Lessons Learned with Unity and WebGLLessons Learned with Unity and WebGL
Lessons Learned with Unity and WebGLLior Tal
 

Ähnlich wie go-man API (20)

Behind the Scenes: Deploying a Low-Latency Multiplayer Game Globally
Behind the Scenes: Deploying a Low-Latency Multiplayer Game GloballyBehind the Scenes: Deploying a Low-Latency Multiplayer Game Globally
Behind the Scenes: Deploying a Low-Latency Multiplayer Game Globally
 
Building fast,scalable game server in node.js
Building fast,scalable game server in node.jsBuilding fast,scalable game server in node.js
Building fast,scalable game server in node.js
 
Making a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancingMaking a game "Just Right" through testing and play balancing
Making a game "Just Right" through testing and play balancing
 
Full stack development in Go
Full stack development in GoFull stack development in Go
Full stack development in Go
 
GDC Next 2013 - Synching Game States Across Multiple Devices
GDC Next 2013 - Synching Game States Across Multiple DevicesGDC Next 2013 - Synching Game States Across Multiple Devices
GDC Next 2013 - Synching Game States Across Multiple Devices
 
Html5 Game Development with Canvas
Html5 Game Development with CanvasHtml5 Game Development with Canvas
Html5 Game Development with Canvas
 
Proving correctness of a multiplayer game server
Proving correctness of a multiplayer game serverProving correctness of a multiplayer game server
Proving correctness of a multiplayer game server
 
SuperStreamer: Enabling Progressive Content Streaming in a Game Engine
SuperStreamer: Enabling Progressive Content Streaming in a Game EngineSuperStreamer: Enabling Progressive Content Streaming in a Game Engine
SuperStreamer: Enabling Progressive Content Streaming in a Game Engine
 
Dedicated Game Servers
Dedicated Game ServersDedicated Game Servers
Dedicated Game Servers
 
Unit 20 - Game Platforms
Unit 20 - Game PlatformsUnit 20 - Game Platforms
Unit 20 - Game Platforms
 
Unit 20 - Game Platforms
Unit 20 - Game PlatformsUnit 20 - Game Platforms
Unit 20 - Game Platforms
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media player
 
Game server development in node.js in jsconf eu
Game server development in node.js in jsconf euGame server development in node.js in jsconf eu
Game server development in node.js in jsconf eu
 
Supersize your production pipe enjmin 2013 v1.1 hd
Supersize your production pipe    enjmin 2013 v1.1 hdSupersize your production pipe    enjmin 2013 v1.1 hd
Supersize your production pipe enjmin 2013 v1.1 hd
 
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...
(GAM303) Beyond Game Servers: Load Testing, Rendering, and Cloud Gaming | AWS...
 
Programming Language Final PPT
Programming Language Final PPTProgramming Language Final PPT
Programming Language Final PPT
 
GDC 2015 - Low-latency Multiplayer Gaming with AWS
GDC 2015 - Low-latency Multiplayer Gaming with AWS GDC 2015 - Low-latency Multiplayer Gaming with AWS
GDC 2015 - Low-latency Multiplayer Gaming with AWS
 
Building Multiplayer Games (w/ Unity)
Building Multiplayer Games (w/ Unity)Building Multiplayer Games (w/ Unity)
Building Multiplayer Games (w/ Unity)
 
Lessons Learned with Unity and WebGL
Lessons Learned with Unity and WebGLLessons Learned with Unity and WebGL
Lessons Learned with Unity and WebGL
 
Lets Play Together
Lets Play TogetherLets Play Together
Lets Play Together
 

Kürzlich hochgeladen

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

go-man API

  • 1. go-man API Learning go-lang & writing a game
  • 2. Why?  By day: I am architect/developer hacking at Java based api’s  By night: Still harboring a passion for video games and regularly write small games to try out new tech on my daily commute (rarely finish them…)
  • 3. Fusion Why not combine both pursuits to retain a level of interest? Goals:-  Better understanding of go-lang  Maybe and actual finished skunkworks project?
  • 4. Dissecting the game  Broke the idea of “pac-man” down to a series of API calls.  Create game  Add player(s)  Move players  Repeat to fade
  • 5. Concurrency  Lightbulb moment!  Multiple players in same game = concurrency
  • 6. Parallelism  Multiple games in single server = parallelism
  • 7. Caveats  Probably not the best way to do this  First golang code I’ve developed  Highly inefficient  Websockets would’ve been MUCH better  BUT, it’s fun to try it
  • 8. Starting the game HTTP POST request to create a new game with following parms:-  Game name  Max number of GoMen  Max number of GoGhosts  Time to wait for players What happens?  Creates new game on server  Creates golang process waiting for timeout to start game.  Creates a single channel to listen for player updates
  • 9. Add players to game HTTP PUT request to add a player to a game with parms:-  Game id  Player name  Player type (GoMan/GoGhost) If total players reached game will begin otherwise game waits for players
  • 10. Time up If wait time expires, game server adds remaining players as CPU controlled players. Creates new golang process for each missing player Dumb AI: Wait for ¼ second. Pick random number between 0-3 Send move request to game channel repeat…
  • 11. Game States “new” – just created “waiting” – waiting for players to join “playing” – game active “over” – all GoMan players have died “won” – all pills eaten
  • 12. Power Pills If a player eats a power pill a separate golang process is submitted that waits for duration of pill. At end of timer game reverts to normal.
  • 13. Game Client Originally I started developing a separate client in go-lang. Stopped development of this because I wanted people to try the game easily on the web. Switched to JavaScript based client as this was the most likely client to consume the API.
  • 14. Game Client - technology  HTML5 + JavaScript  Twitter bootstrap  HTML5 canvas  Ajax calls to RESTful API
  • 15. Game Client - features  List games  Create new game  Join existing game  Play game
  • 16. Game Server Server implements MVC pattern: Models = game board + players View = JSON representation of game state Controller = RESTful API calls to perform CRUD on a game instance.
  • 17. Game Server - technology  Standalone golang executable or  Google app engine app Uses:  Gorillamux (for http routing)  Game state stored in memory
  • 18. Game Server – lessons learned Things I learned during development:- Started using filesystem as simple datastore for games. Got tired of serialisation / deserialisation and realised it was unnecessary for my simple demo. Originally modeled board cells as a 2d byte array. But these force client to have to deal with Base64 encoded binary data (nasty). Changed definition to a 2d array of “runes” for an easier life. BoardCells [][]rune
  • 19. Game Server – logic  All game logic resides on server. Game state / player state etc.  Game clients are completely dumb.  Weakness in API is that players are not authenticated so they could easily fake each others moves by using the other player’s GUID.
  • 20. Game Server – concurrency  Game controller writes PlayerMove requests to a single channel (one per game)  This prevents concurrent updates to same board (two players eaten same pill etc.)  PlayerMove request includes a unique response channel, to return response back to original requester
  • 21. Game Server – concurrent requests move me Game request channel Players can be either remote web players or local CPU controlled players on server
  • 22. Game Server – concurrent responses Players receive a new instance of the Game board after their move Bad move
  • 23. Game Server – stats golang is FAST!  Normal game 1 goman & 4 goghosts runs in browser @ 60fps  5 x 60 (300) requests per second. Average 3k per response. 1MB per sec traffic.  Running locally it is handled with ease 80+ players in a single game  DO NOT PLAY ON MOBILE PHONE WITH DATA CONTRACT!!!
  • 24. Game Models - GameBoard type GameBoard struct { Id string Name string PillsRemaining int Players map[string]*Player MaxGoMenAllowed int MaxGoGhostsAllowed int WaitForPlayersSeconds int State GameState PowerPillsActive int CreatedTime time.Time LastUpdatedTime time.Time GameStartTime time.Time BoardCells [][]rune }
  • 25. Game Models - Player type Player struct { Location Point Id string Type PlayerType State PlayerState Name string cpuControlled bool Score int Lives int }
  • 26. Game Models - GameState const ( NewGame GameState = "new" WaitingForPlayers = "waiting" PlayingGame = "playing" GameWon = "won" GameOver = "over" )
  • 27. Game Models - PlayerState const ( Alive PlayerState = "alive" Dying = "dying" Dead = "dead" Spawning = "spawning" )
  • 28. Game Models - PlayerMove Request interface type PlayerMove struct { GameId string PlayerToMove Player ResponseChannel chan (PlayerMoveResponse) } Response interface type PlayerMoveResponse struct { Board GameBoard Error error } Response written back to channel passed in request
  • 31. Demo Client: http://go-man-client.herokuapp.com/ (Hosted as simple static site) Source: https://github.com/telecoda/go-man-javascript-client API: http://go-man-app.appspot.com (Hosted on single GAE instance) Source: https://github.com/telecoda/go-man-app Wiki: https://github.com/telecoda/go-man-app/wiki