kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

games.go (1357B)


      1 // Copyright 2013 TLH and dsp. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package games
      6 
      7 import (
      8 	"irc"
      9 )
     10 
     11 // This is a base gamestate for irc games
     12 type gameState struct {
     13 	ictx    *irc.Context      // The context to send commands with
     14 	gchan   *chan irc.Message // A chan that intercepts game irc messages
     15 	name    string            // Name of the game
     16 	creator string            // Name of game creator
     17 }
     18 
     19 // A GameEnger will List available games and invoke a new one if it is on the map.
     20 // New games are invoked as new goroutines that communicate via channels.
     21 type GameEnger interface {
     22 	List() []string
     23 	New(string, *irc.Context, *chan irc.Message)
     24 }
     25 
     26 type gameEng struct {
     27 	games map[string]func(*irc.Context, *chan irc.Message)
     28 }
     29 
     30 // Returns all available games as a string array
     31 func (g *gameEng) List() []string {
     32 	r := []string{}
     33 	for k, _ := range g.games {
     34 		r = append(r, k)
     35 	}
     36 	return r
     37 }
     38 
     39 // Initializes games strings with their corresponding goroutines
     40 func NewGameEng() *gameEng {
     41 	return &gameEng{games: map[string]func(*irc.Context, *chan irc.Message){"hangman": cmdHangman}}
     42 
     43 }
     44 
     45 // Fire up a goroutine if the game is in the map
     46 func (g *gameEng) New(a string, ic *irc.Context, gc *chan irc.Message) {
     47 	if f, ok := g.games[a]; ok {
     48 		go f(ic, gc)
     49 	}
     50 }