Skip to content

Instantly share code, notes, and snippets.

@Freezerburn
Created October 19, 2014 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Freezerburn/87c8ec9d5c984ee3d36c to your computer and use it in GitHub Desktop.
Save Freezerburn/87c8ec9d5c984ee3d36c to your computer and use it in GitHub Desktop.
type
MessageKind = enum
MessageQuit,
MessageLog
Message = object
case kind: MessageKind
of MessageLog:
msg: string
of MessageQuit: nil
proc tickThread(tickInP, playerOutP: ptr TChannel[Message]) {.thread.} =
var tickIn = tickInP[]
var playerOut = playerOutP[]
var going = true
while going:
let msg = tickIn.recv()
case msg.kind
of MessageLog:
echo(msg.msg)
of MessageQuit:
going = false
proc playerThread(playerInP, tickOutP: ptr TChannel[Message]) {.thread.} =
var playerIn = playerInP[]
var tickOut = tickOutP[]
let rooms = initRooms()
var currentRoom = rooms[startRoomName]
var going = true
var printDescription = true
while going:
if printDescription:
printDescription = false
echo(currentRoom.description)
stdout.write("Exits are: ")
var directionsToWrite = ""
for direction in low(currentRoom.directions)..high(currentRoom.directions):
if currentRoom.directions[direction] != nil:
directionsToWrite.add(($direction)[3.. -1] & ", ")
echo(directionsToWrite[0.. -3])
stdout.write(">>>> ")
let line = readLine(stdin).strip()
let toProcess = line.split(" ")
if len(toProcess) > 0:
case toProcess[0]
of "exit":
going = false
of "go":
if len(toProcess) > 1:
let maybeDir = toProcess[1].toDirection()
if maybeDir.isSome():
let direction: Direction = unwrap(maybeDir)
if currentRoom.directions[direction] != nil:
let room = currentRoom.directions[direction]
currentRoom = room
printDescription = true
else:
echo("There are no exits in that direction.")
of "test":
# This seems to fail to send, as "TEST TEST TEST" never gets printed.
tickOut.send(Message(kind: MessageLog, msg: "TEST TEST TEST"))
else:
echo("I do not understand that command.")
if line == "exit":
going = false
# Also seems to fail, since the program never ends due to the sync call.
tickOut.send(Message(kind: MessageQuit))
when isMainModule:
var
playerChan: TChannel[Message]
tickChan: TChannel[Message]
open(playerChan)
open(tickChan)
spawn playerThread(addr(playerChan), addr(tickChan))
spawn tickThread(addr(tickChan), addr(playerChan))
sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment