├── .replit ├── .prettierignore ├── .vscode └── settings.json ├── next-env.d.ts ├── public ├── healer.webp ├── hunter.webp ├── citizen.webp ├── prophet.webp └── werewolf.webp ├── .eslintignore ├── tools ├── .eslintrc.json ├── build.sh ├── protoc.sh ├── bot.js └── goci.sh ├── todo.txt ├── lib ├── langs.ts ├── CharacterImg.ts ├── useGameSocket.ts ├── translate.ts └── useMessageHandler.ts ├── lint-staged.config.js ├── pages ├── index.tsx ├── _document.tsx ├── _app.tsx └── game.tsx ├── components ├── Loader.tsx ├── ClientOnly.tsx ├── NameBadge.tsx ├── FellowWolves.tsx ├── NameSelector.tsx ├── Layout.tsx ├── ProphetReveal.tsx ├── PlayerStatus.tsx ├── SkippableDelay.tsx ├── Killed.tsx ├── MainMenu.tsx ├── LinearProgressCircle.tsx ├── LangContext.tsx ├── Lobby.tsx ├── Vote.tsx ├── GameOver.tsx ├── CharacterSpinner.tsx ├── UpdatesLog.tsx └── GameClient.tsx ├── styles └── globals.css ├── .github ├── dependabot.yml └── workflows │ ├── node.js.yml │ ├── go.yml │ └── release.yml ├── backend ├── main.go ├── go.mod ├── game │ ├── wolfvote.go │ ├── start.go │ ├── name.go │ ├── prophetvote.go │ ├── juryvote.go │ ├── healervote.go │ ├── handler.go │ ├── characters.go │ ├── vote.go │ └── game.go ├── statik │ └── statik.go ├── http │ └── http.go ├── protocol │ └── protocol.go └── go.sum ├── .gitignore ├── tsconfig.json ├── .eslintrc.json ├── package.json ├── locales ├── zh.json └── en.json ├── main.proto ├── README.md └── LICENSE /.replit: -------------------------------------------------------------------------------- 1 | run = "npm run dev" 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # Ignore next build output 2 | .next 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.tabSize": 2, 3 | "editor.insertSpaces": true 4 | } 5 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | -------------------------------------------------------------------------------- /public/healer.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerpogo/murdermystery/HEAD/public/healer.webp -------------------------------------------------------------------------------- /public/hunter.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerpogo/murdermystery/HEAD/public/hunter.webp -------------------------------------------------------------------------------- /public/citizen.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerpogo/murdermystery/HEAD/public/citizen.webp -------------------------------------------------------------------------------- /public/prophet.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerpogo/murdermystery/HEAD/public/prophet.webp -------------------------------------------------------------------------------- /public/werewolf.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spencerpogo/murdermystery/HEAD/public/werewolf.webp -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # ESLint ignored files/directories 2 | 3 | # Built HTML 4 | build/ 5 | # Generated protobuf js 6 | pbjs/ 7 | -------------------------------------------------------------------------------- /tools/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "import/no-extraneous-dependencies": "off", 4 | "no-console": "off" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /todo.txt: -------------------------------------------------------------------------------- 1 | - Figure out how to change things in server based on build environment (dev/prod), 2 | maybe using go build tags 3 | - Don't sync votes, for wolf vote don't require consensus 4 | - Require react hooks to translate, using useRouter 5 | - Better method of going to next phase on backend 6 | -------------------------------------------------------------------------------- /lib/langs.ts: -------------------------------------------------------------------------------- 1 | // Eslint no-shadow hates enums?? 2 | // eslint-disable-next-line no-shadow 3 | export enum Lang { 4 | // We want to be able to check if lang is truthy, and the default enum starting 5 | // value, 0, is not, so start at 1 6 | EN = 1, 7 | ZH = 2, 8 | } 9 | 10 | export default Lang; 11 | -------------------------------------------------------------------------------- /lint-staged.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "*.{js,jsx,ts,tsx}": [ 3 | "prettier --write", 4 | "npx eslint --cache --fix", 5 | () => "npx tsc --noemit", 6 | ], 7 | "*.proto": [() => "npm run go-vet", () => "npx tsc --noemit"], 8 | "*.{md,yml,json}": "prettier --write", 9 | "backend/**/*.go": () => "npm run go-vet", 10 | }; 11 | -------------------------------------------------------------------------------- /pages/index.tsx: -------------------------------------------------------------------------------- 1 | import Layout from "components/Layout"; 2 | import MainMenu from "components/MainMenu"; 3 | import { FC } from "react"; 4 | 5 | interface HomeProps {} 6 | 7 | const Home: FC = () => { 8 | return ( 9 | 10 |
11 | 12 |
13 |
14 | ); 15 | }; 16 | 17 | export default Home; 18 | -------------------------------------------------------------------------------- /components/Loader.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Skeleton } from "@chakra-ui/react"; 2 | import { FC } from "react"; 3 | 4 | interface LoaderProps {} 5 | 6 | export const Loader: FC = () => { 7 | return ( 8 | 9 | 10 | 11 | 12 | 13 | ); 14 | }; 15 | 16 | export default Loader; 17 | -------------------------------------------------------------------------------- /components/ClientOnly.tsx: -------------------------------------------------------------------------------- 1 | import { ReactNode, useEffect, useState } from "react"; 2 | 3 | export function useClientOnly(): boolean { 4 | const [hasMounted, setHasMounted] = useState(false); 5 | 6 | useEffect(() => setHasMounted(true), []); 7 | 8 | return hasMounted; 9 | } 10 | 11 | export function ClientOnly({ children }: { children: ReactNode }): ReactNode { 12 | if (useClientOnly()) { 13 | return children; 14 | } 15 | return null; 16 | } 17 | -------------------------------------------------------------------------------- /pages/_document.tsx: -------------------------------------------------------------------------------- 1 | import { ColorModeScript } from "@chakra-ui/react"; 2 | import NextDocument, { Head, Html, Main, NextScript } from "next/document"; 3 | 4 | export default class Document extends NextDocument { 5 | render(): JSX.Element { 6 | return ( 7 | 8 | 9 | 10 | {/* Chakra UI Color Mode Script */} 11 | 12 |
13 | 14 | 15 | 16 | ); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /components/NameBadge.tsx: -------------------------------------------------------------------------------- 1 | import { Text } from "@chakra-ui/react"; 2 | import { FC } from "react"; 3 | 4 | interface NameBadgeProps { 5 | text: string; 6 | } 7 | 8 | export const NameBadge: FC = ({ text }: NameBadgeProps) => { 9 | return ( 10 | 19 | {text} 20 | 21 | ); 22 | }; 23 | 24 | export default NameBadge; 25 | -------------------------------------------------------------------------------- /styles/globals.css: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | padding: 0; 4 | margin: 0; 5 | font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, 6 | Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; 7 | } 8 | 9 | a { 10 | color: inherit; 11 | text-decoration: none; 12 | } 13 | 14 | * { 15 | box-sizing: border-box; 16 | } 17 | 18 | main { 19 | max-width: 38rem; 20 | padding: 1.5rem; 21 | margin: auto; 22 | } 23 | 24 | input, 25 | select, 26 | button, 27 | textarea { 28 | -webkit-appearance: none; 29 | } 30 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | # Maintain frontend npm dependencies 4 | - package-ecosystem: npm 5 | directory: "/" 6 | schedule: 7 | interval: daily 8 | open-pull-requests-limit: 10 9 | 10 | # Maintain backend go dependencies 11 | - package-ecosystem: gomod 12 | directory: "/backend" 13 | schedule: 14 | interval: daily 15 | open-pull-requests-limit: 10 16 | 17 | # Maintain dependencies for GitHub Actions 18 | - package-ecosystem: "github-actions" 19 | directory: "/" 20 | schedule: 21 | interval: "daily" 22 | -------------------------------------------------------------------------------- /lib/CharacterImg.ts: -------------------------------------------------------------------------------- 1 | import { S, STRINGS } from "lib/translate"; 2 | import { murdermystery as protobuf } from "pbjs/protobuf"; 3 | 4 | const C = protobuf.Character; 5 | 6 | export const CHARACTER_INDEXES = [ 7 | C.CITIZEN, 8 | C.WEREWOLF, 9 | C.HEALER, 10 | C.PROPHET, 11 | C.HUNTER, 12 | ]; 13 | export const NAMES = ["citizen", "werewolf", "healer", "prophet", "hunter"]; 14 | export const NAME_STRINGS = [ 15 | S.CITIZEN, 16 | S.WEREWOLF, 17 | S.HEALER, 18 | S.PROPHET, 19 | S.HUNTER, 20 | ]; 21 | 22 | export const characterToString = (c: protobuf.Character): STRINGS => 23 | NAME_STRINGS[CHARACTER_INDEXES.indexOf(c)]; 24 | 25 | export const characterToImg = (c: protobuf.Character): string => 26 | `/${NAMES[CHARACTER_INDEXES.indexOf(c)]}.webp`; 27 | -------------------------------------------------------------------------------- /backend/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | 7 | "github.com/Scoder12/murdermystery/backend/http" 8 | _ "github.com/Scoder12/murdermystery/backend/statik" 9 | "github.com/rakyll/statik/fs" 10 | ) 11 | 12 | // TODO: 13 | // - Create game endpoint that clients can request 14 | // - Upper limit for concurrent games to prevent DOS 15 | // - Lobbies expire if no one connected 16 | // - Clients disconnect if they don't respond to pings or are afk 17 | // - Figure out msg format: maybe 18 | 19 | var addr = flag.String("addr", "127.0.0.1:8080", "http service address") 20 | 21 | func main() { 22 | flag.Parse() 23 | fs, err := fs.New() 24 | if err != nil { 25 | log.Fatal(err) 26 | } 27 | http.StartServer(*addr, fs) 28 | } 29 | -------------------------------------------------------------------------------- /pages/_app.tsx: -------------------------------------------------------------------------------- 1 | /* eslint-disable react/jsx-props-no-spreading */ 2 | import { ChakraProvider, extendTheme } from "@chakra-ui/react"; 3 | import { LangProvider } from "components/LangContext"; 4 | import { AppProps } from "next/app"; 5 | import "../styles/globals.css"; 6 | 7 | const MyApp = ({ Component, pageProps }: AppProps): JSX.Element => { 8 | return ( 9 | 19 | 20 | 21 | 22 | 23 | ); 24 | }; 25 | 26 | export default MyApp; 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | 36 | # IDEA 37 | .idea 38 | 39 | # Generated protobuf code 40 | pbjs/ 41 | backend/protocol/pb 42 | 43 | # Generated statik code 44 | backend/statik/ 45 | 46 | # Debug route for viewing components 47 | pages/debug.tsx 48 | 49 | # ESLint Cache 50 | .eslintcache 51 | 52 | # Husky 53 | .husky 54 | -------------------------------------------------------------------------------- /backend/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Scoder12/murdermystery/backend 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/gin-gonic/gin v1.6.3 7 | github.com/go-playground/validator/v10 v10.4.0 // indirect 8 | github.com/golang/protobuf v1.4.3 9 | github.com/gorilla/mux v1.8.0 10 | github.com/gorilla/websocket v1.4.2 11 | github.com/json-iterator/go v1.1.10 // indirect 12 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 13 | github.com/modern-go/reflect2 v1.0.1 // indirect 14 | github.com/rakyll/statik v0.1.7 15 | github.com/ugorji/go v1.1.12 // indirect 16 | golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee // indirect 17 | golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca // indirect 18 | google.golang.org/protobuf v1.25.0 19 | gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 20 | gopkg.in/yaml.v2 v2.3.0 // indirect 21 | ) 22 | -------------------------------------------------------------------------------- /backend/game/wolfvote.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 7 | "gopkg.in/olahol/melody.v1" 8 | ) 9 | 10 | func (g *Game) callWolfVote() { 11 | g.lock.Lock() 12 | defer g.lock.Unlock() 13 | 14 | wolfSessions, nonWolfSessions := g.SessionsByRole(pb.Character_WEREWOLF) 15 | 16 | go g.callVote(wolfSessions, nonWolfSessions, pb.VoteRequest_KILL, g.wolfVoteHandler(), true) 17 | } 18 | 19 | // Handler assumes game is locked 20 | func (g *Game) wolfVoteHandler() func(*Vote, *melody.Session, *melody.Session) { 21 | return func(v *Vote, voter, killed *melody.Session) { 22 | log.Println("Wolf vote over") 23 | log.Println(killed, "killed by wolves") 24 | g.stageKill(killed, pb.KillReason_WOLVES) 25 | g.vote.End(g, nil) 26 | 27 | // The kill will actually happen after the healer vote 28 | go g.callProphetVote() 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | workflow_dispatch: 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - name: Use Node.js v14.x 18 | uses: actions/setup-node@v2.1.5 19 | with: 20 | node-version: 14.x 21 | 22 | - uses: actions/cache@v2.1.6 23 | with: 24 | path: ${{ github.workspace }}/.next/cache 25 | key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }} 26 | 27 | - uses: bahmutov/npm-install@v1 28 | 29 | - name: Build protocol buffers code 30 | run: bash ./tools/protoc.sh $(pwd) --no-golang 31 | 32 | - name: Lint 33 | run: npm run lint 34 | 35 | - name: Run next build 36 | run: npm run build 37 | -------------------------------------------------------------------------------- /backend/statik/statik.go: -------------------------------------------------------------------------------- 1 | // Code generated by statik. DO NOT EDIT. 2 | 3 | package statik 4 | 5 | import ( 6 | "github.com/rakyll/statik/fs" 7 | ) 8 | 9 | 10 | func init() { 11 | data := "PK\x03\x04\x14\x00\x08\x00\x08\x00+\x18XQ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00 \x00index.htmlUT\x05\x00\x01\x02\x99\x93_$\xcd1\x0e\xc20\x0c\x00\xc0\x9dW\xf8\x01\xa8Vw\xe3\x81\x8d\xa1\x1b< \xa9-\x820q\xe5$C~\x8f\x04\xeb-G\xa5\x7f\x0c,\xd5\xe7E+Sv\x99Le\xe5m\x84hl\xb3u\x8d \xd7\xb4\xbf\xb5\naY\x99\x0e\xbe;\xe4Q\xc5\x14zQ\x08M\x06\x8f\xdb\x19bT\xa0\xddEy\xc1\xeen\x0d\xf3x\x99,\xad\x10\xfe\x99\xf0`\xc2_r\xfa\x06\x00\x00\xff\xffPK\x07\x08k!>\x88o\x00\x00\x00z\x00\x00\x00PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00+\x18XQk!>\x88o\x00\x00\x00z\x00\x00\x00\n\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00index.htmlUT\x05\x00\x01\x02\x99\x93_PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00A\x00\x00\x00\xb0\x00\x00\x00\x00\x00" 12 | fs.Register(data) 13 | } 14 | -------------------------------------------------------------------------------- /components/FellowWolves.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Heading, Stack } from "@chakra-ui/react"; 2 | import { S, useTranslator } from "lib/translate"; 3 | import { FC } from "react"; 4 | import { RightFloatSkippableDelay } from "./SkippableDelay"; 5 | 6 | interface FellowWolvesProps { 7 | names: string[]; 8 | onDone: () => void; 9 | } 10 | 11 | export const FellowWolves: FC = ({ 12 | names, 13 | onDone, 14 | }: FellowWolvesProps) => { 15 | const t = useTranslator(); 16 | 17 | return ( 18 | <> 19 | {t(S.FELLOW_WOLVES)} 20 | {/* Polish: maybe a wolf icon here? */} 21 | 22 | {names.map((name) => ( 23 | 24 | {name} 25 | 26 | ))} 27 | 28 | 29 | 30 | ); 31 | }; 32 | 33 | export default FellowWolves; 34 | -------------------------------------------------------------------------------- /components/NameSelector.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Button, Input, Stack } from "@chakra-ui/react"; 2 | import { STRINGS, useTranslator } from "lib/translate"; 3 | import { ChangeEvent, FC, useState } from "react"; 4 | 5 | interface NameSelectorProps { 6 | onSubmit: (name: string) => void; 7 | } 8 | 9 | export const NameSelector: FC = ({ 10 | onSubmit, 11 | }: NameSelectorProps) => { 12 | const t = useTranslator(); 13 | const [name, setName] = useState(""); 14 | 15 | return ( 16 | 17 | 18 | ) => 21 | setName(evt.target.value) 22 | } 23 | type="text" 24 | /> 25 | 28 | 29 | 30 | ); 31 | }; 32 | 33 | export default NameSelector; 34 | -------------------------------------------------------------------------------- /components/Layout.tsx: -------------------------------------------------------------------------------- 1 | import { Button } from "@chakra-ui/react"; 2 | import Head from "next/head"; 3 | import { FC, ReactNode } from "react"; 4 | import { 5 | STRINGS, 6 | useChangeLanguageText, 7 | useTranslator, 8 | } from "../lib/translate"; 9 | import { useToggleLang } from "./LangContext"; 10 | 11 | export interface LayoutProps { 12 | children: ReactNode; 13 | } 14 | 15 | export const Layout: FC = ({ children }: LayoutProps) => { 16 | const t = useTranslator(); 17 | const title = t(STRINGS.TITLE); 18 | const changeLanguageText = useChangeLanguageText(); 19 | const toggleLang = useToggleLang(); 20 | 21 | return ( 22 | <> 23 | 24 | {title} 25 | 26 | 27 |
28 | {children} 29 | 32 |
33 | 34 | ); 35 | }; 36 | 37 | export default Layout; 38 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "outDir": "build/dist", 5 | "module": "esnext", 6 | "target": "es5", 7 | "lib": ["es6", "dom", "esnext.asynciterable"], 8 | "sourceMap": true, 9 | "allowJs": true, 10 | "jsx": "preserve", 11 | "moduleResolution": "node", 12 | //"rootDir": "src", 13 | "forceConsistentCasingInFileNames": true, 14 | "noImplicitReturns": true, 15 | "noImplicitThis": true, 16 | "noImplicitAny": true, 17 | "strictNullChecks": true, 18 | "suppressImplicitAnyIndexErrors": true, 19 | "noUnusedLocals": true, 20 | "skipLibCheck": true, 21 | "strict": false, 22 | "noEmit": true, 23 | "esModuleInterop": true, 24 | "resolveJsonModule": true, 25 | "isolatedModules": true 26 | }, 27 | "exclude": [ 28 | "node_modules", 29 | "build", 30 | "scripts", 31 | "acceptance-tests", 32 | "webpack", 33 | "jest", 34 | "src/setupTests.ts" 35 | ], 36 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"] 37 | } 38 | -------------------------------------------------------------------------------- /components/ProphetReveal.tsx: -------------------------------------------------------------------------------- 1 | import { Flex, Heading } from "@chakra-ui/react"; 2 | import { FC } from "react"; 3 | import { STRINGS, useTranslator } from "../lib/translate"; 4 | import NameBadge from "./NameBadge"; 5 | import { RightFloatSkippableDelay } from "./SkippableDelay"; 6 | 7 | export interface ProphetRevealProps { 8 | name: string; 9 | good: boolean; 10 | onDone: () => void; 11 | } 12 | 13 | export const ProphetReveal: FC = ({ 14 | name, 15 | good, 16 | onDone, 17 | }: ProphetRevealProps) => { 18 | const t = useTranslator(); 19 | 20 | return ( 21 | <> 22 | 23 | {t(STRINGS.REVEAL_FOUND)} 24 | 25 | 26 | 27 | 28 | 29 | {t(good ? STRINGS.IS_GOOD : STRINGS.IS_BAD)} 30 | 31 | 32 | 33 | ); 34 | }; 35 | 36 | export default ProphetReveal; 37 | -------------------------------------------------------------------------------- /backend/game/start.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 7 | 8 | "github.com/Scoder12/murdermystery/backend/protocol" 9 | 10 | "gopkg.in/olahol/melody.v1" 11 | ) 12 | 13 | func (g *Game) startGameHandler(s *melody.Session, c *Client, msg *pb.StartGame) { 14 | g.lock.Lock() 15 | defer g.lock.Unlock() 16 | 17 | if g.host == nil || g.host != s { 18 | // They are unauthorized, ignore 19 | return 20 | } 21 | if g.started { 22 | // already started, ignore 23 | return 24 | } 25 | online := 0 26 | for s, c := range g.clients { 27 | if s != nil && !s.IsClosed() && c != nil && len(c.name) > 0 { 28 | online++ 29 | } 30 | } 31 | if online < 6 { 32 | msg, err := protocol.Marshal(&pb.Alert{Msg: pb.Alert_NEEDMOREPLAYERS}) 33 | if err != nil { 34 | return 35 | } 36 | err = s.WriteBinary(msg) 37 | printerr(err) 38 | return 39 | } 40 | 41 | // This locks out new players from joining 42 | g.started = true 43 | 44 | var id int32 = 0 45 | if c != nil { 46 | id = c.ID 47 | } 48 | 49 | log.Printf("[%v] Starting %v player game", id, online) 50 | // Run in goroutine so defer fires and unlocks 51 | go g.handleStart() 52 | } 53 | -------------------------------------------------------------------------------- /components/PlayerStatus.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Flex, Heading } from "@chakra-ui/react"; 2 | import { STRINGS, useTranslator } from "lib/translate"; 3 | import { Children, FC } from "react"; 4 | import NameBadge from "./NameBadge"; 5 | import SkippableDelay from "./SkippableDelay"; 6 | 7 | export interface PlayerStatusProps { 8 | aliveNames: string[]; 9 | deadNames: string[]; 10 | onDone: () => void; 11 | } 12 | 13 | export const PlayerStatus: FC = ({ 14 | aliveNames, 15 | deadNames, 16 | onDone, 17 | }: PlayerStatusProps) => { 18 | const t = useTranslator(); 19 | 20 | return ( 21 | 22 | 23 | {t(STRINGS.ALIVE)} 24 | 25 | {Children.map(aliveNames, (n) => ( 26 | 27 | ))} 28 | 29 | 30 | 31 | {t(STRINGS.DEAD)} 32 | 33 | {Children.map(deadNames, (n) => ( 34 | 35 | ))} 36 | 37 | 38 | 39 | 40 | ); 41 | }; 42 | 43 | export default PlayerStatus; 44 | -------------------------------------------------------------------------------- /components/SkippableDelay.tsx: -------------------------------------------------------------------------------- 1 | import { Button, Flex, useTimeout } from "@chakra-ui/react"; 2 | import { STRINGS, useTranslator } from "lib/translate"; 3 | import { FC } from "react"; 4 | import LinearProgressCircle from "./LinearProgressCircle"; 5 | 6 | export interface SkippableDelayProps { 7 | duration: number; 8 | onDone?: () => void; 9 | circleColor?: string; 10 | } 11 | 12 | const SkippableDelay: FC = ({ 13 | duration, 14 | onDone = () => undefined, 15 | circleColor = "gray.300", 16 | }: SkippableDelayProps) => { 17 | const t = useTranslator(); 18 | 19 | useTimeout(() => { 20 | onDone(); 21 | }, duration * 1000); 22 | 23 | return ( 24 | 28 | ); 29 | }; 30 | 31 | export default SkippableDelay; 32 | 33 | export const RightFloatSkippableDelay: FC = ( 34 | props: SkippableDelayProps 35 | ) => { 36 | return ( 37 | 38 | {/* eslint-disable-next-line react/jsx-props-no-spreading */} 39 | 40 | 41 | ); 42 | }; 43 | -------------------------------------------------------------------------------- /backend/game/name.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | "strings" 6 | "time" 7 | 8 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 9 | 10 | "github.com/Scoder12/murdermystery/backend/protocol" 11 | 12 | "gopkg.in/olahol/melody.v1" 13 | ) 14 | 15 | // Treat like const! 16 | var badStrings = []string{ 17 | "\u200B", // zero width space 18 | "\n", 19 | "\r", 20 | "\t", 21 | } 22 | 23 | func (g *Game) setNameHandler(s *melody.Session, c *Client, msg *pb.SetName) { 24 | if c == nil { 25 | return 26 | } 27 | 28 | g.lock.Lock() 29 | defer g.lock.Unlock() 30 | 31 | if len(c.name) > 0 { 32 | // No renames 33 | return 34 | } 35 | 36 | name := msg.GetName() 37 | 38 | for _, bad := range badStrings { 39 | name = strings.ReplaceAll(name, bad, "") 40 | } 41 | name = strings.TrimSpace(name) 42 | 43 | if len(name) == 0 || len(name) > 20 { 44 | msg, err := protocol.Marshal(&pb.Error{Msg: pb.Error_BADNAME}) 45 | if err != nil { 46 | return 47 | } 48 | err = s.WriteBinary(msg) 49 | printerr(err) 50 | 51 | time.AfterFunc(200*time.Millisecond, func() { 52 | err = s.Close() 53 | printerr(err) 54 | }) 55 | } 56 | c.name = name 57 | 58 | log.Printf("[%v] Set name to %s", c.ID, name) 59 | g.syncPlayers() 60 | } 61 | -------------------------------------------------------------------------------- /tools/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | # Usage: ./tools/build.sh [binary output] 6 | 7 | # Assumes that we are in the repo root and `npm ci` has already been run. 8 | if [ ! -f package.json ]; then 9 | >&2 echo "No package.json, are we in the repo root?" 10 | exit 1 11 | fi 12 | 13 | if [[ ! $(command -v statik) ]]; then 14 | >&2 echo "Must install statik to build." 15 | >&2 echo "$ go get github.com/rakyll/statik" 16 | exit 1 17 | fi 18 | 19 | if [ ! -d ./pbjs ]; then 20 | >&2 echo "Must run ./tools/protoc.sh first!" 21 | exit 1 22 | fi 23 | 24 | # Setup build directory 25 | mkdir -p build 26 | 27 | # Generate a next build 28 | npm run build 29 | 30 | # Export HTML 31 | rm -rf build/html 32 | mkdir -p build/html 33 | npm run export -- -o build/html 34 | 35 | # Rewrite /game.html to be /game 36 | mkdir build/html/game 37 | mv build/html/game.html build/html/game/index.html 38 | 39 | # Run statik 40 | echo 41 | echo "======> Running statik..." 42 | statik -src build/html -dest backend -f 43 | 44 | # Build 45 | echo 46 | echo "======> Building binary..." 47 | cd backend 48 | go build -v -o ${1:-"../build/backend"} 49 | cd .. 50 | 51 | echo "Build successful!" 52 | 53 | if [ "$2" == "--no-clean" ]; then 54 | echo "To clean remove the ./build/html directory" 55 | else 56 | rm -rf ./build/html 57 | fi 58 | -------------------------------------------------------------------------------- /components/Killed.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Flex, Heading, Text } from "@chakra-ui/react"; 2 | import { STRINGS, useTranslator } from "lib/translate"; 3 | import { murdermystery as protobuf } from "pbjs/protobuf"; 4 | import { FC } from "react"; 5 | import { RightFloatSkippableDelay } from "./SkippableDelay"; 6 | 7 | export interface KilledProps { 8 | reason: protobuf.KillReason | null | undefined; 9 | onDone: () => void; 10 | } 11 | 12 | export function getPlayerKillText( 13 | reason: protobuf.KillReason | null | undefined 14 | ): STRINGS | null { 15 | switch (reason) { 16 | case protobuf.KillReason.VOTED: 17 | return STRINGS.PDEATH_VOTED; 18 | case protobuf.KillReason.WOLVES: 19 | return STRINGS.PDEATH_WOLVES; 20 | case protobuf.KillReason.HEALERPOISON: 21 | return STRINGS.PDEATH_HEALER; 22 | default: 23 | return null; 24 | } 25 | } 26 | 27 | export const Killed: FC = ({ reason, onDone }: KilledProps) => { 28 | const t = useTranslator(); 29 | const kText = getPlayerKillText(reason); 30 | 31 | return ( 32 | 33 | {t(STRINGS.YOU_WERE_KILLED)} 34 | 35 | {kText ? {t(kText)} : null} 36 | 37 | 38 | 39 | ); 40 | }; 41 | 42 | export default Killed; 43 | -------------------------------------------------------------------------------- /components/MainMenu.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | Box, 3 | Button, 4 | Heading, 5 | Modal, 6 | ModalBody, 7 | ModalCloseButton, 8 | ModalContent, 9 | ModalOverlay, 10 | Stack, 11 | Text, 12 | useDisclosure, 13 | } from "@chakra-ui/react"; 14 | import { STRINGS, useTranslator } from "lib/translate"; 15 | import { FC } from "react"; 16 | 17 | export interface MainMenuProps {} 18 | 19 | export const MainMenu: FC = () => { 20 | const t = useTranslator(); 21 | const { isOpen, onOpen, onClose } = useDisclosure(); 22 | 23 | return ( 24 | <> 25 | 26 | 27 | {t(STRINGS.TITLE)} 28 | 29 | 30 | 33 | 36 | 37 | 38 | 39 | {/* Join Game Modal */} 40 | 41 | 42 | 43 | 44 | 45 | {t(STRINGS.NEED_GAME_LINK)} 46 | 47 | 48 | 49 | 50 | ); 51 | }; 52 | 53 | export default MainMenu; 54 | -------------------------------------------------------------------------------- /backend/http/http.go: -------------------------------------------------------------------------------- 1 | package http 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "strings" 8 | "sync" 9 | 10 | "github.com/Scoder12/murdermystery/backend/game" 11 | "github.com/gin-gonic/gin" 12 | ) 13 | 14 | // Put this closure in a separate function so more memory can be garbage collected 15 | func getDestroyFunc(games map[string]*game.Game, gamesMu *sync.Mutex, gid string) func() { 16 | return func() { 17 | gamesMu.Lock() 18 | delete(games, gid) 19 | gamesMu.Unlock() 20 | } 21 | } 22 | 23 | // StartServer starts the HTTP server 24 | func StartServer(iface string, fs http.FileSystem) { 25 | r := gin.Default() 26 | 27 | games := make(map[string]*game.Game) 28 | var gamesMu sync.Mutex 29 | 30 | // routes 31 | r.GET("/game/:id", func(c *gin.Context) { 32 | gid := c.Param("id") 33 | 34 | gamesMu.Lock() 35 | g, exists := games[gid] 36 | if !exists { 37 | log.Println("Creating new game") 38 | g = game.New(getDestroyFunc(games, &gamesMu, gid)) 39 | games[gid] = g 40 | } 41 | gamesMu.Unlock() 42 | 43 | err := g.HandleRequest(c.Writer, c.Request) 44 | if err != nil { 45 | log.Println(err) 46 | } 47 | }) 48 | 49 | r.NoRoute(func(c *gin.Context) { 50 | p := c.Request.URL.Path 51 | if strings.HasPrefix(p, "/_next/static") { 52 | fmt.Println("has /_next/static") 53 | c.Header("Cache-Control", "public, max-age=31536000, immutable") 54 | } 55 | c.FileFromFS(c.Request.URL.Path, fs) 56 | }) 57 | 58 | log.Fatalln(r.Run(iface)) 59 | } 60 | -------------------------------------------------------------------------------- /tools/protoc.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | # Generates protocol buffers code - YMMV 6 | # Should work on windows with some modifications 7 | # TODO: Require pbjs 8 | 9 | ROOT=${1:-$(pwd)} 10 | 11 | GO_PB_PKG=$ROOT/backend/protocol/pb 12 | JS_PB_PKG=$ROOT/pbjs 13 | 14 | if [[ "$2" != "--no-golang" ]]; then 15 | if [ ! $(command -v protoc) ]; then 16 | >&2 echo "You must install protoc into your path before running this script." 17 | >&2 echo "You can install it here: https://developers.google.com/protocol-buffers/docs/downloads" 18 | exit 1 19 | fi 20 | 21 | if [ ! $(command -v protoc-gen-go) ]; then 22 | >&2 echo "You must install protoc-gen-go into your path before running this script." 23 | >&2 echo "You can install by running: go get google.golang.org/protobuf/cmd/protoc-gen-go" 24 | exit 1 25 | fi 26 | 27 | echo "Generating go code..." 28 | mkdir -p $GO_PB_PKG 29 | protoc -I=$ROOT --go_out=$GO_PB_PKG --go_opt=paths=source_relative $ROOT/*.proto 30 | 31 | # This is absolutely terrible, but protobuf has forced my hand. 32 | echo "type IsServerMessage_Data = isServerMessage_Data" >> $GO_PB_PKG/main.pb.go 33 | fi 34 | 35 | if [[ "$2" != "--no-js" ]]; then 36 | echo "Generating JS code..." 37 | mkdir -p $JS_PB_PKG 38 | npm run pbjs -- -t static-module -w commonjs -o $JS_PB_PKG/protobuf.js $ROOT/*.proto 39 | 40 | echo "Generating Typescript definitions..." 41 | npm run pbts -- -o $JS_PB_PKG/protobuf.d.ts $JS_PB_PKG/protobuf.js 42 | fi 43 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "commonjs": true, 4 | "es2021": true, 5 | "node": true 6 | }, 7 | "extends": [ 8 | "eslint:recommended", 9 | "airbnb", 10 | "standard", 11 | "plugin:prettier/recommended" 12 | ], 13 | "parserOptions": { 14 | "ecmaVersion": 12 15 | }, 16 | "settings": { 17 | "import/resolver": { 18 | "typescript": {} // this loads /tsconfig.json to eslint 19 | } 20 | }, 21 | "rules": { 22 | "import/extensions": "off" 23 | //"no-console": "off" // This will be removed in prod 24 | }, 25 | "overrides": [ 26 | { 27 | "files": ["*.ts", "*.tsx"], 28 | "env": { 29 | "browser": true, 30 | "es2021": true, 31 | "node": true 32 | }, 33 | "extends": [ 34 | "plugin:react/recommended", 35 | "plugin:@typescript-eslint/recommended", 36 | "airbnb/hooks" 37 | ], 38 | "parser": "@typescript-eslint/parser", 39 | "parserOptions": { 40 | "ecmaFeatures": { 41 | "jsx": true 42 | }, 43 | "ecmaVersion": 12, 44 | "sourceType": "module" 45 | }, 46 | "plugins": ["react", "@typescript-eslint"], 47 | "rules": { 48 | "react/react-in-jsx-scope": "off", 49 | "react/jsx-filename-extension": [1, { "extensions": [".tsx", ".ts"] }], 50 | "react/jsx-curly-newline": "off", 51 | "@typescript-eslint/no-empty-interface": "off", 52 | "react/jsx-indent": "off" 53 | } 54 | } 55 | ] 56 | } 57 | -------------------------------------------------------------------------------- /components/LinearProgressCircle.tsx: -------------------------------------------------------------------------------- 1 | /* eslint-disable react/require-default-props */ 2 | import { chakra, keyframes } from "@chakra-ui/system"; 3 | import { FC } from "react"; 4 | 5 | export interface LinearProgressCircleProps { 6 | size: number; 7 | thickness?: number; 8 | color?: string; 9 | duration?: number; 10 | } 11 | 12 | const LinearProgressCircle: FC = ({ 13 | size, 14 | thickness = 4, 15 | color = "white", 16 | duration = 1, 17 | }: LinearProgressCircleProps) => { 18 | const halfSize = size / 2; 19 | const radius = halfSize - thickness * 2; 20 | const circumference = Math.floor(radius * 2 * Math.PI); 21 | 22 | // to set a specific percent, use strokeDashOffset = 23 | // circumference - (percent / 100) * circumference 24 | const increase = keyframes({ 25 | "0%": { 26 | strokeDashoffset: circumference, 27 | }, 28 | "100%": { 29 | strokeDashoffset: 0, 30 | }, 31 | }); 32 | 33 | return ( 34 | 35 | 47 | 48 | ); 49 | }; 50 | 51 | export default LinearProgressCircle; 52 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Build Backend 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | paths: 7 | - .github/workflows/** 8 | - backend/** 9 | - tools/** 10 | - main.proto 11 | pull_request: 12 | branches: [master] 13 | paths: 14 | - .github/workflows/** 15 | - backend/** 16 | - tools/** 17 | - main.proto 18 | workflow_dispatch: 19 | 20 | jobs: 21 | build: 22 | name: Build 23 | runs-on: ubuntu-latest 24 | steps: 25 | - name: Set up Go 1.x 26 | uses: actions/setup-go@v2 27 | with: 28 | go-version: ^1.13 29 | 30 | - name: Check out code into the Go module directory 31 | uses: actions/checkout@v2 32 | 33 | - name: Setup protoc 34 | uses: arduino/setup-protoc@v1.1.2 35 | 36 | - name: Install protoc-gen-go 37 | run: go get -u google.golang.org/protobuf/cmd/protoc-gen-go 38 | 39 | - name: Build protocol buffers code 40 | run: bash tools/protoc.sh . --no-js 41 | 42 | - name: Run Linting 43 | id: lint 44 | run: bash tools/goci.sh backend 45 | continue-on-error: true 46 | 47 | - name: Comment lint errors 48 | if: steps.lint.outputs.body != '' 49 | uses: peter-evans/commit-comment@v1 50 | with: 51 | body: ${{ steps.lint.outputs.body }} 52 | 53 | - name: Stop run if errors occurred 54 | if: steps.lint.outputs.body != '' 55 | run: exit 1 56 | 57 | - name: Build 58 | run: go build -v . 59 | working-directory: backend 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "murdermystery", 3 | "version": "0.2.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "export": "next export", 10 | "pbjs": "pbjs", 11 | "pbts": "pbts", 12 | "eslint": "eslint ./", 13 | "eslint-fix": "eslint ./ --fix", 14 | "type-check": "tsc --noemit", 15 | "lint": "npm run eslint && npm run type-check", 16 | "go-vet": "cd backend && go vet ." 17 | }, 18 | "dependencies": { 19 | "@chakra-ui/react": "^1.6.3", 20 | "@chakra-ui/system": "^1.6.4", 21 | "@emotion/react": "^11.1.5", 22 | "@emotion/styled": "^11.3.0", 23 | "framer-motion": "^4.1.10", 24 | "next": "^10.2.3", 25 | "protobufjs": "^6.10.2", 26 | "react": "16.14.0", 27 | "react-dom": "16.14.0" 28 | }, 29 | "devDependencies": { 30 | "@types/node": "^14.14.41", 31 | "@types/react": "^17.0.8", 32 | "@types/react-dom": "^17.0.3", 33 | "@typescript-eslint/eslint-plugin": "^4.22.0", 34 | "@typescript-eslint/parser": "^4.25.0", 35 | "eslint": "^7.25.0", 36 | "eslint-config-airbnb": "^18.2.1", 37 | "eslint-config-prettier": "^8.3.0", 38 | "eslint-import-resolver-typescript": "^2.4.0", 39 | "eslint-plugin-import": "^2.22.1", 40 | "eslint-plugin-jsx-a11y": "^6.4.1", 41 | "eslint-plugin-prettier": "^3.4.0", 42 | "eslint-plugin-react": "^7.23.2", 43 | "eslint-plugin-react-hooks": "^4.2.0", 44 | "husky": "^6.0.0", 45 | "lint-staged": "^10.5.4", 46 | "prettier": "^2.2.1", 47 | "standard": "^16.0.3", 48 | "typescript": "^4.2.4", 49 | "ws": "^7.4.6" 50 | }, 51 | "husky": { 52 | "hooks": { 53 | "pre-commit": "lint-staged --config lint-staged.config.js" 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /pages/game.tsx: -------------------------------------------------------------------------------- 1 | import { useClientOnly } from "components/ClientOnly"; 2 | import Layout from "components/Layout"; 3 | import NameSelector from "components/NameSelector"; 4 | import { useRouter } from "next/router"; 5 | import { FC, useState } from "react"; 6 | import GameClient from "../components/GameClient"; 7 | import { STRINGS, useTranslator } from "../lib/translate"; 8 | 9 | function useGameContent() { 10 | const t = useTranslator(); 11 | const { query } = useRouter(); 12 | const [name, setName] = useState(""); 13 | 14 | const nameComponent = ( 15 | setName(newName)} /> 16 | ); 17 | 18 | if (!useClientOnly()) { 19 | // When pre-rednering, the id is never set so we don't want the error to be 20 | // pre-rendered so pre-render the name component which will be shown the most often 21 | return nameComponent; 22 | } 23 | 24 | const { id } = query; 25 | const host = window?.location?.host || "localhost:3000"; 26 | const server = 27 | query.srv || 28 | (window?.location?.protocol === "https:" ? "wss://" : "ws://") + 29 | (host === "localhost:3000" ? "localhost:8080" : host); 30 | const queryName = (query.name || "").toString(); 31 | 32 | if (!name) { 33 | if (queryName) { 34 | setName(queryName); 35 | } 36 | return nameComponent; 37 | } 38 | if (!id || Array.isArray(id)) { 39 | return

{t(STRINGS.INVALID_GAME_LINK)}

; 40 | } 41 | return ( 42 | 47 | ); 48 | } 49 | 50 | interface GameProps {} 51 | 52 | export const Game: FC = () => { 53 | return ( 54 | 55 |
{useGameContent()}
56 |
57 | ); 58 | }; 59 | 60 | export default Game; 61 | -------------------------------------------------------------------------------- /components/LangContext.tsx: -------------------------------------------------------------------------------- 1 | import Lang from "lib/langs"; 2 | import { oppositeLang } from "lib/translate"; 3 | import { useRouter } from "next/router"; 4 | import { ParsedUrlQuery } from "querystring"; 5 | import { 6 | createContext, 7 | FC, 8 | PropsWithChildren, 9 | useContext, 10 | useEffect, 11 | useState, 12 | } from "react"; 13 | import { useClientOnly } from "./ClientOnly"; 14 | 15 | export interface LangContextType { 16 | lang: Lang | null; 17 | setLanguage: (l: Lang) => void; 18 | } 19 | 20 | export const LangContext = createContext({ 21 | lang: null, 22 | setLanguage: () => undefined, // This will be set by LangProvider in _app 23 | }); 24 | 25 | export function useLanguage(): Lang | null { 26 | const { lang } = useContext(LangContext); 27 | 28 | if (!useClientOnly()) return null; 29 | 30 | return lang; 31 | } 32 | 33 | export function useToggleLang(): () => void { 34 | const { lang, setLanguage } = useContext(LangContext); 35 | 36 | // if lang is null, toggling is a no-op. 37 | return lang ? () => setLanguage(oppositeLang(lang)) : () => undefined; 38 | } 39 | 40 | export function getDefaultLang(query: ParsedUrlQuery): Lang { 41 | return "zh" in query ? Lang.ZH : Lang.EN; 42 | } 43 | 44 | export interface LangProviderProps {} 45 | 46 | export const LangProvider: FC = ({ 47 | children, 48 | }: PropsWithChildren) => { 49 | const { query } = useRouter(); 50 | 51 | const [lang, setLang] = useState(null); 52 | const isClient = useClientOnly(); 53 | 54 | useEffect(() => { 55 | if (isClient) setLang(getDefaultLang(query)); 56 | }, [query, isClient]); 57 | 58 | return ( 59 | 60 | {children} 61 | 62 | ); 63 | }; 64 | -------------------------------------------------------------------------------- /components/Lobby.tsx: -------------------------------------------------------------------------------- 1 | import { Badge, Button, Flex, Heading, Text } from "@chakra-ui/react"; 2 | import { FC } from "react"; 3 | import { STRINGS, useTranslator } from "../lib/translate"; 4 | import { murdermystery as protobuf } from "../pbjs/protobuf.js"; 5 | 6 | interface LobbyProps { 7 | players: { 8 | [id: string]: protobuf.Players.IPlayer; 9 | }; 10 | hostId: number; 11 | isHost: boolean; 12 | start: () => void; 13 | } 14 | 15 | export const Lobby: FC = ({ 16 | players, 17 | isHost, 18 | hostId, 19 | start, 20 | }: LobbyProps) => { 21 | const t = useTranslator(); 22 | 23 | const hostBadge = ( 24 | 25 | {t(STRINGS.HOST)} 26 | 27 | ); 28 | 29 | return ( 30 | <> 31 | 32 | {t(STRINGS.WAITING_FOR_PLAYERS)} 33 | 34 | 35 | {t(STRINGS.SHARE_TO_INVITE)} 36 | 37 | 38 | {/* Polish: style this a bit more, don't use
    */} 39 |
      40 | {Object.keys(players).map((id) => { 41 | const p = players[id]; 42 | if (!p || !p.name) return null; 43 | const pIsHost = p.id === hostId; 44 | return ( 45 |
    • 46 | {p.name} 47 | {pIsHost && hostBadge} 48 |
    • 49 | ); 50 | })} 51 |
    52 | 61 | 62 | ); 63 | }; 64 | 65 | export default Lobby; 66 | -------------------------------------------------------------------------------- /backend/game/prophetvote.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/Scoder12/murdermystery/backend/protocol" 7 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 8 | "gopkg.in/olahol/melody.v1" 9 | ) 10 | 11 | func (g *Game) callProphetVote() { 12 | g.lock.Lock() 13 | defer g.lock.Unlock() 14 | 15 | prophet, notProphet := g.SessionsByRole(pb.Character_PROPHET) 16 | if len(prophet) > 0 { 17 | // call prophet vote 18 | go g.callVote(prophet, notProphet, pb.VoteRequest_PROPHET, g.prophetVoteHandler(), false) 19 | } else { 20 | // skip prophet vote 21 | go g.callHealerHealVote() 22 | } 23 | } 24 | 25 | // prohetReveal reveals whether choice is good or bad to prophet. Returns true on 26 | // sucess, false otherwise. Assumes game is locked. 27 | func (g *Game) prophetReveal(prophet, choice *melody.Session) bool { 28 | // Get clients 29 | prophetClient := g.clients[prophet] 30 | choiceClient := g.clients[choice] 31 | if choiceClient == nil || prophet == nil { 32 | return false 33 | } 34 | 35 | // They are good if they are anything but werewolf 36 | good := choiceClient.role != pb.Character_WEREWOLF 37 | // Send voter the result 38 | msg, err := protocol.Marshal(&pb.ProphetReveal{Id: choiceClient.ID, Good: good}) 39 | if err != nil { 40 | return false 41 | } 42 | err = prophet.WriteBinary(msg) 43 | printerr(err) 44 | // Log this to spectators 45 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate( 46 | &pb.SpectatorProphetReveal{ 47 | ProphetId: prophetClient.ID, 48 | ChoiceId: choiceClient.ID, 49 | Good: good, 50 | })) 51 | 52 | return true 53 | } 54 | 55 | // Handler assumes game is locked 56 | func (g *Game) prophetVoteHandler() func(*Vote, *melody.Session, *melody.Session) { 57 | return func(v *Vote, voter, candidate *melody.Session) { 58 | if g.vote != v { 59 | return 60 | } 61 | log.Println("Prophet vote over") 62 | 63 | g.prophetReveal(voter, candidate) 64 | 65 | // Doesn't really matter if this is at top or bottom, put at bottom just to be safe 66 | g.vote.End(g, nil) 67 | 68 | go g.callHealerHealVote() 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [published] 6 | workflow_dispatch: 7 | 8 | jobs: 9 | release: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | 14 | - name: Set up Go 1.x 15 | uses: actions/setup-go@v2 16 | with: 17 | go-version: ^1.13 18 | 19 | - name: Use Node.js v14.x 20 | uses: actions/setup-node@v2.1.5 21 | with: 22 | node-version: 14.x 23 | 24 | - name: Check out code 25 | uses: actions/checkout@v2 26 | 27 | - name: Setup protoc 28 | uses: arduino/setup-protoc@v1.1.2 29 | 30 | - name: Install protoc-gen-go 31 | run: go get -u google.golang.org/protobuf/cmd/protoc-gen-go 32 | 33 | - name: Install statik command line tool 34 | run: go get -u github.com/rakyll/statik 35 | 36 | - uses: actions/cache@v2.1.6 37 | with: 38 | path: ${{ github.workspace }}/.next/cache 39 | key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }} 40 | 41 | - uses: bahmutov/npm-install@v1 42 | 43 | - name: Build protocol buffers code 44 | run: bash ./tools/protoc.sh 45 | 46 | - name: Run build script 47 | run: bash ./tools/build.sh 48 | 49 | - name: Upload Release Asset 50 | if: ${{ github.event_name == 'release' }} 51 | id: upload-release-asset 52 | uses: actions/upload-release-asset@v1 53 | env: 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | with: 56 | upload_url: ${{ github.event.release.upload_url }} 57 | asset_path: ./build/backend 58 | asset_name: server_linux_x64 59 | asset_content_type: application/octet-stream 60 | 61 | - name: Install repl.it CLI 62 | run: npm i -g replit 63 | 64 | - name: Set up repl.it CLI 65 | run: replit auth -k "$TOKEN" && replit local 43df7835-2bf0-45ad-9ef9-6b6e1f58dffe 66 | env: 67 | TOKEN: ${{ secrets.REPLIT_TOKEN }} 68 | 69 | - name: Deploy to Repl.it 70 | run: replit bulk \ 71 | cp build/backend repl:murdermystery -- \ 72 | run --restart 73 | -------------------------------------------------------------------------------- /lib/useGameSocket.ts: -------------------------------------------------------------------------------- 1 | import { murdermystery as protobuf } from "pbjs/protobuf"; 2 | import { useEffect, useRef } from "react"; 3 | import { STRINGS } from "./translate"; 4 | 5 | interface GameSocket { 6 | send: (msg: protobuf.IClientMessage) => void; 7 | isConnected: () => boolean; 8 | } 9 | 10 | export default function useGameSocket( 11 | wsUrl: string, 12 | name: string, 13 | parseMessage: (ev: MessageEvent) => void, 14 | onError: (msg: STRINGS) => void 15 | ): GameSocket { 16 | // Main websocket 17 | const wsRef = useRef(null); 18 | let ws = wsRef.current; 19 | 20 | // Send encodes and sends a protobuf message to the server. 21 | const send = (msg: protobuf.IClientMessage) => { 22 | if (!ws) { 23 | // eslint-disable-next-line no-console 24 | console.error("Try to send() while ws is null"); 25 | return; 26 | } 27 | // eslint-disable-next-line no-console 28 | console.log("↑", msg); 29 | ws.send(protobuf.ClientMessage.encode(msg).finish()); 30 | }; 31 | 32 | const sendName = () => { 33 | send({ 34 | setName: { name }, 35 | }); 36 | }; 37 | 38 | // setup websocket 39 | useEffect(() => { 40 | // eslint-disable-next-line no-console 41 | console.log("Connecting to", wsUrl); 42 | try { 43 | wsRef.current = new WebSocket(wsUrl); 44 | } catch (e) { 45 | // eslint-disable-next-line no-console 46 | console.error(e); 47 | onError(STRINGS.ERROR_OPENING_CONN); 48 | return; 49 | } 50 | // Already storing it in a ref, so this warning is invalid 51 | // eslint-disable-next-line react-hooks/exhaustive-deps 52 | ws = wsRef.current; 53 | // Use proper binary type (no blobs) 54 | ws.binaryType = "arraybuffer"; 55 | // handshake when it opens 56 | ws.addEventListener("open", () => sendName()); 57 | // Handle messages 58 | ws.addEventListener("message", (ev: MessageEvent) => parseMessage(ev)); 59 | // Handle discconects 60 | ws.addEventListener("close", () => { 61 | // eslint-disable-next-line no-console 62 | console.log("Connection closed"); 63 | onError(STRINGS.SERVER_DISCONNECTED); 64 | }); 65 | 66 | // Cleanup: close websocket 67 | // eslint-disable-next-line consistent-return 68 | return () => ws?.close(); 69 | }, []); 70 | 71 | return { 72 | send, 73 | isConnected: (): boolean => !!ws && ws.readyState === WebSocket.OPEN, 74 | }; 75 | } 76 | -------------------------------------------------------------------------------- /backend/game/juryvote.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/Scoder12/murdermystery/backend/protocol" 7 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 8 | "gopkg.in/olahol/melody.v1" 9 | ) 10 | 11 | func (g *Game) callJuryVote() { 12 | g.lock.Lock() 13 | defer g.lock.Unlock() 14 | 15 | // Make list of all killed IDs 16 | killedIDs := make([]int32, len(g.killed)) 17 | i := 0 18 | log.Println("Prejury killed:", g.killed) 19 | for s := range g.killed { 20 | c := g.clients[s] 21 | if c != nil { 22 | killedIDs[i] = c.ID 23 | i++ 24 | } 25 | } 26 | // Marshal killReveal 27 | killReveal, err := protocol.Marshal(&pb.KillReveal{Killed_IDs: killedIDs}) 28 | if err != nil { 29 | return 30 | } 31 | // Now that the kills are revealed, commit them so that dead people can't be voted for 32 | g.commitKills() 33 | 34 | // Make a list of all client sessions, sending killreveal message to each 35 | clients := make([]*melody.Session, len(g.clients)) 36 | i = 0 37 | for c := range g.clients { 38 | if c != nil { 39 | clients[i] = c 40 | i++ 41 | // Send KillReveal 42 | err = c.WriteBinary(killReveal) 43 | printerr(err) 44 | } 45 | } 46 | log.Println("Calling jury vote", clients, clients) 47 | go g.callVote(clients, clients, pb.VoteRequest_JURY, g.juryVoteHandler(), true) 48 | } 49 | 50 | func (g *Game) juryVoteHandler() func(*Vote, *melody.Session, *melody.Session) { 51 | return func(v *Vote, voter, candidate *melody.Session) { 52 | if !v.AllVotesIn() { 53 | return 54 | } 55 | 56 | status, winner := v.GetResult() 57 | if status != pb.VoteResultType_TIE && winner == nil { 58 | // something went wrong 59 | log.Println("No vote winner! Tally:", v.Tally()) 60 | status = pb.VoteResultType_NOWINNER 61 | } 62 | var winnerID int32 63 | if winner != nil { 64 | client := g.clients[winner] 65 | if client != nil { 66 | winnerID = client.ID 67 | g.stageKill(winner, pb.KillReason_VOTED) 68 | } 69 | } 70 | 71 | // Send VoteOver 72 | v.End(g, &pb.JuryVoteResult{Status: status, Winner: winnerID}) 73 | g.commitKills() 74 | // Show players who is alive and dead 75 | g.sendPlayerStatus() 76 | 77 | gameOverReason := g.checkForGameOver() 78 | if gameOverReason != pb.GameOver_NONE { 79 | // The game is over. Send game over message and end it. 80 | g.handleGameOver(gameOverReason) 81 | } else { 82 | // The game is not over. Start a new night with the first vote! 83 | go g.callWolfVote() 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /locales/zh.json: -------------------------------------------------------------------------------- 1 | { 2 | "TITLE": "狼人杀", 3 | "JOIN_GAME": "加入游戏", 4 | "JOIN": "加入", 5 | "CREATE_GAME": "创建游戏", 6 | "NEED_GAME_LINK": "要求主持人向您发送指向他们游戏的链接", 7 | "ENTER_NAME": "输入名字", 8 | "INVALID_GAME_LINK": "无效的游戏连结", 9 | "SERVER_DISCONNECTED": "与服务器断开连接", 10 | "START_GAME": "开始游戏", 11 | "WAIT_FOR_HOST": "等待主机开始游戏", 12 | "GAME_ALREADY_STARTED": "游戏已经开始,您不能加入", 13 | "ERROR": "服务器错误", 14 | "WAITING_FOR_PLAYERS": "等待玩家加入", 15 | "SHARE_TO_INVITE": "分享您的链接邀请其他人", 16 | "COPY": "复制", 17 | "HOST": "主持人", 18 | "ERROR_OPENING_CONN": "打开与服务器的连接时出错", 19 | "SERVER_CLOSED_CONN": "服务器关闭了连接", 20 | "INVALID_NAME": "您的名字无效", 21 | "PLAYER_DISCONNECTED": "有人断开连接,游戏结束了", 22 | "ERROR_PERFORMING_ACTION": "处理您的请求时发生错误", 23 | "NEED_MORE_PLAYERS": "您至少需要6个人才能开始", 24 | "YOU_ARE": "你是", 25 | "CITIZEN": "村民", 26 | "WEREWOLF": "狼人", 27 | "HEALER": "女巫", 28 | "PROPHET": "预言家", 29 | "HUNTER": "猎人", 30 | "FELLOW_WOLVES": "别的狼人", 31 | "VOTES": "票数", 32 | "HAS_NOT_VOTED": "还没有投票", 33 | "PLEASE_VOTE": "请投票", 34 | "PICK_KILL": "选择一个人杀死", 35 | "NEED_CONSENSUS": "每个人都要同意", 36 | "IS_NIGHT": "是晚上,你在睡觉", 37 | "PICK_REVEAL": "选择一个人使用你的预言能力", 38 | "REVEAL_FOUND": "使用你的预言能力,你发现", 39 | "IS_GOOD": "是好人", 40 | "IS_BAD": "是坏人", 41 | "YOU_CANT_START": "您还不能开始游戏", 42 | "VOTE_RESULTS": "投票结果", 43 | "N_VOTES": "票:", 44 | "PLEASE_CONFIRM": "清决定", 45 | "WAS_KILLED_CONFIRM_HEAL": "被杀死。你相救他们吗?", 46 | "CONFIRM_POISON": "你有个毒药。你可以现在它或保存它。", 47 | "YES_TO_USING": "要", 48 | "NO_TO_USING": "不要", 49 | "SKIP_USING": "跳过使用", 50 | "OK": "好的", 51 | "SKIP": "跳过", 52 | "WAITING_FOR_VOTE": "等待投票完成", 53 | "PICK_WEREWOLF": "谁是狼人?", 54 | "KILLS_DURING_NIGHT": "在晚上被杀死的人:", 55 | "NONE": "没有", 56 | "CITIZENS_WIN": "村民赢", 57 | "WEREWOLVES_WIN": "狼人赢", 58 | "GAME_OVER": "游戏结束", 59 | "ALIVE": "还活着", 60 | "DEAD": "死的", 61 | "IS": "是", 62 | "ARE_SPECTATOR": "你是一个观众", 63 | "USED_PROPHET": "使用他的预言能力发现", 64 | "USED_HEAL": "救了", 65 | "WAS_KILLED": "死了", 66 | "VOTED_OUT": "被投票死了", 67 | "KILLED_BY_HEALER": "被女巫毒死了", 68 | "KILLED_BY_WOLVES": "被狼人杀死了", 69 | "WAITING_WOLVES_1": "狼人", 70 | "WAITING_WOLVES_2": "在选人杀死", 71 | "WAITING_PROPHET_1": "预言家", 72 | "WAITING_PROPHET_2": "在选一个人来以利用他们的能力", 73 | "WAITING_HEAL_1": "女巫", 74 | "WAITING_HEAL_2": "在决定他要不要救", 75 | "WAITING_POISON_1": "女巫", 76 | "WAITING_POISON_2": "在决定他要不要用毒药", 77 | "WAITING_JURY": "所有人在决定谁会被投票死了", 78 | "VOTE_STARTED": "投票开始", 79 | "WOLF_VOTE_OVER": "狼人选了杀死", 80 | "VOTE_TIED_1": "票与", 81 | "VOTE_TIED_2": "票并列", 82 | "CHANGE_LANGUAGE": "中文", 83 | "YOU_WERE_KILLED": "你被杀死了!", 84 | "PDEATH_VOTED": "你被投票死了", 85 | "PDEATH_HEALER": "你被女巫毒死了", 86 | "PDEATH_WOLVES": "你被狼人杀死了" 87 | } 88 | -------------------------------------------------------------------------------- /backend/game/healervote.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/Scoder12/murdermystery/backend/protocol" 7 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 8 | "gopkg.in/olahol/melody.v1" 9 | ) 10 | 11 | func (g *Game) callHealerHealVote() { 12 | g.lock.Lock() 13 | defer g.lock.Unlock() 14 | 15 | if g.hasHeal { 16 | log.Println("Starting healer vote") 17 | healers, _ := g.SessionsByRole(pb.Character_HEALER) 18 | if len(healers) < 1 { 19 | // Healer is dead, skip this vote. 20 | go g.callJuryVote() 21 | return 22 | } 23 | if len(healers) > 1 { 24 | log.Printf("Error: Healers length >1: %v", healers) 25 | return 26 | } 27 | 28 | // Find ID of killed client and send it to healer 29 | killedSession := g.getKilled() 30 | killedClient := g.clients[killedSession] 31 | if killedClient != nil { 32 | killedIDs := make([]int32, 1) 33 | killedIDs[0] = killedClient.ID 34 | msg, err := protocol.Marshal(&pb.KillReveal{Killed_IDs: killedIDs}) 35 | if err != nil { 36 | return 37 | } 38 | err = healers[0].WriteBinary(msg) 39 | printerr(err) 40 | } 41 | 42 | log.Println("Calling healer vote, g.killed:", g.killed) 43 | go g.callVote(healers, []*melody.Session{}, pb.VoteRequest_HEALERHEAL, func(v *Vote, t, c *melody.Session) {}, false) 44 | } else { 45 | go g.callHealerPoisonVote() 46 | } 47 | } 48 | 49 | func (g *Game) healerHealHandler(healer *Client, confirmed bool) { 50 | if confirmed && g.hasHeal { 51 | // They are using their heal 52 | log.Println("Heal used") 53 | g.hasHeal = false 54 | 55 | // All the clients who were healed 56 | healedIDs := make([]int32, len(g.killed)) 57 | i := 0 58 | for s := range g.killed { 59 | c := g.clients[s] 60 | if c != nil { 61 | healedIDs[i] = c.ID 62 | i++ 63 | } 64 | } 65 | 66 | // Dispatch this to spectators 67 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate(&pb.SpectatorHealerHeal{ 68 | Healer: healer.ID, 69 | Healed: healedIDs, 70 | })) 71 | 72 | // Perform the heal by emptying g.killed 73 | g.resetKills() 74 | } 75 | log.Println("Heal response received, ending vote") 76 | g.vote.End(g, nil) 77 | go g.callHealerPoisonVote() 78 | } 79 | 80 | func (g *Game) callHealerPoisonVote() { 81 | g.lock.Lock() 82 | defer g.lock.Unlock() 83 | 84 | if g.hasPoison { 85 | healer, notHealer := g.SessionsByRole(pb.Character_HEALER) 86 | go g.callVote(healer, notHealer, pb.VoteRequest_HEALERPOISON, g.healerPoisonHandler(), false) 87 | } else { 88 | go g.callJuryVote() 89 | } 90 | } 91 | 92 | func (g *Game) healerPoisonHandler() func(*Vote, *melody.Session, *melody.Session) { 93 | return func(v *Vote, voter, candidate *melody.Session) { 94 | log.Println("Healer Poison vote over") 95 | if g.hasPoison && candidate != nil { 96 | log.Println("Poison used on", candidate) 97 | g.hasPoison = false 98 | g.stageKill(candidate, pb.KillReason_HEALERPOISON) 99 | } 100 | g.vote.End(g, nil) 101 | log.Println("After poison g.killed:", g.killed) 102 | go g.callJuryVote() 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /components/Vote.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Button, Flex, Heading, Text } from "@chakra-ui/react"; 2 | import { STRINGS, useTranslator } from "lib/translate"; 3 | import { FC, useState } from "react"; 4 | import NameBadge from "./NameBadge"; 5 | import { RightFloatSkippableDelay } from "./SkippableDelay"; 6 | 7 | export interface Choice { 8 | name?: string | JSX.Element; 9 | id?: number; 10 | } 11 | 12 | function VotesDisplay({ 13 | candidate, 14 | onVote, 15 | }: { 16 | candidate: Choice; 17 | onVote: (cid: number) => void; 18 | }) { 19 | const candidateName: string | JSX.Element = candidate.name || ""; 20 | const id: number = typeof candidate.id === "number" ? candidate.id : -1; 21 | 22 | return ( 23 | 24 | 27 | 28 | ); 29 | } 30 | 31 | export interface VoteProps { 32 | msg: STRINGS; 33 | desc?: JSX.Element | null; 34 | candidates: Choice[]; 35 | onVote: (candidateID: number) => void; 36 | } 37 | 38 | export const Vote: FC = ({ 39 | msg, 40 | desc, 41 | candidates, 42 | onVote, 43 | }: VoteProps) => { 44 | const t = useTranslator(); 45 | const [didVote, setDidVote] = useState(false); 46 | 47 | if (didVote) { 48 | return {t(STRINGS.WAITING_FOR_VOTE)}; 49 | } 50 | 51 | return ( 52 | <> 53 | {t(msg)} 54 | {desc ? {desc} : null} 55 | 56 | {candidates.map((candidate: Choice) => 57 | candidate.name ? ( 58 | { 62 | onVote(cid); 63 | setDidVote(true); 64 | }} 65 | /> 66 | ) : null 67 | )} 68 | 69 | 70 | ); 71 | }; 72 | 73 | Vote.defaultProps = { 74 | desc: undefined, 75 | }; 76 | 77 | export default Vote; 78 | 79 | export interface Candidate { 80 | id: number; 81 | name: string; 82 | voters: string[]; 83 | } 84 | 85 | export interface VoteResultProps { 86 | votes: Candidate[]; 87 | onDone: () => void; 88 | } 89 | 90 | export const VoteResult: FC = ({ 91 | votes, 92 | onDone, 93 | }: VoteResultProps) => { 94 | const t = useTranslator(); 95 | 96 | return ( 97 | <> 98 | 99 | {t(STRINGS.VOTE_RESULTS)} 100 | {votes.map((c) => ( 101 | 102 | 103 | {c.name} 104 | 105 | {c.voters.length + t(STRINGS.N_VOTES)} 106 | 107 | {c.voters.map((v) => ( 108 | 109 | ))} 110 | 111 | 112 | ))} 113 | 114 | 115 | 116 | ); 117 | }; 118 | -------------------------------------------------------------------------------- /lib/translate.ts: -------------------------------------------------------------------------------- 1 | import { useLanguage } from "components/LangContext"; 2 | import enJSON from "../locales/en.json"; 3 | import zhJSON from "../locales/zh.json"; 4 | import Lang from "./langs"; 5 | 6 | // ESLint is being weird 7 | // eslint-disable-next-line no-shadow 8 | export enum STRINGS { 9 | TITLE, 10 | JOIN_GAME, 11 | JOIN, 12 | CREATE_GAME, 13 | NEED_GAME_LINK, 14 | ENTER_NAME, 15 | INVALID_GAME_LINK, 16 | SERVER_DISCONNECTED, 17 | START_GAME, 18 | WAIT_FOR_HOST, 19 | GAME_ALREADY_STARTED, 20 | ERROR, 21 | WAITING_FOR_PLAYERS, 22 | SHARE_TO_INVITE, 23 | COPY, 24 | HOST, 25 | ERROR_OPENING_CONN, 26 | SERVER_CLOSED_CONN, 27 | INVALID_NAME, 28 | PLAYER_DISCONNECTED, 29 | ERROR_PERFORMING_ACTION, 30 | NEED_MORE_PLAYERS, 31 | YOU_ARE, 32 | CITIZEN, 33 | WEREWOLF, 34 | HEALER, 35 | PROPHET, 36 | HUNTER, 37 | FELLOW_WOLVES, 38 | VOTES, 39 | HAS_NOT_VOTED, 40 | PLEASE_VOTE, 41 | PICK_KILL, 42 | NEED_CONSENSUS, 43 | IS_NIGHT, 44 | PICK_REVEAL, 45 | REVEAL_FOUND, 46 | IS_GOOD, 47 | IS_BAD, 48 | YOU_CANT_START, 49 | VOTE_RESULTS, 50 | N_VOTES, 51 | PLEASE_CONFIRM, 52 | WAS_KILLED_CONFIRM_HEAL, 53 | CONFIRM_POISON, 54 | YES_TO_USING, 55 | NO_TO_USING, 56 | SKIP_USING, 57 | OK, 58 | SKIP, 59 | WAITING_FOR_VOTE, 60 | PICK_WEREWOLF, 61 | KILLS_DURING_NIGHT, 62 | NONE, 63 | CITIZENS_WIN, 64 | WEREWOLVES_WIN, 65 | GAME_OVER, 66 | ALIVE, 67 | DEAD, 68 | IS, 69 | ARE_SPECTATOR, 70 | USED_PROPHET, 71 | USED_HEAL, 72 | WAS_KILLED, 73 | VOTED_OUT, 74 | KILLED_BY_HEALER, 75 | KILLED_BY_WOLVES, 76 | WAITING_WOLVES_1, 77 | WAITING_WOLVES_2, 78 | WAITING_PROPHET_1, 79 | WAITING_PROPHET_2, 80 | WAITING_HEAL_1, 81 | WAITING_HEAL_2, 82 | WAITING_POISON_1, 83 | WAITING_POISON_2, 84 | WAITING_JURY, 85 | VOTE_STARTED, 86 | WOLF_VOTE_OVER, 87 | VOTE_TIED_1, 88 | VOTE_TIED_2, 89 | CHANGE_LANGUAGE, 90 | YOU_WERE_KILLED, 91 | PDEATH_VOTED, 92 | PDEATH_HEALER, 93 | PDEATH_WOLVES, 94 | } 95 | 96 | export const S = STRINGS; 97 | 98 | export type LanguageTranslations = { 99 | [key in keyof typeof STRINGS]: string; 100 | }; 101 | 102 | export const languages: Record = { 103 | [Lang.EN]: enJSON, 104 | [Lang.ZH]: zhJSON, 105 | }; 106 | 107 | export type Translator = (phrase: STRINGS) => string; 108 | 109 | const makeTranslator = (dict: LanguageTranslations): Translator => ( 110 | phrase: STRINGS 111 | ) => { 112 | const key: string = typeof phrase === "string" ? phrase : STRINGS[phrase]; 113 | return dict[key]; 114 | }; 115 | 116 | /** 117 | * Gets the current translator based on the querystring. 118 | * If zh paramater is in querystring, will lookup key in chinese translations, 119 | * otherwise will use english. 120 | */ 121 | export function useTranslator(): Translator { 122 | const lang = useLanguage(); 123 | 124 | return lang ? makeTranslator(languages[lang]) : () => ""; 125 | } 126 | 127 | export function oppositeLang(lang: Lang): Lang { 128 | return lang === Lang.EN ? Lang.ZH : Lang.EN; 129 | } 130 | 131 | export function useChangeLanguageText(): string { 132 | const lang = useLanguage(); 133 | 134 | if (!lang) return ""; 135 | 136 | // Since there are only 2 supported languages, return the text in the other language 137 | const oppLang = oppositeLang(lang); 138 | 139 | return makeTranslator(languages[oppLang])(STRINGS.CHANGE_LANGUAGE); 140 | } 141 | -------------------------------------------------------------------------------- /backend/game/handler.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "time" 7 | 8 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 9 | 10 | "github.com/Scoder12/murdermystery/backend/protocol" 11 | "gopkg.in/olahol/melody.v1" 12 | ) 13 | 14 | func (g *Game) handleJoin(s *melody.Session) { 15 | log.Println("Handling join") 16 | g.lock.Lock() 17 | defer g.lock.Unlock() 18 | log.Println("Lock aquired") 19 | 20 | if g.started { 21 | msg, err := protocol.Marshal(g.getPlayersMsg()) 22 | if err != nil { 23 | return 24 | } 25 | err = s.WriteBinary(msg) 26 | printerr(err) 27 | 28 | go g.addSpectator(s) 29 | return 30 | } 31 | 32 | c := &Client{ID: g.nextID} 33 | g.nextID++ 34 | g.clients[s] = c 35 | 36 | msg, err := protocol.Marshal(&pb.Handshake{Status: pb.Handshake_OK, Id: c.ID}) 37 | if err != nil { 38 | return 39 | } 40 | err = s.WriteBinary(msg) 41 | printerr(err) 42 | 43 | // For efficiency, the timer will be stopped if the client discconects fast enough 44 | c.closeTimer = time.AfterFunc(2*time.Second, func() { 45 | g.lock.Lock() 46 | defer g.lock.Unlock() 47 | 48 | if len(c.name) == 0 { 49 | log.Printf("[%v] Did not name, closing\n", c.ID) 50 | err = s.Close() 51 | printerr(err) 52 | } 53 | }) 54 | 55 | g.updateHost() 56 | } 57 | 58 | func (g *Game) handleDisconnect(s *melody.Session) { 59 | g.lock.Lock() 60 | defer g.lock.Unlock() 61 | 62 | c := g.clients[s] 63 | // They are probably a spectator, ignore the disconnect 64 | if c == nil { 65 | return 66 | } 67 | 68 | log.Printf("[%v] Disconnected (game started: %v)\n", c.ID, g.started) 69 | delete(g.clients, s) 70 | 71 | if g.started { 72 | log.Println("Stopping game") 73 | // Call in a goroutine so deferred game unlock can run 74 | go g.EndWithError(pb.Error_DISCONNECT) 75 | return 76 | } 77 | 78 | if c != nil && c.closeTimer != nil { 79 | log.Println("Stopping timer") 80 | c.closeTimer.Stop() 81 | } 82 | 83 | g.syncPlayers() 84 | } 85 | 86 | func (g *Game) callHandler(s *melody.Session, c *Client, data *pb.ClientMessage) error { 87 | switch msg := data.Data.(type) { 88 | case *pb.ClientMessage_SetName: 89 | g.setNameHandler(s, c, msg.SetName) 90 | return nil 91 | case *pb.ClientMessage_StartGame: 92 | g.startGameHandler(s, c, msg.StartGame) 93 | return nil 94 | case *pb.ClientMessage_Vote: 95 | g.handleVoteMessage(s, c, msg.Vote) 96 | return nil 97 | default: 98 | return fmt.Errorf("unrecognized op") 99 | } 100 | } 101 | 102 | // HandleMsg handles a message from a client 103 | func (g *Game) handleMsg(s *melody.Session, data []byte) { 104 | // https://developers.google.com/protocol-buffers/docs/reference/go-generated#oneof 105 | g.lock.Lock() 106 | c, ok := g.clients[s] 107 | g.lock.Unlock() 108 | 109 | // They are a spectator or some sort of race condition, ignore silently 110 | if !ok { 111 | return 112 | } 113 | //log.Printf("[%v] ↑ %s\n", c.ID, string(msg)) 114 | var msg *pb.ClientMessage = protocol.Unmarshal(data) 115 | if msg == nil { 116 | log.Printf("[%v] Invalid data", c.ID) 117 | err := s.Close() 118 | printerr(err) 119 | return 120 | } 121 | 122 | err := g.callHandler(s, c, msg) 123 | if err != nil { 124 | log.Println(err) 125 | err = s.Close() 126 | printerr(err) 127 | } 128 | } 129 | 130 | func (g *Game) handleStart() { 131 | g.AssignCharacters() 132 | 133 | g.lock.Lock() 134 | // Populate g.players 135 | for s, c := range g.clients { 136 | g.players[s] = c 137 | } 138 | g.lock.Unlock() 139 | 140 | g.revealWolves() 141 | } 142 | -------------------------------------------------------------------------------- /components/GameOver.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Flex, Heading, HStack, Image } from "@chakra-ui/react"; 2 | import { characterToImg, characterToString } from "lib/CharacterImg"; 3 | import { STRINGS, useTranslator } from "lib/translate"; 4 | import { murdermystery as protobuf } from "pbjs/protobuf"; 5 | import { FC } from "react"; 6 | import NameBadge from "./NameBadge"; 7 | 8 | export interface Player { 9 | id: number; 10 | name: string; 11 | role: protobuf.Character; 12 | } 13 | 14 | const C = protobuf.Character; 15 | const R = protobuf.GameOver.Reason; 16 | 17 | export interface CharacterImageProps { 18 | character: protobuf.Character; 19 | width?: string; 20 | } 21 | 22 | export const CharacterImage: FC = ({ 23 | character, 24 | width = "125px", 25 | }: CharacterImageProps) => { 26 | return ; 27 | }; 28 | 29 | export interface PlayerGroupProps { 30 | role: protobuf.Character; 31 | players: Player[]; 32 | } 33 | 34 | export const PlayerGroup: FC = ({ 35 | role, 36 | players, 37 | }: PlayerGroupProps) => { 38 | const t = useTranslator(); 39 | 40 | return ( 41 | 42 | 43 | 44 | {t(characterToString(role))} 45 | 46 | 47 | 48 | {players.map((p: Player) => ( 49 | 50 | ))} 51 | 52 | 53 | 54 | ); 55 | }; 56 | 57 | function winReasonToString(r: protobuf.GameOver.Reason): STRINGS { 58 | switch (r) { 59 | case protobuf.GameOver.Reason.CITIZEN_WIN: 60 | return STRINGS.CITIZENS_WIN; 61 | case protobuf.GameOver.Reason.WEREWOLF_WIN: 62 | return STRINGS.WEREWOLVES_WIN; 63 | default: 64 | return STRINGS.GAME_OVER; 65 | } 66 | } 67 | 68 | export interface GameOverProps { 69 | winReason: protobuf.GameOver.Reason; 70 | players: Player[]; 71 | } 72 | 73 | export const GameOver: FC = ({ 74 | winReason, 75 | players, 76 | }: GameOverProps) => { 77 | const t = useTranslator(); 78 | 79 | const wolves: Player[] = []; 80 | const citizens: Player[] = []; 81 | const special: Player[] = []; 82 | 83 | const getList = (r: protobuf.Character): Player[] => { 84 | switch (r) { 85 | case C.WEREWOLF: 86 | return wolves; 87 | case C.CITIZEN: 88 | return citizens; 89 | default: 90 | return special; 91 | } 92 | }; 93 | 94 | players.forEach((p) => getList(p.role).push(p)); 95 | 96 | const werewolfGroup = ; 97 | const citizenGroup = ; 98 | const specialGroup = special.map((p: Player) => ( 99 | 100 | )); 101 | 102 | let view; 103 | if (winReason === R.WEREWOLF_WIN) { 104 | view = ( 105 | <> 106 | {werewolfGroup} 107 | {citizenGroup} 108 | {specialGroup} 109 | 110 | ); 111 | } else { 112 | view = ( 113 | <> 114 | {citizenGroup} 115 | {specialGroup} 116 | {werewolfGroup} 117 | 118 | ); 119 | } 120 | 121 | return ( 122 | 123 | {t(winReasonToString(winReason))} 124 | {view} 125 | 126 | ); 127 | }; 128 | 129 | export default GameOver; 130 | -------------------------------------------------------------------------------- /locales/en.json: -------------------------------------------------------------------------------- 1 | { 2 | "TITLE": "Murder Mystery", 3 | "JOIN_GAME": "Join Game", 4 | "JOIN": "Join", 5 | "CREATE_GAME": "Create Game", 6 | "NEED_GAME_LINK": "Ask the host to send you their game link", 7 | "ENTER_NAME": "Enter Name", 8 | "INVALID_GAME_LINK": "Invalid game link", 9 | "SERVER_DISCONNECTED": "Disconnected from server", 10 | "START_GAME": "Start Game", 11 | "WAIT_FOR_HOST": "Wait for the host to start the game", 12 | "GAME_ALREADY_STARTED": "The game has already started", 13 | "ERROR": "Error", 14 | "WAITING_FOR_PLAYERS": "Waiting for players", 15 | "SHARE_TO_INVITE": "Share your link to invite others", 16 | "COPY": "Copy", 17 | "HOST": "Host", 18 | "ERROR_OPENING_CONN": "Error opening connection to server", 19 | "SERVER_CLOSED_CONN": "Server closed connection", 20 | "INVALID_NAME": "Your name is invalid", 21 | "PLAYER_DISCONNECTED": "Someone disconnected, reconnection is not yet implemented so game over", 22 | "ERROR_PERFORMING_ACTION": "There was an error while performing that action", 23 | "NEED_MORE_PLAYERS": "You need at least 6 players to start the game", 24 | "YOU_ARE": "You are", 25 | "CITIZEN": "Citizen", 26 | "WEREWOLF": "Werewolf", 27 | "HEALER": "Healer", 28 | "PROPHET": "Prophet", 29 | "HUNTER": "Hunter", 30 | "FELLOW_WOLVES": "Your fellow wolves", 31 | "VOTES": "Votes", 32 | "HAS_NOT_VOTED": "Has not voted", 33 | "PLEASE_VOTE": "Please vote", 34 | "PICK_KILL": "Choose someone to kill", 35 | "NEED_CONSENSUS": "Everyone must agree", 36 | "IS_NIGHT": "It is night, you are sleeping", 37 | "PICK_REVEAL": "Choose someone to reveal", 38 | "REVEAL_FOUND": "Using your prophet ability, you find", 39 | "IS_GOOD": "is a good person", 40 | "IS_BAD": "is a bad person", 41 | "YOU_CANT_START": "You can't start the game yet", 42 | "VOTE_RESULTS": "Vote Results", 43 | "N_VOTES": " votes:", 44 | "PLEASE_CONFIRM": "Please confirm", 45 | "WAS_KILLED_CONFIRM_HEAL": "was killed. Do you want to save them?", 46 | "CONFIRM_POISON": "You have one poison. You can use it on someone or save it.", 47 | "YES_TO_USING": "Yes", 48 | "NO_TO_USING": "No", 49 | "SKIP_USING": "Skip", 50 | "OK": "OK", 51 | "SKIP": "Skip", 52 | "WAITING_FOR_VOTE": "Waiting for vote to finish", 53 | "PICK_WEREWOLF": "Who is the killer?", 54 | "KILLS_DURING_NIGHT": "People killed during the night:", 55 | "NONE": "None", 56 | "CITIZENS_WIN": "Citizens Win", 57 | "WEREWOLVES_WIN": "Werewolves Win", 58 | "GAME_OVER": "Game Over", 59 | "ALIVE": "Alive", 60 | "DEAD": "Dead", 61 | "IS": "is", 62 | "ARE_SPECTATOR": "You are a spectator", 63 | "USED_PROPHET": "used their prophet ability to find:", 64 | "USED_HEAL": "healed", 65 | "WAS_KILLED": "was killed", 66 | "VOTED_OUT": "was voted out", 67 | "KILLED_BY_HEALER": "was poisoned by the healer", 68 | "KILLED_BY_WOLVES": "was killed by the wolves", 69 | "WAITING_WOLVES_1": "The wolves", 70 | "WAITING_WOLVES_2": "are picking someone to kill", 71 | "WAITING_PROPHET_1": "The prophet", 72 | "WAITING_PROPHET_2": "is picking someone", 73 | "WAITING_HEAL_1": "The healer", 74 | "WAITING_HEAL_2": "is deciding whether to heal", 75 | "WAITING_POISON_1": "The healer", 76 | "WAITING_POISON_2": "is deciding if they want to use their poison", 77 | "WAITING_JURY": "All players are deciding who to vote out", 78 | "VOTE_STARTED": "Vote started", 79 | "WOLF_VOTE_OVER": "The wolves decided to kill", 80 | "VOTE_TIED_1": "The jury vote is tied with", 81 | "VOTE_TIED_2": "votes", 82 | "CHANGE_LANGUAGE": "ENG", 83 | "YOU_WERE_KILLED": "You were killed!", 84 | "PDEATH_VOTED": "You were voted out", 85 | "PDEATH_HEALER": "You were poisoned by the healer", 86 | "PDEATH_WOLVES": "You were killed by the wolves" 87 | } 88 | -------------------------------------------------------------------------------- /tools/bot.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const NodeWebSocket = require("ws"); 3 | const util = require("util"); 4 | const { 5 | ServerMessage, 6 | ClientMessage, 7 | VoteRequest, 8 | } = require("../pbjs/protobuf.js").murdermystery; 9 | 10 | const SERVER = process.env.BOT_SERVER || "ws://localhost:8080"; 11 | const MIN_PLAYERS = 6; 12 | 13 | const getName = (i) => `Bot${i + 1}`; 14 | 15 | // Handlers for each message 16 | const handlers = [ 17 | // Sets ctx.isHost 18 | (msg, ctx) => { 19 | if (msg.host && msg.host.isHost) { 20 | ctx.isHost = true; 21 | } 22 | }, 23 | // Will start game if we should 24 | (msg, ctx) => { 25 | if ( 26 | msg.players && 27 | ctx.isHost && 28 | msg.players.players && 29 | msg.players.players.length >= MIN_PLAYERS 30 | ) { 31 | ctx.send({ startGame: {} }); 32 | } 33 | }, 34 | // When a vote starts 35 | (msg, ctx) => { 36 | if (msg.voteRequest) { 37 | // Strategy: every client will randomly vote after a random amount of time. 38 | // If another vote is received, they will copy it instead of voting themself. 39 | 40 | const voteReq = msg.voteRequest; 41 | ctx.voteRequest = voteReq; 42 | const sendRandomVote = () => { 43 | // If the vote is still going on 44 | if (ctx.voteRequest === voteReq) { 45 | const choice = 46 | ctx.voteRequest.type === VoteRequest.Type.HEALERHEAL 47 | ? 1 // 1 = don't heal, 2 = yes, heal 48 | : voteReq.choice_IDs[ 49 | Math.floor(Math.random() * voteReq.choice_IDs.length) 50 | ]; 51 | console.log( 52 | ctx.name, 53 | "got", 54 | ctx.voteRequest, 55 | "heal", 56 | VoteRequest.Type.HEALERHEAL, 57 | "isHeal", 58 | ctx.voteRequest.type === VoteRequest.Type.HEALERHEAL, 59 | "choice", 60 | choice 61 | ); 62 | ctx.send({ vote: { choice } }); 63 | } 64 | }; 65 | ctx.randVoteTimer = setTimeout(sendRandomVote, Math.random() * 1000); 66 | } 67 | }, 68 | // When a vote is received 69 | (msg, ctx) => { 70 | if (msg.voteSync && msg.voteSync.votes) { 71 | const vote = msg.voteSync.votes.filter((v) => v.choice !== -1)[0]; 72 | if (vote && vote.choice) { 73 | if (ctx.randVoteTimer) clearTimeout(ctx.randVoteTimer); 74 | ctx.send({ vote: { choice: vote.choice } }); 75 | } 76 | } 77 | }, 78 | // When a vote is over 79 | (msg, ctx) => { 80 | if (msg.voteOver && ctx.randVoteTimer) { 81 | clearTimeout(ctx.randVoteTimer); 82 | } 83 | }, 84 | ]; 85 | 86 | const runBot = (gid, i) => { 87 | const name = getName(i); 88 | const ws = new NodeWebSocket(`${SERVER}/game/${gid}`); 89 | const ctx = { isHost: false, name }; 90 | 91 | const send = (pbMsg) => { 92 | console.log(`[${name}] ↑`, pbMsg); 93 | ws.send(ClientMessage.encode(pbMsg).finish()); 94 | }; 95 | ctx.send = send; 96 | 97 | ws.on("message", (buffer) => { 98 | const msg = ServerMessage.decode(buffer); 99 | // msg.players gets collapsed so log it top level 100 | console.log( 101 | `[${name}] ↓`, 102 | util.inspect(msg, { showHidden: false, depth: null }) 103 | ); 104 | 105 | handlers.forEach((callback) => callback(msg, ctx)); 106 | }); 107 | 108 | ws.on("open", () => send({ setName: { name } })); 109 | }; 110 | 111 | if (require.main === module) { 112 | const argv = process.argv.slice(1); 113 | 114 | const gameId = argv[1]; 115 | const count = Number(argv[2]); 116 | console.log(argv[2], count); 117 | 118 | if (!gameId || Number.isNaN(count) || count < 1) { 119 | process.stderr.write( 120 | `Usage: [BOT_SERVER=ws://xxx] ${argv[0]} ` 121 | ); 122 | } 123 | 124 | for (let i = 0; i < count; i += 1) { 125 | runBot(gameId, i); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /backend/protocol/protocol.go: -------------------------------------------------------------------------------- 1 | package protocol 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 8 | 9 | "google.golang.org/protobuf/proto" 10 | ) 11 | 12 | // ToServerMessage converts a message that is in the ServerMessage oneof to a ServerMessage object 13 | func ToServerMessage(m interface{ ProtoMessage() }) *pb.ServerMessage { 14 | switch r := m.(type) { 15 | case *pb.Handshake: 16 | return &pb.ServerMessage{Data: &pb.ServerMessage_Handshake{Handshake: r}} 17 | case *pb.Host: 18 | return &pb.ServerMessage{Data: &pb.ServerMessage_Host{Host: r}} 19 | case *pb.Players: 20 | return &pb.ServerMessage{Data: &pb.ServerMessage_Players{Players: r}} 21 | case *pb.Error: 22 | return &pb.ServerMessage{Data: &pb.ServerMessage_Error{Error: r}} 23 | case *pb.Alert: 24 | return &pb.ServerMessage{Data: &pb.ServerMessage_Alert{Alert: r}} 25 | case *pb.SetCharacter: 26 | return &pb.ServerMessage{Data: &pb.ServerMessage_SetCharacter{SetCharacter: r}} 27 | case *pb.FellowWolves: 28 | return &pb.ServerMessage{Data: &pb.ServerMessage_FellowWolves{FellowWolves: r}} 29 | case *pb.VoteRequest: 30 | return &pb.ServerMessage{Data: &pb.ServerMessage_VoteRequest{VoteRequest: r}} 31 | case *pb.VoteOver: 32 | return &pb.ServerMessage{Data: &pb.ServerMessage_VoteOver{VoteOver: r}} 33 | case *pb.ProphetReveal: 34 | return &pb.ServerMessage{Data: &pb.ServerMessage_ProphetReveal{ProphetReveal: r}} 35 | case *pb.SpectatorUpdate: 36 | return &pb.ServerMessage{Data: &pb.ServerMessage_SpectatorUpdate{SpectatorUpdate: r}} 37 | case *pb.KillReveal: 38 | return &pb.ServerMessage{Data: &pb.ServerMessage_KillReveal{KillReveal: r}} 39 | case *pb.Killed: 40 | return &pb.ServerMessage{Data: &pb.ServerMessage_Killed{Killed: r}} 41 | case *pb.GameOver: 42 | return &pb.ServerMessage{Data: &pb.ServerMessage_GameOver{GameOver: r}} 43 | case *pb.BulkSpectatorUpdate: 44 | return &pb.ServerMessage{Data: &pb.ServerMessage_BulkSpectatorUpdate{BulkSpectatorUpdate: r}} 45 | case *pb.PlayerStatus: 46 | return &pb.ServerMessage{Data: &pb.ServerMessage_PlayerStatus{PlayerStatus: r}} 47 | default: 48 | return nil 49 | } 50 | } 51 | 52 | // ToSpectatorUpdate converts a message that is in the SpectatorUpdate oneof to a SpectatorUpdate object 53 | func ToSpectatorUpdate(msg interface{ ProtoMessage() }) *pb.SpectatorUpdate { 54 | switch r := msg.(type) { 55 | case *pb.SpectatorAssignedCharacter: 56 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_SetChar{SetChar: r}} 57 | case *pb.SpectatorProphetReveal: 58 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_ProphetReveal{ProphetReveal: r}} 59 | case *pb.SpectatorHealerHeal: 60 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_HealerHeal{HealerHeal: r}} 61 | case *pb.SpectatorKill: 62 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_Kill{Kill: r}} 63 | case *pb.SpectatorVoteRequest: 64 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_VoteRequest{VoteRequest: r}} 65 | case *pb.SpectatorVoteOver: 66 | return &pb.SpectatorUpdate{Evt: &pb.SpectatorUpdate_VoteOver{VoteOver: r}} 67 | default: 68 | return nil 69 | } 70 | } 71 | 72 | // Marshal marshals a message and handles any errors 73 | func Marshal(message interface{ ProtoMessage() }) ([]byte, error) { 74 | var msg *pb.ServerMessage = ToServerMessage(message) 75 | log.Println("Marshaled:", msg) 76 | data := []byte{} 77 | var err error = nil 78 | if msg == nil { 79 | err = fmt.Errorf("invalid message type") 80 | } else { 81 | data, err = proto.Marshal(msg) 82 | } 83 | if err != nil { 84 | log.Printf("Protocol error: %s\n", err) 85 | return []byte{}, err 86 | } 87 | return data, nil 88 | } 89 | 90 | // Unmarshal decodes a message. If invalid result will be nil 91 | func Unmarshal(data []byte) (result *pb.ClientMessage) { 92 | defer func() { 93 | if r := recover(); r != nil { 94 | result = nil 95 | } 96 | }() 97 | 98 | m := &pb.ClientMessage{} 99 | err := proto.Unmarshal(data, m) 100 | if err != nil { 101 | log.Println(err) 102 | return nil 103 | } 104 | return m 105 | } 106 | -------------------------------------------------------------------------------- /backend/game/characters.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | cryptorand "crypto/rand" 5 | "encoding/binary" 6 | "log" 7 | "math" 8 | "math/rand" 9 | 10 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 11 | 12 | "github.com/Scoder12/murdermystery/backend/protocol" 13 | "gopkg.in/olahol/melody.v1" 14 | ) 15 | 16 | // CharacterMap is a map of character IDs to their string attribute name 17 | var CharacterMap = map[int]string{ 18 | 0: "NoCharacter", 19 | 1: "Citizen", 20 | 2: "Werewolf", 21 | 3: "Healer", 22 | 4: "Prophet", 23 | 5: "Hunter", 24 | } 25 | 26 | // CryptoRandSource is an implementation of math/rand.Source that is backed by crypto/rand. 27 | // All credit to https://stackoverflow.com/a/35208651/9196137 28 | type CryptoRandSource struct{} 29 | 30 | // NewCryptoRandSource makes a new CryptoRandSource. 31 | func NewCryptoRandSource() CryptoRandSource { 32 | return CryptoRandSource{} 33 | } 34 | 35 | // Int63 generates a uniformly-distributed random int64 value in the range [0, 1<<63). 36 | func (CryptoRandSource) Int63() int64 { 37 | var b [8]byte 38 | _, err := cryptorand.Read(b[:]) 39 | if err != nil { 40 | panic(err) 41 | } 42 | // mask off sign bit to ensure positive number 43 | return int64(binary.LittleEndian.Uint64(b[:]) & (1<<63 - 1)) 44 | } 45 | 46 | // Seed sets the seed for the souce. No-op since crypto/rand does not use this. 47 | func (CryptoRandSource) Seed(_ int64) {} 48 | 49 | func genCharacterArray(numPlayers int) []pb.Character { 50 | // There is one healer and one prophet always 51 | res := []pb.Character{pb.Character_HEALER, pb.Character_PROPHET} 52 | var numWolves int = int(math.Floor(float64(numPlayers-2) / 2.0)) 53 | var numCits int = (numPlayers - 2) - numWolves 54 | 55 | log.Println("2 special", numCits, "citizen", numWolves, "wolves") 56 | 57 | for i := 0; i < numCits; i++ { 58 | res = append(res, pb.Character_CITIZEN) 59 | } 60 | for i := 0; i < numWolves; i++ { 61 | res = append(res, pb.Character_WEREWOLF) 62 | } 63 | return res 64 | } 65 | 66 | // AssignCharacters assigns a character to each player 67 | func (g *Game) AssignCharacters() { 68 | g.lock.Lock() 69 | defer g.lock.Unlock() 70 | 71 | if !g.started { 72 | log.Println("Call Game.AssignCharacters() with started=false, returning") 73 | return 74 | } 75 | log.Println("Assigning characters") 76 | 77 | numPlayers := 0 78 | for m, c := range g.clients { 79 | if !m.IsClosed() && len(c.name) > 0 { 80 | numPlayers++ 81 | } 82 | } 83 | roles := genCharacterArray(numPlayers) 84 | // shuffle 85 | // GoSec does not know that we are using crypto/rand with this source and thinks 86 | // this is a vulnerability, so it can be safely ignored. 87 | // #nosec: G404 88 | r := rand.New(NewCryptoRandSource()) 89 | r.Shuffle(len(roles), func(i, j int) { roles[i], roles[j] = roles[j], roles[i] }) 90 | 91 | var i int = 0 92 | for m, c := range g.clients { 93 | // Would like to generate roles lazily in this loop, 94 | // but would be hard to make it both random and assign the correct amount. 95 | // No one is allowed to join/leave while the game is locked so the role array method works fine 96 | 97 | if m.IsClosed() || len(c.name) == 0 { 98 | continue 99 | } 100 | 101 | role := roles[i] 102 | c.role = role 103 | // Tell the client which role they have 104 | msg, err := protocol.Marshal(&pb.SetCharacter{Character: role}) 105 | if err != nil { 106 | return 107 | } 108 | err = m.WriteBinary(msg) 109 | printerr(err) 110 | 111 | // Tell spectators what character this player has 112 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate( 113 | &pb.SpectatorAssignedCharacter{Id: c.ID, Character: role})) 114 | 115 | i++ 116 | } 117 | } 118 | 119 | func (g *Game) revealWolves() { 120 | g.lock.Lock() 121 | defer g.lock.Unlock() 122 | 123 | wolfSessions := []*melody.Session{} 124 | wolfIDs := []int32{} 125 | 126 | for m, c := range g.clients { 127 | if c.role == pb.Character_WEREWOLF { 128 | wolfSessions = append(wolfSessions, m) 129 | wolfIDs = append(wolfIDs, c.ID) 130 | } 131 | } 132 | 133 | msg, err := protocol.Marshal(&pb.FellowWolves{Ids: wolfIDs}) 134 | if err != nil { 135 | return 136 | } 137 | 138 | for _, s := range wolfSessions { 139 | err = s.WriteBinary(msg) 140 | if err != nil { 141 | log.Println(err) 142 | } 143 | } 144 | 145 | // Allow game to be unlocked before calling 146 | go g.callWolfVote() 147 | } 148 | -------------------------------------------------------------------------------- /main.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package murdermystery; 3 | 4 | option go_package = "github.com/Scoder12/murdermystery/backend/protocol/pb"; 5 | 6 | // Server Messages 7 | message Handshake { 8 | enum Status { 9 | UNKNOWN = 0; 10 | OK = 1; 11 | } 12 | Status status = 1; 13 | int32 id = 2; 14 | } 15 | 16 | message Host { bool isHost = 1; } 17 | 18 | message Players { 19 | message Player { 20 | string name = 1; 21 | int32 id = 2; 22 | } 23 | 24 | repeated Player players = 1; 25 | int32 host_id = 2; 26 | } 27 | 28 | message Error { 29 | enum E_type { 30 | UNKNOWN = 0; 31 | DISCONNECT = 1; 32 | BADNAME = 2; 33 | } 34 | E_type msg = 1; 35 | } 36 | 37 | message Alert { 38 | enum Msg { 39 | UNKNOWN = 0; 40 | NEEDMOREPLAYERS = 1; 41 | } 42 | Msg msg = 1; 43 | } 44 | 45 | enum Character { 46 | NONE = 0; 47 | CITIZEN = 1; 48 | WEREWOLF = 2; 49 | HEALER = 3; 50 | PROPHET = 4; 51 | HUNTER = 5; 52 | } 53 | 54 | message SetCharacter { Character character = 1; } 55 | 56 | message FellowWolves { repeated int32 ids = 1; } 57 | 58 | message VoteRequest { 59 | enum Type { 60 | UNKNOWN = 0; 61 | KILL = 1; 62 | PROPHET = 2; 63 | HEALERHEAL = 3; 64 | HEALERPOISON = 4; 65 | JURY = 5; 66 | } 67 | repeated int32 choice_IDs = 1; 68 | Type type = 2; 69 | } 70 | 71 | enum VoteResultType { 72 | NOWINNER = 0; 73 | WIN = 1; 74 | TIE = 2; 75 | } 76 | 77 | message JuryVoteResult { 78 | VoteResultType status = 1; 79 | int32 winner = 2; 80 | } 81 | 82 | message VoteResult { 83 | message CandidateResult { 84 | int32 id = 1; 85 | repeated int32 voters = 2; 86 | } 87 | repeated CandidateResult candidates = 1; 88 | JuryVoteResult jury = 2; 89 | } 90 | 91 | message VoteOver { VoteResult result = 1; } 92 | 93 | message ProphetReveal { 94 | int32 id = 1; 95 | bool good = 2; 96 | } 97 | 98 | message KillReveal { repeated int32 killed_IDs = 1; } 99 | 100 | enum KillReason { 101 | UNKNOWN = 0; 102 | WOLVES = 1; 103 | VOTED = 2; 104 | HEALERPOISON = 3; 105 | } 106 | 107 | message Killed { KillReason reason = 1; } 108 | 109 | message GameOver { 110 | enum Reason { 111 | NONE = 0; 112 | WEREWOLF_WIN = 1; 113 | CITIZEN_WIN = 2; 114 | } 115 | message Player { 116 | int32 id = 1; 117 | Character character = 2; 118 | } 119 | 120 | Reason reason = 1; 121 | repeated Player players = 2; 122 | } 123 | 124 | message SpectatorAssignedCharacter { 125 | int32 id = 1; 126 | Character character = 2; 127 | } 128 | 129 | message SpectatorProphetReveal { 130 | int32 prophet_id = 1; 131 | int32 choice_id = 2; 132 | bool good = 3; 133 | } 134 | 135 | message SpectatorHealerHeal { 136 | int32 healer = 1; 137 | repeated int32 healed = 2; 138 | } 139 | 140 | // Don't need SpectatorHealerPoison because that is a KillReason handled by 141 | // SpecatatorKill. 142 | 143 | message SpectatorKill { 144 | KillReason reason = 1; 145 | int32 killed = 2; 146 | } 147 | 148 | message SpectatorVoteRequest { 149 | repeated int32 voters = 1; 150 | VoteRequest vote_request = 2; 151 | } 152 | 153 | message SpectatorVoteOver { VoteOver vote_over = 3; } 154 | 155 | // No SpectatorGameOver, GameOver is broadcast to everyone 156 | 157 | message SpectatorUpdate { 158 | oneof evt { 159 | SpectatorAssignedCharacter set_char = 1; 160 | SpectatorProphetReveal prophet_reveal = 2; 161 | SpectatorHealerHeal healer_heal = 3; 162 | SpectatorKill kill = 4; 163 | SpectatorVoteRequest vote_request = 5; 164 | SpectatorVoteOver vote_over = 6; 165 | } 166 | } 167 | 168 | message BulkSpectatorUpdate { repeated SpectatorUpdate update = 1; } 169 | 170 | message PlayerStatus { repeated int32 alive = 1; } 171 | 172 | // The Server message 173 | message ServerMessage { 174 | oneof data { 175 | Handshake handshake = 1; 176 | Host host = 2; 177 | Players players = 3; 178 | Error error = 4; 179 | Alert alert = 5; 180 | SetCharacter set_character = 6; 181 | FellowWolves fellow_wolves = 7; 182 | VoteRequest vote_request = 8; 183 | VoteOver vote_over = 9; 184 | ProphetReveal prophet_reveal = 11; 185 | SpectatorUpdate spectator_update = 12; 186 | KillReveal kill_reveal = 13; 187 | Killed killed = 14; 188 | GameOver game_over = 15; 189 | BulkSpectatorUpdate bulk_spectator_update = 16; 190 | PlayerStatus player_status = 17; 191 | } 192 | } 193 | 194 | // Client Messages 195 | message SetName { string name = 1; } 196 | 197 | message StartGame {} 198 | 199 | message ClientVote { int32 choice = 1; } 200 | 201 | // The client message 202 | message ClientMessage { 203 | oneof data { 204 | SetName set_name = 1; 205 | StartGame start_game = 2; 206 | ClientVote vote = 3; 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /tools/goci.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Taken from https://github.com/grandcolline/golang-github-actions/blob/master/entrypoint.sh 4 | # and modified. No offsense to the original author but the code is kind of bad, and it 5 | # should not be used as an example of Scoder12's work. 6 | 7 | set -e 8 | 9 | # Prerequisites 10 | echo "Installing tools..." 11 | go get -u \ 12 | github.com/kisielk/errcheck \ 13 | golang.org/x/tools/cmd/goimports \ 14 | golang.org/x/lint/golint \ 15 | github.com/securego/gosec/cmd/gosec \ 16 | golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow \ 17 | honnef.co/go/tools/cmd/staticcheck 18 | echo "Tools installed." 19 | 20 | # ------------------------ 21 | # Environments 22 | # ------------------------ 23 | WORKING_DIR=$1 24 | SEND_COMMNET=true 25 | FLAGS=$2 26 | IGNORE_DEFER_ERR=$3 27 | 28 | COMMENT="" 29 | SUCCESS=0 30 | 31 | 32 | # ------------------------ 33 | # Functions 34 | # ------------------------ 35 | 36 | # mod_download is getting go modules using go.mod. 37 | mod_download() { 38 | if [ ! -e go.mod ]; then go mod init; fi 39 | go mod download 40 | if [ $? -ne 0 ]; then exit 1; fi 41 | } 42 | 43 | # check_errcheck is excute "errcheck" and generate ${COMMENT} and ${SUCCESS} 44 | check_errcheck() { 45 | if [ "${IGNORE_DEFER_ERR}" = "true" ]; then 46 | IGNORE_COMMAND="| grep -v defer" 47 | fi 48 | 49 | set +e 50 | OUTPUT=$(sh -c "errcheck ${FLAGS} ./... ${IGNORE_COMMAND} $*" 2>&1) 51 | test -z "${OUTPUT}" 52 | SUCCESS=$? 53 | 54 | set -e 55 | if [ ${SUCCESS} -eq 0 ]; then 56 | return 57 | fi 58 | 59 | if [ "${SEND_COMMNET}" = "true" ]; then 60 | COMMENT="## ⚠ errcheck Failed 61 | \`\`\` 62 | ${OUTPUT} 63 | \`\`\` 64 | " 65 | fi 66 | } 67 | 68 | clean_pkgs () { 69 | <&0 grep -vF 'protocol/pb/main.pb.go' | 70 | grep -vF 'statik/statik.go' || true 71 | } 72 | 73 | # check_fmt is excute "go fmt" and generate ${COMMENT} and ${SUCCESS} 74 | check_fmt() { 75 | echo "Running gofmt" 76 | set +e 77 | # Ignore generated protocol buffers code 78 | UNFMT_FILES=$(sh -c "gofmt -l -s ." 2>&1 | clean_pkgs) 79 | test -z "${UNFMT_FILES}" 80 | SUCCESS=$? 81 | 82 | set -e 83 | if [ ${SUCCESS} -eq 0 ]; then 84 | return 85 | fi 86 | 87 | if [ "${SEND_COMMNET}" = "true" ]; then 88 | FMT_OUTPUT="" 89 | for file in ${UNFMT_FILES}; do 90 | FILE_DIFF=$(gofmt -d -e "${file}" | sed -n '/@@.*/,//{/@@.*/d;p}') 91 | FMT_OUTPUT="${FMT_OUTPUT} 92 |
    ${file} 93 | 94 | \`\`\`diff 95 | ${FILE_DIFF} 96 | \`\`\` 97 |
    98 | " 99 | done 100 | COMMENT="## ⚠ gofmt Failed 101 | ${FMT_OUTPUT} 102 | " 103 | fi 104 | } 105 | 106 | # check_imports is excute go imports and generate ${COMMENT} and ${SUCCESS} 107 | check_imports() { 108 | set +e 109 | UNFMT_FILES=$(sh -c "goimports -l . $*" 2>&1 | clean_pkgs) 110 | test -z "${UNFMT_FILES}" 111 | SUCCESS=$? 112 | 113 | set -e 114 | if [ ${SUCCESS} -eq 0 ]; then 115 | return 116 | fi 117 | 118 | if [ "${SEND_COMMNET}" = "true" ]; then 119 | FMT_OUTPUT="" 120 | for file in ${UNFMT_FILES}; do 121 | FILE_DIFF=$(goimports -d -e "${file}" | sed -n '/@@.*/,//{/@@.*/d;p}') 122 | FMT_OUTPUT="${FMT_OUTPUT} 123 |
    ${file} 124 | 125 | \`\`\`diff 126 | ${FILE_DIFF} 127 | \`\`\` 128 |
    129 | " 130 | done 131 | COMMENT="## ⚠ goimports Failed 132 | ${FMT_OUTPUT} 133 | " 134 | fi 135 | 136 | } 137 | 138 | # check_lint is excute golint and generate ${COMMENT} and ${SUCCESS} 139 | check_lint() { 140 | set +e 141 | OUTPUT=$(sh -c "golint -set_exit_status ./... $*" 2>&1) 142 | SUCCESS=$? 143 | 144 | set -e 145 | if [ ${SUCCESS} -eq 0 ]; then 146 | return 147 | fi 148 | 149 | if [ "${SEND_COMMNET}" = "true" ]; then 150 | COMMENT="## ⚠ golint Failed 151 | $(echo "${OUTPUT}" | awk 'END{print}') 152 |
    Show Detail 153 | 154 | \`\`\` 155 | $(echo "${OUTPUT}" | sed -e '$d') 156 | \`\`\` 157 | 158 |
    159 | " 160 | fi 161 | } 162 | 163 | # check_sec is excute gosec and generate ${COMMENT} and ${SUCCESS} 164 | check_sec() { 165 | set +e 166 | gosec -out /tmp/gosecresult.txt ${FLAGS} ./... 167 | SUCCESS=$? 168 | 169 | set -e 170 | if [ ${SUCCESS} -eq 0 ]; then 171 | return 172 | fi 173 | 174 | if [ "${SEND_COMMNET}" = "true" ]; then 175 | COMMENT="## ⚠ gosec Failed 176 | \`\`\` 177 | $(tail -n 6 /tmp/gosecresult.txt) 178 | \`\`\` 179 | 180 |
    Show Detail 181 | 182 | \`\`\` 183 | $(cat /tmp/gosecresult.txt) 184 | \`\`\` 185 | [Code Reference](https://github.com/securego/gosec#available-rules) 186 |
    187 | " 188 | fi 189 | rm /tmp/gosecresult.txt 190 | } 191 | 192 | # check_shadow is excute "go vet -vettool=/go/bin/shadow" and generate ${COMMENT} and ${SUCCESS} 193 | check_shadow() { 194 | set +e 195 | OUTPUT=$(sh -c "go vet -vettool=$(which shadow) ${FLAGS} ./... $*" 2>&1) 196 | SUCCESS=$? 197 | 198 | set -e 199 | if [ ${SUCCESS} -eq 0 ]; then 200 | return 201 | fi 202 | 203 | if [ "${SEND_COMMNET}" = "true" ]; then 204 | COMMENT="## ⚠ shadow Failed 205 | \`\`\` 206 | ${OUTPUT} 207 | \`\`\` 208 | " 209 | fi 210 | } 211 | 212 | # check_staticcheck is excute "staticcheck" and generate ${COMMENT} and ${SUCCESS} 213 | check_staticcheck() { 214 | set +e 215 | OUTPUT=$(sh -c "staticcheck ${FLAGS} ./... $*" 2>&1) 216 | SUCCESS=$? 217 | 218 | set -e 219 | if [ ${SUCCESS} -eq 0 ]; then 220 | return 221 | fi 222 | 223 | if [ "${SEND_COMMNET}" = "true" ]; then 224 | COMMENT="## ⚠ staticcheck Failed 225 | \`\`\` 226 | ${OUTPUT} 227 | \`\`\` 228 | [Checks Document](https://staticcheck.io/docs/checks) 229 | " 230 | fi 231 | } 232 | 233 | # check_vet is excute "go vet" and generate ${COMMENT} and ${SUCCESS} 234 | check_vet() { 235 | set +e 236 | OUTPUT=$(sh -c "go vet ${FLAGS} ./... $*" 2>&1) 237 | SUCCESS=$? 238 | 239 | set -e 240 | if [ ${SUCCESS} -eq 0 ]; then 241 | return 242 | fi 243 | 244 | if [ "${SEND_COMMNET}" = "true" ]; then 245 | COMMENT="## ⚠ vet Failed 246 | \`\`\` 247 | ${OUTPUT} 248 | \`\`\` 249 | " 250 | fi 251 | } 252 | 253 | 254 | # ------------------------ 255 | # Main Flow 256 | # ------------------------ 257 | cd ${GITHUB_WORKSPACE:-.}/${WORKING_DIR:-.} 258 | 259 | for i in {errcheck,fmt,imports,lint,sec,shadow,staticcheck,vet}; do 260 | case $i in 261 | "errcheck" ) 262 | mod_download 263 | check_errcheck 264 | ;; 265 | "fmt" ) 266 | check_fmt 267 | ;; 268 | "imports" ) 269 | check_imports 270 | ;; 271 | "lint" ) 272 | check_lint 273 | ;; 274 | "sec" ) 275 | mod_download 276 | check_sec 277 | ;; 278 | "shadow" ) 279 | mod_download 280 | check_shadow 281 | ;; 282 | "staticcheck" ) 283 | mod_download 284 | check_staticcheck 285 | ;; 286 | "vet" ) 287 | mod_download 288 | check_vet 289 | ;; 290 | * ) 291 | echo "Invalid command" 292 | exit 1 293 | 294 | esac 295 | 296 | if [ ${SUCCESS} -ne 0 ]; then 297 | echo "Check Failed!!" 298 | echo ${COMMENT} 299 | echo 300 | body=${COMMENT} 301 | body="${body//'%'/'%25'}" 302 | body="${body//$'\n'/'%0A'}" 303 | body="${body//$'\r'/'%0D'}" 304 | echo ::set-output name=body::$body 305 | exit ${SUCCESS} 306 | fi 307 | done 308 | -------------------------------------------------------------------------------- /components/CharacterSpinner.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Flex, Heading, Image, Text, useTimeout } from "@chakra-ui/react"; 2 | import { motion } from "framer-motion"; 3 | import { 4 | characterToImg, 5 | characterToString, 6 | CHARACTER_INDEXES, 7 | NAME_STRINGS, 8 | } from "lib/CharacterImg"; 9 | import { S, Translator, useTranslator } from "lib/translate"; 10 | import { murdermystery as protobuf } from "pbjs/protobuf"; 11 | import { 12 | Children, 13 | FC, 14 | MutableRefObject, 15 | ReactNode, 16 | useEffect, 17 | useRef, 18 | useState, 19 | } from "react"; 20 | import SkippableDelay, { RightFloatSkippableDelay } from "./SkippableDelay"; 21 | 22 | const MotionBox = motion(Box); 23 | 24 | const WIDTH = 226; 25 | const HEIGHT = 330; 26 | const REPS = 10; 27 | 28 | const getImgs = (t: Translator) => { 29 | // A single round of images 30 | let names: protobuf.Character[] = []; 31 | for (let round = 0; round < REPS; round += 1) { 32 | names = names.concat(CHARACTER_INDEXES); 33 | } 34 | 35 | return Children.map(names, (name, i: number) => ( 36 | 37 | {t(NAME_STRINGS[i])} 43 | 44 | )); 45 | }; 46 | 47 | /** 48 | * @description This method finds the amount of pixels to go left from the left side of 49 | * the very first card image. It is designed to land very close to the edge of the 50 | * card so it is more exciting, but will always land on specified card. 51 | * @param card_ind The index of the card in filenames which should be landed on 52 | */ 53 | const getTransformAmt = (cardInd: number) => { 54 | // always go at least across all cards 6 times, and account for spinner position 55 | const minDist = WIDTH * CHARACTER_INDEXES.length * 7; 56 | 57 | // A random px amount between 5 and 9 percent of card_width 58 | const rand = Math.random() * (WIDTH * 0.09) + WIDTH * 0.05; 59 | // Either at the beginning or end of the card 60 | const posInCard = Math.random() > 0.5 ? rand : WIDTH - rand; 61 | // The final amount to move 62 | return minDist + cardInd * WIDTH + posInCard; 63 | }; 64 | 65 | const getSpinnerPos = ( 66 | lineContainerRef: MutableRefObject 67 | ) => 68 | lineContainerRef.current 69 | ? lineContainerRef.current.getBoundingClientRect().width / 2 70 | : 0; 71 | 72 | export interface CharacterResultProps { 73 | name: protobuf.Character; 74 | } 75 | 76 | export const CharacterResult: FC = ({ 77 | name, 78 | }: CharacterResultProps) => { 79 | const t = useTranslator(); 80 | 81 | return ( 82 | <> 83 | 84 | {t(S.YOU_ARE)} 85 | 86 | 87 | {t(characterToString(name))} 88 | 89 | 90 | 91 | 92 | 93 | ); 94 | }; 95 | 96 | export interface CharacterDisplayProps { 97 | character: protobuf.Character; 98 | } 99 | 100 | export const CharacterDisplay: FC = ({ 101 | character, 102 | }: CharacterDisplayProps) => { 103 | const t = useTranslator(); 104 | 105 | // get a list of JSX that is all images repeated REPS times 106 | const allImgs: ReactNode[] = getImgs(t); 107 | 108 | const cardInd = CHARACTER_INDEXES.indexOf(character); 109 | if (cardInd === -1) throw new Error(`Invalid character: ${character}`); 110 | 111 | const [isSpinning, setIsSpinning] = useState(false); 112 | const [shouldAnimate, setShouldAnimate] = useState(true); 113 | 114 | // We will only be setting this once, but it depends on Math.random(), and it can't 115 | // be different every render, so store it in state 116 | const transformAmtRef = useRef(-1); 117 | if (transformAmtRef.current === -1) { 118 | transformAmtRef.current = getTransformAmt(cardInd); 119 | } 120 | const transformAmt = transformAmtRef.current; 121 | 122 | // The Box with the images. We need it to get the size of it 123 | const lineContainerRef = useRef(null); 124 | 125 | // Start spinning after 2s 126 | useTimeout(() => setIsSpinning(true), 2000); 127 | 128 | const onResize = () => { 129 | // if the window resizes, the line which represents which card they'll get will 130 | // jump. This will result in the wrong card being displayed in the UI. But we 131 | // can't continue the animation where we left off with a different target, 132 | // so just transform directly to the correct amount without any animation. 133 | // The UI will be "frozen" until the animation would have finished, but this is 134 | // still a better experience than seeing the wrong card "selected". 135 | // Once we setShouldAnimate(false), the component re-renders, the value of 136 | // getSpinnerPos() will change due to the new window size, and the transform 137 | // will be updated accordingly. 138 | setShouldAnimate(false); 139 | }; 140 | 141 | // Add the resize event listener once and properly clean it up afterwards 142 | useEffect(() => { 143 | window.addEventListener("resize", onResize); 144 | return () => window.removeEventListener("resize", onResize); 145 | }, []); 146 | 147 | // Guard for null tranformAmt to make typescript happy, in reality it is always set 148 | const finalTransform = (transformAmt || 0) - getSpinnerPos(lineContainerRef); 149 | 150 | return ( 151 | <> 152 | 155 | 163 | 164 | {allImgs} 165 | 166 | 167 | 168 | {/* Line */} 169 | 170 | 178 | 179 | 180 | ); 181 | }; 182 | 183 | export interface CharacterSpinnerProps { 184 | character: protobuf.Character; 185 | onDone: () => void; 186 | } 187 | 188 | export const CharacterSpinner: FC = ({ 189 | character, 190 | onDone, 191 | }: CharacterSpinnerProps) => { 192 | const cardInd = CHARACTER_INDEXES.indexOf(character); 193 | if (cardInd === -1) throw new Error(`Invalid character: ${character}`); 194 | const [spinDone, setSpinDone] = useState(false); 195 | 196 | if (!spinDone) { 197 | return ( 198 | <> 199 | 200 | setSpinDone(true)} 203 | /> 204 | 205 | ); 206 | } 207 | 208 | return ( 209 | <> 210 | 211 | 212 | 213 | 214 | 215 | ); 216 | }; 217 | 218 | export default CharacterSpinner; 219 | -------------------------------------------------------------------------------- /components/UpdatesLog.tsx: -------------------------------------------------------------------------------- 1 | import { Box, Flex, Heading, Image, Text } from "@chakra-ui/react"; 2 | import { characterToImg } from "lib/CharacterImg"; 3 | import { STRINGS, useTranslator } from "lib/translate"; 4 | import { FC, PropsWithChildren } from "react"; 5 | import { murdermystery as protobuf } from "../pbjs/protobuf.js"; 6 | import NameBadge from "./NameBadge"; 7 | 8 | export function getKillText(reason: protobuf.KillReason): STRINGS { 9 | switch (reason) { 10 | case protobuf.KillReason.VOTED: 11 | return STRINGS.VOTED_OUT; 12 | case protobuf.KillReason.HEALERPOISON: 13 | return STRINGS.KILLED_BY_HEALER; 14 | case protobuf.KillReason.WOLVES: 15 | return STRINGS.KILLED_BY_WOLVES; 16 | default: 17 | return STRINGS.WAS_KILLED; 18 | } 19 | } 20 | 21 | const VT = protobuf.VoteRequest.Type; 22 | 23 | export function getVoteRequestText( 24 | t: protobuf.VoteRequest.Type 25 | ): [STRINGS | null, STRINGS | null, boolean, boolean] { 26 | switch (t) { 27 | case VT.KILL: 28 | return [STRINGS.WAITING_WOLVES_1, STRINGS.WAITING_WOLVES_2, false, true]; 29 | case VT.PROPHET: 30 | return [ 31 | STRINGS.WAITING_PROPHET_1, 32 | STRINGS.WAITING_PROPHET_2, 33 | false, 34 | true, 35 | ]; 36 | case VT.HEALERHEAL: 37 | return [STRINGS.WAITING_HEAL_1, STRINGS.WAITING_HEAL_2, true, true]; 38 | case VT.HEALERPOISON: 39 | return [STRINGS.WAITING_POISON_1, STRINGS.WAITING_POISON_2, false, true]; 40 | case VT.JURY: 41 | return [STRINGS.WAITING_JURY, null, false, false]; 42 | default: 43 | return [STRINGS.VOTE_STARTED, null, false, false]; 44 | } 45 | } 46 | 47 | const JR = protobuf.VoteResultType; 48 | 49 | interface UpdateProps {} 50 | 51 | const Update: FC = ({ 52 | children, 53 | }: PropsWithChildren) => { 54 | return {children}; 55 | }; 56 | 57 | export interface UpdateTextProps { 58 | update: protobuf.ISpectatorUpdate; 59 | IDToName: (id?: number | null) => string; 60 | lastVoteType: protobuf.VoteRequest.Type; 61 | } 62 | 63 | export const UpdateText: FC = ({ 64 | update, 65 | IDToName, 66 | lastVoteType, 67 | }: UpdateTextProps) => { 68 | const t = useTranslator(); 69 | 70 | if (update.setChar) 71 | return ( 72 | 73 | 74 | {t(STRINGS.IS)} 75 | 82 | 83 | ); 84 | if (update.prophetReveal) 85 | return ( 86 | 87 | 88 | {t(STRINGS.USED_PROPHET)} 89 | 90 | 91 | {t(update.prophetReveal.good ? STRINGS.IS_GOOD : STRINGS.IS_BAD)} 92 | 93 | 94 | ); 95 | if (update.healerHeal) 96 | return ( 97 | 98 | 99 | {t(STRINGS.USED_HEAL)} 100 | {(update.healerHeal.healed || []).map((id) => ( 101 | 102 | ))} 103 | 104 | ); 105 | if (update.kill) 106 | return ( 107 | 108 | 109 | 110 | {t(getKillText(update.kill.reason || protobuf.KillReason.UNKNOWN))} 111 | 112 | 113 | ); 114 | if (update.voteRequest) { 115 | const [before, after, showCandidates, showVoters] = getVoteRequestText( 116 | update.voteRequest.voteRequest?.type || VT.UNKNOWN 117 | ); 118 | 119 | return ( 120 | 121 | {before ? {t(before)} : null} 122 | {showVoters 123 | ? (update.voteRequest.voters || []).map((id) => ( 124 | 125 | )) 126 | : null} 127 | {after ? {t(after)} : null} 128 | {showCandidates 129 | ? (update.voteRequest.voteRequest?.choice_IDs || []).map((id) => ( 130 | 131 | )) 132 | : null} 133 | 134 | ); 135 | } 136 | if (update.voteOver) { 137 | if (lastVoteType === VT.KILL) { 138 | const result = (update.voteOver.voteOver?.result?.candidates || [])[0]; 139 | 140 | return ( 141 | 142 | {t(STRINGS.WOLF_VOTE_OVER)} 143 | {result && result.id ? ( 144 | 145 | ) : ( 146 | 147 | )} 148 | 149 | ); 150 | } 151 | if (lastVoteType === VT.JURY) { 152 | const jury = update.voteOver.voteOver?.result?.jury; 153 | 154 | const winner = ( 155 | update.voteOver.voteOver?.result?.candidates || [] 156 | ).reduce<{ 157 | id: number; 158 | voters: number[]; 159 | }>( 160 | (prev, u) => { 161 | if (u && u.id && u.voters) { 162 | return prev.voters.length > u.voters.length 163 | ? prev 164 | : { id: u.id || -1, voters: u.voters }; 165 | } 166 | return prev; 167 | }, 168 | { id: -1, voters: [] } 169 | ); 170 | const totalVotes = ( 171 | update.voteOver.voteOver?.result?.candidates || [] 172 | ).reduce((prev, i) => prev + (i.voters?.length || 0), 0); 173 | 174 | if (jury?.status === JR.WIN) { 175 | return ( 176 | 177 | 178 | {t(STRINGS.VOTED_OUT)} 179 | ( 180 | {winner.voters.length} 181 | / 182 | {totalVotes} 183 | {t(STRINGS.VOTES)} 184 | ) 185 | 186 | ); 187 | } 188 | if (jury?.status === JR.TIE) { 189 | return ( 190 | 191 | {t(STRINGS.VOTE_TIED_1)} 192 | 193 | {winner.voters.length} 194 | 195 | {t(STRINGS.VOTE_TIED_2)} 196 | 197 | ); 198 | } 199 | } 200 | return null; 201 | } 202 | 203 | return null; 204 | }; 205 | 206 | export interface UpdatesLogProps { 207 | updates: protobuf.ISpectatorUpdate[]; 208 | IDToName: (id?: number | null) => string; 209 | } 210 | 211 | export const UpdatesLog: FC = ({ 212 | updates, 213 | IDToName, 214 | }: UpdatesLogProps) => { 215 | const t = useTranslator(); 216 | 217 | const eles = []; 218 | let lastVoteType = VT.UNKNOWN; 219 | for (let i = 0; i < updates.length; i += 1) { 220 | const u = updates[i]; 221 | if (u.voteRequest) { 222 | const ut = u.voteRequest.voteRequest?.type || VT.UNKNOWN; 223 | if (ut !== VT.UNKNOWN) { 224 | lastVoteType = ut; 225 | } 226 | } 227 | eles.push( 228 | 234 | ); 235 | } 236 | 237 | return ( 238 | 239 | {t(STRINGS.ARE_SPECTATOR)} 240 | {eles.slice().reverse()} 241 | 242 | ); 243 | }; 244 | 245 | export default UpdatesLog; 246 | -------------------------------------------------------------------------------- /backend/game/vote.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "log" 5 | "sort" 6 | 7 | "github.com/Scoder12/murdermystery/backend/protocol" 8 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 9 | "gopkg.in/olahol/melody.v1" 10 | ) 11 | 12 | // Vote represents a vote taking place in the game 13 | type Vote struct { 14 | // The type of vote 15 | vType pb.VoteRequest_Type 16 | // The clients who are allowed to vote 17 | voters map[*melody.Session]bool 18 | // The candidates who they can choose from 19 | candidates map[*melody.Session]bool 20 | // The votes received from voters. map[voter]candidate 21 | votes map[*melody.Session]*melody.Session 22 | // The function to call whenever a vote is cast 23 | onChange func(*Vote, *melody.Session, *melody.Session) 24 | // Whether to disclose the results once the vote is over 25 | showResults bool 26 | } 27 | 28 | // _encodeResults encodes the vote into a VoteResult message. 29 | // Assumes game mutex is locked and is intended to be called by Vote.End() 30 | func (v *Vote) _encodeResults(g *Game, jury *pb.JuryVoteResult) *pb.VoteResult { 31 | candidates := make(map[int32][]int32) 32 | for voter, cand := range v.votes { 33 | // Need clients for IDs 34 | voterClient, voterExists := g.clients[voter] 35 | candClient, candExists := g.clients[cand] 36 | // If both are valid 37 | if voterExists && candExists { 38 | // Add voter's ID to candiate's votes 39 | votes, ok := candidates[candClient.ID] 40 | if !ok { 41 | votes = []int32{} 42 | } 43 | votes = append(votes, voterClient.ID) 44 | candidates[candClient.ID] = votes 45 | } 46 | } 47 | 48 | voteResult := []*pb.VoteResult_CandidateResult{} 49 | for candID, voteIDs := range candidates { 50 | voteResult = append(voteResult, &pb.VoteResult_CandidateResult{Id: candID, Voters: voteIDs}) 51 | } 52 | return &pb.VoteResult{Candidates: voteResult, Jury: jury} 53 | } 54 | 55 | // End ends the vote. Assumed game mutex is locked. 56 | func (v *Vote) End(g *Game, jury *pb.JuryVoteResult) { 57 | g.vote = nil 58 | 59 | var result *pb.VoteResult 60 | if v.showResults { 61 | result = v._encodeResults(g, jury) 62 | } else { 63 | result = nil 64 | } 65 | 66 | // Send VoteOver to the voters 67 | voteOver := &pb.VoteOver{Result: result} 68 | msg, err := protocol.Marshal(voteOver) 69 | if err != nil { 70 | return 71 | } 72 | 73 | for s := range v.voters { 74 | if s != nil { 75 | err = s.WriteBinary(msg) 76 | printerr(err) 77 | } 78 | } 79 | 80 | // Dispatch it to spectators 81 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate(&pb.SpectatorVoteOver{ 82 | VoteOver: voteOver, 83 | })) 84 | } 85 | 86 | // HasConcensus return whether all votes are the same 87 | func (v *Vote) HasConcensus() bool { 88 | var choice *melody.Session = nil 89 | var choiceSet bool = false 90 | 91 | for voter := range v.voters { 92 | s := v.votes[voter] 93 | 94 | if !choiceSet { 95 | choice = s 96 | choiceSet = true 97 | continue 98 | } 99 | 100 | if s != choice { 101 | return false 102 | } 103 | } 104 | 105 | // Don't return true if nobody has chosen 106 | return choice != nil 107 | } 108 | 109 | // AllVotesIn returns whether all voters have voted 110 | func (v *Vote) AllVotesIn() bool { 111 | for voter := range v.voters { 112 | _, hasVoted := v.votes[voter] 113 | if !hasVoted { 114 | return false 115 | } 116 | } 117 | return true 118 | } 119 | 120 | // Tally returns a map of candidates to the number of votes they got 121 | func (v *Vote) Tally() map[*melody.Session]int { 122 | scores := make(map[*melody.Session]int) 123 | 124 | for _, choice := range v.votes { 125 | s, ok := scores[choice] 126 | if !ok { 127 | s = 0 128 | } 129 | s++ 130 | scores[choice] = s 131 | } 132 | return scores 133 | } 134 | 135 | // GetResult gets the result of the vote and the winner if any 136 | func (v *Vote) GetResult() (pb.VoteResultType, *melody.Session) { 137 | // Setup data 138 | tally := v.Tally() 139 | scores := make([]int, len(tally)) 140 | i := 0 141 | for _, s := range tally { 142 | scores[i] = s 143 | i++ 144 | } 145 | sort.Ints(scores) 146 | scoreLen := len(scores) 147 | 148 | winningScore := scores[scoreLen-1] 149 | if scoreLen >= 2 { 150 | if winningScore == scores[scoreLen-2] { 151 | // Tie 152 | return pb.VoteResultType_TIE, nil 153 | } 154 | } 155 | 156 | // Find winner 157 | var winner *melody.Session 158 | for choice, score := range tally { 159 | if score == winningScore { 160 | winner = choice 161 | break 162 | } 163 | } 164 | 165 | return pb.VoteResultType_WIN, winner 166 | } 167 | 168 | // IsVoter checks whether a session is in v.voters 169 | func (v *Vote) IsVoter(s *melody.Session) bool { 170 | for i := range v.voters { 171 | if i == s { 172 | return true 173 | } 174 | } 175 | return false 176 | } 177 | 178 | func (g *Game) callVote( 179 | voters, candidates []*melody.Session, 180 | vType pb.VoteRequest_Type, 181 | onChange func(*Vote, *melody.Session, *melody.Session), 182 | showResults bool) { 183 | g.lock.Lock() 184 | defer g.lock.Unlock() 185 | 186 | if g.vote != nil { 187 | g.vote.End(g, nil) 188 | } 189 | 190 | votersMap := make(map[*melody.Session]bool) 191 | for _, s := range voters { 192 | votersMap[s] = true 193 | } 194 | candidatesMap := make(map[*melody.Session]bool) 195 | for _, c := range candidates { 196 | candidatesMap[c] = true 197 | } 198 | 199 | // We don't need to reference the vote type anywhere so not storing it, but can later 200 | // if needed 201 | g.vote = &Vote{ 202 | vType: vType, 203 | voters: votersMap, 204 | candidates: candidatesMap, 205 | votes: make(map[*melody.Session]*melody.Session), 206 | onChange: onChange, 207 | showResults: showResults, 208 | } 209 | 210 | // Get IDS 211 | voterIDs := make([]int32, len(voters)) 212 | i := 0 213 | for _, s := range voters { 214 | c := g.clients[s] 215 | if c != nil { 216 | voterIDs[i] = c.ID 217 | i++ 218 | } 219 | } 220 | 221 | candidateIDs := make([]int32, len(candidates)) 222 | i = 0 223 | for _, s := range candidates { 224 | c := g.clients[s] 225 | if c != nil { 226 | candidateIDs[i] = c.ID 227 | i++ 228 | } 229 | } 230 | 231 | // Send the vote request to the voters 232 | req := &pb.VoteRequest{Choice_IDs: candidateIDs, Type: vType} 233 | msg, err := protocol.Marshal(req) 234 | if err != nil { 235 | return 236 | } 237 | 238 | for _, s := range voters { 239 | err = s.WriteBinary(msg) 240 | printerr(err) 241 | } 242 | 243 | // Dispatch this as a spectator event 244 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate(&pb.SpectatorVoteRequest{ 245 | Voters: voterIDs, 246 | VoteRequest: req, 247 | })) 248 | } 249 | 250 | func (g *Game) handleVoteMessage(s *melody.Session, c *Client, msg *pb.ClientVote) { 251 | g.lock.Lock() 252 | defer g.lock.Unlock() 253 | 254 | if msg.Choice == 0 || g.vote == nil || !g.vote.IsVoter(s) { 255 | return 256 | } 257 | 258 | v := g.vote 259 | // Healer Heal is a special case: they are picking yes/no, not a specific person, so 260 | // change their choice into a yes/no bool instead of a session and use a different 261 | // handler (because the normal handler's signature wouldn't work) 262 | if v.vType == pb.VoteRequest_HEALERHEAL { 263 | g.healerHealHandler(c, msg.Choice == 2) 264 | } else { 265 | var choiceSession *melody.Session 266 | if v.vType == pb.VoteRequest_HEALERPOISON && msg.Choice == -1 { 267 | // Special case: This vote can be "skipped" by using -1 as the choice. 268 | // Set choiceSession to nil and onChange will handle it 269 | log.Println("healerpoison: settings choiceSession to nil") 270 | choiceSession = nil 271 | } else { 272 | // Find corresponding session by ID 273 | choiceSession, _ = g.FindByID(msg.Choice) 274 | if choiceSession == nil { 275 | return 276 | } 277 | _, hasVoted := v.votes[s] 278 | if hasVoted { 279 | // You cannot change your vote after voting 280 | return 281 | } 282 | 283 | // Store vote 284 | v.votes[s] = choiceSession 285 | log.Println("votes:", v.votes, "hasConcensus:", g.vote.HasConcensus()) 286 | } 287 | 288 | v.onChange(v, s, choiceSession) 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Murder Mystery 2 | 3 | Murdermystery is an online multiplayer game of deception. 4 | 5 | ## Run on repl.it 6 | 7 | 1. Create a [new bash repl](https://repl.it/l/bash) 8 | 2. Paste this into `main.sh`: 9 | 10 | ```bash 11 | wget -O murdermystery https://github.com/Scoder12/murdermystery/releases/latest/download/server_linux_x64 12 | 13 | chmod +x murdermystery 14 | ./murdermystery -addr 0.0.0.0:8080 15 | ``` 16 | 17 | 3. Visit your repl url + `/game?id=1` (I didn't implement game creation, so any id value is valid). You'll need 5 of your friends (or other tabs). 18 | 19 | ## Architecture 20 | 21 | The client is built with next.js, typescript, and react. 22 | It uses Chakra UI for styling. 23 | It is meant to be fully internationalized, supporting both english and chinese. 24 | 25 | The server is written with Go. 26 | It uses Gin for HTTP and Melody for websockets. 27 | 28 | Communication between client and server uses protocol buffer wire format binary 29 | messages sent over a WebSocket connection. Although this is overkill for such a small 30 | project, it allows me to learn about protocol buffers hands on. 31 | 32 | ## Building 33 | 34 | ### Client 35 | 36 | In production, the client bundle is exported into HTML and then server by the backend 37 | binary. This means that advanced Next.JS routing features such may not be available so 38 | that the app can be server by a different HTTP server. 39 | 40 | To run the frontend server in development mode: 41 | 42 | ```bash 43 | npm run dev 44 | ``` 45 | 46 | To generate a production build of both the server and client, refer to the backend 47 | section of the building guide. 48 | 49 | ### Protocol Buffers 50 | 51 | The protocol buffers definitions are found in `main.proto`. They generate JS and Go 52 | code for use by the application. I have committed the generated code to the repo anyway 53 | so that it can be built without installing protoc, but if the proto file is changed the 54 | generated code but also be updated so they stay in sync. 55 | 56 | To re-generate the protocol buffers code run `./tools/protoc.sh`. 57 | It works without any options, but you can also pass the repository root as the first 58 | argument and a language to skip as the second argument. The skip MUST be second. 59 | 60 | ``` 61 | ./tools/protoc.sh # OR 62 | ./tools/protoc.sh [--no-js|--no-golang] 63 | ``` 64 | 65 | This will generate the following files: 66 | 67 | ``` 68 | ./backend/protocol/pb/main.pb.go 69 | ./pbjs/protobuf.js 70 | ./pbjs/protobuf.d.ts 71 | ``` 72 | 73 | The script automatically checks if you have the required toolchain installed and tells 74 | you how to install if you don't. 75 | 76 | The build scripts do not support Windows, but with modifications can definitely work on 77 | it. I may add Windows support later but it is not high priority as I develop and deploy 78 | exclusively on linux. 79 | 80 | ### Server 81 | 82 | To generate a development build of the server, ensure the build directory exists, and 83 | run: 84 | 85 | ``` 86 | cd backend 87 | go build -o ../build/backend 88 | ``` 89 | 90 | The directory the build command is run in matters. I have not yet figured out how to 91 | build it while being in the repository root. 92 | 93 | To generate a production build of the server, including bundling the frontend run the 94 | build script: 95 | 96 | ```bash 97 | ./tools/build.sh 98 | ``` 99 | 100 | The arguments for this script work similarly to the protobuf build script: 101 | 102 | ``` 103 | ./tools/build.sh # OR 104 | ./tools/build.sh [--no-clean] 105 | ``` 106 | 107 | If `--no-clean` is passed, the `build/html` directory will not be removed. 108 | 109 | This generates an executable `./build/backend`. To start the server run this file. 110 | 111 | The server will listen on `http://localhost:8080` by default. 112 | It optionally takes an interface to listen on in the `-addr` paramater which is in 113 | the form of `iface:port`. 114 | 115 | To run the binary in production mode, set the environment variable `GIN_MODE=release`. 116 | 117 | It is planned to have a `GOENV=prod` variable that will disable many debug features, 118 | such as the disabled origin check and extra logging, but it is not yet implemented so 119 | the only way to disable these features is to edit the code. 120 | 121 | ```bash 122 | ./build/backend -addr localhost:1234 123 | ``` 124 | 125 | For development, I use `CompileDaemon` to automatically build the binary whenver any 126 | souce files change. It probably isn't the best option but covers my needs fine. 127 | 128 | You can install it like this: 129 | 130 | ```bash 131 | go get github.com/githubnemo/CompileDaemon 132 | ``` 133 | 134 | This is the command I use to run it: 135 | 136 | ```bash 137 | PROJ="/path/to/repo" 138 | CompileDaemon -directory=$PROJ/backend -build='go build -o $PROJ/build/backend' -command '$PROJ/build/backend' -color -log-prefix=false 139 | ``` 140 | 141 | ## Roadmap 142 | 143 | - Core functionality 144 | 145 | - [x] Basic architecture 146 | - [x] Setting Names 147 | - [x] Handling Host 148 | - [x] Starting game 149 | - [x] Assigning characters 150 | - [x] Implementing Votes 151 | - [x] Prophet ability 152 | - [x] Healer ability 153 | - [x] Kills 154 | - [x] Voting to kill 155 | - [x] Showing amount of each character left 156 | - [ ] Spectating 157 | 158 | - Polish 159 | 160 | - [x] Pie indicator for timed components and button to skip 161 | - [ ] Minigame for lobby / night screen 162 | - [ ] Prophet screen animation 163 | - [ ] Character visualization styling 164 | - [ ] And more... 165 | 166 | - Preparing for production 167 | 168 | - [ ] Remove unecessary logs 169 | - [ ] Use environment effictively 170 | - [ ] Server performance improvements 171 | - [ ] Frontend performance improvements (such as `useMemo`) 172 | - [ ] And more... 173 | 174 | ## Other Details 175 | 176 | ### Statik file and git 177 | 178 | The `backend/statik/statik.go` file is a go source file that contains zipped static assets such as HTML/CSS/JS that are served by the binary when it runs. 179 | The binary depends upon being able to import this file and will not build without it. 180 | The only way to generate it is to run statik with the proper arguments. 181 | To make it easy on anyone trying to build the backend, I have checked an placeholder version of this file into git. 182 | It has a simple HTML page explaining why its there and the command to bundle the real UI. 183 | This allows developers to build the binary without having to figure out what statik is because go complains about missing the file. 184 | I also don't want a normal build of the binary, which modifies this file to be checked into git, because this would cause the repository to have a quickly outdated UI bundle included by default which could be confusing. 185 | 186 | TL;DR The statik go file is autogenerated, use this command to prevent changes to it from being picked up by git: 187 | 188 | ``` 189 | git update-index --assume-unchanged backend/statik/statik.go 190 | ``` 191 | 192 | I will not accept pull requests which don't run this command and consequently modify 193 | this file in git. 194 | 195 | To go back to the template version, run 196 | 197 | ``` 198 | git checkout origin/master backend/statik/statik.go 199 | ``` 200 | 201 | ### CI 202 | 203 | The CI system is run by GitHub Actions. I have not written any tests for either the 204 | frontend or backend yet, so the CI just checks if the frontend and backend builds are 205 | successful. This just makes sure no bad code is pushed to the repo. 206 | 207 | The backend CI job only runs whenever the `tools/` or `backend/` paths are changed to 208 | save CI minutes (not that I'm in danger of running out). 209 | 210 | Whenver a GitHub release is created, a release CI job will automatically generate a 211 | production linux amd64 build and attach it to the release. 212 | 213 | Badges: 214 | 215 | ![Node.js CI](https://github.com/Scoder12/murdermystery/workflows/Node.js%20CI/badge.svg) 216 | 217 | ![Build Backend](https://github.com/Scoder12/murdermystery/workflows/Build%20Backend/badge.svg) 218 | 219 | ![Release](https://github.com/Scoder12/murdermystery/workflows/Release/badge.svg) 220 | 221 | ### Bot 222 | 223 | The bot is a script used for testing the server. The server has a player minimum, and 224 | the bot script allows the minimum to be filled automatically. 225 | 226 | In addition, it can be used to stress test the server under high load and consistently 227 | reproduce server-side bugs. 228 | 229 | To use the bot, you must have already built the protocol buffer code as described 230 | above and installed the `ws` dev dependency with `npm`. 231 | 232 | To run the bot, pass a game ID and number of bots, for example: 233 | 234 | ```bash 235 | node ./tools/bot.js asdf 6 236 | ``` 237 | 238 | If you're using linux, the file is already executable and has a proper shebang, so you 239 | can omit `node`. 240 | 241 | ## License 242 | 243 | Copyright 2020 Scoder12. 244 | -------------------------------------------------------------------------------- /lib/useMessageHandler.ts: -------------------------------------------------------------------------------- 1 | import { murdermystery as protobuf } from "pbjs/protobuf"; 2 | import { useRef, useState } from "react"; 3 | import { STRINGS } from "./translate"; 4 | 5 | export interface PlayerIDMap { 6 | [id: string]: protobuf.Players.IPlayer; 7 | } 8 | 9 | export interface AlertContent { 10 | title: STRINGS; 11 | body: STRINGS; 12 | } 13 | 14 | // Typing this function's return would be too complicated, plus it only returns once 15 | // and the type can be inferred. 16 | // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types 17 | export default function useMessageHandler( 18 | onError: (msg: STRINGS) => void, 19 | onGameOver: () => void 20 | ) { 21 | // Our player ID 22 | // Set to -2 so it is different from spectator ID of -1, otherwise we will never 23 | // re-render as a spectator 24 | const [playerID, setPlayerID] = useState(-2); 25 | // Are we the host? Used to determine whether "Start Game" is enabled on Lobby 26 | const [isHost, setIsHost] = useState(false); 27 | // The players we know of. Server will sync these with us whever they update. 28 | const [players, setPlayers] = useState({}); 29 | // Who the host is. Used for showing the "Host" badge next to them in the lobby 30 | const [hostId, setHostId] = useState(-1); 31 | // If set, a modal will pop with alertContent and then it will be cleared. 32 | // Server will tell us when to set this 33 | const [alertContent, setAlertContent] = useState(null); 34 | // Our character. Used by the spinner. 35 | const [character, setCharacter] = useState( 36 | protobuf.Character.NONE 37 | ); 38 | // Whether the character spinner is done 39 | const [spinDone, setSpinDone] = useState(false); 40 | // Fellow wolves. Shown to the player after character spinner. 41 | const [fellowWolves, setFellowWolves] = useState([]); 42 | // Whether the fellow wolves screen still needs to be shown 43 | const [showFellowWolves, setShowFellowWolves] = useState(false); 44 | // Current vote to be shown to the user. 45 | const [voteRequest, setVoteRequest] = useState( 46 | null 47 | ); 48 | // Result of a vote, reset to null after timeout 49 | const [voteResult, setVoteResult] = useState< 50 | protobuf.VoteResult.ICandidateResult[] | null 51 | >(null); 52 | // Current vote type 53 | const [voteType, setVoteType] = useState(0); 54 | // Prophet reveal screen 55 | const [ 56 | prophetReveal, 57 | setProphetReveal, 58 | ] = useState(null); 59 | // The kill revealed to the healer 60 | const [killReveal, setKillReveal] = useState(null); 61 | const [gameIsOver, setGameIsOver] = useState(false); 62 | const gameOverRef = useRef(null); 63 | const [alive, setAlive] = useState(null); 64 | const [spectatorUpdates, setSpectatorUpdates] = useState< 65 | protobuf.ISpectatorUpdate[] | null 66 | >(null); 67 | const [killed, setKilled] = useState(null); 68 | 69 | // Message handlers 70 | function handleHost(msg: protobuf.IHost) { 71 | setIsHost(!!msg.isHost); 72 | } 73 | 74 | function handlePlayers(msg: protobuf.IPlayers) { 75 | const newPlayers: PlayerIDMap = {}; 76 | (msg.players || []).forEach((p) => { 77 | if (p.id && p.name) { 78 | newPlayers[p.id] = p; 79 | } 80 | }); 81 | setPlayers(newPlayers); 82 | setHostId(msg.hostId || -1); 83 | } 84 | 85 | function handleError(err: protobuf.IError) { 86 | let error = STRINGS.ERROR; 87 | if (err.msg === protobuf.Error.E_type.BADNAME) { 88 | error = STRINGS.INVALID_NAME; 89 | } else if (err.msg === protobuf.Error.E_type.DISCONNECT) { 90 | error = STRINGS.PLAYER_DISCONNECTED; 91 | } 92 | onError(error); 93 | } 94 | 95 | function handleAlert(data: protobuf.IAlert) { 96 | let error = STRINGS.ERROR_PERFORMING_ACTION; 97 | if (data.msg === protobuf.Alert.Msg.NEEDMOREPLAYERS) { 98 | error = STRINGS.NEED_MORE_PLAYERS; 99 | } 100 | // TODO: Maybe allow for different alert titles, but so far haven't used any others 101 | setAlertContent({ 102 | title: STRINGS.YOU_CANT_START, 103 | body: error, 104 | }); 105 | } 106 | 107 | function handleSetCharacter(msg: protobuf.ISetCharacter) { 108 | msg.character && setCharacter(msg.character); 109 | } 110 | 111 | function handleHandshake(msg: protobuf.IHandshake) { 112 | if (msg.status !== protobuf.Handshake.Status.OK) { 113 | onError(STRINGS.ERROR); 114 | } 115 | if (msg.id) { 116 | setPlayerID(msg.id); 117 | } 118 | } 119 | 120 | function handleFellowWolves(msg: protobuf.IFellowWolves) { 121 | setFellowWolves(msg.ids || []); 122 | setShowFellowWolves(true); 123 | } 124 | 125 | function handleVoteRequest(msg: protobuf.IVoteRequest) { 126 | if (msg.choice_IDs) { 127 | setVoteRequest(msg); 128 | setVoteType(msg.type || 0); 129 | } 130 | } 131 | 132 | function handleVoteOver(msg: protobuf.IVoteOver) { 133 | // Clear vote data 134 | setVoteRequest(null); 135 | if (killReveal != null) { 136 | setKillReveal(null); 137 | } 138 | if (msg.result && msg.result.candidates) { 139 | setVoteResult(msg.result.candidates); 140 | } 141 | } 142 | 143 | function handleProphetReveal(msg: protobuf.IProphetReveal) { 144 | setProphetReveal(msg); 145 | } 146 | 147 | function handlerHealerKillReveal(msg: protobuf.IKillReveal) { 148 | if (msg.killed_IDs) { 149 | setKillReveal(msg.killed_IDs); 150 | } 151 | } 152 | 153 | function handleGameOver(msg: protobuf.IGameOver) { 154 | gameOverRef.current = msg; 155 | setGameIsOver(true); 156 | onGameOver(); 157 | } 158 | 159 | function handlePlayerStatus(msg: protobuf.IPlayerStatus) { 160 | setAlive(msg.alive || []); 161 | } 162 | 163 | function handleSpectatorUpdate(msg: protobuf.ISpectatorUpdate) { 164 | setSpectatorUpdates((prevUpdates) => (prevUpdates || []).concat([msg])); 165 | } 166 | 167 | function handleBulkSpectatorUpdate(msg: protobuf.IBulkSpectatorUpdate) { 168 | setSpectatorUpdates((prevUpdates) => 169 | (prevUpdates || []).concat(msg.update || []) 170 | ); 171 | } 172 | 173 | function handleKilled(msg: protobuf.IKilled) { 174 | setKilled(msg); 175 | } 176 | 177 | // Call the proper handler based on the ServerMessage. 178 | // Protobuf guarantees only one of these cases will be true due to `oneof`, so this 179 | // is the best way to call the correct handler. 180 | const callHandler = (msg: protobuf.IServerMessage) => { 181 | if (msg.handshake) return handleHandshake(msg.handshake); 182 | if (msg.host) return handleHost(msg.host); 183 | if (msg.players) return handlePlayers(msg.players); 184 | if (msg.error) return handleError(msg.error); 185 | if (msg.alert) return handleAlert(msg.alert); 186 | if (msg.setCharacter) return handleSetCharacter(msg.setCharacter); 187 | if (msg.fellowWolves) return handleFellowWolves(msg.fellowWolves); 188 | if (msg.voteRequest) return handleVoteRequest(msg.voteRequest); 189 | if (msg.voteOver) return handleVoteOver(msg.voteOver); 190 | if (msg.prophetReveal) return handleProphetReveal(msg.prophetReveal); 191 | if (msg.killReveal) return handlerHealerKillReveal(msg.killReveal); 192 | if (msg.gameOver) return handleGameOver(msg.gameOver); 193 | if (msg.playerStatus) return handlePlayerStatus(msg.playerStatus); 194 | if (msg.spectatorUpdate) return handleSpectatorUpdate(msg.spectatorUpdate); 195 | if (msg.bulkSpectatorUpdate) 196 | return handleBulkSpectatorUpdate(msg.bulkSpectatorUpdate); 197 | if (msg.killed) return handleKilled(msg.killed); 198 | throw new Error("Not implemented. "); 199 | }; 200 | 201 | // Process a message from the websocket. 202 | const parseMessage = (ev: MessageEvent) => { 203 | let msg: protobuf.IServerMessage; 204 | try { 205 | msg = protobuf.ServerMessage.decode(new Uint8Array(ev.data)); 206 | // eslint-disable-next-line no-console 207 | console.log("↓", msg); 208 | callHandler(msg); 209 | } catch (e) { 210 | // eslint-disable-next-line no-console 211 | console.error("Message decode error:", e); 212 | } 213 | }; 214 | 215 | return { 216 | // Message Parser 217 | parseMessage, 218 | // State variables 219 | playerID, 220 | isHost, 221 | players, 222 | hostId, 223 | alertContent, 224 | character, 225 | spinDone, 226 | setSpinDone, 227 | fellowWolves, 228 | showFellowWolves, 229 | voteRequest, 230 | voteResult, 231 | voteType, 232 | prophetReveal, 233 | killReveal, 234 | gameIsOver, 235 | gameOverRef, 236 | alive, 237 | spectatorUpdates, 238 | killed, 239 | // State setters 240 | setShowFellowWolves, 241 | setProphetReveal, 242 | setAlertContent, 243 | setVoteResult, 244 | setAlive, 245 | setKilled, 246 | }; 247 | } 248 | -------------------------------------------------------------------------------- /components/GameClient.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | Alert, 3 | AlertIcon, 4 | AlertTitle, 5 | Flex, 6 | HStack, 7 | Modal, 8 | ModalBody, 9 | ModalCloseButton, 10 | ModalContent, 11 | ModalHeader, 12 | ModalOverlay, 13 | Text, 14 | } from "@chakra-ui/react"; 15 | import { S, STRINGS, useTranslator } from "lib/translate"; 16 | import useGameSocket from "lib/useGameSocket"; 17 | import useMessageHandler from "lib/useMessageHandler"; 18 | import { FC, useState } from "react"; 19 | import { murdermystery as protobuf } from "../pbjs/protobuf.js"; 20 | import CharacterSpinner from "./CharacterSpinner"; 21 | import FellowWolves from "./FellowWolves"; 22 | import GameOver from "./GameOver"; 23 | import Killed from "./Killed"; 24 | import Loader from "./Loader"; 25 | import Lobby from "./Lobby"; 26 | import NameBadge from "./NameBadge"; 27 | import PlayerStatus from "./PlayerStatus"; 28 | import ProphetReveal from "./ProphetReveal"; 29 | import UpdatesLog from "./UpdatesLog"; 30 | import Vote, { Candidate, Choice, VoteResult } from "./Vote"; 31 | 32 | const VoteType = protobuf.VoteRequest.Type; // shorthand 33 | 34 | interface GameClientInnerProps { 35 | server: string; 36 | gameId: string; 37 | nameProp: string; 38 | onError: (e: STRINGS) => void; 39 | onGameOver: () => void; 40 | } 41 | 42 | const GameClientInner: FC = ({ 43 | server, 44 | gameId, 45 | nameProp, 46 | onError, 47 | onGameOver, 48 | }: GameClientInnerProps) => { 49 | const t = useTranslator(); 50 | 51 | // State 52 | const { 53 | // Message Parser 54 | parseMessage, 55 | // State variables 56 | playerID, 57 | isHost, 58 | players, 59 | hostId, 60 | alertContent, 61 | character, 62 | spinDone, 63 | setSpinDone, 64 | fellowWolves, 65 | showFellowWolves, 66 | voteRequest, 67 | voteResult, 68 | voteType, 69 | prophetReveal, 70 | killReveal, 71 | gameIsOver, 72 | gameOverRef, 73 | alive, 74 | spectatorUpdates, 75 | killed, 76 | // State setters 77 | setShowFellowWolves, 78 | setProphetReveal, 79 | setAlertContent, 80 | setVoteResult, 81 | setAlive, 82 | setKilled, 83 | } = useMessageHandler(onError, onGameOver); 84 | 85 | const { isConnected, send } = useGameSocket( 86 | `${server}/game/${gameId}`, 87 | nameProp, 88 | parseMessage, 89 | onError 90 | ); 91 | 92 | // Utitility functions 93 | 94 | function typeToMsg( 95 | val: protobuf.VoteRequest.Type | null | undefined 96 | ): STRINGS { 97 | // Decode the vote type 98 | switch (val) { 99 | case VoteType.KILL: 100 | return STRINGS.PICK_KILL; 101 | case VoteType.PROPHET: 102 | return STRINGS.PICK_REVEAL; 103 | case VoteType.HEALERHEAL: 104 | case VoteType.HEALERPOISON: 105 | return STRINGS.PLEASE_CONFIRM; 106 | case VoteType.JURY: 107 | return STRINGS.PICK_WEREWOLF; 108 | default: 109 | return STRINGS.PLEASE_VOTE; 110 | } 111 | } 112 | 113 | function typeToDesc( 114 | val: protobuf.VoteRequest.Type | null | undefined 115 | ): JSX.Element | null { 116 | switch (val) { 117 | case protobuf.VoteRequest.Type.HEALERHEAL: 118 | return ( 119 | 120 | 121 | {(killReveal || [-1]).map((id) => ( 122 | 123 | ))} 124 | 125 | 126 | {t(STRINGS.WAS_KILLED_CONFIRM_HEAL)} 127 | 128 | 129 | ); 130 | case protobuf.VoteRequest.Type.HEALERPOISON: 131 | return {t(STRINGS.CONFIRM_POISON)}; 132 | case protobuf.VoteRequest.Type.JURY: 133 | return ( 134 | <> 135 | 136 | {t(STRINGS.KILLS_DURING_NIGHT)} 137 | 138 | {killReveal && killReveal.length ? ( 139 | 140 | {(killReveal || []).map((id) => ( 141 | 142 | ))} 143 | 144 | ) : ( 145 | 146 | {t(STRINGS.NONE)} 147 | 148 | )} 149 | 150 | ); 151 | default: 152 | return null; 153 | } 154 | } 155 | 156 | const IDToName = (id: number | null | undefined) => 157 | (players[id || -1] || {}).name || ""; 158 | 159 | // Take a list of IDS and return a list of corresponding names 160 | const IDsToNames = (ids: number[]) => 161 | ids.map((id) => (players[id] || {}).name || "").filter((n) => !!n); 162 | 163 | // Process the id list (number[]) into [ [id, name] ] 164 | const voteRequestToCandidates = (vr: protobuf.IVoteRequest): Choice[] => { 165 | if (vr.type === protobuf.VoteRequest.Type.HEALERHEAL) { 166 | // Special case: this is a yes/no vote 167 | return [ 168 | { name: t(STRINGS.YES_TO_USING), id: 2 }, 169 | { name: t(STRINGS.NO_TO_USING), id: 1 }, 170 | ]; 171 | } 172 | const candidates: Choice[] = []; 173 | (vr.choice_IDs || []).forEach((candidateID) => { 174 | const name: string = (players[candidateID] || {}).name || ""; 175 | if (name) { 176 | candidates.push({ 177 | name, 178 | id: candidateID, 179 | }); 180 | } 181 | }); 182 | if (vr.type === protobuf.VoteRequest.Type.HEALERPOISON) { 183 | candidates.push({ 184 | id: -1, 185 | name: {t(STRINGS.SKIP_USING)}, 186 | }); 187 | } 188 | return candidates; 189 | }; 190 | 191 | // UI 192 | 193 | // The order of these checks is important. 194 | 195 | // Don't care if connection is open but handshake is incomplete, in that case render 196 | // an empty lobby instead 197 | 198 | let view; // The main component we will render 199 | if (gameIsOver) { 200 | const gameOver: protobuf.IGameOver = gameOverRef.current || {}; 201 | view = ( 202 | ({ 205 | id: p.id || -1, 206 | name: IDToName(p.id), 207 | role: p.character || protobuf.Character.NONE, 208 | }))} 209 | /> 210 | ); 211 | } else if (!isConnected()) { 212 | // If the game is over, we don't care if the server is disconnected, so only 213 | // check this after checking for gameover. 214 | // Use the websocket readyState as the single source of truth for whether the 215 | // connection is open. 216 | return ; 217 | } else if (killed) { 218 | view = setKilled(null)} />; 219 | } else if (spectatorUpdates) { 220 | view = ; 221 | } else if (playerID === -1) { 222 | // We are a spectator, and we are waiting for updates. 223 | view = ; 224 | } else if (!character) { 225 | view = ( 226 | send({ startGame: {} })} 231 | /> 232 | ); 233 | } else if (!spinDone) { 234 | view = ( 235 | setSpinDone(true)} 238 | /> 239 | ); 240 | } else if (showFellowWolves) { 241 | view = ( 242 | id !== playerID))} 244 | onDone={() => setShowFellowWolves(false)} 245 | /> 246 | ); 247 | } else if (voteResult) { 248 | const votes: Candidate[] = []; 249 | voteResult.forEach((c) => { 250 | const candidateName = IDToName(c.id); 251 | if (candidateName && c.voters && c.voters.length) { 252 | votes.push({ 253 | id: c.id || -1, 254 | name: candidateName, 255 | voters: IDsToNames(c.voters), 256 | }); 257 | } 258 | }); 259 | view = setVoteResult(null)} />; 260 | } else if (alive && alive.length) { 261 | const aliveNames: string[] = []; 262 | const deadNames: string[] = []; 263 | 264 | Object.keys(players).forEach((id) => { 265 | const { name } = players[id]; 266 | if (name) { 267 | if (alive.includes(Number(id))) { 268 | aliveNames.push(name); 269 | } else { 270 | deadNames.push(name); 271 | } 272 | } 273 | }); 274 | 275 | view = ( 276 | setAlive(null)} 280 | /> 281 | ); 282 | } else if (voteRequest) { 283 | view = ( 284 | 289 | send({ vote: { choice: candidateID } }) 290 | } 291 | /> 292 | ); 293 | } else if (prophetReveal) { 294 | view = ( 295 | setProphetReveal(null)} 299 | /> 300 | ); 301 | } else { 302 | // TODO: Prettify this, maybe an image here 303 | view =

    {t(S.IS_NIGHT)}

    ; 304 | } 305 | 306 | return ( 307 | <> 308 | {view} 309 | setAlertContent(null)} 311 | isOpen={alertContent != null} 312 | isCentered 313 | > 314 | 315 | 316 | 317 | {alertContent ? t(alertContent.title) : ""} 318 | 319 | {alertContent ? t(alertContent.body) : ""} 320 | 321 | 322 | 323 | 324 | ); 325 | }; 326 | 327 | interface GameClientProps { 328 | server: string; 329 | id: string; 330 | nameProp: string; 331 | } 332 | 333 | export const GameClient: FC = ({ 334 | server, 335 | id, 336 | nameProp, 337 | }: GameClientProps) => { 338 | const t = useTranslator(); 339 | 340 | const [gameOver, setGameOver] = useState(false); 341 | const [error, setError] = useState(null); 342 | // The onError function will set the error variable only if it is not already set. If 343 | // it is called rapidly, the error variable will be out of date and it could clobber 344 | // the error. The canSet variable allow it to ensure it only sets once per render. 345 | let canSet = true; 346 | 347 | if (!gameOver && error) { 348 | return ( 349 | 350 | 351 | {t(error)} 352 | 353 | ); 354 | } 355 | return ( 356 | { 361 | if (canSet && !error) { 362 | canSet = false; 363 | setError(err); 364 | } 365 | }} 366 | onGameOver={() => setGameOver(true)} 367 | /> 368 | ); 369 | }; 370 | 371 | export default GameClient; 372 | -------------------------------------------------------------------------------- /backend/game/game.go: -------------------------------------------------------------------------------- 1 | package game 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "strings" 8 | "sync" 9 | "time" 10 | 11 | "github.com/Scoder12/murdermystery/backend/protocol/pb" 12 | 13 | "github.com/Scoder12/murdermystery/backend/protocol" 14 | "gopkg.in/olahol/melody.v1" 15 | ) 16 | 17 | // printerr handles an error 18 | func printerr(e error) { 19 | if e != nil { 20 | log.Printf("printerr: %s", e) 21 | } 22 | } 23 | 24 | // Client represents a client in the game 25 | type Client struct { 26 | // Identifier of the client. Should be treated as constant! 27 | ID int32 28 | 29 | // A timer used for waiting on the client 30 | // This timer should be cleared if the client discconects 31 | closeTimer *time.Timer 32 | 33 | // name of the client 34 | name string 35 | 36 | // role of the client 37 | role pb.Character 38 | } 39 | 40 | // Game represents a running game 41 | type Game struct { 42 | // The melody server for this game 43 | m *melody.Melody 44 | 45 | // The function that will de-allocate the game when it finishes 46 | destroyFn func() 47 | 48 | // Next id 49 | nextID int32 50 | 51 | // A lock to prevent data races, must be used when reading/writing game attributes 52 | lock sync.Mutex 53 | 54 | // All alive players 55 | clients map[*melody.Session]*Client 56 | 57 | // All players. Only available after game starts. 58 | players map[*melody.Session]*Client 59 | 60 | // The spectators in this server 61 | spectators map[*melody.Session]bool 62 | 63 | // Whether the game has been started 64 | started bool 65 | 66 | // The host of the game 67 | host *melody.Session 68 | 69 | // The current vote taking place, nil if there is none 70 | vote *Vote 71 | 72 | // The players that were killed during the night 73 | killed map[*melody.Session]pb.KillReason 74 | 75 | // Healer capabilities 76 | hasHeal bool 77 | hasPoison bool 78 | 79 | // Spectator events 80 | spectatorMsgs []*pb.SpectatorUpdate 81 | } 82 | 83 | // New constructs a new game 84 | func New(destroyFn func()) *Game { 85 | g := &Game{ 86 | m: melody.New(), 87 | nextID: 1, 88 | destroyFn: destroyFn, 89 | clients: make(map[*melody.Session]*Client), 90 | players: make(map[*melody.Session]*Client), 91 | spectators: make(map[*melody.Session]bool), 92 | killed: make(map[*melody.Session]pb.KillReason), 93 | hasHeal: true, 94 | hasPoison: true, 95 | spectatorMsgs: make([]*pb.SpectatorUpdate, 0), 96 | } 97 | // For debug 98 | g.m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true } 99 | 100 | g.m.HandleConnect(g.handleJoin) 101 | g.m.HandleDisconnect(g.handleDisconnect) 102 | g.m.HandleMessageBinary(g.handleMsg) 103 | g.m.HandleSentMessageBinary(func(s *melody.Session, msg []byte) { 104 | g.lock.Lock() 105 | defer g.lock.Unlock() 106 | var b strings.Builder 107 | fmt.Fprintf(&b, "[%v] ↓ [", g.getID(s)) 108 | for i, c := range msg { 109 | if i != 0 { 110 | fmt.Fprint(&b, ",") 111 | } 112 | fmt.Fprintf(&b, "%v", int(c)) 113 | } 114 | fmt.Fprintf(&b, "]\n") 115 | log.Print(b.String()) 116 | }) 117 | 118 | return g 119 | } 120 | 121 | // HandleRequest wraps the internal melody session's HandleRequest method 122 | func (g *Game) HandleRequest(w http.ResponseWriter, r *http.Request) error { 123 | return g.m.HandleRequest(w, r) 124 | } 125 | 126 | // End ends the game 127 | func (g *Game) End() { 128 | g.lock.Lock() 129 | defer g.lock.Unlock() 130 | 131 | if !g.started { 132 | return 133 | } 134 | g.started = false 135 | 136 | log.Println("Game ending") 137 | err := g.m.Close() 138 | printerr(err) 139 | g.destroyFn() 140 | } 141 | 142 | // EndWithError sends a pb.Error message then ends the game 143 | func (g *Game) EndWithError(reason pb.Error_EType) { 144 | g.lock.Lock() 145 | defer g.lock.Unlock() 146 | 147 | msg, err := protocol.Marshal(&pb.Error{Msg: reason}) 148 | if err != nil { 149 | return 150 | } 151 | err = g.m.BroadcastBinary(msg) 152 | printerr(err) 153 | time.AfterFunc(200*time.Millisecond, func() { 154 | g.End() 155 | }) 156 | } 157 | 158 | // Returns whether the host is valid. Assumes game is locked! 159 | func (g *Game) hostValid() bool { 160 | if g.host == nil || g.host.IsClosed() { 161 | return false 162 | } 163 | c := g.clients[g.host] 164 | return c != nil 165 | } 166 | 167 | // UpdateHost ensures the host of the game is valid. Assumes game is locked! 168 | func (g *Game) updateHost() { 169 | if g.hostValid() { 170 | return 171 | } 172 | // Find the earliest person to join (lowest PID) that is still online 173 | var bestM *melody.Session = nil 174 | var bestID int32 = -1 175 | for m, c := range g.clients { 176 | if bestID == -1 || c.ID < bestID { 177 | bestM = m 178 | } 179 | } 180 | if bestM != nil { 181 | msg, err := protocol.Marshal(&pb.Host{IsHost: true}) 182 | if err != nil { 183 | return 184 | } 185 | err = bestM.WriteBinary(msg) 186 | if err == nil { 187 | log.Println(err) 188 | } 189 | g.host = bestM 190 | } 191 | } 192 | 193 | func (g *Game) getPlayersMsg() *pb.Players { 194 | players := []*pb.Players_Player{} 195 | // Save hostID 196 | var hostID int32 = -1 197 | h, ok := g.clients[g.host] 198 | if ok { 199 | hostID = h.ID 200 | } 201 | 202 | for _, c := range g.clients { 203 | if c == nil || len(c.name) == 0 { 204 | continue 205 | } 206 | players = append(players, &pb.Players_Player{Name: c.name, Id: c.ID}) 207 | } 208 | 209 | return &pb.Players{Players: players, HostId: hostID} 210 | } 211 | 212 | // syncPlayers syncs player names between the server and all clients 213 | func (g *Game) syncPlayers() { 214 | msg, err := protocol.Marshal(g.getPlayersMsg()) 215 | if err != nil { 216 | return 217 | } 218 | err = g.m.BroadcastBinary(msg) 219 | printerr(err) 220 | } 221 | 222 | // dispatchSpectatorUpdate sends a spectator update to all spectators. Assumes game is 223 | // locked. 224 | func (g *Game) dispatchSpectatorUpdate(ev *pb.SpectatorUpdate) { 225 | g.spectatorMsgs = append(g.spectatorMsgs, ev) 226 | 227 | // Send the event to all spectators 228 | msg, err := protocol.Marshal(ev) 229 | if err != nil { 230 | return 231 | } 232 | for s := range g.spectators { 233 | err = s.WriteBinary(msg) 234 | printerr(err) 235 | } 236 | } 237 | 238 | func (g *Game) addSpectator(s *melody.Session) { 239 | g.lock.Lock() 240 | defer g.lock.Unlock() 241 | 242 | msg, err := protocol.Marshal(&pb.BulkSpectatorUpdate{Update: g.spectatorMsgs}) 243 | if err != nil { 244 | return 245 | } 246 | err = s.WriteBinary(msg) 247 | printerr(err) 248 | 249 | // Add the client to the spectators list 250 | g.spectators[s] = true 251 | } 252 | 253 | // A helper function to get the ID of a client 254 | // lock hub before calling! 255 | func (g *Game) getID(s *melody.Session) int32 { 256 | c, ok := g.clients[s] 257 | if !ok { 258 | return -1 259 | } 260 | return c.ID 261 | } 262 | 263 | // FindByID finds a session and client from an ID. Both will be nil if invalid. 264 | // Assumes game is locked! 265 | func (g *Game) FindByID(id int32) (*melody.Session, *Client) { 266 | for s, c := range g.clients { 267 | if c != nil && c.ID == id { 268 | return s, c 269 | } 270 | } 271 | return nil, nil 272 | } 273 | 274 | // SessionsByRole returns all sessions that have and do not have a given role 275 | // Assumes game is locked! 276 | func (g *Game) SessionsByRole(role pb.Character) ([]*melody.Session, []*melody.Session) { 277 | hasRole := []*melody.Session{} 278 | doesntHaveRole := []*melody.Session{} 279 | 280 | for s, c := range g.clients { 281 | if c != nil { 282 | if c.role == role { 283 | hasRole = append(hasRole, s) 284 | } else { 285 | doesntHaveRole = append(doesntHaveRole, s) 286 | } 287 | } 288 | } 289 | return hasRole, doesntHaveRole 290 | } 291 | 292 | func (g *Game) getKilled() *melody.Session { 293 | log.Println("killed:", g.killed) 294 | var r *melody.Session 295 | for s := range g.killed { 296 | r = s 297 | break 298 | } 299 | if r == nil { 300 | log.Panicln("[PANIC] Try to getKilled() when g.killed is empty!") 301 | } 302 | return r 303 | } 304 | 305 | // stageKill sets a value in g.killed. 306 | func (g *Game) stageKill(s *melody.Session, reason pb.KillReason) { 307 | log.Println("Killed", s, "Reason:", reason) 308 | g.killed[s] = reason 309 | } 310 | 311 | // kill kills a player. Assumes game is locked. 312 | func (g *Game) kill(s *melody.Session) { 313 | c, ok := g.clients[s] 314 | if !ok { 315 | log.Println("Tried to kill invalid client") 316 | return 317 | } 318 | log.Printf("Killing [%v] %s\n", c.ID, c.name) 319 | reason, ok := g.killed[s] 320 | if !ok { 321 | reason = pb.KillReason_UNKNOWN 322 | } 323 | // Tell the client they have been killed 324 | msg, err := protocol.Marshal(&pb.Killed{Reason: reason}) 325 | if err != nil { 326 | return 327 | } 328 | err = s.WriteBinary(msg) 329 | printerr(err) 330 | 331 | // Log this to spectators 332 | g.dispatchSpectatorUpdate(protocol.ToSpectatorUpdate(&pb.SpectatorKill{ 333 | Killed: c.ID, 334 | Reason: reason, 335 | })) 336 | 337 | // Remove them from players 338 | delete(g.clients, s) 339 | // Make them a spectator 340 | go g.addSpectator(s) 341 | } 342 | 343 | // commitKills kills all staged kills in g.killed. Assumes game is locked. 344 | func (g *Game) commitKills() { 345 | log.Println("Commiting kills") 346 | for s := range g.killed { 347 | g.kill(s) 348 | } 349 | g.resetKills() 350 | } 351 | 352 | func (g *Game) resetKills() { 353 | g.killed = make(map[*melody.Session]pb.KillReason) 354 | } 355 | 356 | // checkForGameOver checks whether the game is over. Assumes game is locked. 357 | func (g *Game) checkForGameOver() pb.GameOver_Reason { 358 | // There are two ways in which the werewolves can win. If they kill all special roles 359 | // or if they kill all citizens. 360 | // If all wolves die, then the wolves lose. 361 | 362 | // Check if any wolves are alive. 363 | wolves, _ := g.SessionsByRole(pb.Character_WEREWOLF) 364 | if len(wolves) == 0 { 365 | log.Println("No wolves, citizens win") 366 | return pb.GameOver_CITIZEN_WIN 367 | } 368 | // Check if any citizens are alive 369 | citizens, specialAndWolves := g.SessionsByRole(pb.Character_CITIZEN) 370 | if len(citizens) == 0 { 371 | log.Println("No citizens, werewolves win") 372 | return pb.GameOver_WEREWOLF_WIN 373 | } 374 | // Check if any special roles are alive 375 | for _, s := range specialAndWolves { 376 | c := g.clients[s] 377 | if c != nil { 378 | // If they are not a citizen and not a werewolf, they are special 379 | if c.role != pb.Character_WEREWOLF { 380 | // and they are alive so the game is still going 381 | log.Println("Found special, game still going") 382 | return pb.GameOver_NONE 383 | } 384 | } 385 | } 386 | // If we got here, there were no special people in the non-citizen list, so the 387 | // werewolves won. 388 | log.Println("No specials, werewolves win") 389 | return pb.GameOver_WEREWOLF_WIN 390 | } 391 | 392 | func (g *Game) handleGameOver(reason pb.GameOver_Reason) { 393 | // Reveal all player characters 394 | players := make([]*pb.GameOver_Player, len(g.players)) 395 | i := 0 396 | for _, c := range g.players { 397 | players[i] = &pb.GameOver_Player{Id: c.ID, Character: c.role} 398 | i++ 399 | } 400 | // Marshal gameover msg 401 | msg, err := protocol.Marshal(&pb.GameOver{Reason: reason, Players: players}) 402 | if err != nil { 403 | return 404 | } 405 | 406 | err = g.m.BroadcastBinary(msg) 407 | printerr(err) 408 | 409 | // Allow time for messages to be sent before closing all connections 410 | time.AfterFunc(200*time.Millisecond, func() { 411 | // End the game 412 | log.Println("Ending game") 413 | g.End() 414 | }) 415 | } 416 | 417 | func (g *Game) sendPlayerStatus() { 418 | alive := make([]int32, len(g.clients)) 419 | i := 0 420 | for _, c := range g.clients { 421 | alive[i] = c.ID 422 | i++ 423 | } 424 | msg, err := protocol.Marshal(&pb.PlayerStatus{Alive: alive}) 425 | if err != nil { 426 | return 427 | } 428 | for s := range g.clients { 429 | err = s.WriteBinary(msg) 430 | printerr(err) 431 | } 432 | } 433 | -------------------------------------------------------------------------------- /backend/go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/Scoder12/murdermystery v0.0.0-20201016183525-ec183932272e h1:n0stH8VcDpr7FClO1rz3exuakhsiYOfDTfSCkVQLZpM= 4 | github.com/Scoder12/murdermystery v0.0.0-20201023225151-d1d98b1ba944 h1://ZMBQEafxHte/ztgu0vLPMMBnkncc3h/IKRwSvoOZI= 5 | github.com/Scoder12/murdermystery v0.1.3 h1:0qbrU2B02T1/EMLQWCJfgy38w9VZ7EvY3YFU6/7ST10= 6 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 7 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 8 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 9 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 10 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 11 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 12 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 13 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 14 | github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= 15 | github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= 16 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 17 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 18 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 19 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 20 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 21 | github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= 22 | github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= 23 | github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70= 24 | github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 25 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 26 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 27 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 28 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 29 | github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= 30 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 31 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 32 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 33 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 34 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 35 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 36 | github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= 37 | github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= 38 | github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 39 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 40 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 41 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 42 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 43 | github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 44 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 45 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 46 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 47 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 48 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 49 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 50 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 51 | github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= 52 | github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 53 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 54 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 55 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 56 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 57 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 58 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 59 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 60 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 61 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 62 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 63 | github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= 64 | github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 65 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 66 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 67 | github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ= 68 | github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc= 69 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 70 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 71 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 72 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 73 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 74 | github.com/ugorji/go v1.1.12 h1:CzCiySZwEUgSwlgT8bTjFfV3rR6+Ti0atNsQkCRJnek= 75 | github.com/ugorji/go v1.1.12/go.mod h1:Ne4Hz4JKQXNr/qi+hC0ovwseF9muoPjAZp2lylyih70= 76 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 77 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 78 | github.com/ugorji/go/codec v1.1.12 h1:pv4DBnMb5X9XXCNC0DyEmhU3I/61gWDdyH7iZps5DLs= 79 | github.com/ugorji/go/codec v1.1.12/go.mod h1:U/SFD954ms+MwaHihwfeIz/sGz5OFgHt81tHc+Duy5k= 80 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 81 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 82 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 83 | golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI= 84 | golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 85 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 86 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 87 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 88 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 89 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 90 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 91 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 92 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 93 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 94 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 95 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 96 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 97 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 98 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 99 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 100 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 101 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= 102 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 103 | golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca h1:mLWBs1i4Qi5cHWGEtn2jieJQ2qtwV/gT0A2zLrmzaoE= 104 | golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 105 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 106 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 107 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 108 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 109 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 110 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 111 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 112 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 113 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 114 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 115 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 116 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 117 | google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= 118 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 119 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 120 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 121 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 122 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 123 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 124 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 125 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 126 | google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 127 | google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= 128 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 129 | google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 130 | google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= 131 | google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= 132 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 133 | gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 h1:sY2a+y0j4iDrajJcorb+a0hJIQ6uakU5gybjfLWHlXo= 134 | gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376/go.mod h1:BHKOc1m5wm8WwQkMqYBoo4vNxhmF7xg8+xhG8L+Cy3M= 135 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 136 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 137 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 138 | gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= 139 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 140 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 141 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 142 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | --------------------------------------------------------------------------------