Commit 356383c7 authored by Ralf's avatar Ralf

fix message send ordering and save some threads

On branch master
Changes not staged for commit:
	modified:   eliza/Chatbot/Eliza.pm
no changes added to commit (use "git add" and/or "git commit -a")
parent 5c43661b
......@@ -4,11 +4,14 @@ import socket
import labyrinth
import random
import sys
import os
from util import log_file
from treasure import Treasure
from euclid import Euclid
from extern import Extern
scriptDir = os.path.dirname(os.path.realpath(__file__))
class Game:
def __init__(self, labyrinthFileName, adminPW, logFileName=None, port=1234):
self.players = []
......@@ -22,7 +25,7 @@ class Game:
euclid = Euclid(self)
etile = random.choice(self.labyrinth.getFreeTiles())
#self.labyrinth.createThing(euclid,etile)
eliza = Extern(self, "Eliza", "Z", "eliza", "simple")
eliza = Extern(self, "Eliza", "Z", os.path.join(scriptDir, "eliza"), "./simple")
ztile = random.choice(self.labyrinth.getFreeTiles())
self.labyrinth.createThing(eliza,ztile)
# set up socket
......
......@@ -148,20 +148,15 @@ class Labyrinth:
field.things.remove(thing)
self.game.showAdmins()
def tell(self,teller,verb,message):
sourceField = teller.field
receivers = []
for neighborField in [sourceField.neighbor(dy,dx) for dy,dx in directions.values()]:
if neighborField is None:
continue
for thing in neighborField.things:
if thing is teller:
continue
receivers.append(thing)
thing.tell(teller,verb,message)
def tellNeighbors(self, teller, verb, message):
self.tell(teller, teller.neighborThings(), verb, message)
def tell(self,teller,tolds,verb,message):
for thing in tolds:
assert thing != teller
thing.tell(teller,verb,message)
for admin in self.game.admins:
if hasattr(teller,'uid'):
admin.send("player {0} {1}s: {2}".format(teller.uid, verb, message))
else:
admin.send("{0} {1}s: {2}".format(str(teller), verb, message))
return receivers
\ No newline at end of file
......@@ -64,7 +64,7 @@ class Npc (Thing):
self.currentDialogLevel = newdialvl
assert (newmsg is None) == (newverb is None)
if newmsg is not None:
self.game.labyrinth.tell(self, newverb, newmsg)
self.game.labyrinth.tellNeighbors(self, newverb, newmsg)
else:
teller.tell(self, "does not react", "")
self.afterAnswering()
\ No newline at end of file
from util import *
from thing import Thing
import thing
import treasure
from labyrinth import directions as directionOffsets
from labyrinth import directions_inv as directions_t
......@@ -32,7 +32,7 @@ obstinacyTable = [
playerUID = 0
class Player(Thing):
class Player(thing.Thing):
def __init__(self, game, conn):
global playerUID
super().__init__(game)
......@@ -84,7 +84,7 @@ class Player(Thing):
travelDistance = 1
while pushTarget is not None and not pushTarget.isWalkable():
travelDistance+=1
pushTarget = pushTarget.neighbor(dx=dx,dy=dy)
pushTarget = pushTarget.neighborThings(dx=dx,dy=dy)
if pushTarget is None:
self.send("Another player pushed you out of the playing field.\nYou are dead.\nGood Bye.")
self.game.removePlayer(self)
......@@ -149,10 +149,11 @@ class Player(Thing):
elif verb in sayVerbs:
msg = " ".join(words[1:])
receivers = self.game.labyrinth.tell(self,verb,msg)
receivers = list(self.neighborThings())
someoneClose = any(isinstance(thing,Player) or isinstance(thing,Npc) for thing in receivers)
if someoneClose:
self.send("You say: "+msg)
self.game.labyrinth.tell(self, receivers, verb, msg)
else:
wallClose = any(isinstance(thing,Wall) for thing in receivers)
if wallClose:
......
......@@ -47,4 +47,4 @@ class Questionman (Npc):
def afterAnswering(self):
if self.correctAnswerGiven:
self.correctAnswerGiven = False
self.relocate()
\ No newline at end of file
self.relocate()
......@@ -17,3 +17,14 @@ class Thing():
def __repr__(self):
return "?"
def neighborThings(self):
import labyrinth
for thing in self.field.things:
if thing != self:
yield thing
for dy, dx in labyrinth.directions.values():
field = self.field.neighbor(dx=dx, dy=dy)
if field is not None:
yield from field.things
import threading, traceback, sys
from datetime import datetime
from select import select
# run a Python command asynchronously
def fire_and_forget(f):
......@@ -17,9 +18,20 @@ def send_async(conn, data):
conn.send(data)
except OSError:
pass
fire_and_forget(do_send)
# wait some time to write in this thread
r, w, x = select([], [conn], [], 0.1)
if w:
do_send()
else:
# instead of blocking, just send from another thread
fire_and_forget(do_send)
def log_file(file, *args, clock=False):
if clock:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S: "), end="", file=file)
print(*args, file=file, flush=True)
def read_timeout(file, size, time):
r, w, x = select([file], [], [], time)
if not r: return None
return file.read(size)
from thing import Thing
import thing
class Wall(Thing):
class Wall(thing.Thing):
def __init__(self,game):
super().__init__(game)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment