# coding=UTF-8
#
# thickishstring server
# Copyright © 2013 David Given
#
# This software is redistributable under the terms of the Simplified BSD
# open source license. Please see the COPYING file in the distribution for
# the full text.

from ts.DBInstance import *
from ts.DBRoom import *
from ts.DBRealm import *
from ts.exceptions import InvalidObjectReference

# Functions of the form cmd_FNORD are executed when an *authenticated*
# player tries to execute command 'FNORD'.

def cmd_action(connection, message):
	try:
		actionid = int(message["actionid"])
	except KeyError:
		connection.onInvalidInput()
		return
		
	connection.player.onAction(actionid)
	
# The player has asked to be warped to another room and instance.

def cmd_warp(connection, message):
	try:
		instance = DBInstance(int(message["instance"]))
		if ("room" in message):
			room = DBRoom(int(message["room"]))
		else:
			room = "entrypoint"
	except (KeyError, InvalidObjectReference):
		connection.onInvalidInput()
		return
			
	connection.player.onWarp(instance, room)
	
# The player is saying something.

def cmd_say(connection, message):
	try:
		text = str(message["text"])
	except KeyError:
		connection.onInvalidInput()
		return
			
	connection.player.onSay(text)
		
# The player wants to create a room.

def cmd_createroom(connection, message):
	try:
		instance = DBInstance(int(message["instance"]))
		name = str(message["name"])
		title = str(message["title"])
	except (KeyError, InvalidObjectReference):
		connection.onInvalidInput()
		return

	instance.checkOwner(connection.player)			
	connection.player.onCreateRoom(instance, name, title)
		
# The player wants to delete a room.

def cmd_delroom(connection, message):
	try:
		room = DBRoom(int(message["room"]))
	except (KeyError, InvalidObjectReference):
		connection.onInvalidInput()
		return

	room.checkOwner(connection.player)			
	connection.player.onDestroyRoom(room)

# The player wants to create a realm.

def cmd_createrealm(connection, message):
	try:
		name = str(message["name"])
	except KeyError:
		connection.onInvalidInput()
		return

	connection.player.onCreateRealm(name)
		
# The player wants to rename a realm.

def cmd_renamerealm(connection, message):
	try:
		realm = DBRealm(int(message["realmid"]))
		newname = str(message["newname"])
	except (KeyError, InvalidObjectReference):
		connection.onInvalidInput()
		return

	realm.checkOwner(connection.player)			
	connection.player.onRenameRealm(realm, newname)
	
# The player wants to edit a room.

def cmd_editroom(connection, message):
	try:
		room = DBRoom(int(message["room"]))
		name = str(message["name"])
		title = str(message["title"])
		description = str(message["description"])
		actions = {}
		
		# Sanity check the list of actions.
		
		for k, v in message["actions"].iteritems():
			actions[int(k)] = {
				"type": str(v["type"]),
				"description": str(v["description"]),
				"target": str(v["target"])
			}	
	except (KeyError, InvalidObjectReference):
		connection.onInvalidInput()
		return

	room.checkOwner(connection.player)			
	connection.player.onEditRoom(room, name, title, description, actions)

@@ -1125,19 +1125,23 @@
 %09text = 
-str
+unicode
 (message
@@ -1381,35 +1381,39 @@
 ce%22%5D))%0A%09%09name = 
-str
+unicode
 (message%5B%22name%22%5D
@@ -1416,35 +1416,39 @@
 me%22%5D)%0A%09%09title = 
-str
+unicode
 (message%5B%22title%22
@@ -2006,35 +2006,39 @@
 %0A%09try:%0A%09%09name = 
-str
+unicode
 (message%5B%22name%22%5D
@@ -2277,27 +2277,31 @@
 %09%09newname = 
-str
+unicode
 (message%5B%22ne
@@ -2608,27 +2608,31 @@
 ))%0A%09%09name = 
-str
+unicode
 (message%5B%22na
@@ -2647,19 +2647,23 @@
 title = 
-str
+unicode
 (message
@@ -2689,19 +2689,23 @@
 ption = 
-str
+unicode
 (message
@@ -2861,19 +2861,23 @@
 %22type%22: 
-str
+unicode
 (v%5B%22type
@@ -2900,19 +2900,23 @@
 ption%22: 
-str
+unicode
 (v%5B%22desc
@@ -2941,19 +2941,23 @@
 arget%22: 
-str
+unicode
 (v%5B%22targ

Add Unicode support.