initial commit

trunk
nate smith 2023-05-25 21:11:27 -07:00
commit 8652c31ed8
2 changed files with 405 additions and 0 deletions

117
main.go 100644
View File

@ -0,0 +1,117 @@
package main
import (
_ "embed"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"time"
)
//go:embed platforms.json
var platformJSON []byte
type Platform struct {
ID int `json:"platform_id"`
Name string `json:"platform_name"`
}
type platformData struct {
Platforms []Platform
}
type gameData struct {
Games []struct {
Title string
Platforms []Platform
}
}
func _main(apiKey string) error {
var foundGame string
var foundPlatform string
var pd platformData
if err := json.Unmarshal(platformJSON, &pd); err != nil {
panic(err)
}
//ps := pd.Platforms
v := url.Values{}
v.Add("format", "normal")
v.Add("api_key", apiKey)
u := fmt.Sprintf("https://api.mobygames.com/v1/games/random?%s", v.Encode())
res, err := http.Get(u)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("sad satus: %d", res.StatusCode)
}
var gd gameData
if err = json.NewDecoder(res.Body).Decode(&gd); err != nil {
return err
}
rand.Seed(time.Now().Unix())
choice := rand.Intn(len(gd.Games))
for ix, game := range gd.Games {
if ix != choice {
continue
}
foundGame = game.Title
foundPlatform = game.Platforms[0].Name
}
utterances := []string{
"huh",
"wow",
"whoa",
"well",
"geez",
"neato",
}
pauses := []string{
"...",
", ",
". ",
"! ",
"; ",
}
suffices := []string{
"",
".",
"!",
"...",
}
utterance := utterances[rand.Intn(len(utterances))]
pause := pauses[rand.Intn(len(pauses))]
suffix := suffices[rand.Intn(len(suffices))]
fmt.Printf("%s%sgames sure have come a long way since %s for %s%s\n", utterance, pause, foundGame, foundPlatform, suffix)
return nil
}
func main() {
apiKey := os.Getenv("MOBYKEY")
if apiKey == "" {
panic("MOBYKEY not found in env")
}
if err := _main(apiKey); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

288
platforms.json 100644
View File

@ -0,0 +1,288 @@
{
"platforms": [
{
"platform_id": 35,
"platform_name": "3DO"
},
{
"platform_id": 265,
"platform_name": "Altair 680"
},
{
"platform_id": 222,
"platform_name": "Altair 8800"
},
{
"platform_id": 19,
"platform_name": "Amiga"
},
{
"platform_id": 56,
"platform_name": "Amiga CD32"
},
{
"platform_id": 245,
"platform_name": "Apple I"
},
{
"platform_id": 31,
"platform_name": "Apple II"
},
{
"platform_id": 51,
"platform_name": "Apple IIgs"
},
{
"platform_id": 28,
"platform_name": "Atari 2600"
},
{
"platform_id": 33,
"platform_name": "Atari 5200"
},
{
"platform_id": 34,
"platform_name": "Atari 7800"
},
{
"platform_id": 92,
"platform_name": "BBC Micro"
},
{
"platform_id": 73,
"platform_name": "CD-i"
},
{
"platform_id": 29,
"platform_name": "ColecoVision"
},
{
"platform_id": 27,
"platform_name": "Commodore 64"
},
{
"platform_id": 2,
"platform_name": "DOS"
},
{
"platform_id": 8,
"platform_name": "Dreamcast"
},
{
"platform_id": 102,
"platform_name": "FM Towns"
},
{
"platform_id": 10,
"platform_name": "Game Boy"
},
{
"platform_id": 12,
"platform_name": "Game Boy Advance"
},
{
"platform_id": 11,
"platform_name": "Game Boy Color"
},
{
"platform_id": 25,
"platform_name": "Game Gear"
},
{
"platform_id": 14,
"platform_name": "GameCube"
},
{
"platform_id": 16,
"platform_name": "Genesis"
},
{
"platform_id": 30,
"platform_name": "Intellivision"
},
{
"platform_id": 17,
"platform_name": "Jaguar"
},
{
"platform_id": 18,
"platform_name": "Lynx"
},
{
"platform_id": 57,
"platform_name": "MSX"
},
{
"platform_id": 74,
"platform_name": "Macintosh"
},
{
"platform_id": 135,
"platform_name": "Mattel Aquarius"
},
{
"platform_id": 97,
"platform_name": "Microvision"
},
{
"platform_id": 32,
"platform_name": "N-Gage"
},
{
"platform_id": 22,
"platform_name": "NES"
},
{
"platform_id": 36,
"platform_name": "Neo Geo"
},
{
"platform_id": 52,
"platform_name": "Neo Geo Pocket"
},
{
"platform_id": 53,
"platform_name": "Neo Geo Pocket Color"
},
{
"platform_id": 174,
"platform_name": "New Nintendo 3DS"
},
{
"platform_id": 101,
"platform_name": "Nintendo 3DS"
},
{
"platform_id": 9,
"platform_name": "Nintendo 64"
},
{
"platform_id": 44,
"platform_name": "Nintendo DS"
},
{
"platform_id": 75,
"platform_name": "Odyssey"
},
{
"platform_id": 78,
"platform_name": "Odyssey 2"
},
{
"platform_id": 149,
"platform_name": "PC-6001"
},
{
"platform_id": 201,
"platform_name": "PC-8000"
},
{
"platform_id": 94,
"platform_name": "PC-88"
},
{
"platform_id": 95,
"platform_name": "PC-98"
},
{
"platform_id": 59,
"platform_name": "PC-FX"
},
{
"platform_id": 105,
"platform_name": "PS Vita"
},
{
"platform_id": 46,
"platform_name": "PSP"
},
{
"platform_id": 112,
"platform_name": "Pippin"
},
{
"platform_id": 6,
"platform_name": "PlayStation"
},
{
"platform_id": 7,
"platform_name": "PlayStation 2"
},
{
"platform_id": 81,
"platform_name": "PlayStation 3"
},
{
"platform_id": 21,
"platform_name": "SEGA 32X"
},
{
"platform_id": 20,
"platform_name": "SEGA CD"
},
{
"platform_id": 26,
"platform_name": "SEGA Master System"
},
{
"platform_id": 23,
"platform_name": "SEGA Saturn"
},
{
"platform_id": 15,
"platform_name": "SNES"
},
{
"platform_id": 131,
"platform_name": "Sinclair QL"
},
{
"platform_id": 127,
"platform_name": "SuperGrafx"
},
{
"platform_id": 40,
"platform_name": "TurboGrafx-16"
},
{
"platform_id": 38,
"platform_name": "Virtual Boy"
},
{
"platform_id": 82,
"platform_name": "Wii"
},
{
"platform_id": 132,
"platform_name": "Wii U"
},
{
"platform_id": 3,
"platform_name": "Windows"
},
{
"platform_id": 13,
"platform_name": "Xbox"
},
{
"platform_id": 69,
"platform_name": "Xbox 360"
},
{
"platform_id": 41,
"platform_name": "ZX Spectrum"
},
{
"platform_id": 118,
"platform_name": "ZX80"
},
{
"platform_id": 119,
"platform_name": "ZX81"
},
{
"platform_id": 211,
"platform_name": "Zune"
}
]
}